// File: /workspace/github_c_makefile_star2_O0/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
subq $0x80, %rsp
movq $0x0, -0x8(%rbp)
leaq -0x8(%rbp), %rdi
callq 0xe630
movl %eax, -0xc(%rbp)
movl $0x0, -0x10(%rbp)
movl $0x4, -0x14(%rbp)
movl -0x10(%rbp), %eax
movl -0xc(%rbp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jge 0xeadc
movl $0x0, -0x18(%rbp)
movl $0x0, -0x1c(%rbp)
movslq -0x1c(%rbp), %rcx
leaq 0x6d29(%rip), %rax      # 0x15470
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
leaq 0xb493(%rip), %rcx      # 0x19be8
movl (%rcx), %ecx
cmpq $0x0, (%rax,%rcx,8)
je 0xeaa8
movslq -0x1c(%rbp), %rcx
leaq 0x6d03(%rip), %rax      # 0x15470
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
leaq 0xb46d(%rip), %rcx      # 0x19be8
movl (%rcx), %ecx
movq (%rax,%rcx,8), %rax
movq %rax, -0x78(%rbp)
movq -0x8(%rbp), %rax
movslq -0x10(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x70(%rbp)
movslq -0x1c(%rbp), %rcx
leaq 0x6cd1(%rip), %rax      # 0x15470
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
leaq 0xb43b(%rip), %rcx      # 0x19be8
movl (%rcx), %ecx
movq (%rax,%rcx,8), %rdi
callq 0xe0a0
movq -0x78(%rbp), %rdi
movq -0x70(%rbp), %rsi
movq %rax, %rdx
callq 0xe050
cmpl $0x0, %eax
jne 0xea98
movslq -0x1c(%rbp), %rcx
leaq 0x48e4(%rip), %rax      # 0x130c0
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movw (%rax), %ax
movw %ax, -0x1e(%rbp)
movslq -0x1c(%rbp), %rcx
leaq 0x6c7b(%rip), %rax      # 0x15470
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
leaq 0xb3e5(%rip), %rcx      # 0x19be8
movl (%rcx), %ecx
movq (%rax,%rcx,8), %rsi
leaq 0x29ea(%rip), %rdi      # 0x111fa
callq 0xe0d0
cmpl $0x0, %eax
jne 0xe82c
cmpl $0x0, -0x14(%rbp)
je 0xe826
cmpl $0x3, -0x14(%rbp)
jne 0xe82c
movw $0xb0, -0x1e(%rbp)
movslq -0x1c(%rbp), %rcx
leaq 0x6c39(%rip), %rax      # 0x15470
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
leaq 0xb3a3(%rip), %rcx      # 0x19be8
movl (%rcx), %ecx
movq (%rax,%rcx,8), %rsi
leaq 0xa36(%rip), %rdi       # 0xf288
callq 0xe0d0
cmpl $0x0, %eax
jne 0xe87d
leaq 0xb3ad(%rip), %rax      # 0x19c10
testb $0x1, (%rax)
je 0xe87d
movl $0x1, -0x18(%rbp)
movl -0x10(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x10(%rbp)
jmp 0xeaa8
movzwl 0xb394(%rip), %eax      # 0x19c18
cmpl $0x0, %eax
jne 0xe958
movslq -0x1c(%rbp), %rcx
leaq 0x4828(%rip), %rax      # 0x130c0
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
cmpl $0x0, 0x4(%rax)
jne 0xe8e5
movl $0x1, %edi
callq 0xe100
movq %rax, 0xb36a(%rip)      # 0x19c20
movzwl -0x1e(%rbp), %eax
andl $0xff, %eax
movb %al, %dl
movq 0xb358(%rip), %rax      # 0x19c20
movw 0xb349(%rip), %cx       # 0x19c18
movw %cx, %si
addw $0x1, %si
movw %si, 0xb33b(%rip)       # 0x19c18
movzwl %cx, %ecx
movb %dl, (%rax,%rcx)
jmp 0xe953
movl $0x2, %edi
callq 0xe100
movq %rax, 0xb32a(%rip)      # 0x19c20
movzwl -0x1e(%rbp), %eax
sarl $0x8, %eax
andl $0xff, %eax
movb %al, %dl
movq 0xb315(%rip), %rax      # 0x19c20
movw 0xb306(%rip), %cx       # 0x19c18
movw %cx, %si
addw $0x1, %si
movw %si, 0xb2f8(%rip)       # 0x19c18
movzwl %cx, %ecx
movb %dl, (%rax,%rcx)
movzwl -0x1e(%rbp), %eax
andl $0xff, %eax
movb %al, %dl
movq 0xb2e8(%rip), %rax      # 0x19c20
movw 0xb2d9(%rip), %cx       # 0x19c18
movw %cx, %si
addw $0x1, %si
movw %si, 0xb2cb(%rip)       # 0x19c18
movzwl %cx, %ecx
movb %dl, (%rax,%rcx)
jmp 0xea34
movslq -0x1c(%rbp), %rcx
leaq 0x475d(%rip), %rax      # 0x130c0
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
cmpl $0x0, 0x4(%rax)
jne 0xe9be
movq 0xb2a9(%rip), %rdi      # 0x19c20
movw 0xb29a(%rip), %ax       # 0x19c18
addw $0x1, %ax
movw %ax, 0xb28f(%rip)       # 0x19c18
movzwl %ax, %eax
movl %eax, %esi
callq 0xe120
movq %rax, 0xb286(%rip)      # 0x19c20
movzwl -0x1e(%rbp), %eax
andl $0xff, %eax
movb %al, %dl
movq 0xb274(%rip), %rax      # 0x19c20
movzwl 0xb265(%rip), %ecx      # 0x19c18
subl $0x1, %ecx
movslq %ecx, %rcx
movb %dl, (%rax,%rcx)
jmp 0xea32
movzwl 0xb253(%rip), %eax      # 0x19c18
addl $0x2, %eax
movw %ax, 0xb249(%rip)       # 0x19c18
movq 0xb24a(%rip), %rdi      # 0x19c20
movzwl 0xb23b(%rip), %eax      # 0x19c18
movl %eax, %esi
callq 0xe120
movq %rax, 0xb235(%rip)      # 0x19c20
movzwl -0x1e(%rbp), %eax
sarl $0x8, %eax
andl $0xff, %eax
movb %al, %dl
movq 0xb220(%rip), %rax      # 0x19c20
movzwl 0xb211(%rip), %ecx      # 0x19c18
subl $0x2, %ecx
movslq %ecx, %rcx
movb %dl, (%rax,%rcx)
movzwl -0x1e(%rbp), %eax
andl $0xff, %eax
movb %al, %dl
movq 0xb1fe(%rip), %rax      # 0x19c20
movzwl 0xb1ef(%rip), %ecx      # 0x19c18
subl $0x1, %ecx
movslq %ecx, %rcx
movb %dl, (%rax,%rcx)
jmp 0xea34
movzwl -0x1e(%rbp), %eax
addq 0xb1e9(%rip), %rax      # 0x19c28
movq %rax, 0xb1e2(%rip)      # 0x19c28
movslq -0x1c(%rbp), %rcx
leaq 0x6a1f(%rip), %rax      # 0x15470
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
leaq 0xb189(%rip), %rcx      # 0x19be8
movl (%rcx), %ecx
movq (%rax,%rcx,8), %rdi
callq 0xe0a0
movq %rax, %rcx
movslq -0x10(%rbp), %rax
addq %rcx, %rax
movl %eax, -0x10(%rbp)
movslq -0x1c(%rbp), %rcx
leaq 0x463e(%rip), %rax      # 0x130c0
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
movl %eax, -0x14(%rbp)
movl $0x1, -0x18(%rbp)
jmp 0xeaa8
jmp 0xea9a
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0xe73c
cmpl $0x0, -0x18(%rbp)
jne 0xead7
movq 0x4543(%rip), %rax      # 0x12ff8
movq (%rax), %rdi
movq -0x8(%rbp), %rax
movslq -0x10(%rbp), %rcx
movsbl (%rax,%rcx), %edx
leaq 0x2771(%rip), %rsi      # 0x1123c
movb $0x0, %al
callq 0xe0e0
callq 0xe030
jmp 0xe71d
leaq 0xb125(%rip), %rax      # 0x19c08
movq (%rax), %rdi
leaq 0x99f(%rip), %rsi       # 0xf48c
callq 0xe130
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
jne 0xeb24
movq 0x44f4(%rip), %rax      # 0x12ff8
movq (%rax), %rdi
leaq 0xb0fa(%rip), %rax      # 0x19c08
movq (%rax), %rdx
leaq 0x273a(%rip), %rsi      # 0x11252
movb $0x0, %al
callq 0xe0e0
callq 0xe030
movq 0x2762(%rip), %rax      # 0x1128d
movq %rax, -0x33(%rbp)
movw 0x275f(%rip), %ax       # 0x11295
movw %ax, -0x2b(%rbp)
movb 0x2757(%rip), %al       # 0x11297
movb %al, -0x29(%rbp)
leaq -0x33(%rbp), %rdi
movq -0x28(%rbp), %rcx
movl $0xb, %esi
movl $0x1, %edx
callq 0xe140
movl $0x2a, %edi
movl $0x1, %esi
callq 0xe0c0
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rdi
leaq 0x26eb(%rip), %rsi      # 0x11263
callq 0xe060
movq -0x40(%rbp), %rdi
movq -0x28(%rbp), %rcx
movl $0x2a, %esi
movl $0x1, %edx
callq 0xe140
movzwl 0xb07d(%rip), %eax      # 0x19c18
addl $0x2, %eax
movw %ax, 0xb073(%rip)       # 0x19c18
movb $0xd, -0x60(%rbp)
movb $0x0, -0x5f(%rbp)
movw 0xb064(%rip), %ax       # 0x19c18
movb %al, -0x5e(%rbp)
movzwl 0xb05a(%rip), %eax      # 0x19c18
sarl $0x8, %eax
movb %al, -0x5d(%rbp)
movb $0x5, -0x5c(%rbp)
leaq 0xb029(%rip), %rax      # 0x19bf8
movb (%rax), %al
movb %al, -0x5b(%rbp)
leaq 0xb01d(%rip), %rax      # 0x19bf8
movb 0x1(%rax), %al
movb %al, -0x5a(%rbp)
leaq 0xb010(%rip), %rax      # 0x19bf8
movb 0x2(%rax), %al
movb %al, -0x59(%rbp)
leaq 0xb003(%rip), %rax      # 0x19bf8
movb 0x3(%rax), %al
movb %al, -0x58(%rbp)
leaq 0xaff6(%rip), %rax      # 0x19bf8
movb 0x4(%rax), %al
movb %al, -0x57(%rbp)
leaq 0xafe9(%rip), %rax      # 0x19bf8
movb 0x5(%rax), %al
movb %al, -0x56(%rbp)
leaq 0xafdc(%rip), %rax      # 0x19bf8
movb 0x6(%rax), %al
movb %al, -0x55(%rbp)
leaq 0xafcf(%rip), %rax      # 0x19bf8
movb 0x7(%rax), %al
movb %al, -0x54(%rbp)
movb $0x0, -0x53(%rbp)
movb $0x0, -0x52(%rbp)
movw 0xafda(%rip), %ax       # 0x19c18
movb %al, -0x51(%rbp)
movzwl 0xafd0(%rip), %eax      # 0x19c18
sarl $0x8, %eax
movb %al, -0x50(%rbp)
movb $0x4, -0x4f(%rbp)
movb $0x0, -0x4e(%rbp)
movl $0x0, -0x64(%rbp)
movslq -0x64(%rbp), %rax
cmpq $0x13, %rax
jae 0xec89
movslq -0x64(%rbp), %rax
movzbl -0x60(%rbp,%rax), %eax
addq 0xafb1(%rip), %rax      # 0x19c28
movq %rax, 0xafaa(%rip)      # 0x19c28
movl -0x64(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x64(%rbp)
jmp 0xec5d
movzwl 0xaf88(%rip), %eax      # 0x19c18
addq $0x13, %rax
movw %ax, -0x66(%rbp)
movq -0x28(%rbp), %rcx
leaq -0x66(%rbp), %rdi
movl $0x2, %esi
movl $0x1, %edx
callq 0xe140
leaq -0x60(%rbp), %rdi
movq -0x28(%rbp), %rcx
movl $0x13, %esi
movl $0x1, %edx
callq 0xe140
movq 0xaf53(%rip), %rdi      # 0x19c20
movzwl 0xaf44(%rip), %eax      # 0x19c18
subl $0x2, %eax
movslq %eax, %rsi
movq -0x28(%rbp), %rcx
movl $0x1, %edx
callq 0xe140
movq 0xaf39(%rip), %rax      # 0x19c28
andq $0xffff, %rax           # imm = 0xFFFF
movw %ax, -0x68(%rbp)
movq -0x28(%rbp), %rcx
leaq -0x68(%rbp), %rdi
movl $0x2, %esi
movl $0x1, %edx
callq 0xe140
movq -0x28(%rbp), %rdi
callq 0xe090
addq $0x80, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)