// File: /workspace/github_c_makefile_star2_O2/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 $0x68, %rsp
leaq -0x80(%rbp), %r14
andq $0x0, (%r14)
movq %r14, %rdi
callq 0xe53c
decl %eax
movl %eax, -0x64(%rbp)
movzwl 0xb60b(%rip), %eax      # 0x19c18
movl %eax, -0x2c(%rbp)
movq 0xb609(%rip), %rax      # 0x19c20
movq %rax, -0x40(%rbp)
movq 0xb606(%rip), %r15      # 0x19c28
xorl %ecx, %ecx
pushq $0x4
popq %rax
movl %eax, -0x34(%rbp)
movq (%r14), %rax
movq %rax, -0x78(%rbp)
leaq 0xb5b0(%rip), %r13      # 0x19be8
leaq 0xb5d1(%rip), %rax      # 0x19c10
movq %r15, -0x70(%rbp)
movl (%r13), %r15d
movb (%rax), %al
movb %al, -0x30(%rbp)
cmpl -0x64(%rbp), %ecx
jge 0xe7fa
movl %ecx, -0x44(%rbp)
movslq %ecx, %r13
addq -0x78(%rbp), %r13
leaq 0x4a56(%rip), %r14      # 0x130bc
leaq 0x6e03(%rip), %rbx      # 0x15470
movq (%rbx,%r15,8), %r12
testq %r12, %r12
je 0xe98a
movq %r12, %rdi
callq 0xe0a0
movq %r12, %rdi
movq %r13, %rsi
movq %rax, %rdx
callq 0xe060
addq $0x18, %rbx
addq $0xc, %r14
testl %eax, %eax
jne 0xe66d
movzbl (%r12), %eax
movl %eax, %ecx
subl $0x2d, %ecx
jne 0xe6ae
movzbl 0x1(%r12), %ecx
negl %ecx
leaq 0xb531(%rip), %r13      # 0x19be8
movzwl -0x8(%r14), %edx
testl %ecx, %ecx
jne 0xe6d0
cmpl $0x3, -0x34(%rbp)
je 0xe6cc
cmpl $0x0, -0x34(%rbp)
jne 0xe6d0
movw $0xb0, %dx
subl $0x20, %eax
jne 0xe6db
movzbl 0x1(%r12), %eax
negl %eax
testl %eax, %eax
jne 0xe6f1
testb $0x1, -0x30(%rbp)
je 0xe6f1
movl -0x44(%rbp), %ecx
incl %ecx
jmp 0xe64c
movl -0x4(%r14), %eax
cmpw $0x0, -0x2c(%rbp)
je 0xe73e
movl %edx, -0x30(%rbp)
testl %eax, %eax
movq -0x70(%rbp), %r15
je 0xe773
movl -0x2c(%rbp), %eax
addl $0x2, %eax
movw %ax, 0xb504(%rip)       # 0x19c18
movl %eax, -0x2c(%rbp)
movzwl %ax, %r12d
movq -0x40(%rbp), %rdi
movq %r12, %rsi
callq 0xe120
movq %rax, 0xb4f2(%rip)      # 0x19c20
movl -0x30(%rbp), %ecx
movq %r12, %rdx
movb %ch, -0x2(%rax,%rdx)
movq %rax, -0x40(%rbp)
jmp 0xe7a0
movl %edx, %r12d
testl %eax, %eax
movq -0x70(%rbp), %r15
je 0xe7a7
pushq $0x2
popq %rdi
callq 0xe100
movq %rax, 0xb4c8(%rip)      # 0x19c20
movl %r12d, %ecx
movb %ch, (%rax)
movw $0x2, 0xb4b2(%rip)      # 0x19c18
movq %rax, -0x40(%rbp)
movb %cl, 0x1(%rax)
movw $0x2, %ax
jmp 0xe7cc
movl -0x2c(%rbp), %eax
incl %eax
movw %ax, 0xb499(%rip)       # 0x19c18
movl %eax, -0x2c(%rbp)
movzwl %ax, %r12d
movq -0x40(%rbp), %rdi
movq %r12, %rsi
callq 0xe120
movq %rax, 0xb487(%rip)      # 0x19c20
movq %rax, -0x40(%rbp)
movl -0x30(%rbp), %ecx
movb %cl, -0x1(%rax,%r12)
jmp 0xe7cf
pushq $0x1
popq %rdi
callq 0xe100
movq %rax, 0xb46a(%rip)      # 0x19c20
movw $0x1, 0xb459(%rip)      # 0x19c18
movq %rax, -0x40(%rbp)
movl %r12d, %ecx
movb %cl, (%rax)
movw $0x1, %ax
movl %eax, -0x2c(%rbp)
movzwl %cx, %eax
addq %rax, %r15
movq %r15, 0xb44c(%rip)      # 0x19c28
movl (%r13), %eax
movq -0x18(%rbx,%rax,8), %rdi
callq 0xe0a0
movl -0x44(%rbp), %ecx
addl %eax, %ecx
movl (%r14), %eax
movl %eax, -0x34(%rbp)
jmp 0xe638
leaq 0xb407(%rip), %r14      # 0x19c08
movq (%r14), %rdi
leaq 0xc66(%rip), %rsi       # 0xf471
callq 0xe130
testq %rax, %rax
je 0xe9ac
movq %rax, %rbx
movabsq $0x2a46333849542a2a, %rax # imm = 0x2A46333849542A2A
leaq -0x90(%rbp), %rdi
movq %rax, (%rdi)
movl $0xa0a1a2a, 0x7(%rdi)   # imm = 0xA0A1A2A
pushq $0xb
popq %rsi
pushq $0x1
popq %r14
movq %r14, %rdx
movq %rbx, %rcx
callq 0xe140
pushq $0x2a
popq %r15
movq %r15, %rdi
movq %r14, %rsi
callq 0xe0c0
movups 0x29e9(%rip), %xmm0     # 0x11248
movups %xmm0, (%rax)
movups 0x29ef(%rip), %xmm0     # 0x11258
movups %xmm0, 0x10(%rax)
movups 0x29ee(%rip), %xmm0     # 0x11262
movups %xmm0, 0x1a(%rax)
movq %rax, %rdi
movq %r15, %rsi
movq %r14, %rdx
movq %rbx, %rcx
callq 0xe140
movzwl 0xb388(%rip), %eax      # 0x19c18
movl %eax, %ecx
addl $0x2, %ecx
movw %cx, 0xb37c(%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 0xb340(%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)
andw $0x0, -0x53(%rbp)
movb %cl, -0x51(%rbp)
movb %dl, -0x50(%rbp)
movw $0x4, -0x4f(%rbp)
xorl %ecx, %ecx
movq 0xb339(%rip), %rdx      # 0x19c28
cmpq $0x13, %rcx
je 0xe902
movzbl -0x60(%rbp,%rcx), %esi
addq %rsi, %rdx
incq %rcx
jmp 0xe8ef
movq %rdx, 0xb31f(%rip)      # 0x19c28
addl $0x15, %eax
leaq -0x68(%rbp), %rdi
movw %ax, (%rdi)
pushq $0x2
popq %r14
pushq $0x1
popq %r15
movq %r14, %rsi
movq %r15, %rdx
movq %rbx, %rcx
callq 0xe140
leaq -0x60(%rbp), %rdi
pushq $0x13
popq %rsi
movq %r15, %rdx
movq %rbx, %rcx
callq 0xe140
movq 0xb2de(%rip), %rdi      # 0x19c20
movzwl 0xb2cf(%rip), %esi      # 0x19c18
addq $-0x2, %rsi
movq %r15, %rdx
movq %rbx, %rcx
callq 0xe140
movzwl 0xb2c9(%rip), %eax      # 0x19c28
leaq -0x66(%rbp), %rdi
movw %ax, (%rdi)
movq %r14, %rsi
movq %r15, %rdx
movq %rbx, %rcx
callq 0xe140
movq %rbx, %rdi
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xe090
movq 0x4667(%rip), %rax      # 0x12ff8
movq (%rax), %rdi
movsbl (%r13), %edx
leaq 0x2881(%rip), %rsi      # 0x11221
xorl %eax, %eax
callq 0xe0e0
callq 0xe040
movq 0x4645(%rip), %rax      # 0x12ff8
movq (%rax), %rdi
movq (%r14), %rdx
leaq 0x2877(%rip), %rsi      # 0x11237
xorl %eax, %eax
callq 0xe0e0
callq 0xe040