// File: /workspace/github_c_makefile_star2_O2/GregorR[P]lstg/source/main.c
// Function: main
int main(int argc, char **argv)
{
  struct object  *aProcess,
                 *aContext,
                 *o;
  int             size,
                  i,
                  staticSize,
                  dynamicSize;
  FILE           *fp;
  char           *imageFileName = NULL,
                 *p;

#ifndef LITTLE_SMALLTALK_IMAGE
  imageFileName = "LittleSmalltalk.image";
#endif

  staticSize = DefaultStaticSize;
  dynamicSize = DefaultDynamicSize;
  lstDebugging = 0;

  /*
   * See if our environment tells us what TMPDIR to use
   */
  p = getenv("TMPDIR");
  if(p)
    lstTmpDir = strdup(p);

  /*
     first parse arguments 
   */
  for(i = 1; i < argc; i++)
  {
    if(strcmp(argv[i], "-i") == 0)
    {
      imageFileName = argv[++i];
    }
    else if(strcmp(argv[i], "-s") == 0)
    {
      staticSize = atoi(argv[++i]);
    }
    else if(strcmp(argv[i], "-d") == 0)
    {
      dynamicSize = atoi(argv[++i]);
    }
    else if(strcmp(argv[i], "-g") == 0)
    {
      lstDebugging = 1;
    }
    else if (argv[i][0] != '-')
    {
      break;
    }
  }
  lstArgv = argv;
  lstArgc = argc;

  gcinit(staticSize, dynamicSize);

  /*
     read in the method from the image file 
   */
#ifdef LITTLE_SMALLTALK_IMAGE
  if(imageFileName) {
#endif
    fp = fopen(imageFileName, "rb");
    if(!fp)
    {
      fprintf(stderr, "cannot open image file: %s\n", imageFileName);
      exit(1);
    }
#ifdef LITTLE_SMALLTALK_IMAGE
  }
  else
  {
    fp = fmemopen(LITTLE_SMALLTALK_IMAGE, LITTLE_SMALLTALK_IMAGE_LEN, "r");
  }
#endif
  if(lstDebugging)
    printf("%d objects in image\n", fileIn(fp));
  else
    fileIn(fp);
  fclose(fp);

  lstPrimitivesInit();

  /*
     build a context around it 
   */

  aProcess = staticAllocate(3);
  /*
     context should be dynamic 
   */
  aContext = gcalloc(contextSize);
  aContext->class = ContextClass;

  aProcess->data[contextInProcess] = aContext;
  size = integerValue(initialMethod->data[stackSizeInMethod]);
  aContext->data[stackInContext] = staticAllocate(size);
  aContext->data[argumentsInContext] = nilObject;

  aContext->data[temporariesInContext] = staticAllocate(19);
  aContext->data[bytePointerInContext] = newInteger(0);
  aContext->data[stackTopInContext] = newInteger(0);
  aContext->data[previousContextInContext] = nilObject;
  aContext->data[methodInContext] = initialMethod;

  /*
     now go do it 
   */
  rootStack[rootTop++] = aProcess;

  switch (execute(aProcess, 0))
  {
    case 2:
      if (lstDebugging) printf("User defined return\n");
      break;

    case 3:
      printf("can't find method in call\n");
      aProcess = rootStack[--rootTop];
      o = aProcess->data[resultInProcess];
      printf("Unknown method: %s\n", bytePtr(o));
      aContext = aProcess->data[contextInProcess];
      backTrace(aContext);
      break;

    case 4:
      if (lstDebugging) printf("\nnormal return\n");
      break;

    case 5:
      printf("time out\n");
      break;

    default:
      printf("unknown return code\n");
      break;
  }
  if (lstDebugging)
  {
    printf("cache hit %u miss %u", cacheHit, cacheMiss);
#define SCALE (1000)
    while((cacheHit > INT_MAX / SCALE) || (cacheMiss > INT_MAX / SCALE))
    {
      cacheHit /= 10;
      cacheMiss /= 10;
    }
    i = (SCALE * cacheHit) / (cacheHit + cacheMiss);
    printf(" ratio %u.%u%%\n", i / 10, i % 10);
    printf("%u garbage collections\n", gccount);
  }

  lstPrimitivesRelease();

  return (0);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rsi, %r14
movl %edi, %ebp
leaq 0x42c6a(%rip), %rax     # 0x46c54
andl $0x0, (%rax)
leaq 0x323e(%rip), %rdi      # 0x7232
callq 0x2040
testq %rax, %rax
je 0x400d
movq %rax, %rdi
callq 0x2350
movq %rax, 0x3d723(%rip)     # 0x41730
movl $0x9c40, %r15d          # imm = 0x9C40
pushq $0x1
popq %r13
xorl %ebx, %ebx
movl $0x9c40, %r12d          # imm = 0x9C40
cmpl %ebp, %r13d
jge 0x40a3
movslq %r13d, %rax
movq (%r14,%rax,8), %rcx
cmpb $0x2d, (%rcx)
jne 0x40a3
cmpb $0x69, 0x1(%rcx)
jne 0x403c
cmpb $0x0, 0x2(%rcx)
je 0x406f
cmpb $0x73, 0x1(%rcx)
jne 0x4048
cmpb $0x0, 0x2(%rcx)
je 0x4079
cmpb $0x64, 0x1(%rcx)
jne 0x4054
cmpb $0x0, 0x2(%rcx)
je 0x408b
cmpb $0x67, 0x1(%rcx)
jne 0x409b
cmpb $0x0, 0x2(%rcx)
jne 0x409b
leaq 0x42bed(%rip), %rax     # 0x46c54
movl $0x1, (%rax)
jmp 0x409b
incl %r13d
movq 0x8(%r14,%rax,8), %rbx
jmp 0x409b
incl %r13d
movq 0x8(%r14,%rax,8), %rdi
callq 0x22f0
movl %eax, %r15d
jmp 0x409b
incl %r13d
movq 0x8(%r14,%rax,8), %rdi
callq 0x22f0
movl %eax, %r12d
incl %r13d
jmp 0x401f
leaq 0x42b9e(%rip), %rax     # 0x46c48
movq %r14, (%rax)
leaq 0x42b8c(%rip), %rax     # 0x46c40
movl %ebp, (%rax)
movl %r15d, %edi
movl %r12d, %esi
callq 0x4470
testq %rbx, %rbx
je 0x4100
leaq 0x316c(%rip), %rsi      # 0x7239
movq %rbx, %rdi
callq 0x22a0
movq %rax, %r14
testq %rax, %rax
jne 0x411c
movq 0x5f14(%rip), %rax      # 0x9ff8
movq (%rax), %rdi
leaq 0x314e(%rip), %rsi      # 0x723c
movq %rbx, %rdx
xorl %eax, %eax
callq 0x21d0
pushq $0x1
popq %rdi
callq 0x2320
movl 0x3d626(%rip), %esi     # 0x4172c
leaq 0x60d3(%rip), %rdi      # 0xa1e0
leaq 0x3144(%rip), %rdx      # 0x7258
callq 0x20a0
movq %rax, %r14
leaq 0x42b31(%rip), %rax     # 0x46c54
movl (%rax), %ebx
movq %r14, %rdi
callq 0x49d2
testl %ebx, %ebx
je 0x4141
leaq 0x3122(%rip), %rdi      # 0x725a
movl %eax, %esi
xorl %eax, %eax
callq 0x2130
movq %r14, %rdi
callq 0x20e0
xorl %eax, %eax
callq 0x52bc
pushq $0x3
popq %rdi
callq 0x4924
movq %rax, %rbx
leaq 0x418b6(%rip), %rax     # 0x45a18
movq (%rax), %r14
addq $-0x48, %r14
movq %r14, (%rax)
leaq 0x4189d(%rip), %rax     # 0x45a10
cmpq (%rax), %r14
jae 0x4185
pushq $0x7
popq %rdi
callq 0x45fa
movq %rax, %r14
jmp 0x418c
movl $0x1c, (%r14)
leaq 0x417dd(%rip), %rax     # 0x45970
movq (%rax), %rax
movq %rax, 0x8(%r14)
movq %r14, 0x10(%rbx)
leaq 0x4180b(%rip), %r15     # 0x459b0
movq (%r15), %rax
movq 0x28(%rax), %rdi
shrq %rdi
callq 0x4924
movq %rax, 0x28(%r14)
leaq 0x41779(%rip), %r13     # 0x45938
movq (%r13), %rax
movq %rax, 0x18(%r14)
pushq $0x13
popq %rdi
callq 0x4924
movq %rax, 0x20(%r14)
pushq $0x1
popq %rax
movq %rax, 0x30(%r14)
movq %rax, 0x38(%r14)
movq (%r13), %rax
movq %rax, 0x40(%r14)
movq (%r15), %rax
movq %rax, 0x10(%r14)
leaq 0x417dc(%rip), %r14     # 0x459d0
movl (%r14), %eax
leal 0x1(%rax), %ecx
movl %ecx, (%r14)
leaq 0x4183c(%rip), %r15     # 0x45a40
movq %rbx, (%r15,%rax,8)
movq %rbx, %rdi
xorl %esi, %esi
callq 0x24a9
addl $-0x2, %eax
cmpl $0x3, %eax
ja 0x4361
leaq 0x2fd3(%rip), %rcx      # 0x71f8
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
leaq 0x42a1f(%rip), %rax     # 0x46c54
cmpl $0x0, (%rax)
je 0x440e
leaq 0x30c1(%rip), %rdi      # 0x7306
jmp 0x4368
leaq 0x42a03(%rip), %rax     # 0x46c54
cmpl $0x0, (%rax)
je 0x440e
leaq 0x307c(%rip), %rdi      # 0x72dd
jmp 0x4368
leaq 0x3067(%rip), %rdi      # 0x72d4
jmp 0x4368
leaq 0x3073(%rip), %rdi      # 0x72ec
callq 0x20b0
movl (%r14), %eax
decl %eax
movl %eax, (%r14)
movq (%r15,%rax,8), %rbx
movq 0x20(%rbx), %rsi
addq $0x10, %rsi
leaq 0x2fd6(%rip), %rdi      # 0x726f
xorl %eax, %eax
callq 0x2130
movq 0x10(%rbx), %r14
leaq 0x3083(%rip), %rdi      # 0x732e
callq 0x20b0
leaq 0x2ff0(%rip), %r12      # 0x72a7
leaq 0x3013(%rip), %rbx      # 0x72d1
leaq 0x3007(%rip), %r15      # 0x72cc
testq %r14, %r14
je 0x436d
cmpq (%r13), %r14
je 0x436d
movq 0x10(%r14), %rax
movq 0x10(%rax), %rsi
addq $0x10, %rsi
leaq 0x2fd5(%rip), %rdi      # 0x72c0
xorl %eax, %eax
callq 0x2130
movq 0x18(%r14), %rbp
testq %rbp, %rbp
je 0x4350
cmpq (%r13), %rbp
je 0x4350
pushq $0x28
popq %rdi
callq 0x2080
xorl %r13d, %r13d
movl (%rbp), %eax
shrl $0x2, %eax
cmpq %rax, %r13
jae 0x4341
testq %r13, %r13
movq %rbx, %rsi
cmoveq %r12, %rsi
movq 0x10(%rbp,%r13,8), %rax
movq 0x8(%rax), %rax
movq 0x10(%rax), %rdx
addq $0x10, %rdx
movq %r15, %rdi
xorl %eax, %eax
callq 0x2130
incq %r13
jmp 0x430c
pushq $0x29
popq %rdi
callq 0x2080
leaq 0x415e8(%rip), %r13     # 0x45938
pushq $0xa
popq %rdi
callq 0x2080
movq 0x40(%r14), %r14
jmp 0x42c5
leaq 0x2fb2(%rip), %rdi      # 0x731a
callq 0x20b0
leaq 0x428e0(%rip), %rax     # 0x46c54
cmpl $0x0, (%rax)
je 0x440e
pushq $0xa
popq %rbx
movl 0x41632(%rip), %esi     # 0x459b8
movl 0x41630(%rip), %edx     # 0x459bc
leaq 0x2ef0(%rip), %rdi      # 0x7283
xorl %eax, %eax
callq 0x2130
movl 0x41618(%rip), %esi     # 0x459b8
movl 0x41616(%rip), %ecx     # 0x459bc
movl $0x20c49c, %edi         # imm = 0x20C49C
cmpl %edi, %esi
setae %al
cmpl %edi, %ecx
setae %dl
orb %al, %dl
cmpb $0x1, %dl
jne 0x43da
movl %esi, %eax
xorl %edx, %edx
divl %ebx
movl %eax, %esi
movl %eax, 0x415ee(%rip)     # 0x459b8
movl %ecx, %eax
xorl %edx, %edx
divl %ebx
movl %eax, %ecx
movl %eax, 0x415e4(%rip)     # 0x459bc
jmp 0x43ab
imull $0x3e8, %esi, %eax      # imm = 0x3E8
addl %esi, %ecx
xorl %edx, %edx
divl %ecx
xorl %edx, %edx
divl %ebx
leaq 0x2ea7(%rip), %rdi      # 0x7298
movl %eax, %esi
xorl %eax, %eax
callq 0x2130
movl 0x415c0(%rip), %esi     # 0x459c0
leaq 0x2ea1(%rip), %rdi      # 0x72a8
xorl %eax, %eax
callq 0x2130
xorl %eax, %eax
callq 0x52bd
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop