// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0xc0, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq $0x0, -0x30(%rbp)
movq -0x28(%rbp), %rsi
leaq -0x30(%rbp), %rdi
callq 0xc7a0
movq %rax, -0x38(%rbp)
cmpq $0x0, -0x38(%rbp)
jne 0xcf07
movl $0x0, -0x4(%rbp)
jmp 0xd5cd
cmpq $0x40, -0x30(%rbp)
jae 0xcf3b
movq 0x80db(%rip), %rax      # 0x14ff0
movq (%rax), %rdi
leaq 0x532d(%rip), %rsi      # 0x1224c
movb $0x0, %al
callq 0x10d0
movq -0x38(%rbp), %rdi
callq 0x1030
movl $0x0, -0x4(%rbp)
jmp 0xd5cd
movq -0x38(%rbp), %rax
movzbl (%rax), %eax
cmpl $0x4d, %eax
jne 0xcf54
movq -0x38(%rbp), %rax
movzbl 0x1(%rax), %eax
cmpl $0x5a, %eax
je 0xcf81
movq 0x8095(%rip), %rax      # 0x14ff0
movq (%rax), %rdi
leaq 0x530a(%rip), %rsi      # 0x1226f
movb $0x0, %al
callq 0x10d0
movq -0x38(%rbp), %rdi
callq 0x1030
movl $0x0, -0x4(%rbp)
jmp 0xd5cd
movq -0x38(%rbp), %rdi
addq $0x3c, %rdi
movl $0x4, %esi
callq 0xd5e0
movl %eax, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
movq -0x30(%rbp), %rcx
subq $0x18, %rcx
cmpq %rcx, %rax
jbe 0xcfd3
movq 0x8043(%rip), %rax      # 0x14ff0
movq (%rax), %rdi
leaq 0x52c5(%rip), %rsi      # 0x1227c
movb $0x0, %al
callq 0x10d0
movq -0x38(%rbp), %rdi
callq 0x1030
movl $0x0, -0x4(%rbp)
jmp 0xd5cd
movq -0x38(%rbp), %rax
movl -0x3c(%rbp), %ecx
addq %rcx, %rax
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rax
movzbl (%rax), %eax
cmpl $0x50, %eax
jne 0xd014
movq -0x48(%rbp), %rax
movzbl 0x1(%rax), %eax
cmpl $0x45, %eax
jne 0xd014
movq -0x48(%rbp), %rax
movzbl 0x2(%rax), %eax
cmpl $0x0, %eax
jne 0xd014
movq -0x48(%rbp), %rax
movzbl 0x3(%rax), %eax
cmpl $0x0, %eax
je 0xd041
movq 0x7fd5(%rip), %rax      # 0x14ff0
movq (%rax), %rdi
leaq 0x527e(%rip), %rsi      # 0x122a3
movb $0x0, %al
callq 0x10d0
movq -0x38(%rbp), %rdi
callq 0x1030
movl $0x0, -0x4(%rbp)
jmp 0xd5cd
movq -0x48(%rbp), %rdi
addq $0x6, %rdi
movl $0x2, %esi
callq 0xd5e0
movl %eax, -0x4c(%rbp)
movq -0x48(%rbp), %rdi
addq $0x14, %rdi
movl $0x2, %esi
callq 0xd5e0
movl %eax, -0x50(%rbp)
imull $0x28, -0x4c(%rbp), %eax
movl %eax, -0x54(%rbp)
movl -0x50(%rbp), %eax
cmpq -0x30(%rbp), %rax
ja 0xd092
movq -0x30(%rbp), %rax
movl -0x50(%rbp), %ecx
subq %rcx, %rax
movl -0x3c(%rbp), %ecx
addl $0x18, %ecx
movl %ecx, %ecx
cmpq %rcx, %rax
jae 0xd0bf
movq 0x7f57(%rip), %rax      # 0x14ff0
movq (%rax), %rdi
leaq 0x521f(%rip), %rsi      # 0x122c2
movb $0x0, %al
callq 0x10d0
movq -0x38(%rbp), %rdi
callq 0x1030
movl $0x0, -0x4(%rbp)
jmp 0xd5cd
cmpl $0x4c, -0x50(%rbp)
jae 0xd0f2
movq 0x7f24(%rip), %rax      # 0x14ff0
movq (%rax), %rdi
leaq 0x5218(%rip), %rsi      # 0x122ee
movb $0x0, %al
callq 0x10d0
movq -0x38(%rbp), %rdi
callq 0x1030
movl $0x0, -0x4(%rbp)
jmp 0xd5cd
movq -0x48(%rbp), %rax
addq $0x18, %rax
movq %rax, -0x60(%rbp)
movq -0x60(%rbp), %rdi
movl $0x2, %esi
callq 0xd5e0
movl %eax, -0x64(%rbp)
movq -0x60(%rbp), %rdi
addq $0x10, %rdi
movl $0x4, %esi
callq 0xd5e0
movl %eax, -0x68(%rbp)
movq -0x60(%rbp), %rdi
addq $0x1c, %rdi
movl $0x4, %esi
callq 0xd5e0
movl %eax, -0x6c(%rbp)
movq -0x60(%rbp), %rdi
addq $0x48, %rdi
movl $0x4, %esi
callq 0xd5e0
movl %eax, -0x70(%rbp)
cmpl $0x20b, -0x64(%rbp)     # imm = 0x20B
jne 0xd184
movq 0x7e92(%rip), %rax      # 0x14ff0
movq (%rax), %rdi
leaq 0x51a4(%rip), %rsi      # 0x1230c
movb $0x0, %al
callq 0x10d0
movq -0x38(%rbp), %rdi
callq 0x1030
movl $0x0, -0x4(%rbp)
jmp 0xd5cd
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
subl -0x6c(%rbp), %eax
cmpl -0x68(%rbp), %eax
jae 0xd1be
movq 0x7e58(%rip), %rax      # 0x14ff0
movq (%rax), %rdi
leaq 0x518f(%rip), %rsi      # 0x12331
movb $0x0, %al
callq 0x10d0
movq -0x38(%rbp), %rdi
callq 0x1030
movl $0x0, -0x4(%rbp)
jmp 0xd5cd
cmpq $0x0, -0x20(%rbp)
je 0xd368
cmpl $0x70, -0x50(%rbp)
jb 0xd1fe
movq -0x60(%rbp), %rdi
addq $0x68, %rdi
movl $0x4, %esi
callq 0xd5e0
movl %eax, -0x80(%rbp)
movq -0x60(%rbp), %rdi
addq $0x6c, %rdi
movl $0x4, %esi
callq 0xd5e0
movl %eax, -0x84(%rbp)
jmp 0xd20f
movl $0x0, -0x80(%rbp)
movl $0x0, -0x84(%rbp)
cmpl $0xc8, -0x50(%rbp)
jb 0xd250
movq -0x60(%rbp), %rdi
addq $0xc0, %rdi
movl $0x4, %esi
callq 0xd5e0
movl %eax, -0x88(%rbp)
movq -0x60(%rbp), %rdi
addq $0xc4, %rdi
movl $0x4, %esi
callq 0xd5e0
movl %eax, -0x8c(%rbp)
jmp 0xd264
movl $0x0, -0x88(%rbp)
movl $0x0, -0x8c(%rbp)
cmpl $0x0, -0x84(%rbp)
jbe 0xd2c3
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
subl -0x6c(%rbp), %eax
cmpl -0x80(%rbp), %eax
jb 0xd294
movl -0x6c(%rbp), %ecx
addl -0x80(%rbp), %ecx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
subl %ecx, %eax
movl -0x84(%rbp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jae 0xd2c1
movq 0x7d55(%rip), %rax      # 0x14ff0
movq (%rax), %rdi
leaq 0x50b0(%rip), %rsi      # 0x12355
movb $0x0, %al
callq 0x10d0
movq -0x38(%rbp), %rdi
callq 0x1030
movl $0x0, -0x4(%rbp)
jmp 0xd5cd
jmp 0xd2c3
cmpl $0x0, -0x8c(%rbp)
jbe 0xd328
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
subl -0x6c(%rbp), %eax
cmpl -0x88(%rbp), %eax
jb 0xd2f9
movl -0x6c(%rbp), %ecx
addl -0x88(%rbp), %ecx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
subl %ecx, %eax
movl -0x8c(%rbp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jae 0xd326
movq 0x7cf0(%rip), %rax      # 0x14ff0
movq (%rax), %rdi
leaq 0x5076(%rip), %rsi      # 0x12380
movb $0x0, %al
callq 0x10d0
movq -0x38(%rbp), %rdi
callq 0x1030
movl $0x0, -0x4(%rbp)
jmp 0xd5cd
jmp 0xd328
movl -0x6c(%rbp), %ecx
movq -0x20(%rbp), %rax
movl %ecx, (%rax)
movl -0x6c(%rbp), %ecx
addl -0x80(%rbp), %ecx
movq -0x20(%rbp), %rax
movl %ecx, 0x4(%rax)
movl -0x84(%rbp), %ecx
movq -0x20(%rbp), %rax
movl %ecx, 0x8(%rax)
movl -0x6c(%rbp), %ecx
addl -0x88(%rbp), %ecx
movq -0x20(%rbp), %rax
movl %ecx, 0xc(%rax)
movl -0x8c(%rbp), %ecx
movq -0x20(%rbp), %rax
movl %ecx, 0x10(%rax)
movl -0x54(%rbp), %eax
cmpq -0x30(%rbp), %rax
ja 0xd38b
movq -0x30(%rbp), %rax
movl -0x54(%rbp), %ecx
subq %rcx, %rax
movl -0x3c(%rbp), %ecx
addl $0x18, %ecx
addl -0x50(%rbp), %ecx
movl %ecx, %ecx
cmpq %rcx, %rax
jae 0xd3b8
movq 0x7c5e(%rip), %rax      # 0x14ff0
movq (%rax), %rdi
leaq 0x5012(%rip), %rsi      # 0x123ae
movb $0x0, %al
callq 0x10d0
movq -0x38(%rbp), %rdi
callq 0x1030
movl $0x0, -0x4(%rbp)
jmp 0xd5cd
movq -0x60(%rbp), %rax
movl -0x50(%rbp), %ecx
addq %rcx, %rax
movq %rax, -0x78(%rbp)
movl $0x0, -0x7c(%rbp)
movl -0x7c(%rbp), %eax
cmpl -0x4c(%rbp), %eax
jae 0xd59a
movq -0x78(%rbp), %rax
imull $0x28, -0x7c(%rbp), %ecx
movl %ecx, %ecx
addq %rcx, %rax
movq %rax, -0x98(%rbp)
movq -0x98(%rbp), %rdi
addq $0x8, %rdi
movl $0x4, %esi
callq 0xd5e0
movl %eax, -0x9c(%rbp)
movq -0x98(%rbp), %rdi
addq $0xc, %rdi
movl $0x4, %esi
callq 0xd5e0
movl %eax, -0xa0(%rbp)
movq -0x98(%rbp), %rdi
addq $0x10, %rdi
movl $0x4, %esi
callq 0xd5e0
movl %eax, -0xa4(%rbp)
movq -0x98(%rbp), %rdi
addq $0x14, %rdi
movl $0x4, %esi
callq 0xd5e0
movl %eax, -0xa8(%rbp)
cmpl $0x0, -0xa8(%rbp)
jne 0xd46c
xorl %eax, %eax
movl %eax, -0xb0(%rbp)
jmp 0xd4a0
movl -0x9c(%rbp), %eax
cmpl -0xa4(%rbp), %eax
jae 0xd488
movl -0x9c(%rbp), %eax
movl %eax, -0xb4(%rbp)
jmp 0xd494
movl -0xa4(%rbp), %eax
movl %eax, -0xb4(%rbp)
movl -0xb4(%rbp), %eax
movl %eax, -0xb0(%rbp)
movl -0xb0(%rbp), %eax
movl %eax, -0xac(%rbp)
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
subl -0x6c(%rbp), %eax
cmpl -0xa0(%rbp), %eax
jb 0xd4e2
cmpl $0x0, -0x9c(%rbp)
jbe 0xd512
movl -0x6c(%rbp), %ecx
addl -0xa0(%rbp), %ecx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
subl %ecx, %eax
movl -0x9c(%rbp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jae 0xd512
movq 0x7b07(%rip), %rax      # 0x14ff0
movq (%rax), %rdi
movl -0x7c(%rbp), %edx
leaq 0x4ee2(%rip), %rsi      # 0x123d8
movb $0x0, %al
callq 0x10d0
movq -0x38(%rbp), %rdi
callq 0x1030
movl $0x0, -0x4(%rbp)
jmp 0xd5cd
movq -0x30(%rbp), %rax
movl -0xa8(%rbp), %ecx
subq %rcx, %rax
movl -0xac(%rbp), %ecx
cmpq %rcx, %rax
jae 0xd557
movq 0x7abf(%rip), %rax      # 0x14ff0
movq (%rax), %rdi
movl -0x7c(%rbp), %edx
leaq 0x4ebe(%rip), %rsi      # 0x123fc
movb $0x0, %al
callq 0x10d0
movq -0x38(%rbp), %rdi
callq 0x1030
movl $0x0, -0x4(%rbp)
jmp 0xd5cd
movl -0x6c(%rbp), %edi
addl -0xa0(%rbp), %edi
movl -0x9c(%rbp), %esi
callq 0x7c60
movq -0x38(%rbp), %rdi
movl -0xa8(%rbp), %eax
addq %rax, %rdi
movl -0x6c(%rbp), %esi
addl -0xa0(%rbp), %esi
movl -0xac(%rbp), %edx
callq 0x7ad0
movl -0x7c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x7c(%rbp)
jmp 0xd3cd
cmpq $0x0, -0x10(%rbp)
je 0xd5ad
movl -0x6c(%rbp), %ecx
addl -0x68(%rbp), %ecx
movq -0x10(%rbp), %rax
movl %ecx, (%rax)
cmpq $0x0, -0x18(%rbp)
je 0xd5bd
movl -0x70(%rbp), %ecx
movq -0x18(%rbp), %rax
movl %ecx, (%rax)
movq -0x38(%rbp), %rdi
callq 0x1030
movl $0x1, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0xc0, %rsp
popq %rbp
retq
nopl (%rax)