// File: ksherlock[P]wdc-utils/omf.cpp
// Function: save_omf(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, std::vector<omf::segment, std::allocator<omf::segment>>&, unsigned int)
void save_omf(const std::string &path, std::vector<omf::segment> &segments, unsigned flags) {

	// expressload doesn't support links to other files. 
	// fortunately, we don't either.

	std::vector<uint8_t> expr_headers;
	std::vector<unsigned> expr_offsets;


	bool compress = !(flags & OMF_NO_COMPRESS);
	bool super = !(flags & OMF_NO_SUPER);
	bool expressload = !(flags & OMF_NO_EXPRESS);
	bool v1 = flags & OMF_V1;

	if (v1) {
		expressload = false;
		super = false;
	}

	int fd;
	fd = open(path.c_str(), O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666);
	if (fd < 0) {
		err(EX_CANTCREAT, "Unable to open %s", path.c_str());
	}


	uint32_t offset = 0;
	if (expressload) {
		for (auto &s : segments) {
			s.segnum++;
			for (auto &r : s.intersegs) r.segment++;
		}

		// calculate express load segment size.
		// sizeof includes the trailing 0, so no need to add in byte size.
		offset = sizeof(omf_header) + 10 + sizeof("~ExpressLoad");

		offset += 6; // lconst + end
		offset += 6;  // header.
		for (auto &s : segments) {
			offset += 8 + 2;
			offset += sizeof(omf_express_header) + 10;
			offset += s.segname.length() + 1;
		}

		lseek(fd, offset, SEEK_SET);
	}


	for (auto &s : segments) {
		omf_header h;
		h.length = s.data.size() + s.reserved_space;
		h.kind = s.kind;
		h.banksize = s.data.size() > 0xffff ? 0x0000 : 0x010000;
		h.segnum = s.segnum;
		h.alignment = s.alignment;
		h.reserved_space = s.reserved_space;
		h.org = s.org;

		uint32_t reserved_space = 0;
		if (expressload) {
			std::swap(reserved_space, h.reserved_space);
		}

		// length field INCLUDES reserved space.  Express expand reserved space.


		std::vector<uint8_t> data;

		// push segname and load name onto data.
		// data.insert(data.end(), 10, ' ');
		push(data, s.loadname, 10);
		push(data, s.segname);

		h.dispname = sizeof(omf_header);
		h.dispdata = sizeof(omf_header) + data.size();



		uint32_t lconst_offset = offset + sizeof(omf_header) + data.size() + 5;
		uint32_t lconst_size = s.data.size() + reserved_space;


		//lconst record
		push(data, (uint8_t)omf::LCONST);
		push(data, (uint32_t)lconst_size);

		size_t data_offset = data.size();

		data.insert(data.end(), s.data.begin(), s.data.end());

		if (reserved_space) {
			data.insert(data.end(), reserved_space, 0);
		}

		uint32_t reloc_offset = offset + sizeof(omf_header) + data.size();
		uint32_t reloc_size = 0;

		reloc_size = add_relocs(data, data_offset, s, compress, super);

		// end-of-record
		push(data, (uint8_t)omf::END);

		h.bytecount = data.size() + sizeof(omf_header);

		if (expressload) {

			expr_offsets.emplace_back(expr_headers.size());

			if (lconst_size == 0) lconst_offset = 0;
			if (reloc_size == 0) reloc_offset = 0;


			push(expr_headers, (uint32_t)lconst_offset);
			push(expr_headers, (uint32_t)lconst_size);
			push(expr_headers, (uint32_t)reloc_offset);
			push(expr_headers, (uint32_t)reloc_size);

			push(expr_headers, h.unused1);
			push(expr_headers, h.lablen);
			push(expr_headers, h.numlen);
			push(expr_headers, h.version);
			push(expr_headers, h.banksize);
			push(expr_headers, h.kind);
			push(expr_headers, h.unused2);
			push(expr_headers, h.org);
			push(expr_headers, h.alignment);
			push(expr_headers, h.numsex);
			push(expr_headers, h.unused3);
			push(expr_headers, h.segnum);
			push(expr_headers, h.entry);
			push(expr_headers, (uint16_t)(h.dispname));
			push(expr_headers, h.dispdata);

			expr_headers.insert(expr_headers.end(), 10, ' ');
			push(expr_headers, s.segname);
		}

		if (v1) to_v1(h);
		to_little(h);

		offset += write(fd, &h, sizeof(h));
		offset += write(fd, data.data(), data.size());

		// version 1 needs 512-byte padding for all but final segment.
		if (v1 && &s != &segments.back()) {
			static uint8_t zero[512];
			offset += write(fd, zero, 512 - (offset & 511) );
		}
	}

	if (expressload) {
		omf_header h;
		h.segnum = 1;
		h.banksize = 0x00010000;
		h.kind = 0x8001;
		h.dispname = 0x2c;
		h.dispdata = 0x43;

		unsigned fudge = 10 * segments.size();

		h.length = 6 + expr_headers.size() + fudge;

		std::vector<uint8_t> data;
		data.insert(data.begin(), 10, ' ');
		push(data, std::string("~ExpressLoad"));
		push(data, (uint8_t)0xf2); // lconst.
		push(data, (uint32_t)h.length);

		push(data, (uint32_t)0); // reserved
		push(data, (uint16_t)(segments.size() - 1)); // seg count - 1


		for (auto &offset : expr_offsets) {
			push(data, (uint16_t)(fudge + offset));
			push(data, (uint16_t)0);
			push(data, (uint32_t)0);
			fudge -= 8;
		}

		for (auto &s : segments) {
			push(data, (uint16_t)s.segnum);
		}

		data.insert(data.end(), expr_headers.begin(), expr_headers.end());
		push(data, (uint8_t)0); // end.

		h.bytecount = data.size() + sizeof(omf_header);

		to_little(h);
		lseek(fd, 0, SEEK_SET);
		write(fd, &h, sizeof(h));
		write(fd, data.data(), data.size());

	}

	close(fd);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x390, %rsp            # imm = 0x390
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
leaq -0x30(%rbp), %rdi
callq 0xcb40
leaq -0x48(%rbp), %rdi
callq 0x25e10
movl -0x14(%rbp), %eax
andl $0x4, %eax
cmpl $0x0, %eax
setne %al
xorb $-0x1, %al
andb $0x1, %al
movb %al, -0x49(%rbp)
movl -0x14(%rbp), %eax
andl $0x2, %eax
cmpl $0x0, %eax
setne %al
xorb $-0x1, %al
andb $0x1, %al
movb %al, -0x4a(%rbp)
movl -0x14(%rbp), %eax
andl $0x8, %eax
cmpl $0x0, %eax
setne %al
xorb $-0x1, %al
andb $0x1, %al
movb %al, -0x4b(%rbp)
movl -0x14(%rbp), %eax
andl $0x1, %eax
cmpl $0x0, %eax
setne %al
andb $0x1, %al
movb %al, -0x4c(%rbp)
testb $0x1, -0x4c(%rbp)
je 0x24830
movb $0x0, -0x4b(%rbp)
movb $0x0, -0x4a(%rbp)
movq -0x8(%rbp), %rdi
callq 0x3130
movq %rax, %rdi
xorl %eax, %eax
movl $0x241, %esi            # imm = 0x241
movl $0x1b6, %edx            # imm = 0x1B6
callq 0x3280
movl %eax, -0x2ac(%rbp)
jmp 0x24855
movl -0x2ac(%rbp), %eax
movl %eax, -0x50(%rbp)
cmpl $0x0, -0x50(%rbp)
jge 0x24896
movq -0x8(%rbp), %rdi
callq 0x3130
movq %rax, %rdx
leaq 0x6e06(%rip), %rsi      # 0x2b67d
xorl %eax, %eax
movl $0x49, %edi
callq 0x3300
jmp 0x24885
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
jmp 0x25630
movl $0x0, -0x60(%rbp)
testb $0x1, -0x4b(%rbp)
je 0x24a52
movq -0x10(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rdi
callq 0xd560
movq %rax, -0x70(%rbp)
movq -0x68(%rbp), %rdi
callq 0xd590
movq %rax, -0x78(%rbp)
leaq -0x70(%rbp), %rdi
leaq -0x78(%rbp), %rsi
callq 0xd5c0
testb $0x1, %al
jne 0x248df
jmp 0x2498c
leaq -0x70(%rbp), %rdi
callq 0xd600
movq %rax, -0x80(%rbp)
movq -0x80(%rbp), %rax
movw (%rax), %cx
addw $0x1, %cx
movw %cx, (%rax)
movq -0x80(%rbp), %rax
addq $0x68, %rax
movq %rax, -0x88(%rbp)
movq -0x88(%rbp), %rdi
callq 0x1b960
movq %rax, -0x90(%rbp)
movq -0x88(%rbp), %rdi
callq 0x1b7a0
movq %rax, -0x98(%rbp)
leaq -0x90(%rbp), %rdi
leaq -0x98(%rbp), %rsi
callq 0x25bf0
testb $0x1, %al
jne 0x24948
jmp 0x2497c
leaq -0x90(%rbp), %rdi
callq 0x1be10
movq %rax, -0xa0(%rbp)
movq -0xa0(%rbp), %rax
movw 0xa(%rax), %cx
addw $0x1, %cx
movw %cx, 0xa(%rax)
leaq -0x90(%rbp), %rdi
callq 0x25c90
jmp 0x2492f
jmp 0x2497e
leaq -0x70(%rbp), %rdi
callq 0xd7e0
jmp 0x248c9
movl $0x43, -0x60(%rbp)
movl -0x60(%rbp), %eax
addl $0x6, %eax
movl %eax, -0x60(%rbp)
movl -0x60(%rbp), %eax
addl $0x6, %eax
movl %eax, -0x60(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0xa8(%rbp)
movq -0xa8(%rbp), %rdi
callq 0xd560
movq %rax, -0xb0(%rbp)
movq -0xa8(%rbp), %rdi
callq 0xd590
movq %rax, -0xb8(%rbp)
leaq -0xb0(%rbp), %rdi
leaq -0xb8(%rbp), %rsi
callq 0xd5c0
testb $0x1, %al
jne 0x249ef
jmp 0x24a43
leaq -0xb0(%rbp), %rdi
callq 0xd600
movq %rax, -0xc0(%rbp)
movl -0x60(%rbp), %eax
addl $0xa, %eax
movl %eax, -0x60(%rbp)
movl -0x60(%rbp), %eax
addq $0x3a, %rax
movl %eax, -0x60(%rbp)
movq -0xc0(%rbp), %rdi
addq $0x30, %rdi
callq 0x34a0
movq %rax, %rcx
addq $0x1, %rcx
movl -0x60(%rbp), %eax
addq %rcx, %rax
movl %eax, -0x60(%rbp)
leaq -0xb0(%rbp), %rdi
callq 0xd7e0
jmp 0x249d6
movl -0x50(%rbp), %edi
movl -0x60(%rbp), %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x35a0
movq -0x10(%rbp), %rax
movq %rax, -0xc8(%rbp)
movq -0xc8(%rbp), %rdi
callq 0xd560
movq %rax, -0xd0(%rbp)
movq -0xc8(%rbp), %rdi
callq 0xd590
movq %rax, -0xd8(%rbp)
leaq -0xd0(%rbp), %rdi
leaq -0xd8(%rbp), %rsi
callq 0xd5c0
testb $0x1, %al
jne 0x24a9f
jmp 0x25196
leaq -0xd0(%rbp), %rdi
callq 0xd600
movq %rax, -0xe0(%rbp)
leaq -0x10c(%rbp), %rdi
callq 0x25e30
movq -0xe0(%rbp), %rdi
addq $0x50, %rdi
callq 0xa700
movq -0xe0(%rbp), %rcx
movl 0x8(%rcx), %ecx
addq %rcx, %rax
movl %eax, -0x104(%rbp)
movq -0xe0(%rbp), %rax
movw 0x2(%rax), %ax
movw %ax, -0xf8(%rbp)
movq -0xe0(%rbp), %rdi
addq $0x50, %rdi
callq 0xa700
movq %rax, %rdx
movl $0x10000, %eax          # imm = 0x10000
xorl %ecx, %ecx
cmpq $0xffff, %rdx           # imm = 0xFFFF
cmoval %ecx, %eax
movl %eax, -0xfc(%rbp)
movq -0xe0(%rbp), %rax
movw (%rax), %ax
movw %ax, -0xea(%rbp)
movq -0xe0(%rbp), %rax
movl 0x4(%rax), %eax
movl %eax, -0xf0(%rbp)
movq -0xe0(%rbp), %rax
movl 0x8(%rax), %eax
movl %eax, -0x108(%rbp)
movq -0xe0(%rbp), %rax
movl 0xc(%rax), %eax
movl %eax, -0xf4(%rbp)
movl $0x0, -0x110(%rbp)
testb $0x1, -0x4b(%rbp)
je 0x24b85
leaq -0x10c(%rbp), %rsi
addq $0x4, %rsi
leaq -0x110(%rbp), %rdi
callq 0x25eb0
leaq -0x128(%rbp), %rdi
movq %rdi, -0x2b8(%rbp)
callq 0xcb40
movq -0x2b8(%rbp), %rdi
movq -0xe0(%rbp), %rsi
addq $0x10, %rsi
movl $0xa, %edx
callq 0x23720
jmp 0x24bb6
movq -0xe0(%rbp), %rsi
addq $0x30, %rsi
leaq -0x128(%rbp), %rdi
callq 0x23670
jmp 0x24bcf
movw $0x2c, -0xe4(%rbp)
leaq -0x128(%rbp), %rdi
movq %rdi, -0x2c0(%rbp)
callq 0xa700
movq -0x2c0(%rbp), %rdi
addl $0x2c, %eax
movw %ax, -0xe2(%rbp)
movl -0x60(%rbp), %eax
movl %eax, -0x2c4(%rbp)
callq 0xa700
movl -0x2c4(%rbp), %ecx
movl %eax, %edx
movl %ecx, %eax
movl %edx, %ecx
leal 0x31(%rax,%rcx), %eax
movl %eax, -0x12c(%rbp)
movq -0xe0(%rbp), %rdi
addq $0x50, %rdi
callq 0xa700
movq -0x2c0(%rbp), %rdi
movl -0x110(%rbp), %ecx
addl %ecx, %eax
movl %eax, -0x130(%rbp)
movl $0xf2, %esi
callq 0x23550
jmp 0x24c51
movl -0x130(%rbp), %esi
leaq -0x128(%rbp), %rdi
callq 0x235e0
jmp 0x24c65
leaq -0x128(%rbp), %rdi
movq %rdi, -0x2d8(%rbp)
callq 0xa700
movq -0x2d8(%rbp), %rdi
movq %rax, -0x138(%rbp)
callq 0xa870
movq %rax, -0x148(%rbp)
leaq -0x140(%rbp), %rdi
leaq -0x148(%rbp), %rsi
callq 0xa8a0
movq -0xe0(%rbp), %rdi
addq $0x50, %rdi
callq 0xce10
movq %rax, -0x150(%rbp)
movq -0xe0(%rbp), %rdi
addq $0x50, %rdi
callq 0xa870
movq -0x2d8(%rbp), %rdi
movq %rax, -0x158(%rbp)
movq -0x140(%rbp), %rsi
movq -0x150(%rbp), %rdx
movq -0x158(%rbp), %rcx
callq 0x25d10
movq %rax, -0x2d0(%rbp)
jmp 0x24cfd
movq -0x2d0(%rbp), %rax
movq %rax, -0x160(%rbp)
cmpl $0x0, -0x110(%rbp)
je 0x24da4
leaq -0x128(%rbp), %rdi
movq %rdi, -0x2e8(%rbp)
callq 0xa870
movq %rax, -0x170(%rbp)
leaq -0x168(%rbp), %rdi
leaq -0x170(%rbp), %rsi
callq 0xa8a0
movq -0x2e8(%rbp), %rdi
movl -0x110(%rbp), %eax
movl %eax, %edx
movb $0x0, -0x171(%rbp)
movq -0x168(%rbp), %rsi
leaq -0x171(%rbp), %rcx
callq 0xa9a0
movq %rax, -0x2e0(%rbp)
jmp 0x24d77
movq -0x2e0(%rbp), %rax
movq %rax, -0x180(%rbp)
jmp 0x24da4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
leaq -0x128(%rbp), %rdi
callq 0xcc10
jmp 0x25630
movl -0x60(%rbp), %eax
movl %eax, -0x2fc(%rbp)
leaq -0x128(%rbp), %rdi
movq %rdi, -0x2f8(%rbp)
callq 0xa700
movl -0x2fc(%rbp), %edx
movq -0x2f8(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movl %ecx, %edx
movl %edx, %ecx
leal 0x2c(%rax,%rcx), %eax
movl %eax, -0x184(%rbp)
movl $0x0, -0x188(%rbp)
movq -0x138(%rbp), %rsi
movq -0xe0(%rbp), %rdx
movzbl -0x4a(%rbp), %r8d
movzbl -0x49(%rbp), %ecx
andl $0x1, %ecx
andl $0x1, %r8d
callq 0x23800
movl %eax, -0x2ec(%rbp)
jmp 0x24e15
movl -0x2ec(%rbp), %eax
movl %eax, -0x188(%rbp)
leaq -0x128(%rbp), %rdi
xorl %esi, %esi
callq 0x23550
jmp 0x24e31
leaq -0x128(%rbp), %rdi
callq 0xa700
addq $0x2c, %rax
movl %eax, -0x10c(%rbp)
testb $0x1, -0x4b(%rbp)
je 0x25066
leaq -0x30(%rbp), %rdi
callq 0xa700
movq %rax, -0x190(%rbp)
leaq -0x48(%rbp), %rdi
leaq -0x190(%rbp), %rsi
callq 0x25ee0
jmp 0x24e73
cmpl $0x0, -0x130(%rbp)
jne 0x24e86
movl $0x0, -0x12c(%rbp)
cmpl $0x0, -0x188(%rbp)
jne 0x24e99
movl $0x0, -0x184(%rbp)
movl -0x12c(%rbp), %esi
leaq -0x30(%rbp), %rdi
callq 0x235e0
jmp 0x24eaa
movl -0x130(%rbp), %esi
leaq -0x30(%rbp), %rdi
callq 0x235e0
jmp 0x24ebb
movl -0x184(%rbp), %esi
leaq -0x30(%rbp), %rdi
callq 0x235e0
jmp 0x24ecc
movl -0x188(%rbp), %esi
leaq -0x30(%rbp), %rdi
callq 0x235e0
jmp 0x24edd
movzbl -0x100(%rbp), %esi
leaq -0x30(%rbp), %rdi
callq 0x23550
jmp 0x24eef
movzbl -0xff(%rbp), %esi
leaq -0x30(%rbp), %rdi
callq 0x23550
jmp 0x24f01
movzbl -0xfe(%rbp), %esi
leaq -0x30(%rbp), %rdi
callq 0x23550
jmp 0x24f13
movzbl -0xfd(%rbp), %esi
leaq -0x30(%rbp), %rdi
callq 0x23550
jmp 0x24f25
movl -0xfc(%rbp), %esi
leaq -0x30(%rbp), %rdi
callq 0x235e0
jmp 0x24f36
movzwl -0xf8(%rbp), %esi
leaq -0x30(%rbp), %rdi
callq 0x23580
jmp 0x24f48
movzwl -0xf6(%rbp), %esi
leaq -0x30(%rbp), %rdi
callq 0x23580
jmp 0x24f5a
movl -0xf4(%rbp), %esi
leaq -0x30(%rbp), %rdi
callq 0x235e0
jmp 0x24f6b
movl -0xf0(%rbp), %esi
leaq -0x30(%rbp), %rdi
callq 0x235e0
jmp 0x24f7c
movzbl -0xec(%rbp), %esi
leaq -0x30(%rbp), %rdi
callq 0x23550
jmp 0x24f8e
movzbl -0xeb(%rbp), %esi
leaq -0x30(%rbp), %rdi
callq 0x23550
jmp 0x24fa0
movzwl -0xea(%rbp), %esi
leaq -0x30(%rbp), %rdi
callq 0x23580
jmp 0x24fb2
movl -0xe8(%rbp), %esi
leaq -0x30(%rbp), %rdi
callq 0x235e0
jmp 0x24fc3
movzwl -0xe4(%rbp), %esi
leaq -0x30(%rbp), %rdi
callq 0x23580
jmp 0x24fd5
movzwl -0xe2(%rbp), %esi
leaq -0x30(%rbp), %rdi
callq 0x23580
jmp 0x24fe7
leaq -0x30(%rbp), %rdi
movq %rdi, -0x310(%rbp)
callq 0xa870
movq %rax, -0x1a0(%rbp)
leaq -0x198(%rbp), %rdi
leaq -0x1a0(%rbp), %rsi
callq 0xa8a0
movq -0x310(%rbp), %rdi
movb $0x20, -0x1a1(%rbp)
movq -0x198(%rbp), %rsi
movl $0xa, %edx
leaq -0x1a1(%rbp), %rcx
callq 0xa9a0
movq %rax, -0x308(%rbp)
jmp 0x25040
movq -0x308(%rbp), %rax
movq %rax, -0x1b0(%rbp)
movq -0xe0(%rbp), %rsi
addq $0x30, %rsi
leaq -0x30(%rbp), %rdi
callq 0x23670
jmp 0x25064
jmp 0x25066
testb $0x1, -0x4c(%rbp)
je 0x2507c
leaq -0x10c(%rbp), %rdi
callq 0x25650
jmp 0x2507a
jmp 0x2507c
leaq -0x10c(%rbp), %rdi
callq 0x256a0
jmp 0x2508a
movl -0x50(%rbp), %edi
leaq -0x10c(%rbp), %rsi
movl $0x2c, %edx
callq 0x30f0
movq %rax, -0x318(%rbp)
jmp 0x250a7
movq -0x318(%rbp), %rcx
movl -0x60(%rbp), %eax
addl %ecx, %eax
movl %eax, -0x60(%rbp)
movl -0x50(%rbp), %eax
movl %eax, -0x32c(%rbp)
leaq -0x128(%rbp), %rdi
movq %rdi, -0x338(%rbp)
callq 0xcbf0
movq -0x338(%rbp), %rdi
movq %rax, -0x328(%rbp)
callq 0xa700
movl -0x32c(%rbp), %edi
movq -0x328(%rbp), %rsi
movq %rax, %rdx
callq 0x30f0
movq %rax, -0x320(%rbp)
jmp 0x25103
movq -0x320(%rbp), %rcx
movl -0x60(%rbp), %eax
addq %rcx, %rax
movl %eax, -0x60(%rbp)
testb $0x1, -0x4c(%rbp)
je 0x25179
movq -0xe0(%rbp), %rax
movq %rax, -0x340(%rbp)
movq -0x10(%rbp), %rdi
callq 0xc4d0
movq %rax, %rcx
movq -0x340(%rbp), %rax
cmpq %rcx, %rax
je 0x25179
movl -0x50(%rbp), %edi
movl -0x60(%rbp), %ecx
andl $0x1ff, %ecx            # imm = 0x1FF
movl $0x200, %eax            # imm = 0x200
subl %ecx, %eax
movl %eax, %edx
leaq 0x1a785(%rip), %rsi     # 0x3f8e0
callq 0x30f0
movq %rax, -0x348(%rbp)
jmp 0x25169
movq -0x348(%rbp), %rcx
movl -0x60(%rbp), %eax
addq %rcx, %rax
movl %eax, -0x60(%rbp)
leaq -0x128(%rbp), %rdi
callq 0xcc10
leaq -0xd0(%rbp), %rdi
callq 0xd7e0
jmp 0x24a83
testb $0x1, -0x4b(%rbp)
je 0x2560b
leaq -0x1dc(%rbp), %rdi
callq 0x25e30
movw $0x1, -0x1ba(%rbp)
movl $0x10000, -0x1cc(%rbp)  # imm = 0x10000
movw $0x8001, -0x1c8(%rbp)   # imm = 0x8001
movw $0x2c, -0x1b4(%rbp)
movw $0x43, -0x1b2(%rbp)
movq -0x10(%rbp), %rdi
callq 0xc510
movl %eax, %ecx
addl %ecx, %ecx
movl %ecx, %eax
leal (%rax,%rax,4), %eax
movl %eax, -0x1e0(%rbp)
leaq -0x30(%rbp), %rdi
callq 0xa700
movl %eax, %edx
movl -0x1e0(%rbp), %eax
movl %eax, %ecx
movl %edx, %eax
leal 0x6(%rax,%rcx), %eax
movl %eax, -0x1d4(%rbp)
leaq -0x1f8(%rbp), %rdi
movq %rdi, -0x358(%rbp)
callq 0xcb40
movq -0x358(%rbp), %rdi
callq 0xce10
movq %rax, -0x208(%rbp)
leaq -0x200(%rbp), %rdi
leaq -0x208(%rbp), %rsi
callq 0xa8a0
movq -0x358(%rbp), %rdi
movb $0x20, -0x209(%rbp)
movq -0x200(%rbp), %rsi
movl $0xa, %edx
leaq -0x209(%rbp), %rcx
callq 0xa9a0
movq %rax, -0x350(%rbp)
jmp 0x25279
movq -0x350(%rbp), %rax
movq %rax, -0x218(%rbp)
leaq -0x239(%rbp), %rdi
movq %rdi, -0x360(%rbp)
callq 0x3550
movq -0x360(%rbp), %rdx
leaq 0x68c6(%rip), %rsi      # 0x2bb6e
leaq -0x238(%rbp), %rdi
callq 0xbea0
jmp 0x252b6
leaq -0x1f8(%rbp), %rdi
leaq -0x238(%rbp), %rsi
callq 0x23670
jmp 0x252cb
leaq -0x238(%rbp), %rdi
callq 0x31f0
leaq -0x239(%rbp), %rdi
callq 0x3370
leaq -0x1f8(%rbp), %rdi
movl $0xf2, %esi
callq 0x23550
jmp 0x252f6
movl -0x1d4(%rbp), %esi
leaq -0x1f8(%rbp), %rdi
callq 0x235e0
jmp 0x2530a
leaq -0x1f8(%rbp), %rdi
xorl %esi, %esi
callq 0x235e0
jmp 0x2531a
movq -0x10(%rbp), %rdi
callq 0xc510
decl %eax
movzwl %ax, %esi
leaq -0x1f8(%rbp), %rdi
callq 0x23580
jmp 0x25336
leaq -0x48(%rbp), %rax
movq %rax, -0x248(%rbp)
movq -0x248(%rbp), %rdi
callq 0x25f60
movq %rax, -0x250(%rbp)
movq -0x248(%rbp), %rdi
callq 0x25f90
movq %rax, -0x258(%rbp)
leaq -0x250(%rbp), %rdi
leaq -0x258(%rbp), %rsi
callq 0x25fc0
testb $0x1, %al
jne 0x25383
jmp 0x25440
leaq -0x250(%rbp), %rdi
callq 0x26000
movq %rax, -0x260(%rbp)
movl -0x1e0(%rbp), %eax
movq -0x260(%rbp), %rcx
movl (%rcx), %ecx
addl %ecx, %eax
movzwl %ax, %esi
leaq -0x1f8(%rbp), %rdi
callq 0x23580
jmp 0x253b8
leaq -0x1f8(%rbp), %rdi
xorl %esi, %esi
callq 0x23580
jmp 0x253c8
leaq -0x1f8(%rbp), %rdi
xorl %esi, %esi
callq 0x235e0
jmp 0x253d8
movl -0x1e0(%rbp), %eax
subl $0x8, %eax
movl %eax, -0x1e0(%rbp)
leaq -0x250(%rbp), %rdi
callq 0x26020
jmp 0x25367
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
jmp 0x255fd
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
jmp 0x2542f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
leaq -0x238(%rbp), %rdi
callq 0x31f0
leaq -0x239(%rbp), %rdi
callq 0x3370
jmp 0x255fd
movq -0x10(%rbp), %rax
movq %rax, -0x268(%rbp)
movq -0x268(%rbp), %rdi
callq 0xd560
movq %rax, -0x270(%rbp)
movq -0x268(%rbp), %rdi
callq 0xd590
movq %rax, -0x278(%rbp)
leaq -0x270(%rbp), %rdi
leaq -0x278(%rbp), %rsi
callq 0xd5c0
testb $0x1, %al
jne 0x2548a
jmp 0x254c5
leaq -0x270(%rbp), %rdi
callq 0xd600
movq %rax, -0x280(%rbp)
movq -0x280(%rbp), %rax
movzwl (%rax), %esi
leaq -0x1f8(%rbp), %rdi
callq 0x23580
jmp 0x254b5
jmp 0x254b7
leaq -0x270(%rbp), %rdi
callq 0xd7e0
jmp 0x25471
leaq -0x1f8(%rbp), %rdi
movq %rdi, -0x370(%rbp)
callq 0xa870
movq %rax, -0x290(%rbp)
leaq -0x288(%rbp), %rdi
leaq -0x290(%rbp), %rsi
callq 0xa8a0
leaq -0x30(%rbp), %rdi
movq %rdi, -0x378(%rbp)
callq 0xce10
movq -0x378(%rbp), %rdi
movq %rax, -0x298(%rbp)
callq 0xa870
movq -0x370(%rbp), %rdi
movq %rax, -0x2a0(%rbp)
movq -0x288(%rbp), %rsi
movq -0x298(%rbp), %rdx
movq -0x2a0(%rbp), %rcx
callq 0x25d10
movq %rax, -0x368(%rbp)
jmp 0x25546
movq -0x368(%rbp), %rax
movq %rax, -0x2a8(%rbp)
leaq -0x1f8(%rbp), %rdi
xorl %esi, %esi
callq 0x23550
jmp 0x25564
leaq -0x1f8(%rbp), %rdi
callq 0xa700
addl $0x2c, %eax
movl %eax, -0x1dc(%rbp)
leaq -0x1dc(%rbp), %rdi
callq 0x256a0
jmp 0x25587
movl -0x50(%rbp), %edi
xorl %edx, %edx
movl %edx, %esi
callq 0x35a0
movl -0x50(%rbp), %edi
leaq -0x1dc(%rbp), %rsi
movl $0x2c, %edx
callq 0x30f0
jmp 0x255a9
movl -0x50(%rbp), %eax
movl %eax, -0x384(%rbp)
leaq -0x1f8(%rbp), %rdi
movq %rdi, -0x390(%rbp)
callq 0xcbf0
movq -0x390(%rbp), %rdi
movq %rax, -0x380(%rbp)
callq 0xa700
movl -0x384(%rbp), %edi
movq -0x380(%rbp), %rsi
movq %rax, %rdx
callq 0x30f0
jmp 0x255ef
leaq -0x1f8(%rbp), %rdi
callq 0xcc10
jmp 0x2560b
leaq -0x1f8(%rbp), %rdi
callq 0xcc10
jmp 0x25630
movl -0x50(%rbp), %edi
callq 0x3600
jmp 0x25615
leaq -0x48(%rbp), %rdi
callq 0x26040
leaq -0x30(%rbp), %rdi
callq 0xcc10
addq $0x390, %rsp            # imm = 0x390
popq %rbp
retq
leaq -0x48(%rbp), %rdi
callq 0x26040
leaq -0x30(%rbp), %rdi
callq 0xcc10
movq -0x58(%rbp), %rdi
callq 0x3540
nopl (%rax,%rax)