// File: /workspace/github_c_makefile_star2_O2/mikecat[P]x86_interpreter/read_pe.c
// Function: read_pe
int read_pe(uint32_t* eip_value, uint32_t* stack_size, pe_import_params* import_params, const char* filename) {
	size_t filesize = 0;
	uint8_t* filedata = read_whole_file(&filesize, filename);
	uint32_t newheader_offset;
	uint8_t* newheader;
	uint32_t num_section, optheader_size, section_table_size;
	uint8_t* optheader;
	uint32_t magic, entrypoint, image_base, stack_reserve;
	uint8_t* section_table;
	uint32_t i;
	if (filedata == 0) return 0;
	if (filesize < 64) {
		fprintf(stderr, "file size too small for PE header\n");
		free(filedata); return 0;
	}
	if (filedata[0] != 'M' || filedata[1] != 'Z') {
		fprintf(stderr, "not PE file\n");
		free(filedata); return 0;
	}
	newheader_offset = read_num(filedata + 60, 4);
	if (newheader_offset > filesize - 24) {
		fprintf(stderr, "file size too small for PE new header\n");
		free(filedata); return 0;
	}
	newheader = filedata + newheader_offset;
	if (newheader[0] != 'P' || newheader[1] != 'E' || newheader[2] != 0x00 || newheader[3] != 0x00) {
		fprintf(stderr, "wrong PE new header signature\n");
		free(filedata); return 0;
	}
	num_section = read_num(newheader + 6, 2);
	optheader_size = read_num(newheader + 20, 2);
	section_table_size = 40 * num_section;
	if (optheader_size > filesize || filesize - optheader_size < newheader_offset + 24) {
		fprintf(stderr, "file size too small for PE optional header\n");
		free(filedata); return 0;
	}
	if (optheader_size < 76) { /* TODO */
		fprintf(stderr, "PE optional header too small\n");
		free(filedata); return 0;
	}
	optheader = newheader + 24;
	magic = read_num(optheader + 0, 2);
	entrypoint = read_num(optheader + 16, 4);
	image_base = read_num(optheader + 28, 4);
	stack_reserve = read_num(optheader + 72, 4);
	if (magic == 0x20b) {
		fprintf(stderr, "unsupported 64-bit PE file detected\n");
		free(filedata); return 0;
	}
	if (UINT32_MAX - image_base < entrypoint) {
		fprintf(stderr, "PE entrypoint out of address space\n");
		free(filedata); return 0;
	}
	if (import_params != NULL) {
		uint32_t import_addr, import_size, iat_addr, iat_size;
		if (optheader_size >= 112) {
			import_addr = read_num(optheader + 104, 4);
			import_size = read_num(optheader + 108, 4);
		} else {
			import_addr = 0;
			import_size = 0;
		}
		if (optheader_size >= 200) {
			iat_addr = read_num(optheader + 192, 4);
			iat_size = read_num(optheader + 196, 4);
		} else {
			iat_addr = 0;
			iat_size = 0;
		}
		if (import_size > 0) {
			if (UINT32_MAX - image_base < import_addr ||
			UINT32_MAX - (image_base + import_addr) < import_size - 1) {
				fprintf(stderr, "PE import informaton out of address space\n");
				free(filedata); return 0;
			}
		}
		if (iat_size > 0) {
			if (UINT32_MAX - image_base < iat_addr ||
			UINT32_MAX - (image_base + iat_addr) < iat_size - 1) {
				fprintf(stderr, "PE import address table out of address space\n");
				free(filedata); return 0;
			}
		}
		import_params->image_base = image_base;
		import_params->import_addr = image_base + import_addr;
		import_params->import_size = import_size;
		import_params->iat_addr = image_base + iat_addr;
		import_params->iat_size = iat_size;
	}
	if (section_table_size > filesize || filesize - section_table_size < newheader_offset + 24 + optheader_size) {
		fprintf(stderr, "file size too small for PE section table\n");
		free(filedata); return 0;
	}
	section_table = optheader + optheader_size;
	for (i = 0; i < num_section; i++) {
		uint8_t* section_data = section_table + 40 * i;
		uint32_t size, addr, file_size, file_offset;
		uint32_t load_size;
		size = read_num(section_data + 8, 4);
		addr = read_num(section_data + 12, 4);
		file_size = read_num(section_data + 16, 4);
		file_offset = read_num(section_data + 20, 4);
		load_size = (file_offset == 0 ? 0 : (size < file_size ? size : file_size));
		if (UINT32_MAX - image_base < addr || (size > 0 && UINT32_MAX - (image_base + addr) < size - 1)) {
			fprintf(stderr, "PE section %"PRIu32" out of address space\n", i);
			free(filedata); return 0;
		}
		if (filesize - file_offset < load_size) {
			fprintf(stderr, "PE section %"PRIu32" out of file\n", i);
			free(filedata); return 0;
		}
		dmemory_allocate(image_base + addr, size);
		dmemory_write(filedata + file_offset, image_base + addr, load_size);
	}
	if (eip_value != NULL) *eip_value = image_base + entrypoint;
	if (stack_size != NULL) *stack_size = stack_reserve;
	free(filedata);
	return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %rdx, %r12
movq %rsi, %r14
movq %rdi, %r15
leaq 0x48(%rsp), %rdi
andq $0x0, (%rdi)
movq %rcx, %rsi
callq 0x8bbc
xorl %ebp, %ebp
testq %rax, %rax
je 0x9608
movq %rax, %rbx
movq 0x48(%rsp), %rax
cmpq $0x3f, %rax
jbe 0x93a6
cmpb $0x4d, (%rbx)
jne 0x9376
cmpb $0x5a, 0x1(%rbx)
jne 0x9376
movq %r12, 0x18(%rsp)
leaq 0x3c(%rbx), %rdi
pushq $0x4
popq %rsi
movq %rax, %r12
callq 0x9619
movl %eax, %r13d
movq %r12, 0x10(%rsp)
leaq -0x18(%r12), %rax
cmpq %r13, %rax
jb 0x9542
movq %r15, 0x60(%rsp)
cmpb $0x50, (%rbx,%r13)
jne 0x9354
leaq (%rbx,%r13), %r15
cmpb $0x45, 0x1(%r15)
jne 0x9354
cmpb $0x0, 0x2(%r15)
jne 0x9354
cmpb $0x0, 0x3(%r15)
jne 0x9354
movq %r14, 0x58(%rsp)
movq %rbx, (%rsp)
leaq 0x6(%r15), %rdi
pushq $0x2
popq %rbx
movl %ebx, %esi
callq 0x9619
movl %eax, %ebp
leaq 0x14(%r15), %rdi
movl %ebx, %esi
callq 0x9619
movl %eax, %ecx
movq 0x10(%rsp), %rax
subq %rcx, %rax
jb 0x938e
addl $0x18, %r13d
cmpq %r13, %rax
jb 0x938e
cmpl $0x4b, %ecx
jbe 0x957e
movq %rcx, 0x20(%rsp)
movl %ebp, 0x3c(%rsp)
leaq 0x18(%r15), %rdi
pushq $0x2
popq %rsi
movq %rdi, 0x40(%rsp)
callq 0x9619
movl %eax, %ebx
leaq 0x28(%r15), %rdi
pushq $0x4
popq %rbp
movl %ebp, %esi
callq 0x9619
movl %eax, %r14d
leaq 0x34(%r15), %rdi
movl %ebp, %esi
callq 0x9619
movl %eax, %r12d
leaq 0x60(%r15), %rdi
movl %ebp, %esi
callq 0x9619
cmpl $0x20b, %ebx            # imm = 0x20B
je 0x9593
movl %r12d, %ecx
notl %ecx
movl %ecx, 0xc(%rsp)
cmpl %ecx, %r14d
ja 0x95a8
movl %eax, 0x34(%rsp)
movl %r14d, 0x38(%rsp)
cmpq $0x0, 0x18(%rsp)
movq %r12, 0x28(%rsp)
movq 0x20(%rsp), %r14
je 0x93da
cmpl $0x70, %r14d
jb 0x936c
leaq 0x80(%r15), %rdi
pushq $0x4
popq %rbx
movl %ebx, %esi
callq 0x9619
movq %rax, 0x50(%rsp)
leaq 0x84(%r15), %rdi
movl %ebx, %esi
callq 0x9619
movl %eax, %edi
xorl %eax, %eax
movl $0x0, %ebp
cmpl $0xc8, %r14d
jb 0x92f6
movl %edi, %r12d
leaq 0xd8(%r15), %rdi
pushq $0x4
popq %rbx
movl %ebx, %esi
callq 0x9619
movl %eax, %ebp
addq $0xdc, %r15
movq %r15, %rdi
movl %ebx, %esi
callq 0x9619
movl %r12d, %edi
testl %edi, %edi
movq 0x28(%rsp), %r12
movq 0x50(%rsp), %rsi
je 0x931e
cmpl 0xc(%rsp), %esi
ja 0x95e1
leal (%rsi,%r12), %ecx
movl %edi, %edx
negl %edx
cmpl %edx, %ecx
ja 0x95e1
testl %eax, %eax
je 0x93be
cmpl 0xc(%rsp), %ebp
ja 0x933c
leal (%r12,%rbp), %ecx
movl %eax, %edx
negl %edx
cmpl %edx, %ecx
jbe 0x93c0
movq 0x8cb5(%rip), %rax      # 0x11ff8
movq (%rax), %rcx
leaq 0x3f07(%rip), %rdi      # 0xd254
pushq $0x2d
jmp 0x95f4
movq 0x8c9d(%rip), %rax      # 0x11ff8
movq (%rax), %rcx
leaq 0x3e12(%rip), %rdi      # 0xd177
pushq $0x1e
jmp 0x9555
xorl %esi, %esi
xorl %edi, %edi
xorl %eax, %eax
xorl %ebp, %ebp
jmp 0x93c0
movq 0x8c7b(%rip), %rax      # 0x11ff8
movq (%rax), %rcx
leaq 0x3dbc(%rip), %rdi      # 0xd143
pushq $0xc
jmp 0x9555
movq 0x8c63(%rip), %rax      # 0x11ff8
movq (%rax), %rcx
leaq 0x3df7(%rip), %rdi      # 0xd196
pushq $0x2b
jmp 0x95f4
movq 0x8c4b(%rip), %rax      # 0x11ff8
movq (%rax), %rcx
leaq 0x3d69(%rip), %rdi      # 0xd120
pushq $0x22
jmp 0x9555
xorl %eax, %eax
movq 0x18(%rsp), %rcx
movl %r12d, (%rcx)
addl %r12d, %esi
movl %esi, 0x4(%rcx)
movl %edi, 0x8(%rcx)
addl %r12d, %ebp
movl %ebp, 0xc(%rcx)
movl %eax, 0x10(%rcx)
movl 0x3c(%rsp), %edx
imull $0x28, %edx, %eax
movl %eax, %eax
movq 0x10(%rsp), %rcx
subq %rax, %rcx
jb 0x952a
addl %r14d, %r13d
cmpq %r13, %rcx
jb 0x952a
addq %r14, 0x40(%rsp)
movl %edx, %eax
imulq $0x28, %rax, %rax
movq %rax, 0x20(%rsp)
xorl %r12d, %r12d
xorl %eax, %eax
movq 0x40(%rsp), %rbp
pushq $0x4
popq %r14
cmpq %r12, 0x20(%rsp)
je 0x94f4
movl %eax, 0x10(%rsp)
movq %r12, 0x18(%rsp)
andl $-0x8, %r12d
leaq (%r12,%rbp), %rdi
addq $0x8, %rdi
movl %r14d, %esi
callq 0x9619
movl %eax, %r15d
leaq (%r12,%rbp), %rdi
addq $0xc, %rdi
movl %r14d, %esi
callq 0x9619
movl %eax, %ebx
leaq (%r12,%rbp), %rdi
addq $0x10, %rdi
movl %r14d, %esi
callq 0x9619
movl %eax, %r13d
leaq (%r12,%rbp), %rdi
addq $0x14, %rdi
movl %r14d, %esi
callq 0x9619
cmpl %r13d, %r15d
cmovbl %r15d, %r13d
testl %eax, %eax
cmovel %eax, %r13d
cmpl 0xc(%rsp), %ebx
movq 0x28(%rsp), %rsi
ja 0x956b
testl %r15d, %r15d
je 0x94ac
leal (%rbx,%rsi), %ecx
movl %r15d, %edx
negl %edx
cmpl %edx, %ecx
ja 0x956b
movq 0x48(%rsp), %rcx
movl %eax, %r12d
subq %r12, %rcx
movl %r13d, %eax
cmpq %rax, %rcx
jb 0x95bd
addl %esi, %ebx
movl %ebx, %edi
movl %r15d, %esi
callq 0x5e70
addq (%rsp), %r12
movq %r12, %rdi
movl %ebx, %esi
movl %r13d, %edx
callq 0x5d3e
movl 0x10(%rsp), %eax
incl %eax
movq 0x18(%rsp), %r12
addq $0x28, %r12
jmp 0x941b
movq 0x60(%rsp), %rax
testq %rax, %rax
je 0x9509
movq 0x28(%rsp), %rcx
addl 0x38(%rsp), %ecx
movl %ecx, (%rax)
movq 0x58(%rsp), %rax
testq %rax, %rax
movq (%rsp), %rdi
je 0x951d
movl 0x34(%rsp), %ecx
movl %ecx, (%rax)
callq 0x1030
pushq $0x1
popq %rbp
jmp 0x9608
movq 0x8ac7(%rip), %rax      # 0x11ff8
movq (%rax), %rcx
leaq 0x3d47(%rip), %rdi      # 0xd282
pushq $0x29
jmp 0x95f4
movq 0x8aaf(%rip), %rax      # 0x11ff8
movq (%rax), %rcx
leaq 0x3bfd(%rip), %rdi      # 0xd150
pushq $0x26
popq %rsi
pushq $0x1
popq %rdx
callq 0x1190
movq %rbx, %rdi
callq 0x1030
jmp 0x9608
movq 0x8a86(%rip), %rax      # 0x11ff8
movq (%rax), %rdi
leaq 0x3d30(%rip), %rsi      # 0xd2ac
jmp 0x95ce
movq 0x8a73(%rip), %rax      # 0x11ff8
movq (%rax), %rcx
leaq 0x3c33(%rip), %rdi      # 0xd1c2
pushq $0x1d
jmp 0x95f4
movq 0x8a5e(%rip), %rax      # 0x11ff8
movq (%rax), %rcx
leaq 0x3c3c(%rip), %rdi      # 0xd1e0
pushq $0x24
jmp 0x95f4
movq 0x8a49(%rip), %rax      # 0x11ff8
movq (%rax), %rcx
leaq 0x3c4c(%rip), %rdi      # 0xd205
pushq $0x23
jmp 0x95f4
movq 0x8a34(%rip), %rax      # 0x11ff8
movq (%rax), %rdi
leaq 0x3d02(%rip), %rsi      # 0xd2d0
xorl %ebp, %ebp
movl 0x10(%rsp), %edx
xorl %eax, %eax
callq 0x10d0
movq (%rsp), %rdi
jmp 0x9561
movq 0x8a10(%rip), %rax      # 0x11ff8
movq (%rax), %rcx
leaq 0x3c37(%rip), %rdi      # 0xd229
pushq $0x2a
popq %rsi
pushq $0x1
popq %rdx
callq 0x1190
movq (%rsp), %rdi
callq 0x1030
xorl %ebp, %ebp
movl %ebp, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq