// This is a SyReC-file describing a circuit sorting an array of 4 unsigned integers using the bubblesort algorithm with a fixed (maximal) number of iterations.
// This file has been taken from RevLib (www.revlib.org). 

module main( inout a[4] )

  wire tmp, ta1[4], ta2[2], ta3, j(2), swapped1(3), swapped2(2), swapped3(1) 

  j ^= 1;
  for $i = 0 to 2 do  
    ta1[$i] ^= a[$i];
    if ( ta1[$i] > a[j] ) then
      a[$i] <=> tmp;
      a[j] <=> tmp;
      a[$i] <=> tmp;
      ~= swapped1.$i
    else
      skip
    fi swapped1.$i;
    ++= j
  rof;

  j ^= 1;
  for $i = 0 to 1 do  
    ta2[$i] ^= a[$i];
    if ( ta2[$i] > a[j] ) then
      a[$i] <=> tmp;
      a[j] <=> tmp;
      a[$i] <=> tmp;
      ~= swapped2.$i
    else
      skip
    fi swapped2.$i;
    ++= j
  rof;
  ~= j;

  j ^= 1;
  for $i = 0 to 0 do  
    ta3 ^= a[$i];
    if ( ta3 > a[j] ) then
      a[$i] <=> tmp;
      a[j] <=> tmp;
      a[$i] <=> tmp;
      ~= swapped3.$i
    else
      skip
    fi swapped3.$i
  rof 
