// File: /workspace/github_c_makefile_star2_O3/stickyPiston[P]ti-basic-compiler/src/tic.c
// Function: compile
void compile() {
  char *content = NULL;
  int filesize = readFile(&content);
  int index = 0; 
  TokenType previousTokenType = TOKEN_NONE;
  while (index < filesize - 1) {
    int isParsed = 0;
    for (int i = 0; stringTable[i][lang] != NULL; i++) {
      if (strncmp(stringTable[i][lang], content + index, strlen(stringTable[i][lang])) == 0) {
        uint16_t opcode = functionTable[i].opcode;
        if (strcmp("-", stringTable[i][lang]) == 0 && (previousTokenType == TOKEN_OPERATOR || previousTokenType == TOKEN_OTHER)) {
          opcode = 0xb0;
        }

        if (strcmp(" ", stringTable[i][lang]) == 0 && ignoreWhitespace) {
          isParsed = 1;
          index++;
          break;
        }

        if (programLength == 0) {
          if (functionTable[i].opcodeType == OPCODE_1BYTE) {
            program = malloc(1);
            program[programLength++] = (unsigned char)(opcode & 0xff);
          } else {
            program = malloc(2);
            program[programLength++] = (unsigned char)(opcode >> 8 & 0xff);
            program[programLength++] = (unsigned char)(opcode & 0xff);
          }
        } else {
          if (functionTable[i].opcodeType == OPCODE_1BYTE) {
            program = realloc(program, ++programLength);
            program[programLength - 1] = (unsigned char)(opcode & 0xff);
          } else {
            programLength += 2;
            program = realloc(program, programLength);
            program[programLength - 2] = (unsigned char)(opcode >> 8 & 0xff);
            program[programLength - 1] = (unsigned char)(opcode & 0xff);
          }
        }
        dataSum += opcode;
        index += strlen(stringTable[i][lang]);
        previousTokenType = functionTable[i].tokenType;
        isParsed = 1;
        break;
      }
    }
    if (!isParsed) {
      fprintf(stderr, "Unrecognised token %c", *(content + index));
      abort();
    }
  }

  FILE *outFile = fopen(outputFile, "w");
  if (outFile == NULL) {
    fprintf(stderr, "Couldn't open %s", outputFile);
    abort();
  }

  // Append header to file
  unsigned char header[] = { 0x2a, 0x2a, 0x54, 0x49, 0x38, 0x33, 0x46, 0x2a, 0x1a, 0x0a, 0x0a };
  fwrite(header, 11, 1, outFile);

  char *comment = calloc(42, 1);
  strcpy(comment, "Made using stickyPiston/ti-basic-compiler");
  fwrite(comment, 42, 1, outFile);

  // Append program and program information to file
  programLength += 2;
  unsigned char varEntry[] = { 0x0d, 0x00, programLength, programLength >> 8,  0x05, programName[0], programName[1], programName[2], programName[3], programName[4], programName[5], programName[6], programName[7], 0x00, 0x00, programLength, programLength >> 8, 0x04, 0x00 };

  for (int i = 0; i < sizeof(varEntry); i++) dataSum += varEntry[i];

  uint16_t dataLength = sizeof(varEntry) + programLength;

  fwrite(&dataLength, 2, 1, outFile);
  fwrite(varEntry, sizeof(varEntry), 1, outFile);
  fwrite(program, programLength - 2, 1, outFile);
  
  // Append checksum to file
  uint16_t checksum = dataSum & 0xffff;
  fwrite(&checksum, 2, 1, outFile);

  fclose(outFile);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
leaq -0x90(%rbp), %rdi
movq $0x0, (%rdi)
callq 0xe5fc
movl %eax, -0x3c(%rbp)
cmpl $0x2, %eax
jl 0xe8e0
decl -0x3c(%rbp)
movq 0xb565(%rip), %rax      # 0x19c28
movq %rax, -0x78(%rbp)
movq 0xb552(%rip), %rax      # 0x19c20
movq %rax, -0x38(%rbp)
movzwl 0xb53e(%rip), %r12d     # 0x19c18
movq -0x90(%rbp), %rax
movq %rax, -0x70(%rbp)
leaq 0xb4fc(%rip), %rax      # 0x19be8
movl (%rax), %eax
movq %rax, -0x80(%rbp)
movl $0x4, -0x2c(%rbp)
xorl %eax, %eax
movl %r12d, -0x40(%rbp)
movl -0x80(%rbp), %r13d
leaq 0x6d66(%rip), %rcx      # 0x15470
movq (%rcx,%r13,8), %r12
movl %eax, -0x44(%rbp)
cltq
movq %rax, -0x88(%rbp)
testq %r12, %r12
je 0xea79
movq -0x70(%rbp), %rax
movq -0x88(%rbp), %rcx
leaq (%rax,%rcx), %r14
leaq 0x498f(%rip), %r15      # 0x130c8
leaq 0x6d30(%rip), %rbx      # 0x15470
movq %r12, %rdi
callq 0xe0a0
movq %r12, %rdi
movq %r14, %rsi
movq %rax, %rdx
callq 0xe060
testl %eax, %eax
je 0xe771
movq 0x18(%rbx,%r13,8), %r12
addq $0x18, %rbx
addq $0xc, %r15
testq %r12, %r12
jne 0xe740
jmp 0xea79
movzbl (%r12), %eax
movl %eax, %ecx
subl $0x2d, %ecx
jne 0xe783
movzbl 0x1(%r12), %ecx
negl %ecx
movzwl -0x8(%r15), %edx
testl %ecx, %ecx
jne 0xe79e
cmpl $0x3, -0x2c(%rbp)
je 0xe79a
cmpl $0x0, -0x2c(%rbp)
jne 0xe79e
movw $0xb0, %dx
subl $0x20, %eax
jne 0xe7a9
movzbl 0x1(%r12), %eax
negl %eax
movl -0x40(%rbp), %r12d
testl %eax, %eax
jne 0xe7c9
leaq 0xb456(%rip), %rax      # 0x19c10
cmpb $0x1, (%rax)
jne 0xe7c9
movl -0x44(%rbp), %eax
incl %eax
jmp 0xe8ab
movl -0x4(%r15), %eax
testw %r12w, %r12w
je 0xe80d
movl %edx, %r13d
testl %eax, %eax
je 0xe845
addl $0x2, %r12d
movw %r12w, 0xb432(%rip)     # 0x19c18
movzwl %r12w, %r14d
movq -0x38(%rbp), %rdi
movq %r14, %rsi
callq 0xe120
movq %rax, 0xb423(%rip)      # 0x19c20
movl %r13d, %ecx
movq %r14, %rdx
movb %ch, -0x2(%rax,%rdx)
movq %rax, -0x38(%rbp)
jmp 0xe86e
movl %edx, %r14d
testl %eax, %eax
je 0xe8b6
movl $0x2, %edi
callq 0xe100
movq %rax, 0xb3f7(%rip)      # 0x19c20
movl %r14d, %ecx
movb %ch, (%rax)
movw $0x2, 0xb3e1(%rip)      # 0x19c18
movq %rax, -0x38(%rbp)
movb %cl, 0x1(%rax)
movw $0x2, %r12w
jmp 0xe873
incl %r12d
movw %r12w, 0xb3c8(%rip)     # 0x19c18
movzwl %r12w, %r14d
movq -0x38(%rbp), %rdi
movq %r14, %rsi
callq 0xe120
movq %rax, 0xb3b9(%rip)      # 0x19c20
movq %rax, -0x38(%rbp)
movl %r13d, %ecx
movb %cl, -0x1(%rax,%r14)
movzwl %cx, %eax
movq -0x78(%rbp), %rcx
addq %rax, %rcx
movq %rcx, -0x78(%rbp)
movq %rcx, 0xb3a0(%rip)      # 0x19c28
leaq 0xb359(%rip), %rax      # 0x19be8
movl (%rax), %eax
movq %rax, -0x80(%rbp)
movq (%rbx,%rax,8), %rdi
callq 0xe0a0
movl -0x44(%rbp), %ecx
addl %eax, %ecx
movl %ecx, %eax
movl (%r15), %ecx
movl %ecx, -0x2c(%rbp)
cmpl -0x3c(%rbp), %eax
jl 0xe6fb
jmp 0xe8e0
movl $0x1, %edi
callq 0xe100
movq %rax, 0xb359(%rip)      # 0x19c20
movw $0x1, 0xb348(%rip)      # 0x19c18
movq %rax, -0x38(%rbp)
movl %r14d, %ecx
movb %cl, (%rax)
movw $0x1, %r12w
jmp 0xe873
leaq 0xb321(%rip), %rax      # 0x19c08
movq (%rax), %rdi
leaq 0xb80(%rip), %rsi       # 0xf471
callq 0xe130
testq %rax, %rax
je 0xea89
movq %rax, %rbx
movabsq $0x2a46333849542a2a, %rax # imm = 0x2A46333849542A2A
leaq -0xa0(%rbp), %rdi
movq %rax, (%rdi)
movl $0xa0a1a2a, 0x7(%rdi)   # imm = 0xA0A1A2A
movl $0xb, %esi
movl $0x1, %edx
movq %rbx, %rcx
callq 0xe140
movl $0x2a, %edi
movl $0x1, %esi
callq 0xe0c0
movups 0x2903(%rip), %xmm0     # 0x11248
movups %xmm0, (%rax)
movups 0x2909(%rip), %xmm0     # 0x11258
movups %xmm0, 0x10(%rax)
movups 0x2908(%rip), %xmm0     # 0x11262
movups %xmm0, 0x1a(%rax)
movl $0x2a, %esi
movl $0x1, %edx
movq %rax, %rdi
movq %rbx, %rcx
callq 0xe140
movzwl 0xb29e(%rip), %eax      # 0x19c18
movl %eax, %ecx
addl $0x2, %ecx
movw %cx, 0xb292(%rip)       # 0x19c18
movw $0xd, -0x60(%rbp)
movb %cl, -0x5e(%rbp)
movl %ecx, %edx
shrl $0x8, %edx
movb %dl, -0x5d(%rbp)
movb $0x5, -0x5c(%rbp)
leaq 0xb256(%rip), %rsi      # 0x19bf8
movb (%rsi), %dil
movb %dil, -0x5b(%rbp)
movl 0x1(%rsi), %edi
movl %edi, -0x5a(%rbp)
movzwl 0x5(%rsi), %edi
movw %di, -0x56(%rbp)
movb 0x7(%rsi), %sil
movb %sil, -0x54(%rbp)
movw $0x0, -0x53(%rbp)
movb %cl, -0x51(%rbp)
movb %dl, -0x50(%rbp)
movw $0x4, -0x4f(%rbp)
xorl %ecx, %ecx
movq 0xb24e(%rip), %rdx      # 0x19c28
movzbl -0x60(%rbp,%rcx), %esi
addq %rsi, %rdx
incq %rcx
cmpq $0x13, %rcx
jne 0xe9da
movq %rdx, 0xb236(%rip)      # 0x19c28
addl $0x15, %eax
leaq -0x64(%rbp), %rdi
movw %ax, (%rdi)
movl $0x2, %esi
movl $0x1, %edx
movq %rbx, %rcx
callq 0xe140
leaq -0x60(%rbp), %rdi
movl $0x13, %esi
movl $0x1, %edx
movq %rbx, %rcx
callq 0xe140
movq 0xb1f5(%rip), %rdi      # 0x19c20
movzwl 0xb1e6(%rip), %esi      # 0x19c18
addq $-0x2, %rsi
movl $0x1, %edx
movq %rbx, %rcx
callq 0xe140
movzwl 0xb1de(%rip), %eax      # 0x19c28
leaq -0x62(%rbp), %rdi
movw %ax, (%rdi)
movl $0x2, %esi
movl $0x1, %edx
movq %rbx, %rcx
callq 0xe140
movq %rbx, %rdi
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xe090
movq -0x70(%rbp), %rdi
movq -0x88(%rbp), %rsi
callq 0xe1b6
callq 0xe18b