/* BEEBS janne_complex benchmark

   Copyright (C) 2014 Embecosm Limited and University of Bristol

   Contributor James Pallister <james.pallister@bristol.ac.uk>

   This file is part of the Bristol/Embecosm Embedded Benchmark Suite.

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program. If not, see <http://www.gnu.org/licenses/>. */

#include "support.h"

/* This scale factor will be changed to equalise the runtime of the
   benchmarks. */
#define SCALE_FACTOR    (REPEAT_FACTOR >> 0)


/*----------------------------------------------------------------------
 *  WCET Benchmark created by Andreas Ermedahl, Uppsala university,
 *  May 2000.
 *
 *  The purpose of this benchmark is to have two loop where the inner
 *  loops max number of iterations depends on the outer loops current
 *  iterations.  The results corresponds to something Jannes flow-analysis
 *  should produce.
 *
 * The example appeard for the first time in:
 *
 *  @InProceedings{Ermedahl:Annotations,
 * author =       "A. Ermedahl and J. Gustafsson",
 * title =        "Deriving Annotations for Tight Calculation of Execution Time",
 * year =         1997,
 * month =        aug,
 * booktitle =    EUROPAR97,
 * publisher =    "Springer Verlag",
 * pages =        "1298-1307"
 * }
 *
 * The result of Jannes tool is something like:
 *   outer loop:       1   2   3   4   5   6   7   8   9   10   11
 *   inner loop max:   5   9   8   7   4   2   1   1   1   1    1
 *
 *----------------------------------------------------------------------*/

int complex(int a, int b)
{
  while(a < 30)
    {
      while(b < a)
	{
	  if(b > 5)
	    b = b * 3;
	  else
	    b = b + 2;
	  if(b >= 10 && b <= 12)
	    a = a + 10;
	  else
	    a = a + 1;
	}
      a = a + 2;
      b = b - 10;
    }
  return 1;
}

static int a, b;

int benchmark()
{
  /* a = [1..30] b = [1..30] */
  int answer = 0;
  /* if(answer)
     {a = 1; b = 1;}
     else
     {a = 30; b = 30;} */
  answer = complex(a, b);
  return answer;
}

void initialise_benchmark() {
  a = 1;
  b = 1;
}

int verify_benchmark(int r)
{
  int exp = 1;
  if (r != exp)
    return 0;
  return 1;
}


