// File: /workspace/github_c_makefile_star2_O0/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
subq $0x100, %rsp            # imm = 0x100
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq $0x0, -0x18(%rbp)
movq $0x0, -0x20(%rbp)
movq $0x0, -0x28(%rbp)
movw $0x0, -0x2a(%rbp)
movl $0x1, -0x30(%rbp)
movl -0x30(%rbp), %eax
cmpl -0x8(%rbp), %eax
jge 0x137a
movq -0x10(%rbp), %rax
movslq -0x30(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
leaq 0x1d17(%rip), %rsi      # 0x3004
callq 0x10b0
cmpl $0x0, %eax
jne 0x1322
movl -0x30(%rbp), %eax
addl $0x1, %eax
cmpl -0x8(%rbp), %eax
jge 0x1322
movq -0x10(%rbp), %rax
movl -0x30(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x18(%rbp)
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0x136c
movq -0x10(%rbp), %rax
movslq -0x30(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
leaq 0x1cd2(%rip), %rsi      # 0x3007
callq 0x10b0
cmpl $0x0, %eax
jne 0x136a
movl -0x30(%rbp), %eax
addl $0x1, %eax
cmpl -0x8(%rbp), %eax
jge 0x136a
movq -0x10(%rbp), %rax
movl -0x30(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x20(%rbp)
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0x136c
jmp 0x136c
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0x12ce
cmpq $0x0, -0x18(%rbp)
jne 0x1399
leaq 0x1c82(%rip), %rdi      # 0x300a
movb $0x0, %al
callq 0x1090
movl $0x1, %edi
callq 0x1160
movq -0x18(%rbp), %rdi
leaq 0x1c64(%rip), %rsi      # 0x3008
callq 0x1110
movq %rax, -0x38(%rbp)
cmpq $0x0, -0x38(%rbp)
jne 0x13ef
movq -0x18(%rbp), %rax
movq %rax, -0xf0(%rbp)
callq 0x1040
movl (%rax), %edi
callq 0x1180
movq -0xf0(%rbp), %rsi
movq %rax, %rdx
leaq 0x1c51(%rip), %rdi      # 0x302d
movb $0x0, %al
callq 0x1090
movl $0x0, -0x4(%rbp)
jmp 0x1f9f
cmpq $0x0, -0x20(%rbp)
je 0x14b3
movq -0x20(%rbp), %rdi
leaq 0x1c03(%rip), %rsi      # 0x3008
callq 0x1110
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
jne 0x1450
movq -0x20(%rbp), %rax
movq %rax, -0xf8(%rbp)
callq 0x1040
movl (%rax), %edi
callq 0x1180
movq -0xf8(%rbp), %rsi
movq %rax, %rdx
leaq 0x1bf0(%rip), %rdi      # 0x302d
movb $0x0, %al
callq 0x1090
movl $0x0, -0x4(%rbp)
jmp 0x1f9f
movq -0x28(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
movl $0x2, %edx
callq 0x1100
movq -0x28(%rbp), %rdi
callq 0x10c0
movq %rax, -0x40(%rbp)
movq -0x28(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x1100
cmpq $0x8000, -0x40(%rbp)    # imm = 0x8000
jle 0x14ab
movq -0x40(%rbp), %rsi
leaq 0x1bb6(%rip), %rdi      # 0x3049
movl $0x8000, %edx           # imm = 0x8000
movb $0x0, %al
callq 0x1090
movl $0x0, -0x4(%rbp)
jmp 0x1f9f
movq -0x40(%rbp), %rax
movw %ax, -0x2a(%rbp)
movq -0x38(%rbp), %rdi
callq 0x1fb0
movw %ax, -0x42(%rbp)
movzwl -0x42(%rbp), %esi
leaq 0x1b98(%rip), %rdi      # 0x3063
movb $0x0, %al
callq 0x1090
movzwl -0x42(%rbp), %eax
movl %eax, %edi
shlq %rdi
callq 0x10e0
movq %rax, -0x50(%rbp)
movq $0x0, -0x58(%rbp)
movq $0x0, -0x60(%rbp)
movl $0x0, -0x6c(%rbp)
movl $0x0, -0x70(%rbp)
movl $0x2000, %edi           # imm = 0x2000
callq 0x10e0
movq %rax, -0x78(%rbp)
movq -0x38(%rbp), %rdi
leaq -0x78(%rbp), %rsi
callq 0x2080
movq -0x38(%rbp), %rdx
leaq -0x58(%rbp), %rdi
leaq -0x60(%rbp), %rsi
callq 0x1140
movq %rax, -0x68(%rbp)
cmpq $-0x1, %rax
je 0x1d9a
movl -0x70(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x70(%rbp)
movq -0x58(%rbp), %rax
movsbl (%rax), %eax
cmpl $0xa, %eax
je 0x1d84
movq -0x58(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x3b, %eax
je 0x1d84
movq -0x58(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x40, %eax
je 0x1d84
movl -0x6c(%rbp), %eax
movzwl -0x42(%rbp), %ecx
cmpl %ecx, %eax
jge 0x1d84
movq -0x58(%rbp), %rdi
leaq 0x1aed(%rip), %rsi      # 0x307c
callq 0x1120
movq %rax, -0x80(%rbp)
movl $0x0, -0x88(%rbp)
xorl %eax, %eax
cmpq $0x0, -0x80(%rbp)
movb %al, -0xf9(%rbp)
je 0x15c1
cmpl $0x4, -0x88(%rbp)
setl %al
movb %al, -0xf9(%rbp)
movb -0xf9(%rbp), %al
testb $0x1, %al
jne 0x15d0
jmp 0x1d68
cmpl $0x0, -0x88(%rbp)
jne 0x1bf8
movq -0x80(%rbp), %rdi
leaq 0x1a98(%rip), %rsi      # 0x3080
callq 0x10b0
cmpl $0x0, %eax
jne 0x1796
movb $0x0, -0x89(%rbp)
movb $0x0, -0x8a(%rbp)
xorl %eax, %eax
movl %eax, %edi
leaq 0x1a75(%rip), %rsi      # 0x3084
callq 0x1120
movq %rax, -0x80(%rbp)
cmpq $0x0, -0x80(%rbp)
je 0x163e
movq -0x80(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x72, %eax
jne 0x163e
movq -0x80(%rbp), %rdi
addq $0x1, %rdi
callq 0x1130
movb %al, -0x8b(%rbp)
xorl %eax, %eax
movl %eax, %edi
leaq 0x1a3b(%rip), %rsi      # 0x3084
callq 0x1120
movq %rax, -0x80(%rbp)
cmpq $0x0, -0x80(%rbp)
je 0x1678
movq -0x80(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x72, %eax
jne 0x1678
movq -0x80(%rbp), %rdi
addq $0x1, %rdi
callq 0x1130
movb %al, -0x8c(%rbp)
xorl %eax, %eax
movl %eax, %edi
leaq 0x1a01(%rip), %rsi      # 0x3084
callq 0x1120
movq %rax, -0x80(%rbp)
movq -0x80(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x40, %eax
jne 0x1782
movq -0x80(%rbp), %rax
movq %rax, -0x98(%rbp)
leaq -0x98(%rbp), %rdi
leaq 0x19d1(%rip), %rsi      # 0x3086
callq 0x10f0
leaq -0x98(%rbp), %rdi
leaq 0x19b4(%rip), %rsi      # 0x307c
callq 0x10f0
movq %rax, -0xa0(%rbp)
movq -0xa0(%rbp), %rdi
movq -0x78(%rbp), %rsi
leaq -0x89(%rbp), %rdx
leaq -0x8a(%rbp), %rcx
callq 0x2260
movw $0xb000, -0x82(%rbp)    # imm = 0xB000
movzbl -0x8b(%rbp), %ecx
shll $0x8, %ecx
movzwl -0x82(%rbp), %eax
orl %ecx, %eax
movw %ax, -0x82(%rbp)
movzbl -0x89(%rbp), %ecx
movzwl -0x82(%rbp), %eax
orl %ecx, %eax
movw %ax, -0x82(%rbp)
movw -0x82(%rbp), %dx
movq -0x50(%rbp), %rax
movslq -0x6c(%rbp), %rcx
movw %dx, (%rax,%rcx,2)
movl -0x6c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x6c(%rbp)
movw $0xb000, -0x82(%rbp)    # imm = 0xB000
movzbl -0x8c(%rbp), %ecx
shll $0x8, %ecx
movzwl -0x82(%rbp), %eax
orl %ecx, %eax
movw %ax, -0x82(%rbp)
movzbl -0x8a(%rbp), %ecx
movzwl -0x82(%rbp), %eax
orl %ecx, %eax
movw %ax, -0x82(%rbp)
movl -0x88(%rbp), %eax
addl $0x3, %eax
movl %eax, -0x88(%rbp)
jmp 0x1be4
movq -0x80(%rbp), %rdi
leaq 0x18e7(%rip), %rsi      # 0x3088
callq 0x10b0
cmpl $0x0, %eax
jne 0x17d9
movw $0x0, -0x82(%rbp)
movzwl -0x82(%rbp), %eax
orl $0x0, %eax
movw %ax, -0x82(%rbp)
movl -0x88(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x88(%rbp)
jmp 0x1be2
movq -0x80(%rbp), %rdi
leaq 0x18a9(%rip), %rsi      # 0x308d
callq 0x10b0
cmpl $0x0, %eax
jne 0x181e
movw $0x0, -0x82(%rbp)
movzwl -0x82(%rbp), %eax
orl $0x100, %eax            # imm = 0x100
movw %ax, -0x82(%rbp)
movl -0x88(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x88(%rbp)
jmp 0x1be0
movq -0x80(%rbp), %rdi
leaq 0x1868(%rip), %rsi      # 0x3091
callq 0x10b0
cmpl $0x0, %eax
jne 0x1863
movw $0x0, -0x82(%rbp)
movzwl -0x82(%rbp), %eax
orl $0x200, %eax            # imm = 0x200
movw %ax, -0x82(%rbp)
movl -0x88(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x88(%rbp)
jmp 0x1bde
movq -0x80(%rbp), %rdi
leaq 0x1827(%rip), %rsi      # 0x3095
callq 0x10b0
cmpl $0x0, %eax
jne 0x18a8
movw $0x0, -0x82(%rbp)
movzwl -0x82(%rbp), %eax
orl $0x300, %eax            # imm = 0x300
movw %ax, -0x82(%rbp)
movl -0x88(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x88(%rbp)
jmp 0x1bdc
movq -0x80(%rbp), %rdi
leaq 0x17e6(%rip), %rsi      # 0x3099
callq 0x10b0
cmpl $0x0, %eax
jne 0x18ed
movw $0x0, -0x82(%rbp)
movzwl -0x82(%rbp), %eax
orl $0x400, %eax            # imm = 0x400
movw %ax, -0x82(%rbp)
movl -0x88(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x88(%rbp)
jmp 0x1bda
movq -0x80(%rbp), %rdi
leaq 0x17a5(%rip), %rsi      # 0x309d
callq 0x10b0
cmpl $0x0, %eax
jne 0x1932
movw $0x0, -0x82(%rbp)
movzwl -0x82(%rbp), %eax
orl $0x500, %eax            # imm = 0x500
movw %ax, -0x82(%rbp)
movl -0x88(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x88(%rbp)
jmp 0x1bd8
movq -0x80(%rbp), %rdi
leaq 0x1764(%rip), %rsi      # 0x30a1
callq 0x10b0
cmpl $0x0, %eax
jne 0x1977
movw $0x0, -0x82(%rbp)
movzwl -0x82(%rbp), %eax
orl $0x600, %eax            # imm = 0x600
movw %ax, -0x82(%rbp)
movl -0x88(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x88(%rbp)
jmp 0x1bd6
movq -0x80(%rbp), %rdi
leaq 0x1723(%rip), %rsi      # 0x30a5
callq 0x10b0
cmpl $0x0, %eax
jne 0x19bc
movw $0x0, -0x82(%rbp)
movzwl -0x82(%rbp), %eax
orl $0x700, %eax            # imm = 0x700
movw %ax, -0x82(%rbp)
movl -0x88(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x88(%rbp)
jmp 0x1bd4
movq -0x80(%rbp), %rdi
leaq 0x16e3(%rip), %rsi      # 0x30aa
callq 0x10b0
cmpl $0x0, %eax
jne 0x19df
movw $0x1000, -0x82(%rbp)    # imm = 0x1000
jmp 0x1bd2
movq -0x80(%rbp), %rdi
leaq 0x16c4(%rip), %rsi      # 0x30ae
callq 0x10b0
cmpl $0x0, %eax
jne 0x1a02
movw $0x2000, -0x82(%rbp)    # imm = 0x2000
jmp 0x1bd0
movq -0x80(%rbp), %rdi
leaq 0x16a5(%rip), %rsi      # 0x30b2
callq 0x10b0
cmpl $0x0, %eax
jne 0x1a25
movw $0x3000, -0x82(%rbp)    # imm = 0x3000
jmp 0x1bce
movq -0x80(%rbp), %rdi
leaq 0x1687(%rip), %rsi      # 0x30b7
callq 0x10b0
cmpl $0x0, %eax
jne 0x1a48
movw $0x4000, -0x82(%rbp)    # imm = 0x4000
jmp 0x1bcc
movq -0x80(%rbp), %rdi
leaq 0x1669(%rip), %rsi      # 0x30bc
callq 0x10b0
cmpl $0x0, %eax
jne 0x1a6b
movw $0x5000, -0x82(%rbp)    # imm = 0x5000
jmp 0x1bca
movq -0x80(%rbp), %rdi
leaq 0x164a(%rip), %rsi      # 0x30c0
callq 0x10b0
cmpl $0x0, %eax
jne 0x1a8e
movw $0x6000, -0x82(%rbp)    # imm = 0x6000
jmp 0x1bc8
movq -0x80(%rbp), %rdi
leaq 0x162b(%rip), %rsi      # 0x30c4
callq 0x10b0
cmpl $0x0, %eax
jne 0x1ab1
movw $0x7000, -0x82(%rbp)    # imm = 0x7000
jmp 0x1bc6
movq -0x80(%rbp), %rdi
leaq 0x160c(%rip), %rsi      # 0x30c8
callq 0x10b0
cmpl $0x0, %eax
jne 0x1ad4
movw $0x8000, -0x82(%rbp)    # imm = 0x8000
jmp 0x1bc4
movq -0x80(%rbp), %rdi
leaq 0x15ed(%rip), %rsi      # 0x30cc
callq 0x10b0
cmpl $0x0, %eax
jne 0x1af7
movw $0x9000, -0x82(%rbp)    # imm = 0x9000
jmp 0x1bc2
movq -0x80(%rbp), %rdi
leaq 0x15ce(%rip), %rsi      # 0x30d0
callq 0x10b0
cmpl $0x0, %eax
jne 0x1b1a
movw $0xa000, -0x82(%rbp)    # imm = 0xA000
jmp 0x1bc0
movq -0x80(%rbp), %rdi
leaq 0x15af(%rip), %rsi      # 0x30d4
callq 0x10b0
cmpl $0x0, %eax
jne 0x1b3d
movw $0xb000, -0x82(%rbp)    # imm = 0xB000
jmp 0x1bbe
movq -0x80(%rbp), %rdi
leaq 0x1590(%rip), %rsi      # 0x30d8
callq 0x10b0
cmpl $0x0, %eax
jne 0x1b5d
movw $0xc000, -0x82(%rbp)    # imm = 0xC000
jmp 0x1bbc
movq -0x80(%rbp), %rdi
leaq 0x1575(%rip), %rsi      # 0x30dd
callq 0x10b0
cmpl $0x0, %eax
jne 0x1b7d
movw $0xd000, -0x82(%rbp)    # imm = 0xD000
jmp 0x1bba
movq -0x80(%rbp), %rdi
leaq 0x1554(%rip), %rsi      # 0x30dc
callq 0x10b0
cmpl $0x0, %eax
jne 0x1b9d
movw $0xe000, -0x82(%rbp)    # imm = 0xE000
jmp 0x1bb8
movl -0x70(%rbp), %esi
leaq 0x1539(%rip), %rdi      # 0x30e0
movb $0x0, %al
callq 0x1090
movl $0x1, %edi
callq 0x1160
jmp 0x1bba
jmp 0x1bbc
jmp 0x1bbe
jmp 0x1bc0
jmp 0x1bc2
jmp 0x1bc4
jmp 0x1bc6
jmp 0x1bc8
jmp 0x1bca
jmp 0x1bcc
jmp 0x1bce
jmp 0x1bd0
jmp 0x1bd2
jmp 0x1bd4
jmp 0x1bd6
jmp 0x1bd8
jmp 0x1bda
jmp 0x1bdc
jmp 0x1bde
jmp 0x1be0
jmp 0x1be2
jmp 0x1be4
movl -0x88(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x88(%rbp)
jmp 0x1d4f
cmpl $0x4, -0x88(%rbp)
jge 0x1d4d
movq -0x80(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x72, %eax
jne 0x1c80
movl $0x0, -0xa4(%rbp)
cmpl $0x1, -0x88(%rbp)
jne 0x1c2e
movl $0x8, -0xa4(%rbp)
cmpl $0x2, -0x88(%rbp)
jne 0x1c41
movl $0x4, -0xa4(%rbp)
cmpl $0x3, -0x88(%rbp)
jne 0x1c54
movl $0x0, -0xa4(%rbp)
movq -0x80(%rbp), %rdi
addq $0x1, %rdi
callq 0x1130
movl -0xa4(%rbp), %ecx
shll %cl, %eax
movl %eax, %ecx
movzwl -0x82(%rbp), %eax
orl %ecx, %eax
movw %ax, -0x82(%rbp)
jmp 0x1d3e
movq -0x80(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x23, %eax
je 0x1c9c
movq -0x80(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x24, %eax
jne 0x1d3c
movq -0x80(%rbp), %rax
movq %rax, -0xb0(%rbp)
movq -0x80(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x23, %eax
jne 0x1cda
leaq -0xb0(%rbp), %rdi
leaq 0x1443(%rip), %rsi      # 0x3104
callq 0x10f0
movq -0xb0(%rbp), %rdi
callq 0x1130
movb %al, -0xb1(%rbp)
jmp 0x1d16
movq -0x80(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x24, %eax
jne 0x1d14
leaq -0xb0(%rbp), %rdi
leaq 0x1412(%rip), %rsi      # 0x3106
callq 0x10f0
movq -0xb0(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
movl $0x10, %edx
callq 0x10d0
movb %al, -0xb1(%rbp)
jmp 0x1d16
movzbl -0xb1(%rbp), %ecx
movzwl -0x82(%rbp), %eax
orl %ecx, %eax
movw %ax, -0x82(%rbp)
movl -0x88(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x88(%rbp)
jmp 0x1d3e
movl -0x88(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x88(%rbp)
jmp 0x1d4f
xorl %eax, %eax
movl %eax, %edi
leaq 0x132a(%rip), %rsi      # 0x3084
callq 0x1120
movq %rax, -0x80(%rbp)
jmp 0x15a2
movw -0x82(%rbp), %dx
movq -0x50(%rbp), %rax
movslq -0x6c(%rbp), %rcx
movw %dx, (%rax,%rcx,2)
movl -0x6c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x6c(%rbp)
movq -0x58(%rbp), %rdi
callq 0x1030
movq $0x0, -0x58(%rbp)
jmp 0x151d
movl $0x0, -0xb8(%rbp)
cmpl $0x400, -0xb8(%rbp)     # imm = 0x400
jge 0x1e17
movq -0x78(%rbp), %rax
movslq -0xb8(%rbp), %rcx
cmpq $0x0, (%rax,%rcx,8)
je 0x1e04
movq -0x78(%rbp), %rax
movslq -0xb8(%rbp), %rcx
movq (%rax,%rcx,8), %rax
cmpq $0x0, 0x8(%rax)
je 0x1df0
movq -0x78(%rbp), %rax
movslq -0xb8(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq 0x8(%rax), %rdi
callq 0x1030
movq -0x78(%rbp), %rax
movslq -0xb8(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0x1030
jmp 0x1e06
movl -0xb8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xb8(%rbp)
jmp 0x1da4
movq -0x78(%rbp), %rdi
callq 0x1030
leaq -0x18(%rbp), %rdi
leaq 0x12dd(%rip), %rsi      # 0x3108
callq 0x10f0
movq %rax, -0xc0(%rbp)
cmpq $0x0, -0xc0(%rbp)
je 0x1e4c
movq -0xc0(%rbp), %rax
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rdi
leaq 0x12b3(%rip), %rsi      # 0x310a
callq 0x1150
movq %rax, %rdi
leaq 0x12ae(%rip), %rsi      # 0x3114
callq 0x1110
movq %rax, -0xc8(%rbp)
movw -0x42(%rbp), %ax
movw %ax, -0xca(%rbp)
movq -0xc8(%rbp), %rcx
leaq -0xca(%rbp), %rdi
movl $0x2, %esi
movl $0x1, %edx
callq 0x1170
movq -0x50(%rbp), %rdi
movzwl -0x42(%rbp), %eax
movl %eax, %edx
movq -0xc8(%rbp), %rcx
movl $0x2, %esi
callq 0x1170
movq %rax, -0xd8(%rbp)
movw -0x2a(%rbp), %ax
movw %ax, -0xda(%rbp)
movq -0xc8(%rbp), %rcx
leaq -0xda(%rbp), %rdi
movl $0x2, %esi
movl $0x1, %edx
callq 0x1170
cmpw $0x0, -0x2a(%rbp)
je 0x1f42
movzwl -0x2a(%rbp), %eax
movl %eax, %edi
callq 0x10e0
movq %rax, -0xe8(%rbp)
movq -0xe8(%rbp), %rdi
movzwl -0x2a(%rbp), %eax
movl %eax, %edx
movq -0x28(%rbp), %rcx
movl $0x1, %esi
callq 0x1060
movq -0xe8(%rbp), %rdi
movzwl -0x2a(%rbp), %eax
movl %eax, %edx
movq -0xc8(%rbp), %rcx
movl $0x2, %esi
callq 0x1170
movq -0xe8(%rbp), %rdi
callq 0x1030
movq -0xc8(%rbp), %rdi
callq 0x1070
movq -0x50(%rbp), %rdi
callq 0x1030
movq $0x0, -0x50(%rbp)
movq -0xd8(%rbp), %rax
movzwl -0x42(%rbp), %ecx
cmpq %rcx, %rax
je 0x1f7f
leaq 0x11a1(%rip), %rdi      # 0x3117
movb $0x0, %al
callq 0x1090
jmp 0x1f98
movq -0xd8(%rbp), %rsi
movq -0x18(%rbp), %rdx
leaq 0x11a1(%rip), %rdi      # 0x3132
movb $0x0, %al
callq 0x1090
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x100, %rsp            # imm = 0x100
popq %rbp
retq
nopl (%rax,%rax)