// File: /workspace/github_c_makefile_star2_O0/minkcv[P]vm/assembler.c
// Function: createLabelMap
void createLabelMap(FILE* src, LabelAddress*** labelMap)
{
    char* line = NULL;
    size_t toRead = 0; // Read until \n
    ssize_t charsRead;
    int labelCount = 0;
    uint16_t instrCount = 0;
    while ((charsRead = getline(&line, &toRead, src)) != -1)
    {
        if(line[0] == '@')
        {
            // Create the label entry in labelMap
            char* start = line;
            char* beforeAt = strsep(&line, "@");
            char* labelName = strsep(&line, " ;\n"); // Trim any comments or other text off the end
            LabelAddress* labeladdr = malloc(sizeof(LabelAddress));
            labeladdr->label = malloc(sizeof(char) * (strlen(labelName) + 1));
            strcpy(labeladdr->label, labelName);
            labeladdr->segAddr = instrCount / JUMP_SEGMENT_SIZE;
            labeladdr->offsetAddr = instrCount % JUMP_SEGMENT_SIZE;
            (*labelMap)[labelCount] = labeladdr;
            labelCount++;
            free(start);
            start = NULL;
            labelName = NULL;
            beforeAt = NULL;
            line = NULL;
        }
        else if (line[0] != ';' && line[0] != '\n')
        {
            char* token = strtok(line, " ");
            if (!strcmp(token, "LRL"))
                instrCount++;
            instrCount++;
            free(line);
            line = NULL;
        }
        else
        {
            free(line);
            line = NULL;
        }
    }
    rewind(src);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq $0x0, -0x18(%rbp)
movq $0x0, -0x20(%rbp)
movl $0x0, -0x2c(%rbp)
movw $0x0, -0x2e(%rbp)
movq -0x8(%rbp), %rdx
leaq -0x18(%rbp), %rdi
leaq -0x20(%rbp), %rsi
callq 0x1140
movq %rax, -0x28(%rbp)
cmpq $-0x1, %rax
je 0x2243
movq -0x18(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x40, %eax
jne 0x21bf
movq -0x18(%rbp), %rax
movq %rax, -0x38(%rbp)
leaq -0x18(%rbp), %rdi
leaq 0xf97(%rip), %rsi       # 0x3086
callq 0x10f0
movq %rax, -0x40(%rbp)
leaq -0x18(%rbp), %rdi
leaq 0xf79(%rip), %rsi       # 0x307c
callq 0x10f0
movq %rax, -0x48(%rbp)
movl $0x10, %edi
callq 0x10e0
movq %rax, -0x50(%rbp)
movq -0x48(%rbp), %rdi
callq 0x1080
movq %rax, %rdi
addq $0x1, %rdi
shlq $0x0, %rdi
callq 0x10e0
movq %rax, %rcx
movq -0x50(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x50(%rbp), %rax
movq 0x8(%rax), %rdi
movq -0x48(%rbp), %rsi
callq 0x1050
movzwl -0x2e(%rbp), %eax
movl $0x100, %ecx            # imm = 0x100
cltd
idivl %ecx
movb %al, %cl
movq -0x50(%rbp), %rax
movb %cl, (%rax)
movzwl -0x2e(%rbp), %eax
movl $0x100, %ecx            # imm = 0x100
cltd
idivl %ecx
movb %dl, %cl
movq -0x50(%rbp), %rax
movb %cl, 0x1(%rax)
movq -0x50(%rbp), %rdx
movq -0x10(%rbp), %rax
movq (%rax), %rax
movslq -0x2c(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
movq -0x38(%rbp), %rdi
callq 0x1030
movq $0x0, -0x38(%rbp)
movq $0x0, -0x48(%rbp)
movq $0x0, -0x40(%rbp)
movq $0x0, -0x18(%rbp)
jmp 0x223e
movq -0x18(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x3b, %eax
je 0x222b
movq -0x18(%rbp), %rax
movsbl (%rax), %eax
cmpl $0xa, %eax
je 0x222b
movq -0x18(%rbp), %rdi
leaq 0xea2(%rip), %rsi       # 0x3084
callq 0x1120
movq %rax, -0x58(%rbp)
movq -0x58(%rbp), %rdi
leaq 0xe8a(%rip), %rsi       # 0x3080
callq 0x10b0
cmpl $0x0, %eax
jne 0x220c
movw -0x2e(%rbp), %ax
addw $0x1, %ax
movw %ax, -0x2e(%rbp)
movw -0x2e(%rbp), %ax
addw $0x1, %ax
movw %ax, -0x2e(%rbp)
movq -0x18(%rbp), %rdi
callq 0x1030
movq $0x0, -0x18(%rbp)
jmp 0x223c
movq -0x18(%rbp), %rdi
callq 0x1030
movq $0x0, -0x18(%rbp)
jmp 0x223e
jmp 0x20ad
movq -0x8(%rbp), %rdi
callq 0x10a0
addq $0x60, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)