// File: /workspace/github_c_makefile_star2_O1/mikecat[P]x86_interpreter/pe_import.c
// Function: pe_import_initialize
int pe_import_initialize(const pe_import_params* params, uint32_t work_start, uint32_t argc, uint32_t argv) {
	uint32_t iat_end = params->iat_addr + (params->iat_size > 0 ? params->iat_size - 1 : 0);
	if (!pe_libs_initialize(work_start, argc, argv)) return 0;
	free(imported_libs);
	imported_libs = NULL;
	imported_lib_count = 0;
	if (params->import_size >= 20) {
		uint32_t i;
		for (i = 0; i * 20 <= params->import_size - 20; i++) {
			uint32_t this_addr = params->import_addr + i * 20;
			uint8_t data[20];
			uint32_t name_ptr;
			uint32_t j;
			int all_zero = 1;
			imported_lib_info* new_libs;
			int lib_id;
			/* 情報をロードする */
			if (!dmemory_is_allocated(this_addr, 20)) {
				fprintf(stderr, "PE import descriptor %"PRIu32" not allocated!\n", i);
				return 0;
			}
			dmemory_read(data, this_addr, 20);
			for (j = 0; j < 20; j++) {
				if (data[j] != 0) all_zero = 0;
			}
			if (all_zero) break;
			new_libs = realloc(imported_libs, sizeof(*imported_libs) * (i + 1));
			if (new_libs == NULL) {
				perror("realloc");
				return 0;
			}
			/* 情報をデコードする */
			imported_libs = new_libs;
			imported_libs[i].int_addr = read_num(data + 0, 4);
			imported_libs[i].iat_addr = read_num(data + 16, 4);
			name_ptr = read_num(data + 12, 4);
			if (UINT32_MAX - params->image_base < imported_libs[i].int_addr ||
			UINT32_MAX - params->image_base < imported_libs[i].iat_addr ||
			UINT32_MAX - params->image_base < name_ptr) {
				fprintf(stderr, "some address in PE import descriptor %"PRIu32" out of address space!\n", i);
				return 0;
			}
			imported_libs[i].has_int = (imported_libs[i].int_addr != 0);
			imported_libs[i].int_addr += params->image_base;
			imported_libs[i].iat_addr += params->image_base;
			imported_libs[i].iat_size = 0;
			name_ptr += params->image_base;
			/* インポート対象の名前情報を得る */
			imported_libs[i].name = dmem_read_string(name_ptr);
			if (imported_libs[i].name == NULL) {
				fprintf(stderr, "name in PE import descriptor %"PRIu32" is invalid!\n", i);
				return 0;
			}
			lib_id = get_lib_id(imported_libs[i].name);
			/* INT/IATから情報を得る */
			if (!imported_libs[i].has_int) imported_libs[i].int_addr = imported_libs[i].iat_addr;
			imported_libs[i].funcs = NULL;
			imported_libs[i].func_num = 0;
			if (iat_end >= 3) {
				for (j = 0; imported_libs[i].iat_addr + j * 4 <= iat_end - 3; j++) {
					uint8_t entry[4];
					uint32_t entry_value;
					func_info* new_funcs;
					func_info* new_func;
					if (UINT32_MAX - imported_libs[i].int_addr < j * 4 ||
					!dmemory_is_allocated(imported_libs[i].int_addr + j * 4, 4)) {
						fprintf(stderr, "unallocated entry exists in INT/IAT of PE import descriptor %"PRIu32"\n", i);
						return 0;
					}
					dmemory_read(entry, imported_libs[i].int_addr + j * 4, 4);
					entry_value = read_num(entry, 4);
					if (entry_value == 0) break;
					new_funcs = realloc(imported_libs[i].funcs, sizeof(*new_funcs) * (j + 1));
					if (new_funcs == NULL) {
						perror("realloc");
						return 0;
					}
					imported_libs[i].funcs = new_funcs;
					new_func = &imported_libs[i].funcs[j];
					if (entry_value & UINT32_C(0x80000000)) {
						new_func->is_ord = 1;
						new_func->name = NULL;
						new_func->hint_or_ord = entry_value & 0xffff;
					} else {
						uint8_t hint[2];
						if (UINT32_MAX - params->image_base < entry_value + 2 ||
						!dmemory_is_allocated(params->image_base + entry_value, 2) ||
						(new_func->name = dmem_read_string(params->image_base + entry_value + 2)) == NULL) {
							fprintf(stderr, "invalid entry exists in INT/IAT of PE import descriptor %"PRIu32"\n", i);
							return 0;
						}
						dmemory_read(hint, params->image_base + entry_value, 2);
						new_func->is_ord = 0;
						new_func->hint_or_ord = read_num(hint, 2);
					}
					write_num(entry, get_buffer_address(lib_id, new_func->name, imported_libs[i].iat_addr + j * 4), 4);
					dmemory_write(entry, imported_libs[i].iat_addr + j * 4, 4);
				}
				imported_libs[i].func_num = j;
				imported_libs[i].iat_size = j * 4;
			}
		}
		imported_lib_count = i;
	}
	return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rdi, %r14
movl 0x10(%rdi), %r15d
xorl %ebx, %ebx
subl $0x1, %r15d
cmovbl %ebx, %r15d
addl 0xc(%rdi), %r15d
movl %esi, %edi
movl %edx, %esi
movl %ecx, %edx
callq 0xbf94
testl %eax, %eax
je 0xbeac
movq 0x12443(%rip), %rdi     # 0x1ddb0
callq 0x1030
movq $0x0, 0x12433(%rip) # 0x1ddb0
movl $0x0, 0x12431(%rip)     # 0x1ddb8
movl $0x1, %ebx
cmpl $0x14, 0x8(%r14)
jb 0xbeac
movq %r15, 0x50(%rsp)
leal -0x3(%r15), %ebp
xorl %r13d, %r13d
leaq 0x30(%rsp), %rbx
leaq 0xc(%rsp), %r12
xorl %r15d, %r15d
movq $0x0, 0x10(%rsp)
movq %r14, 0x20(%rsp)
addl 0x4(%r14), %r15d
movl %r15d, %edi
movl $0x14, %esi
callq 0x7134
testl %eax, %eax
je 0xbded
movq %rbx, %rdi
movl %r15d, %esi
movl $0x14, %edx
callq 0x6c34
movl $0x1, %eax
xorl %ecx, %ecx
cmpb $0x0, 0x30(%rsp,%rcx)
cmovnel %r13d, %eax
incq %rcx
cmpq $0x14, %rcx
jne 0xb9ee
movl $0x2, %ecx
testl %eax, %eax
je 0xba3d
testl %ecx, %ecx
jne 0xbe93
movq 0x10(%rsp), %rcx
movl %ecx, %eax
incl %eax
leal 0x4(,%rcx,4), %ecx
leal (%rcx,%rcx,4), %r15d
movl 0x8(%r14), %ecx
addl $-0x14, %ecx
movl %eax, %edx
movq %rdx, 0x10(%rsp)
cmpl %ecx, %r15d
jbe 0xb9be
jmp 0xbe9d
movq 0x1236c(%rip), %rdi     # 0x1ddb0
movq 0x10(%rsp), %rax
incl %eax
shlq $0x3, %rax
leaq (%rax,%rax,4), %rsi
callq 0x1130
testq %rax, %rax
je 0xbe51
movq %rax, 0x12348(%rip)     # 0x1ddb0
xorl %ecx, %ecx
xorl %edx, %edx
movzbl (%rbx), %edi
shll %cl, %edi
orl %edi, %edx
addq $0x8, %rcx
incq %rbx
cmpq $0x20, %rcx
jne 0xba6c
movl 0x10(%rsp), %esi
leaq (%rsi,%rsi,4), %r15
leaq (%rax,%r15,8), %r8
movl %edx, (%rax,%r15,8)
xorl %ecx, %ecx
leaq 0x40(%rsp), %rdi
xorl %r9d, %r9d
movzbl (%rdi), %r10d
shll %cl, %r10d
orl %r10d, %r9d
addq $0x8, %rcx
incq %rdi
cmpq $0x20, %rcx
jne 0xba9a
leaq (%rax,%r15,8), %r10
addq $0x4, %r10
movl %r9d, 0x4(%rax,%r15,8)
xorl %ecx, %ecx
leaq 0x3c(%rsp), %r11
xorl %edi, %edi
movzbl (%r11), %ebx
shll %cl, %ebx
orl %ebx, %edi
addq $0x8, %rcx
incq %r11
cmpq $0x20, %rcx
jne 0xbac7
movl (%r14), %ecx
movl %ecx, %r11d
notl %r11d
cmpl %r11d, %edx
ja 0xbe35
cmpl %r11d, 0x4(%r8)
ja 0xbe35
cmpl %r11d, %edi
ja 0xbe35
xorl %r11d, %r11d
testl %edx, %edx
setne %r11b
movl %r11d, 0xc(%r8)
addl %ecx, %edx
movl %edx, (%r8)
addl %ecx, %r9d
movl %r9d, (%r10)
shlq $0x3, %rsi
leaq (%rsi,%rsi,4), %rbx
movl $0x0, 0x8(%rax,%rbx)
addl %ecx, %edi
callq 0x72dc
movq 0x12279(%rip), %rcx     # 0x1ddb0
movq %rax, 0x10(%rcx,%rbx)
testq %rax, %rax
je 0xbe67
movq %rax, %rdi
callq 0xc146
movl %eax, 0x2c(%rsp)
movq 0x12258(%rip), %rax     # 0x1ddb0
cmpl $0x0, 0xc(%rax,%r15,8)
leaq 0x30(%rsp), %rbx
jne 0xbb6e
leaq (%rax,%r15,8), %rcx
movl 0x4(%rcx), %edx
movl %edx, (%rcx)
xorl %ecx, %ecx
movq $0x0, 0x18(%rax,%r15,8)
movl $0x0, 0x20(%rax,%r15,8)
cmpl $0x3, 0x50(%rsp)
jb 0xba09
movq 0x1221c(%rip), %rax     # 0x1ddb0
movl $0x0, %ecx
movl $0x0, %r14d
cmpl %ebp, 0x4(%rax,%r15,8)
ja 0xbe13
xorl %r14d, %r14d
xorl %ebx, %ebx
movl %ebp, 0x18(%rsp)
movl (%rax,%r15,8), %edi
addl %r14d, %edi
jb 0xbd65
movl $0x4, %esi
callq 0x7134
testl %eax, %eax
je 0xbd65
movq 0x121d7(%rip), %rax     # 0x1ddb0
movl (%rax,%r15,8), %esi
addl %r14d, %esi
movq %r12, %rdi
movl $0x4, %edx
callq 0x6c34
xorl %ecx, %ecx
movq %r12, %rax
xorl %r12d, %r12d
movzbl (%rax), %edx
shll %cl, %edx
orl %edx, %r12d
addq $0x8, %rcx
incq %rax
cmpq $0x20, %rcx
jne 0xbbf5
testl %r12d, %r12d
je 0xbce9
movq 0x12196(%rip), %rbp     # 0x1ddb0
movq 0x18(%rbp,%r15,8), %rdi
leal 0x1(%rbx), %eax
shlq $0x3, %rax
leaq (%rax,%rax,2), %rsi
callq 0x1130
testq %rax, %rax
je 0xbda8
leaq 0x18(,%r15,8), %rcx
addq %rbp, %rcx
movq %rax, (%rcx)
movl %ebx, %ecx
leaq (%rcx,%rcx,2), %rcx
leaq (%rax,%rcx,8), %rbp
testl %r12d, %r12d
js 0xbcf3
movq 0x20(%rsp), %rax
movl (%rax), %edi
movl $0xfffffffd, %eax       # imm = 0xFFFFFFFD
subl %r12d, %eax
cmpl %edi, %eax
jb 0xbd89
addl %r12d, %edi
movl $0x2, %esi
callq 0x7134
testl %eax, %eax
je 0xbd89
movq 0x20(%rsp), %rax
movl (%rax), %eax
leal (%r12,%rax), %edi
addl $0x2, %edi
callq 0x72dc
movq %rax, 0x8(%rbp)
testq %rax, %rax
je 0xbd89
movq 0x20(%rsp), %rax
addl (%rax), %r12d
leaq 0x1e(%rsp), %rdi
movl %r12d, %esi
movl $0x2, %edx
callq 0x6c34
movl $0x0, (%rbp)
xorl %ecx, %ecx
leaq 0x1e(%rsp), %rdx
xorl %eax, %eax
movzbl (%rdx), %esi
shll %cl, %esi
orl %esi, %eax
addq $0x8, %rcx
incq %rdx
cmpq $0x10, %rcx
jne 0xbccf
movw %ax, 0x10(%rbp)
jmp 0xbd07
movl $0x8, %eax
jmp 0xbdbd
movl $0x1, (%rbp)
movq $0x0, 0x8(%rbp)
movw %r12w, 0x10(%rbp)
leaq 0xc(%rsp), %r12
movq 0x8(%rbp), %rsi
movq 0x12099(%rip), %rax     # 0x1ddb0
movl 0x4(%rax,%r15,8), %edx
addl %r14d, %edx
movl 0x2c(%rsp), %edi
callq 0xc1ac
movq %r12, %rdx
xorl %ecx, %ecx
movl %eax, %esi
shrl %cl, %esi
movb %sil, (%rdx)
addq $0x8, %rcx
incq %rdx
cmpq $0x20, %rcx
jne 0xbd2d
movq 0x12068(%rip), %rax     # 0x1ddb0
movl 0x4(%rax,%r15,8), %esi
addl %r14d, %esi
movq %r12, %rdi
movl $0x4, %edx
callq 0x6d8a
xorl %eax, %eax
movl 0x18(%rsp), %ebp
jmp 0xbdc2
movq 0x728c(%rip), %rax      # 0x12ff8
movq (%rax), %rdi
leaq 0x26c4(%rip), %rsi      # 0xe43a
movq 0x10(%rsp), %rdx
xorl %eax, %eax
callq 0x10d0
movl $0x1, %eax
jmp 0xbdc2
movq 0x7268(%rip), %rax      # 0x12ff8
movq (%rax), %rdi
leaq 0x26e0(%rip), %rsi      # 0xe47a
movq 0x10(%rsp), %rdx
xorl %eax, %eax
callq 0x10d0
jmp 0xbdb4
leaq 0x2233(%rip), %rdi      # 0xdfe2
callq 0x1160
movl $0x1, %eax
movl 0x18(%rsp), %ebp
leaq 0xc(%rsp), %r12
testl %eax, %eax
jne 0xbe0c
movq 0x11fe3(%rip), %rax     # 0x1ddb0
movl %ebx, %ecx
incl %ecx
leal 0x4(,%rbx,4), %r14d
movl 0x4(%rax,%r15,8), %edx
addl %r14d, %edx
movl %ecx, %ebx
cmpl %ebp, %edx
jbe 0xbbb3
jmp 0xbe13
movq 0x7204(%rip), %rax      # 0x12ff8
movq (%rax), %rdi
leaq 0x25a8(%rip), %rsi      # 0xe3a6
movq 0x10(%rsp), %rdx
xorl %eax, %eax
callq 0x10d0
jmp 0xbe5d
movl %ebx, %ecx
cmpl $0x8, %eax
jne 0xbe48
movq 0x11f96(%rip), %rax     # 0x1ddb0
movl %ecx, 0x20(%rax,%r15,8)
movl %r14d, 0x8(%rax,%r15,8)
movq 0x20(%rsp), %r14
leaq 0x30(%rsp), %rbx
xorl %ecx, %ecx
jmp 0xba09
movq 0x71bc(%rip), %rax      # 0x12ff8
movq (%rax), %rdi
leaq 0x2588(%rip), %rsi      # 0xe3ce
jmp 0xbe78
movl %eax, %ecx
movq 0x20(%rsp), %r14
jmp 0xbe89
leaq 0x218a(%rip), %rdi      # 0xdfe2
callq 0x1160
movl $0x1, %ecx
jmp 0xba09
movq 0x718a(%rip), %rax      # 0x12ff8
movq (%rax), %rdi
leaq 0x2595(%rip), %rsi      # 0xe40d
movq 0x10(%rsp), %rdx
xorl %eax, %eax
callq 0x10d0
movl $0x1, %ecx
leaq 0x30(%rsp), %rbx
jmp 0xba09
movq 0x10(%rsp), %rax
cmpl $0x2, %ecx
jne 0xbeaa
movl %eax, 0x11f15(%rip)     # 0x1ddb8
movl $0x1, %ebx
jmp 0xbeac
xorl %ebx, %ebx
movl %ebx, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq