// File: /workspace/github_c_makefile_star2_O1/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
subq $0x18, %rsp
movq %rsi, %r14
movl %edi, %ebx
leaq 0x428d1(%rip), %rax     # 0x46c54
movl $0x0, (%rax)
leaq 0x2e86(%rip), %rdi      # 0x7216
callq 0x2040
testq %rax, %rax
je 0x43a9
movq %rax, %rdi
callq 0x2350
movq %rax, 0x3d387(%rip)     # 0x41730
cmpl $0x2, %ebx
jl 0x4482
movl $0x1, %r12d
movl $0x9c40, 0x8(%rsp)      # imm = 0x9C40
movq $0x0, 0x10(%rsp)
leaq 0x2e4d(%rip), %r13      # 0x721d
movl $0x9c40, 0xc(%rsp)      # imm = 0x9C40
movslq %r12d, %r15
movq (%r14,%r15,8), %rbp
movq %rbp, %rdi
movq %r13, %rsi
callq 0x2190
testl %eax, %eax
je 0x442f
movq %rbp, %rdi
leaq 0x2e28(%rip), %rsi      # 0x7220
callq 0x2190
testl %eax, %eax
je 0x443f
movq %rbp, %rdi
leaq 0x2e18(%rip), %rsi      # 0x7223
callq 0x2190
testl %eax, %eax
je 0x4453
movq %rbp, %rdi
leaq 0x2e08(%rip), %rsi      # 0x7226
callq 0x2190
testl %eax, %eax
je 0x4467
cmpb $0x2d, (%rbp)
je 0x4474
jmp 0x449b
leal 0x1(%r15), %r12d
movq 0x8(%r14,%r15,8), %rax
movq %rax, 0x10(%rsp)
jmp 0x4474
leal 0x1(%r15), %r12d
movq 0x8(%r14,%r15,8), %rdi
callq 0x22f0
movl %eax, 0xc(%rsp)
jmp 0x4474
leal 0x1(%r15), %r12d
movq 0x8(%r14,%r15,8), %rdi
callq 0x22f0
movl %eax, 0x8(%rsp)
jmp 0x4474
leaq 0x427e6(%rip), %rax     # 0x46c54
movl $0x1, (%rax)
incl %r12d
cmpl %ebx, %r12d
jl 0x43d8
jmp 0x449b
movl $0x9c40, 0xc(%rsp)      # imm = 0x9C40
movq $0x0, 0x10(%rsp)
movl $0x9c40, 0x8(%rsp)      # imm = 0x9C40
leaq 0x427a6(%rip), %rax     # 0x46c48
movq %r14, (%rax)
leaq 0x42794(%rip), %rax     # 0x46c40
movl %ebx, (%rax)
movl 0xc(%rsp), %edi
movl 0x8(%rsp), %esi
callq 0x48a9
movq 0x10(%rsp), %r14
testq %r14, %r14
je 0x44e4
leaq 0x2d5d(%rip), %rsi      # 0x7229
movq %r14, %rdi
callq 0x22a0
movq %rax, %rbx
testq %rax, %rax
jne 0x4500
movq %r14, %rdi
callq 0x23a0
movl 0x3d242(%rip), %esi     # 0x4172c
leaq 0x5cef(%rip), %rdi      # 0xa1e0
leaq 0x2d50(%rip), %rdx      # 0x7248
callq 0x20a0
movq %rax, %rbx
leaq 0x4274d(%rip), %rax     # 0x46c54
movl (%rax), %ebp
movq %rbx, %rdi
callq 0x4e2a
testl %ebp, %ebp
je 0x4525
leaq 0x2d2e(%rip), %rdi      # 0x724a
movl %eax, %esi
xorl %eax, %eax
callq 0x2130
movq %rbx, %rdi
callq 0x20e0
xorl %eax, %eax
callq 0x5780
movl $0x3, %edi
callq 0x4d75
movq %rax, %rbx
leaq 0x414d0(%rip), %rax     # 0x45a18
movq (%rax), %r14
addq $-0x48, %r14
movq %r14, (%rax)
leaq 0x414b7(%rip), %rax     # 0x45a10
cmpq (%rax), %r14
jae 0x456d
movl $0x7, %edi
callq 0x4a29
movq %rax, %r14
jmp 0x4574
movl $0x1c, (%r14)
leaq 0x413f5(%rip), %rax     # 0x45970
movq (%rax), %rax
movq %rax, 0x8(%r14)
movq %r14, 0x10(%rbx)
leaq 0x41423(%rip), %r15     # 0x459b0
movq (%r15), %rax
movq 0x28(%rax), %rdi
shrq %rdi
callq 0x4d75
movq %rax, 0x28(%r14)
leaq 0x41391(%rip), %r12     # 0x45938
movq (%r12), %rax
movq %rax, 0x18(%r14)
movl $0x13, %edi
callq 0x4d75
movq %rax, 0x20(%r14)
movl $0x1, %eax
movq %rax, 0x30(%r14)
movq %rax, 0x38(%r14)
movq (%r12), %rax
movq %rax, 0x40(%r14)
movq (%r15), %rax
movq %rax, 0x10(%r14)
leaq 0x413f0(%rip), %r14     # 0x459d0
movl (%r14), %eax
leal 0x1(%rax), %ecx
movl %ecx, (%r14)
leaq 0x41450(%rip), %r12     # 0x45a40
movq %rbx, (%r12,%rax,8)
movq %rbx, %rdi
xorl %esi, %esi
callq 0x24fa
addl $-0x2, %eax
cmpl $0x3, %eax
ja 0x4768
leaq 0x2bcb(%rip), %rcx      # 0x71dc
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
leaq 0x42633(%rip), %rax     # 0x46c54
cmpl $0x0, (%rax)
je 0x4774
leaq 0x2cc5(%rip), %rdi      # 0x72f6
jmp 0x476f
leaq 0x42617(%rip), %rax     # 0x46c54
cmpl $0x0, (%rax)
je 0x4774
leaq 0x2c80(%rip), %rdi      # 0x72cd
jmp 0x476f
leaq 0x2c6b(%rip), %rdi      # 0x72c4
jmp 0x476f
leaq 0x2c77(%rip), %rdi      # 0x72dc
callq 0x20b0
movl (%r14), %eax
decl %eax
movl %eax, (%r14)
movq (%r12,%rax,8), %rbx
movq 0x20(%rbx), %rsi
addq $0x10, %rsi
leaq 0x2bda(%rip), %rdi      # 0x725f
xorl %eax, %eax
callq 0x2130
movq 0x10(%rbx), %r12
leaq 0x2c87(%rip), %rdi      # 0x731e
callq 0x20b0
testq %r12, %r12
leaq 0x41292(%rip), %r15     # 0x45938
je 0x4774
cmpq %r12, (%r15)
je 0x4774
leaq 0x2bdb(%rip), %r13      # 0x7297
leaq 0x2bfe(%rip), %rbp      # 0x72c1
leaq 0x2bf2(%rip), %r14      # 0x72bc
movq 0x10(%r12), %rax
movq 0x10(%rax), %rsi
addq $0x10, %rsi
leaq 0x2bd2(%rip), %rdi      # 0x72b0
xorl %eax, %eax
callq 0x2130
movq 0x18(%r12), %rbx
testq %rbx, %rbx
je 0x4749
cmpq (%r15), %rbx
je 0x4749
movl $0x28, %edi
callq 0x2080
cmpl $0x4, (%rbx)
jb 0x4738
xorl %r15d, %r15d
testq %r15, %r15
movq %rbp, %rsi
cmoveq %r13, %rsi
movq 0x10(%rbx,%r15,8), %rax
movq 0x8(%rax), %rax
movq 0x10(%rax), %rdx
addq $0x10, %rdx
movq %r14, %rdi
xorl %eax, %eax
callq 0x2130
incq %r15
movl (%rbx), %eax
shrl $0x2, %eax
cmpq %rax, %r15
jb 0x4706
movl $0x29, %edi
callq 0x2080
leaq 0x411ef(%rip), %r15     # 0x45938
movl $0xa, %edi
callq 0x2080
movq 0x40(%r12), %r12
testq %r12, %r12
je 0x4774
cmpq (%r15), %r12
jne 0x46ca
jmp 0x4774
leaq 0x2b9b(%rip), %rdi      # 0x730a
callq 0x20b0
leaq 0x424d9(%rip), %rax     # 0x46c54
cmpl $0x0, (%rax)
je 0x4845
movl 0x4122e(%rip), %esi     # 0x459b8
movl 0x4122c(%rip), %edx     # 0x459bc
leaq 0x2adc(%rip), %rdi      # 0x7273
xorl %eax, %eax
callq 0x2130
movl 0x41214(%rip), %ecx     # 0x459b8
movl 0x41212(%rip), %esi     # 0x459bc
movl $0x20c49c, %eax         # imm = 0x20C49C
cmpl %eax, %ecx
setae %dl
cmpl %eax, %esi
setae %al
orb %dl, %al
cmpb $0x1, %al
jne 0x4800
movl $0xcccccccd, %eax       # imm = 0xCCCCCCCD
movl $0x147ae18, %edx        # imm = 0x147AE18
movl %esi, %edi
movl %ecx, %r8d
movl %ecx, %ecx
imulq %rax, %rcx
shrq $0x23, %rcx
movl %esi, %esi
imulq %rax, %rsi
shrq $0x23, %rsi
cmpl %edx, %r8d
setae %r8b
cmpl %edx, %edi
setae %dil
orb %r8b, %dil
jne 0x47c9
movl %ecx, 0x411be(%rip)     # 0x459b8
movl %esi, 0x411bc(%rip)     # 0x459bc
imull $0x3e8, %ecx, %eax      # imm = 0x3E8
addl %ecx, %esi
xorl %edx, %edx
divl %esi
movl $0xcccccccd, %esi       # imm = 0xCCCCCCCD
imulq %rax, %rsi
shrq $0x23, %rsi
leal (%rsi,%rsi), %ecx
leal (%rcx,%rcx,4), %ecx
subl %ecx, %eax
leaq 0x2a60(%rip), %rdi      # 0x7288
movl %eax, %edx
xorl %eax, %eax
callq 0x2130
movl 0x41189(%rip), %esi     # 0x459c0
leaq 0x2a5a(%rip), %rdi      # 0x7298
xorl %eax, %eax
callq 0x2130
xorl %eax, %eax
callq 0x5781
xorl %eax, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nopl (%rax)