// File: /workspace/github_c_makefile_star2_O1/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 -0x88(%rbp), %r15
movq $0x0, (%r15)
movq %r15, %rdi
callq 0xe5f0
decl %eax
movl %eax, -0x6c(%rbp)
movzwl 0xb568(%rip), %eax      # 0x19c18
movl %eax, -0x38(%rbp)
movq 0xb566(%rip), %rax      # 0x19c20
movq %rax, -0x40(%rbp)
movq 0xb563(%rip), %rax      # 0x19c28
movq %rax, -0x78(%rbp)
xorl %esi, %esi
movl $0x4, -0x2c(%rbp)
movq (%r15), %rax
movq %rax, -0x80(%rbp)
leaq 0xb508(%rip), %r14      # 0x19be8
cmpl -0x6c(%rbp), %esi
jge 0xe925
movl (%r14), %ebx
leaq 0x6d7d(%rip), %rax      # 0x15470
movq (%rax,%rbx,8), %r15
testq %r15, %r15
sete %r13b
je 0xe910
movq %rsi, -0x68(%rbp)
movslq %esi, %r12
addq -0x80(%rbp), %r12
movq %r15, %rdi
callq 0xe0a0
movq %r15, %rdi
movq %r12, %rsi
movq %rax, %rdx
callq 0xe060
testl %eax, %eax
je 0xe769
leaq 0x6d40(%rip), %rax      # 0x15470
leaq (%rax,%rbx,8), %r14
addq $0x18, %r14
xorl %ebx, %ebx
movq (%r14), %r15
testq %r15, %r15
sete %r13b
je 0xe797
movq %r15, %rdi
callq 0xe0a0
movq %r15, %rdi
movq %r12, %rsi
movq %rax, %rdx
callq 0xe060
addq $0x18, %r14
incq %rbx
testl %eax, %eax
jne 0xe73a
jmp 0xe76b
xorl %ebx, %ebx
leaq (%rbx,%rbx,2), %r12
leaq 0x494a(%rip), %rax      # 0x130c0
movzwl (%rax,%r12,4), %ebx
leaq 0x2a78(%rip), %rdi      # 0x111fa
movq %r15, %rsi
callq 0xe0d0
testl %eax, %eax
je 0xe7a7
leaq 0xb453(%rip), %r14      # 0x19be8
jmp 0xe7be
movq -0x68(%rbp), %rsi
leaq 0xb446(%rip), %r14      # 0x19be8
jmp 0xe910
cmpl $0x3, -0x2c(%rbp)
leaq 0xb436(%rip), %r14      # 0x19be8
je 0xe7ba
cmpl $0x0, -0x2c(%rbp)
jne 0xe7be
movw $0xb0, %bx
leaq 0xac3(%rip), %rdi       # 0xf288
movq %r15, %rsi
callq 0xe0d0
testl %eax, %eax
jne 0xe7e8
leaq 0xb438(%rip), %rax      # 0x19c10
cmpb $0x1, (%rax)
jne 0xe7e8
movq -0x68(%rbp), %rsi
incl %esi
jmp 0xe910
leaq 0x48d1(%rip), %rax      # 0x130c0
leaq (%rax,%r12,4), %r15
movl 0x4(%r15), %eax
movl -0x38(%rbp), %ecx
testw %cx, %cx
je 0xe836
movl %ebx, -0x2c(%rbp)
testl %eax, %eax
je 0xe87a
addl $0x2, %ecx
movw %cx, 0xb408(%rip)       # 0x19c18
movl %ecx, -0x38(%rbp)
movzwl %cx, %ebx
movq -0x40(%rbp), %rdi
movq %rbx, %rsi
callq 0xe120
movq %rax, 0xb3f7(%rip)      # 0x19c20
movl -0x2c(%rbp), %ecx
movb %ch, -0x2(%rax,%rbx)
movq %rax, -0x40(%rbp)
jmp 0xe8a3
movzwl %cx, %ecx
movq %rcx, -0x38(%rbp)
testl %eax, %eax
je 0xe8a9
movl $0x2, %edi
callq 0xe100
movq %rax, 0xb3ce(%rip)      # 0x19c20
movw $0x1, 0xb3bd(%rip)      # 0x19c18
movq -0x38(%rbp), %rcx
movb %bh, (%rax,%rcx)
movl %ebx, %ecx
movw $0x2, 0xb3ab(%rip)      # 0x19c18
movq %rax, -0x40(%rbp)
movb %cl, 0x1(%rax)
movw $0x2, %ax
jmp 0xe8d4
incl %ecx
movw %cx, 0xb395(%rip)       # 0x19c18
movl %ecx, -0x38(%rbp)
movzwl %cx, %ebx
movq -0x40(%rbp), %rdi
movq %rbx, %rsi
callq 0xe120
movq %rax, 0xb384(%rip)      # 0x19c20
movq %rax, -0x40(%rbp)
movl -0x2c(%rbp), %ecx
movb %cl, -0x1(%rax,%rbx)
jmp 0xe8d7
movl $0x1, %edi
callq 0xe100
movq %rax, 0xb366(%rip)      # 0x19c20
movw $0x1, 0xb355(%rip)      # 0x19c18
movq %rax, -0x40(%rbp)
movq -0x38(%rbp), %rcx
movb %bl, (%rax,%rcx)
movl %ebx, %ecx
movw $0x1, %ax
movl %eax, -0x38(%rbp)
movzwl %cx, %eax
movq -0x78(%rbp), %rcx
addq %rax, %rcx
movq %rcx, -0x78(%rbp)
movq %rcx, 0xb33c(%rip)      # 0x19c28
movl (%r14), %eax
leaq 0x6b7a(%rip), %rcx      # 0x15470
leaq (%rcx,%r12,8), %rcx
movq (%rcx,%rax,8), %rdi
callq 0xe0a0
movq -0x68(%rbp), %rsi
addl %eax, %esi
movl 0x8(%r15), %eax
movl %eax, -0x2c(%rbp)
testb %r13b, %r13b
je 0xe6e0
leaq -0x88(%rbp), %rdi
callq 0xe1b6
leaq 0xb2dc(%rip), %rax      # 0x19c08
movq (%rax), %rdi
leaq 0xb56(%rip), %rsi       # 0xf48c
callq 0xe130
testq %rax, %rax
je 0xeabe
movq %rax, %rbx
movabsq $0x2a46333849542a2a, %rax # imm = 0x2A46333849542A2A
leaq -0x98(%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 0x28d9(%rip), %xmm0     # 0x11263
movups %xmm0, (%rax)
movups 0x28df(%rip), %xmm0     # 0x11273
movups %xmm0, 0x10(%rax)
movups 0x28de(%rip), %xmm0     # 0x1127d
movups %xmm0, 0x1a(%rax)
movl $0x2a, %esi
movl $0x1, %edx
movq %rax, %rdi
movq %rbx, %rcx
callq 0xe140
movzwl 0xb259(%rip), %eax      # 0x19c18
movl %eax, %ecx
addl $0x2, %ecx
movw %cx, 0xb24d(%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 0xb211(%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 0xb209(%rip), %rdx      # 0x19c28
movzbl -0x60(%rbp,%rcx), %esi
addq %rsi, %rdx
incq %rcx
cmpq $0x13, %rcx
jne 0xea1f
movq %rdx, 0xb1f1(%rip)      # 0x19c28
addl $0x15, %eax
leaq -0x70(%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 0xb1b0(%rip), %rdi      # 0x19c20
movzwl 0xb1a1(%rip), %esi      # 0x19c18
addq $-0x2, %rsi
movl $0x1, %edx
movq %rbx, %rcx
callq 0xe140
movzwl 0xb199(%rip), %eax      # 0x19c28
leaq -0x6e(%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
callq 0xe18b