#include <stdio.h>
#include "pseudo_regs.h"

int collatz(int n) {
      int count = 0 ;
      int boolTest ;
      goto loopTestLoc;
  loopStartLoc:
      boolTest = (n % 2 == 0) ;
      if (boolTest == 0) goto nIsOdd ;
      n = n / 2 ;
      goto joinPaths ;
  nIsOdd:
      n = 3 * n + 1 ;
  joinPaths:
      count = count + 1 ;
  loopTestLoc:
      if (n != 1) goto loopStartLoc ;
      return count ;
}

void fill_collatz_table(int table[], int size) {
  int iGTsize ;
  if (size<1) {
    return ;
  }
  // table[0] = 0 ;
  *table = 0 ;
  int i=1 ;
  goto loopTest ;
  loopStart:
    *(int *)((unsigned long)(void *)table+i*sizeof(*table)) = collatz(i) ;
    ++i ;
  loopTest:
    iGTsize = i<size ;
    if (iGTsize) goto loopStart ;
}

void print_table(int table[] , int size) {
  for(int i=1; i<size; ++i) {
    fprintf(stdout, "%4d --> %8d\n", i, table[i]) ;
  }
}

int main(int argc, char *argv[]) {
  int table[200] ;
  int size = 200 ;
  fill_collatz_table(table, size) ;
  print_table(table, size) ;
}
