// File: /workspace/github_c_makefile_star2_O3/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, %ebx
leaq 0x427fc(%rip), %rax     # 0x46c54
movl $0x0, (%rax)
leaq 0x2dd9(%rip), %rdi      # 0x723e
callq 0x2040
testq %rax, %rax
je 0x447e
movq %rax, %rdi
callq 0x2350
movq %rax, 0x3d2b2(%rip)     # 0x41730
cmpl $0x2, %ebx
jl 0x4526
movl $0x1, %r13d
movl $0x9c40, %ebp           # imm = 0x9C40
xorl %r15d, %r15d
movl $0x9c40, %r12d          # imm = 0x9C40
movslq %r13d, %rax
movq (%r14,%rax,8), %rcx
cmpb $0x2d, (%rcx)
jne 0x4534
cmpb $0x69, 0x1(%rcx)
jne 0x44b7
cmpb $0x0, 0x2(%rcx)
je 0x44ea
cmpb $0x73, 0x1(%rcx)
jne 0x44c3
cmpb $0x0, 0x2(%rcx)
je 0x44f5
cmpb $0x64, 0x1(%rcx)
jne 0x44cf
cmpb $0x0, 0x2(%rcx)
je 0x4508
cmpb $0x67, 0x1(%rcx)
jne 0x4518
cmpb $0x0, 0x2(%rcx)
jne 0x4518
leaq 0x42772(%rip), %rax     # 0x46c54
movl $0x1, (%rax)
jmp 0x4518
leal 0x1(%rax), %r13d
movq 0x8(%r14,%rax,8), %r15
jmp 0x4518
leal 0x1(%rax), %r13d
movq 0x8(%r14,%rax,8), %rdi
callq 0x22f0
movl %eax, %r12d
jmp 0x4518
leal 0x1(%rax), %r13d
movq 0x8(%r14,%rax,8), %rdi
callq 0x22f0
movl %eax, %ebp
incl %r13d
cmpl %ebx, %r13d
jl 0x449b
jmp 0x4534
movl $0x9c40, %r12d          # imm = 0x9C40
xorl %r15d, %r15d
movl $0x9c40, %ebp           # imm = 0x9C40
leaq 0x4270d(%rip), %rax     # 0x46c48
movq %r14, (%rax)
leaq 0x426fb(%rip), %rax     # 0x46c40
movl %ebx, (%rax)
movl %r12d, %edi
movl %ebp, %esi
callq 0x4939
testq %r15, %r15
je 0x4575
leaq 0x2ce8(%rip), %rsi      # 0x7245
movq %r15, %rdi
callq 0x22a0
movq %rax, %rbx
testq %rax, %rax
jne 0x4591
movq %r15, %rdi
callq 0x23a0
movl 0x3d1b1(%rip), %esi     # 0x4172c
leaq 0x5c5e(%rip), %rdi      # 0xa1e0
leaq 0x2cdb(%rip), %rdx      # 0x7264
callq 0x20a0
movq %rax, %rbx
leaq 0x426bc(%rip), %rax     # 0x46c54
movl (%rax), %ebp
movq %rbx, %rdi
callq 0x4eba
testl %ebp, %ebp
je 0x45b6
leaq 0x2cb9(%rip), %rdi      # 0x7266
movl %eax, %esi
xorl %eax, %eax
callq 0x2130
movq %rbx, %rdi
callq 0x20e0
xorl %eax, %eax
callq 0x5804
movl $0x3, %edi
callq 0x4e05
movq %rax, %rbx
leaq 0x4143f(%rip), %rax     # 0x45a18
movq (%rax), %r14
addq $-0x48, %r14
movq %r14, (%rax)
leaq 0x41426(%rip), %rax     # 0x45a10
cmpq (%rax), %r14
jae 0x45fe
movl $0x7, %edi
callq 0x4ac3
movq %rax, %r14
jmp 0x4605
movl $0x1c, (%r14)
leaq 0x41364(%rip), %rax     # 0x45970
movq (%rax), %rax
movq %rax, 0x8(%r14)
movq %r14, 0x10(%rbx)
leaq 0x41392(%rip), %r15     # 0x459b0
movq (%r15), %rax
movq 0x28(%rax), %rdi
shrq %rdi
callq 0x4e05
movq %rax, 0x28(%r14)
leaq 0x41300(%rip), %r12     # 0x45938
movq (%r12), %rax
movq %rax, 0x18(%r14)
movl $0x13, %edi
callq 0x4e05
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 0x4135f(%rip), %r14     # 0x459d0
movl (%r14), %eax
leal 0x1(%rax), %ecx
movl %ecx, (%r14)
leaq 0x413bf(%rip), %r12     # 0x45a40
movq %rbx, (%r12,%rax,8)
movq %rbx, %rdi
xorl %esi, %esi
callq 0x255c
addl $-0x2, %eax
cmpl $0x3, %eax
ja 0x47f9
leaq 0x2b62(%rip), %rcx      # 0x7204
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
leaq 0x425a2(%rip), %rax     # 0x46c54
cmpl $0x0, (%rax)
je 0x48d6
leaq 0x2c50(%rip), %rdi      # 0x7312
jmp 0x4800
leaq 0x42586(%rip), %rax     # 0x46c54
cmpl $0x0, (%rax)
je 0x48d6
leaq 0x2c0b(%rip), %rdi      # 0x72e9
jmp 0x4800
leaq 0x2bf6(%rip), %rdi      # 0x72e0
jmp 0x4800
leaq 0x2c02(%rip), %rdi      # 0x72f8
callq 0x20b0
movl (%r14), %eax
decl %eax
movl %eax, (%r14)
movq (%r12,%rax,8), %rbx
movq 0x20(%rbx), %rsi
addq $0x10, %rsi
leaq 0x2b65(%rip), %rdi      # 0x727b
xorl %eax, %eax
callq 0x2130
movq 0x10(%rbx), %r12
leaq 0x2c12(%rip), %rdi      # 0x733a
callq 0x20b0
testq %r12, %r12
leaq 0x41201(%rip), %r15     # 0x45938
je 0x4805
cmpq %r12, (%r15)
je 0x4805
leaq 0x2b66(%rip), %r13      # 0x72b3
leaq 0x2b89(%rip), %rbp      # 0x72dd
leaq 0x2b7d(%rip), %r14      # 0x72d8
movq 0x10(%r12), %rax
movq 0x10(%rax), %rsi
addq $0x10, %rsi
leaq 0x2b5d(%rip), %rdi      # 0x72cc
xorl %eax, %eax
callq 0x2130
movq 0x18(%r12), %rbx
testq %rbx, %rbx
je 0x47da
cmpq (%r15), %rbx
je 0x47da
movl $0x28, %edi
callq 0x2080
cmpl $0x4, (%rbx)
jb 0x47c9
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 0x4797
movl $0x29, %edi
callq 0x2080
leaq 0x4115e(%rip), %r15     # 0x45938
movl $0xa, %edi
callq 0x2080
movq 0x40(%r12), %r12
testq %r12, %r12
je 0x4805
cmpq (%r15), %r12
jne 0x475b
jmp 0x4805
leaq 0x2b26(%rip), %rdi      # 0x7326
callq 0x20b0
leaq 0x42448(%rip), %rax     # 0x46c54
cmpl $0x0, (%rax)
je 0x48d6
movl 0x4119d(%rip), %esi     # 0x459b8
movl 0x4119b(%rip), %edx     # 0x459bc
leaq 0x2a67(%rip), %rdi      # 0x728f
xorl %eax, %eax
callq 0x2130
movl 0x41183(%rip), %ecx     # 0x459b8
movl 0x41181(%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 0x4891
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 0x485a
movl %ecx, 0x4112d(%rip)     # 0x459b8
movl %esi, 0x4112b(%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 0x29eb(%rip), %rdi      # 0x72a4
movl %eax, %edx
xorl %eax, %eax
callq 0x2130
movl 0x410f8(%rip), %esi     # 0x459c0
leaq 0x29e5(%rip), %rdi      # 0x72b4
xorl %eax, %eax
callq 0x2130
xorl %eax, %eax
callq 0x5805
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop