// File: /workspace/github_c_makefile_star2_O2/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, %r12
movl 0x10(%rdi), %eax
xorl %ebp, %ebp
subl $0x1, %eax
cmovbl %ebp, %eax
addl 0xc(%rdi), %eax
movq %rax, 0x20(%rsp)
movl %esi, %edi
movl %edx, %esi
movl %ecx, %edx
callq 0xa1d0
testl %eax, %eax
je 0xa09c
movq 0x130d2(%rip), %rdi     # 0x1cdb0
callq 0x1030
andq $0x0, 0x130c5(%rip)     # 0x1cdb0
andl $0x0, 0x130c6(%rip)     # 0x1cdb8
movl 0x8(%r12), %eax
cmpl $0x14, %eax
jb 0xa057
movq 0x20(%rsp), %rcx
addl $-0x3, %ecx
movl %ecx, 0x10(%rsp)
xorl %r14d, %r14d
pushq $0x14
popq %rsi
xorl %ebx, %ebx
imull $0x14, %ebx, %ebp
addl $-0x14, %eax
cmpl %eax, %ebp
ja 0xa051
addl 0x4(%r12), %ebp
movl %ebp, %edi
movl %esi, %r15d
callq 0x606d
testl %eax, %eax
je 0xa0c0
leaq 0x30(%rsp), %rdi
movl %ebp, %esi
movl %r15d, %edx
callq 0x5bd0
xorl %ecx, %ecx
pushq $0x1
popq %rax
pushq $0x4
popq %r15
cmpq $0x14, %rcx
je 0x9d65
cmpb $0x0, 0x30(%rsp,%rcx)
cmovnel %r14d, %eax
incq %rcx
jmp 0x9d51
testl %eax, %eax
jne 0xa051
movq 0x1303c(%rip), %rdi     # 0x1cdb0
leal 0x1(%rbx), %r13d
imulq $0x28, %r13, %rsi
callq 0x1130
testq %rax, %rax
je 0xa08e
movq %rax, %r14
movq %r13, 0x48(%rsp)
movq %rax, 0x13017(%rip)     # 0x1cdb0
leaq 0x30(%rsp), %rdi
movl %r15d, %esi
callq 0xa0e8
movl %eax, %ebp
movq %rbx, 0x8(%rsp)
movl %ebx, %eax
imulq $0x28, %rax, %rbx
movl %ebp, (%r14,%rbx)
leaq 0x40(%rsp), %rdi
movl %r15d, %esi
callq 0xa0e8
movl %eax, %r13d
movl %eax, 0x4(%r14,%rbx)
leaq 0x3c(%rsp), %rdi
movl %r15d, %esi
callq 0xa0e8
movl (%r12), %ecx
movl %ecx, %edx
notl %edx
cmpl %edx, %ebp
ja 0xa0ad
cmpl %edx, %r13d
ja 0xa0ad
cmpl %edx, %eax
ja 0xa0ad
leaq (%r14,%rbx), %rdx
addq $0x4, %rdx
movq %r14, %rsi
addq %rbx, %rsi
xorl %edi, %edi
testl %ebp, %ebp
setne %dil
movl %edi, 0xc(%rsi)
addl %ecx, %ebp
movl %ebp, (%rsi)
addl %ecx, %r13d
movl %r13d, (%rdx)
andl $0x0, 0x8(%r14,%rbx)
addl %ecx, %eax
movl %eax, %edi
callq 0x61ee
movq 0x12f7d(%rip), %rcx     # 0x1cdb0
movq %rax, 0x10(%rcx,%rbx)
testq %rax, %rax
je 0xa0d5
movq %rax, %rdi
callq 0xa381
movl %eax, 0x14(%rsp)
movq 0x12f5c(%rip), %rax     # 0x1cdb0
cmpl $0x0, 0xc(%rax,%rbx)
jne 0x9e64
leaq (%rax,%rbx), %rcx
movl 0x4(%rcx), %edx
movl %edx, (%rcx)
andq $0x0, 0x18(%rax,%rbx)
andl $0x0, 0x20(%rax,%rbx)
cmpl $0x3, 0x20(%rsp)
leaq 0x1c(%rsp), %r13
jb 0xa03c
xorl %r14d, %r14d
movq %rbx, 0x28(%rsp)
leal (,%r14,4), %ebp
movl 0x4(%rax,%rbx), %ecx
addl %ebp, %ecx
cmpl 0x10(%rsp), %ecx
ja 0xa033
movl (%rax,%rbx), %edi
addl %ebp, %edi
jb 0xa05c
movl %r15d, %esi
callq 0x606d
testl %eax, %eax
je 0xa05c
movq 0x12eef(%rip), %rax     # 0x1cdb0
movl (%rax,%rbx), %esi
addl %ebp, %esi
movq %r13, %rdi
movl %r15d, %edx
callq 0x5bd0
movq %r13, %rdi
movl %r15d, %esi
callq 0xa0e8
testl %eax, %eax
je 0xa02c
movl %eax, %r13d
movq %r12, %r15
movq 0x12ebf(%rip), %r12     # 0x1cdb0
movq 0x18(%r12,%rbx), %rdi
leal 0x1(%r14), %eax
movq %rax, 0x50(%rsp)
imulq $0x18, %rax, %rsi
callq 0x1130
testq %rax, %rax
je 0xa08e
leaq (%r12,%rbx), %rcx
addq $0x18, %rcx
movq %rax, (%rcx)
movl %r14d, %ecx
imulq $0x18, %rcx, %r14
addq %rax, %r14
testl %r13d, %r13d
js 0x9faf
movl (%r15), %edi
pushq $-0x3
popq %rax
subl %r13d, %eax
cmpl %edi, %eax
jb 0xa06f
movq %r15, %r12
addl %r13d, %edi
pushq $0x2
popq %rsi
callq 0x606d
testl %eax, %eax
je 0xa06f
movl (%r12), %eax
leal (%rax,%r13), %edi
addl $0x2, %edi
callq 0x61ee
movq %rax, 0x8(%r14)
testq %rax, %rax
je 0xa06f
movl (%r12), %esi
addl %r13d, %esi
leaq 0x1a(%rsp), %r13
movq %r13, %rdi
pushq $0x2
popq %r12
movl %r12d, %edx
callq 0x5bd0
andl $0x0, (%r14)
movq %r13, %rdi
movl %r12d, %esi
callq 0xa0e8
movw %ax, 0x10(%r14)
movq 0x8(%r14), %rsi
movq 0x12e03(%rip), %r12     # 0x1cdb0
jmp 0x9fc2
movl $0x1, (%r14)
andq $0x0, 0x8(%r14)
movw %r13w, 0x10(%r14)
xorl %esi, %esi
movq 0x28(%rsp), %rax
movl 0x4(%r12,%rax), %edx
addl %ebp, %edx
movl 0x14(%rsp), %edi
callq 0xa3e0
xorl %ecx, %ecx
leaq 0x1c(%rsp), %r13
movq %r13, %rdx
movq %r15, %r12
pushq $0x4
popq %r15
cmpq $0x20, %rcx
je 0x9ffe
movl %eax, %esi
shrl %cl, %esi
movb %sil, (%rdx)
incq %rdx
addq $0x8, %rcx
jmp 0x9fe8
movq 0x12dab(%rip), %rax     # 0x1cdb0
movq 0x28(%rsp), %rbx
addl 0x4(%rax,%rbx), %ebp
movq %r13, %rdi
movl %ebp, %esi
movl %r15d, %edx
callq 0x5d3e
movq 0x12d8e(%rip), %rax     # 0x1cdb0
movq 0x50(%rsp), %r14
jmp 0x9e87
movq 0x12d7d(%rip), %rax     # 0x1cdb0
movl %r14d, 0x20(%rax,%rbx)
movl %ebp, 0x8(%rax,%rbx)
movl 0x8(%r12), %eax
movq 0x48(%rsp), %rbx
xorl %r14d, %r14d
pushq $0x14
popq %rsi
jmp 0x9d14
movl %ebx, 0x12d61(%rip)     # 0x1cdb8
pushq $0x1
popq %rbp
jmp 0xa09c
movq 0x7f95(%rip), %rax      # 0x11ff8
movq (%rax), %rdi
leaq 0x3355(%rip), %rsi      # 0xd3c2
jmp 0xa080
movq 0x7f82(%rip), %rax      # 0x11ff8
movq (%rax), %rdi
leaq 0x3382(%rip), %rsi      # 0xd402
movq 0x8(%rsp), %rdx
xorl %eax, %eax
callq 0x10d0
jmp 0xa09a
leaq 0x2ed5(%rip), %rdi      # 0xcf6a
callq 0x1160
xorl %ebp, %ebp
movl %ebp, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x7f44(%rip), %rax      # 0x11ff8
movq (%rax), %rdi
leaq 0x3298(%rip), %rsi      # 0xd356
jmp 0xa080
movq 0x7f31(%rip), %rax      # 0x11ff8
movq (%rax), %rdi
leaq 0x325d(%rip), %rsi      # 0xd32e
movl %ebx, %edx
jmp 0xa085
movq 0x7f1c(%rip), %rax      # 0x11ff8
movq (%rax), %rdi
leaq 0x32af(%rip), %rsi      # 0xd395
jmp 0xa080