// File: /workspace/github_c_makefile_star2_O1/minkcv[P]vm/assembler.c
// Function: main
int main (int argc, char** argv)
{
    char* filename = NULL;
    char* romfilename = NULL;
    FILE* romSrc = NULL;
    uint16_t romSize = 0;
    for (int i = 1; i < argc; i++)
    {
        if (!strcmp(argv[i], "-f") && i + 1 < argc)
        {
	    filename = argv[i + 1];
	    i++;
	    continue;
        }

	if (!strcmp(argv[i], "-r") && i + 1 < argc)
        {
	    romfilename = argv[i + 1];
	    i++;
	    continue;
        }
    }
    if (filename == NULL)
    {
        printf("Usage: assember -f sourcefile.asm\n");
        exit(1);
    }

    FILE* src = fopen(filename, "r");
    if (!src)
    {
        printf("Failed to open file %s: %s\n", filename, strerror(errno));
        return 0;
    }

    if (romfilename) {
	romSrc = fopen(romfilename, "r");
	if (!romSrc)
	{
	    printf("Failed to open file %s: %s\n", romfilename, strerror(errno));
	    return 0;
	}

	fseek(romSrc, 0, SEEK_END);
	long romsize_full = ftell(romSrc);
	fseek(romSrc, 0, SEEK_SET);
	if (romsize_full > 128 * 256) {
	    printf("ROM is too large: %d > %d", romsize_full, 128 * 256);
	    return 0;
	}
	romSize = romsize_full;
    }

    uint16_t numInstructions = countInstructions(src); 
    printf("counted %d instructions\n", numInstructions);
    uint16_t* binary = malloc(sizeof(uint16_t) * numInstructions);
    char* line = NULL; // Causes getline to use malloc, we free this for every line
    size_t toRead = 0; // Passing 0 to getline reads until \n
    ssize_t charsRead;
    int instrIndex = 0;
    int lineCount = 0; // Used for error printing
    LabelAddress** labelMap = malloc(sizeof(LabelAddress*) * MAX_LABELS);
    createLabelMap(src, &labelMap);

    // Read each line in the file
    while ((charsRead = getline(&line, &toRead, src)) != -1)
    {
	lineCount++;
	// If line is not a command and we haven't reached the end of the non comment code.
	if (line[0] != '\n' && line[0] != ';' && line[0] != '@' && instrIndex < numInstructions)
	{
	    // Parse tokens separated by a space or comment
	    char* token = strtok(line, " ;\n");
	    uint16_t instr; // Assembled instruction
	    int arg = 0; // Which arg we are currently parsing
	    while (token != NULL && arg < 4)
	    {
		if (arg == 0)
		{
		    if (!strcmp(token, "LRL")) // Special assembler macro "Load Registers Label"
		    {
			uint8_t segAddr = 0;
			uint8_t offsetAddr = 0;
			uint8_t reg1;
			uint8_t reg2;
			token = strtok(NULL, " "); // Get the next token
			if (token != NULL && token[0] == 'r')
			    reg1 = atoi(token + 1);
			token = strtok(NULL, " "); // Get the next token
			if (token != NULL && token[0] == 'r')
			    reg2 = atoi(token + 1);
			token = strtok(NULL, " "); // Get the next token
			if (token[0] == '@')
			{
			    char* label = token;
			    strsep(&label, "@");
			    char* labelName = strsep(&label, " ;\n"); // Trim any comments or other text off the end
			    getAddressesOfLabel(labelName, labelMap, &segAddr, &offsetAddr);
			    instr = LRC << 12;
			    instr |= reg1 << 8;
			    instr |= segAddr;
			    binary[instrIndex] = instr; // Save the assembled instruction
			    instrIndex++;
			    instr = LRC << 12;
			    instr |= reg2 << 8;
			    instr |= offsetAddr;
			}
			arg += 3; // Parsed the entire instruction here
		    }
		    else if (!strcmp(token, "HALT"))
		    {
			instr = EXT << 12;
			instr |= EXT_HALT << 8;
			arg++;
		    }
		    else if (!strcmp(token, "CPY"))
		    {
			instr = EXT << 12;
			instr |= EXT_CPY << 8;
			arg++;
		    }
		    else if (!strcmp(token, "NOT"))
		    {
			instr = EXT << 12;
			instr |= EXT_NOT << 8;
			arg++;
		    }
		    else if (!strcmp(token, "LSL"))
		    {
			instr = EXT << 12;
			instr |= EXT_LSL << 8;
			arg++;
		    }
		    else if (!strcmp(token, "LSR"))
		    {
			instr = EXT << 12;
			instr |= EXT_LSR << 8;
			arg++;
		    }
		    else if (!strcmp(token, "JMP"))
		    {
			instr = EXT << 12;
			instr |= EXT_JMP << 8;
			arg++;
		    }
		    else if (!strcmp(token, "NOP"))
		    {
			instr = EXT << 12;
			instr |= EXT_NOP << 8;
			arg++;
		    }
		    else if (!strcmp(token, "BRKP"))
		    {
			instr = EXT << 12;
			instr |= EXT_BRKP << 8;
			arg++;
		    }
		    else if (!strcmp(token, "ADD"))
			instr = ADD << 12;
		    else if (!strcmp(token, "SUB"))
			instr = SUB << 12;
		    else if (!strcmp(token, "ADDC"))
			instr = ADDC << 12;
		    else if (!strcmp(token, "SUBC"))
			instr = SUBC << 12;
		    else if (!strcmp(token, "CMP"))
			instr = CMP << 12;
		    else if (!strcmp(token, "JLT"))
			instr = JLT << 12;
		    else if (!strcmp(token, "JGT"))
			instr = JGT << 12;
		    else if (!strcmp(token, "JEQ"))
			instr = JEQ << 12;
		    else if (!strcmp(token, "LDR"))
			instr = LDR << 12;
		    else if (!strcmp(token, "STR"))
			instr = STR << 12;
		    else if (!strcmp(token, "LRC"))
			instr = LRC << 12;
		    else if (!strcmp(token, "AND"))
			instr = AND << 12;
		    else if (!strcmp(token, "OR"))
			instr = OR << 12;
		    else if (!strcmp(token, "XOR"))
			instr = XOR << 12;
		    else
		    {
			printf("Not a known instruction on line %d\n", lineCount);
			exit(1);
		    }
		    arg++;
		}
		else if (arg < 4)
		{
		    if (token[0] == 'r')
		    {
			int shift = 0;
			if (arg == 1)
			    shift = 8;
			if (arg == 2)
			    shift = 4;
			if (arg == 3)
			    shift = 0;
			instr |= (atoi(token + 1) << shift);
		    }
		    else if (token[0] == '#' || token[0] == '$') // Token is a constant
		    {
			char* num = token;
			uint8_t constant;
			if (token[0] == '#') // A constant in base 10
			{
			    // Remove the # from the string
			    strsep(&num, "#");
			    // Convert to integer
			    constant = (uint8_t)atoi(num);
			}
			else if (token[0] == '$')
			{
			    // Remove the $ from the string
			    strsep(&num, "$");
			    // Convert to base 16
			    constant = (uint8_t)strtol(num, NULL, 16);
			}
			instr |= constant;
			arg++;
		    }
		    arg++;
		}
		token = strtok(NULL, " "); // Get the next token
	    }
	    binary[instrIndex] = retro_cpu_to_le16(instr); // Save the assembled instruction
	    instrIndex++;
	}
	// Free and null the line pointer. getline will reallocate for us
	free(line);
	line = NULL;
    }

    // Free the label map
    for (int i = 0; i < MAX_LABELS; i++)
    {
	if (labelMap[i] != NULL)
	{
	    if (labelMap[i]->label != NULL)
	    {
		free(labelMap[i]->label);
	    }
	    free(labelMap[i]);
	}
    }
    free(labelMap);

    char* filenameNoExtension = strsep(&filename, ".");
    if (filenameNoExtension != NULL)
	filename = filenameNoExtension;

    // Now write the binary array out to a file with .vaporbin appended to the name
    FILE* bin = fopen(strcat(filename, ".vaporbin"), "wb");
    uint16_t numInstructionsLe = retro_cpu_to_le16(numInstructions);
    // Prefix binary with length
    fwrite(&numInstructionsLe, sizeof(uint16_t), 1, bin);
    size_t written = fwrite(binary, sizeof(uint16_t), numInstructions, bin);
    uint16_t romSizeLe = retro_cpu_to_le16(romSize);
    fwrite(&romSizeLe, sizeof(uint16_t), 1, bin);
    if (romSize)
    {
	uint8_t *buf = malloc(romSize);
	fread(buf, sizeof(uint8_t), romSize, romSrc);
	fwrite(buf, sizeof(uint16_t), romSize, bin);
	free(buf);
    }
    fclose(bin);
    free(binary);
    binary = NULL;
    // Check that the instructions were written correctly
    if (written != numInstructions)
    {
	printf("Error writing binary file\n");
    }
    else
    {
	printf("%zd instructions written to %s\n", written, filename);
    }
    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movq $0x0, -0x48(%rbp)
cmpl $0x2, %edi
jl 0x1328
movq %rsi, %r14
movl %edi, %r15d
movl $0x1, %r13d
movq $0x0, -0x30(%rbp)
leaq 0xd3c(%rip), %r12       # 0x2004
movslq %r13d, %rax
movq (%r14,%rax,8), %rbx
movq %rbx, %rdi
movq %r12, %rsi
callq 0x10c0
testl %eax, %eax
jne 0x12f4
leal 0x1(%r13), %eax
cmpl %r15d, %eax
jge 0x12f4
movslq %eax, %rcx
movq (%r14,%rcx,8), %rcx
movq %rcx, -0x48(%rbp)
jmp 0x131b
movq %rbx, %rdi
leaq 0xd09(%rip), %rsi       # 0x2007
callq 0x10c0
testl %eax, %eax
jne 0x131e
leal 0x1(%r13), %eax
cmpl %r15d, %eax
jge 0x131e
movslq %eax, %rcx
movq (%r14,%rcx,8), %rcx
movq %rcx, -0x30(%rbp)
movl %eax, %r13d
incl %r13d
cmpl %r15d, %r13d
jl 0x12c8
jmp 0x1330
movq $0x0, -0x30(%rbp)
movq -0x48(%rbp), %rdi
testq %rdi, %rdi
je 0x1b66
leaq 0xcc4(%rip), %rsi       # 0x2008
callq 0x1120
testq %rax, %rax
movq -0x30(%rbp), %r15
je 0x13a8
movq %rax, %rbx
testq %r15, %r15
je 0x13c4
leaq 0xca7(%rip), %rsi       # 0x2008
movq %r15, %rdi
callq 0x1120
testq %rax, %rax
je 0x13cf
movq %rax, %r14
movq %rbx, %r12
movq %rax, %rdi
xorl %esi, %esi
movl $0x2, %edx
callq 0x1110
movq %r14, %rdi
callq 0x10d0
movq %rax, %rbx
movq %r14, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x1110
cmpq $0x8001, %rbx           # imm = 0x8001
jge 0x13ed
movl %ebx, %r15d
jmp 0x1406
movq -0x48(%rbp), %rbx
callq 0x1040
movl (%rax), %edi
callq 0x1180
leaq 0xc4b(%rip), %rdi       # 0x200a
movq %rbx, %rsi
jmp 0x13e5
movq %rbx, %r12
xorl %r15d, %r15d
xorl %r14d, %r14d
jmp 0x1413
callq 0x1040
movl (%rax), %edi
callq 0x1180
leaq 0xc28(%rip), %rdi       # 0x200a
movq %r15, %rsi
movq %rax, %rdx
jmp 0x1b3d
leaq 0xc32(%rip), %rdi       # 0x2026
xorl %r15d, %r15d
movq %rbx, %rsi
movl $0x8000, %edx           # imm = 0x8000
xorl %eax, %eax
callq 0x10a0
cmpq $0x8000, %rbx           # imm = 0x8000
jg 0x1b52
movq %r15, -0x98(%rbp)
movq %r14, -0x90(%rbp)
movq %r12, %r15
movq %r12, %rdi
callq 0x1b8f
movl %eax, %ebx
leaq 0xc0b(%rip), %rdi       # 0x2040
xorl %r13d, %r13d
movl %eax, %esi
xorl %eax, %eax
callq 0x10a0
movl %ebx, -0x74(%rbp)
movl %ebx, %r12d
leaq (%r12,%r12), %rdi
callq 0x10f0
movq %rax, -0x68(%rbp)
leaq -0x70(%rbp), %rbx
movq %r13, (%rbx)
leaq -0xb0(%rbp), %r14
movq %r13, (%r14)
movl $0x2000, %edi           # imm = 0x2000
callq 0x10f0
leaq -0xb8(%rbp), %rsi
movq %rax, -0x80(%rbp)
movq %rax, (%rsi)
movq %r15, %rdi
callq 0x1c55
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x1150
cmpq $-0x1, %rax
je 0x19f2
leaq 0xbb9(%rip), %rbx       # 0x205d
leaq 0xbb6(%rip), %r13       # 0x2061
movq %rax, -0x88(%rbp)
movl %eax, -0x58(%rbp)
movl %eax, -0x54(%rbp)
movl %eax, -0x30(%rbp)
xorl %r14d, %r14d
movq $0x0, -0x50(%rbp)
movq %r12, -0xa8(%rbp)
movq %r15, -0xa0(%rbp)
incl %r14d
movq -0x70(%rbp), %rdi
movzbl (%rdi), %eax
addl $-0xa, %eax
cmpl $0x36, %eax
ja 0x1525
movabsq $0x42000000000001, %rcx # imm = 0x42000000000001
btq %rax, %rcx
jae 0x1525
movq -0x70(%rbp), %rdi
callq 0x1030
movq $0x0, -0x70(%rbp)
leaq -0x70(%rbp), %rdi
leaq -0xb0(%rbp), %rsi
movq %r15, %rdx
callq 0x1150
cmpq $-0x1, %rax
jne 0x14d4
jmp 0x19f2
cmpl %r12d, -0x50(%rbp)
jge 0x14f6
leaq 0xb27(%rip), %rsi       # 0x2059
callq 0x1130
testq %rax, %rax
je 0x19c7
movq %rax, %r12
xorl %r15d, %r15d
movl %r14d, -0x5c(%rbp)
testl %r15d, %r15d
je 0x15a0
movzbl (%r12), %eax
leal -0x23(%rax), %ecx
cmpl $0x2, %ecx
jae 0x17c5
movq %r12, -0x40(%rbp)
movzbl (%r12), %eax
cmpl $0x24, %eax
je 0x18e8
cmpl $0x23, %eax
movq -0x88(%rbp), %rax
jne 0x1908
leaq -0x40(%rbp), %rdi
leaq 0xb54(%rip), %rsi       # 0x20e1
callq 0x1100
movq -0x40(%rbp), %rdi
callq 0x1140
jmp 0x1908
movq %r12, %rdi
movq %rbx, %rsi
callq 0x10c0
testl %eax, %eax
je 0x180c
movq %r12, %rdi
leaq 0xaa8(%rip), %rsi       # 0x2065
callq 0x10c0
movl $0x2, %r15d
testl %eax, %eax
je 0x1940
movq %r12, %rdi
leaq 0xa90(%rip), %rsi       # 0x206a
callq 0x10c0
testl %eax, %eax
je 0x1949
movq %r12, %rdi
leaq 0xa7d(%rip), %rsi       # 0x206e
callq 0x10c0
testl %eax, %eax
je 0x194f
movq %r12, %rdi
leaq 0xa6a(%rip), %rsi       # 0x2072
callq 0x10c0
testl %eax, %eax
je 0x1955
movq %r12, %rdi
leaq 0xa57(%rip), %rsi       # 0x2076
callq 0x10c0
testl %eax, %eax
je 0x195b
movq %r12, %rdi
leaq 0xa44(%rip), %rsi       # 0x207a
callq 0x10c0
testl %eax, %eax
je 0x1961
movq %r12, %rdi
leaq 0xa31(%rip), %rsi       # 0x207e
callq 0x10c0
testl %eax, %eax
je 0x1967
movq %r12, %rdi
leaq 0xa1e(%rip), %rsi       # 0x2082
callq 0x10c0
testl %eax, %eax
je 0x196d
movq %r12, %rdi
leaq 0xa0c(%rip), %rsi       # 0x2087
callq 0x10c0
movl $0x1, %r15d
testl %eax, %eax
je 0x1973
movq %r12, %rdi
leaq 0x9f3(%rip), %rsi       # 0x208b
callq 0x10c0
testl %eax, %eax
je 0x1979
movq %r12, %rdi
leaq 0x9e0(%rip), %rsi       # 0x208f
callq 0x10c0
testl %eax, %eax
je 0x197f
movq %r12, %rdi
leaq 0x9ce(%rip), %rsi       # 0x2094
callq 0x10c0
testl %eax, %eax
je 0x1985
movq %r12, %rdi
leaq 0x9bc(%rip), %rsi       # 0x2099
callq 0x10c0
testl %eax, %eax
je 0x198b
movq %r12, %rdi
leaq 0x9a9(%rip), %rsi       # 0x209d
callq 0x10c0
testl %eax, %eax
je 0x1991
movq %r12, %rdi
leaq 0x996(%rip), %rsi       # 0x20a1
callq 0x10c0
testl %eax, %eax
je 0x1997
movq %r12, %rdi
leaq 0x983(%rip), %rsi       # 0x20a5
callq 0x10c0
testl %eax, %eax
je 0x199d
movq %r12, %rdi
leaq 0x970(%rip), %rsi       # 0x20a9
callq 0x10c0
testl %eax, %eax
je 0x19a3
movq %r12, %rdi
leaq 0x95d(%rip), %rsi       # 0x20ad
callq 0x10c0
testl %eax, %eax
je 0x19a9
movq %r12, %rdi
leaq 0x94a(%rip), %rsi       # 0x20b1
callq 0x10c0
testl %eax, %eax
je 0x19af
movq %r12, %rdi
leaq 0x937(%rip), %rsi       # 0x20b5
callq 0x10c0
testl %eax, %eax
je 0x19b5
movq %r12, %rdi
leaq 0x925(%rip), %rsi       # 0x20ba
callq 0x10c0
testl %eax, %eax
je 0x19bb
movq %r12, %rdi
leaq 0x90d(%rip), %rsi       # 0x20b9
callq 0x10c0
movw $0xe000, %cx            # imm = 0xE000
movl %ecx, -0x30(%rbp)
testl %eax, %eax
je 0x191b
jmp 0x1b7c
cmpl $0x72, %eax
jne 0x1918
cmpl $0x1, %r15d
movq %r13, %r14
movq %rbx, %r13
sete %bl
shlb $0x3, %bl
incq %r12
movq %r12, %rdi
callq 0x1140
cmpl $0x2, %r15d
movzbl %bl, %ecx
movq %r13, %rbx
movq %r14, %r13
movl -0x5c(%rbp), %r14d
movl $0x4, %edx
cmovel %edx, %ecx
shll %cl, %eax
orl %eax, -0x30(%rbp)
jmp 0x1918
xorl %eax, %eax
movb %al, -0x34(%rbp)
movb %al, -0x31(%rbp)
xorl %edi, %edi
movq %r13, %rsi
callq 0x1130
testq %rax, %rax
je 0x1836
cmpb $0x72, (%rax)
jne 0x1836
incq %rax
movq %rax, %rdi
callq 0x1140
movl %eax, -0x54(%rbp)
xorl %edi, %edi
movq %r13, %rsi
callq 0x1130
testq %rax, %rax
je 0x1858
cmpb $0x72, (%rax)
jne 0x1858
incq %rax
movq %rax, %rdi
callq 0x1140
movl %eax, -0x58(%rbp)
xorl %edi, %edi
movq %r13, %rsi
callq 0x1130
cmpb $0x40, (%rax)
jne 0x18e0
movq %rax, -0x40(%rbp)
leaq -0x40(%rbp), %r14
movq %r14, %rdi
leaq 0x7ea(%rip), %rsi       # 0x2063
callq 0x1100
movq %r14, %rdi
movl -0x5c(%rbp), %r14d
leaq 0x7cd(%rip), %rsi       # 0x2059
callq 0x1100
movq %rax, %rdi
movq -0x80(%rbp), %rsi
leaq -0x34(%rbp), %rdx
leaq -0x31(%rbp), %rcx
callq 0x1db5
movzbl -0x54(%rbp), %eax
shll $0x8, %eax
movzbl -0x34(%rbp), %ecx
orl %eax, %ecx
movl $0xb000, %edx           # imm = 0xB000
orl %edx, %ecx
movq -0x50(%rbp), %rsi
movslq %esi, %rsi
movq -0x68(%rbp), %rax
movw %cx, (%rax,%rsi,2)
incl %esi
movq %rsi, -0x50(%rbp)
movzbl -0x58(%rbp), %eax
shll $0x8, %eax
movzbl -0x31(%rbp), %ecx
orl %eax, %ecx
orl %edx, %ecx
movl %ecx, -0x30(%rbp)
movl $0x4, %r15d
jmp 0x191b
leaq -0x40(%rbp), %rdi
leaq 0x7f0(%rip), %rsi       # 0x20e3
callq 0x1100
movq -0x40(%rbp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x10e0
movq %rax, -0x88(%rbp)
movzbl %al, %eax
orl %eax, -0x30(%rbp)
incl %r15d
incl %r15d
xorl %edi, %edi
movq %r13, %rsi
callq 0x1130
testq %rax, %rax
je 0x19c7
movq %rax, %r12
cmpl $0x4, %r15d
jl 0x154a
jmp 0x19c7
movl $0x0, -0x30(%rbp)
jmp 0x191b
movw $0x100, %ax             # imm = 0x100
jmp 0x19bf
movw $0x200, %ax             # imm = 0x200
jmp 0x19bf
movw $0x300, %ax             # imm = 0x300
jmp 0x19bf
movw $0x400, %ax             # imm = 0x400
jmp 0x19bf
movw $0x500, %ax             # imm = 0x500
jmp 0x19bf
movw $0x600, %ax             # imm = 0x600
jmp 0x19bf
movw $0x700, %ax             # imm = 0x700
jmp 0x19bf
movw $0x1000, %ax            # imm = 0x1000
jmp 0x19bf
movw $0x2000, %ax            # imm = 0x2000
jmp 0x19bf
movw $0x3000, %ax            # imm = 0x3000
jmp 0x19bf
movw $0x4000, %ax            # imm = 0x4000
jmp 0x19bf
movw $0x5000, %ax            # imm = 0x5000
jmp 0x19bf
movw $0x6000, %ax            # imm = 0x6000
jmp 0x19bf
movw $0x7000, %ax            # imm = 0x7000
jmp 0x19bf
movw $0x8000, %ax            # imm = 0x8000
jmp 0x19bf
movw $0x9000, %ax            # imm = 0x9000
jmp 0x19bf
movw $0xa000, %ax            # imm = 0xA000
jmp 0x19bf
movw $0xb000, %ax            # imm = 0xB000
jmp 0x19bf
movw $0xc000, %ax            # imm = 0xC000
jmp 0x19bf
movw $0xd000, %ax            # imm = 0xD000
movl %eax, -0x30(%rbp)
jmp 0x191b
movq -0x50(%rbp), %rcx
movslq %ecx, %rcx
movq -0x68(%rbp), %rax
movl -0x30(%rbp), %edx
movw %dx, (%rax,%rcx,2)
incl %ecx
movq %rcx, -0x50(%rbp)
movq -0xa8(%rbp), %r12
movq -0xa0(%rbp), %r15
jmp 0x14f6
xorl %ebx, %ebx
movq -0x80(%rbp), %r14
movq (%r14,%rbx,8), %rax
testq %rax, %rax
je 0x1a18
movq 0x8(%rax), %rdi
testq %rdi, %rdi
je 0x1a0f
callq 0x1030
movq (%r14,%rbx,8), %rdi
callq 0x1030
incq %rbx
cmpq $0x400, %rbx            # imm = 0x400
jne 0x19f8
movq %r14, %rdi
callq 0x1030
leaq 0x6b2(%rip), %rsi       # 0x20e5
leaq -0x48(%rbp), %rdi
callq 0x1100
testq %rax, %rax
je 0x1a45
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rbx
movq %rbx, %rdi
callq 0x1090
movabsq $0x6962726f7061762e, %rcx # imm = 0x6962726F7061762E
movq %rcx, (%rbx,%rax)
movw $0x6e, 0x8(%rbx,%rax)
leaq 0x684(%rip), %rsi       # 0x20f1
movq %rbx, %rdi
callq 0x1120
movq %rax, %rbx
leaq -0x40(%rbp), %rdi
movl -0x74(%rbp), %eax
movw %ax, (%rdi)
movl $0x2, %esi
movl $0x1, %edx
movq %rbx, %rcx
callq 0x1170
movl $0x2, %esi
movq -0x68(%rbp), %rdi
movq %r12, %rdx
movq %rbx, %rcx
callq 0x1170
movq %rax, %r14
leaq -0x34(%rbp), %rdi
movq -0x98(%rbp), %r15
movw %r15w, (%rdi)
movl $0x2, %esi
movl $0x1, %edx
movq %rbx, %rcx
callq 0x1170
testw %r15w, %r15w
je 0x1b19
movzwl %r15w, %r15d
movq %r15, %rdi
callq 0x10f0
movq %r12, %r13
movq %rax, %r12
movl $0x1, %esi
movq %rax, %rdi
movq %r15, %rdx
movq -0x90(%rbp), %rcx
callq 0x1070
movl $0x2, %esi
movq %r12, %rdi
movq %r15, %rdx
movq %rbx, %rcx
callq 0x1170
movq %r12, %rdi
movq %r13, %r12
callq 0x1030
movq %rbx, %rdi
callq 0x1080
movq -0x68(%rbp), %rdi
callq 0x1030
cmpq %r12, %r14
jne 0x1b46
movq -0x48(%rbp), %rdx
leaq 0x5ba(%rip), %rdi       # 0x20f4
movq %r14, %rsi
xorl %eax, %eax
callq 0x10a0
jmp 0x1b52
leaq 0x5e6(%rip), %rdi       # 0x2133
callq 0x1060
xorl %eax, %eax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x5e0(%rip), %rdi       # 0x214d
callq 0x1060
movl $0x1, %edi
callq 0x1160
leaq 0x53a(%rip), %rdi       # 0x20bd
movl %r14d, %esi
xorl %eax, %eax
callq 0x10a0
jmp 0x1b72