// File: /workspace/github_c_makefile_star2_O3/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 $0x68, %rsp
movq %rdi, %r13
movl 0x10(%rdi), %r14d
xorl %ebx, %ebx
subl $0x1, %r14d
cmovbl %ebx, %r14d
addl 0xc(%rdi), %r14d
movl %esi, %edi
movl %edx, %esi
movl %ecx, %edx
callq 0xb3ac
testl %eax, %eax
je 0xb2c9
movq 0x12fb3(%rip), %rdi     # 0x1ddb0
callq 0x1030
movq $0x0, 0x12fa3(%rip) # 0x1ddb0
movl $0x0, 0x12fa1(%rip)     # 0x1ddb8
movl $0x1, %ebx
cmpl $0x14, 0x8(%r13)
jb 0xb2c9
leaq 0x40(%rsp), %r15
leaq 0x3c(%rsp), %rbp
leal -0x3(%r14), %eax
movl %eax, 0x14(%rsp)
xorl %ebx, %ebx
xorl %r12d, %r12d
movq %r14, 0x50(%rsp)
movq %r13, 0x28(%rsp)
addl 0x4(%r13), %ebx
movl %ebx, %edi
movl $0x14, %esi
callq 0x6ec9
testl %eax, %eax
je 0xb299
leaq 0x30(%rsp), %rdi
movl %ebx, %esi
movl $0x14, %edx
callq 0x6a34
pxor %xmm0, %xmm0
xorl %eax, %eax
pxor %xmm2, %xmm2
pcmpeqd %xmm3, %xmm3
movd 0x30(%rsp,%rax), %xmm1
punpcklbw %xmm1, %xmm1    # xmm1 = xmm1[0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7]
punpcklwd %xmm1, %xmm1    # xmm1 = xmm1[0,0,1,1,2,2,3,3]
pcmpeqb %xmm2, %xmm1
pxor %xmm3, %xmm1
por %xmm1, %xmm0
addq $0x4, %rax
cmpq $0x14, %rax
jne 0xae7f
pslld $0x1f, %xmm0
movmskps %xmm0, %eax
testl %eax, %eax
je 0xb23d
movq 0x12ef6(%rip), %rdi     # 0x1ddb0
leal 0x1(%r12), %eax
movq %rax, 0x58(%rsp)
leaq (,%rax,8), %rax
leaq (%rax,%rax,4), %rsi
callq 0x1130
testq %rax, %rax
je 0xb278
movq %rax, 0x12ecb(%rip)     # 0x1ddb0
xorl %ecx, %ecx
leaq 0x30(%rsp), %rsi
xorl %edx, %edx
movzbl (%rsi), %edi
shll %cl, %edi
orl %edi, %edx
addq $0x8, %rcx
incq %rsi
cmpq $0x20, %rcx
jne 0xaeee
movl %r12d, %esi
leaq (%rsi,%rsi,4), %rcx
leaq (%rax,%rcx,8), %r8
movq %rcx, 0x8(%rsp)
movl %edx, (%rax,%rcx,8)
xorl %ecx, %ecx
movq %r15, %rdi
xorl %r9d, %r9d
movzbl (%rdi), %r10d
shll %cl, %r10d
orl %r10d, %r9d
addq $0x8, %rcx
incq %rdi
cmpq $0x20, %rcx
jne 0xaf1d
movq 0x8(%rsp), %rcx
leaq (%rax,%rcx,8), %r10
addq $0x4, %r10
movl %r9d, 0x4(%rax,%rcx,8)
xorl %ecx, %ecx
movq %rbp, %r11
xorl %edi, %edi
movzbl (%r11), %ebx
shll %cl, %ebx
orl %ebx, %edi
addq $0x8, %rcx
incq %r11
cmpq $0x20, %rcx
jne 0xaf4d
movl (%r13), %ecx
movl %ecx, %r11d
notl %r11d
cmpl %r11d, %edx
ja 0xb286
cmpl %r11d, %r9d
ja 0xb286
cmpl %r11d, %edi
ja 0xb286
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 0x70d9
movq 0x12df3(%rip), %rcx     # 0x1ddb0
movq %rbx, 0x60(%rsp)
movq %rax, 0x10(%rcx,%rbx)
testq %rax, %rax
je 0xb2ac
movq %rax, %rdi
callq 0xb55a
movl %eax, 0x18(%rsp)
movq 0x12dcd(%rip), %rax     # 0x1ddb0
movq 0x8(%rsp), %rsi
cmpl $0x0, 0xc(%rax,%rsi,8)
jne 0xaff8
leaq (%rax,%rsi,8), %rcx
movl 0x4(%rcx), %edx
movl %edx, (%rcx)
movq $0x0, 0x18(%rax,%rsi,8)
movl $0x0, 0x20(%rax,%rsi,8)
cmpl $0x3, %r14d
jb 0xb21a
xorl %r15d, %r15d
movl 0x14(%rsp), %ecx
cmpl %ecx, 0x4(%rax,%rsi,8)
leaq 0x1c(%rsp), %rbx
jbe 0xb02c
xorl %ebp, %ebp
jmp 0xb1fd
movq %r12, 0x20(%rsp)
xorl %ebp, %ebp
xorl %r12d, %r12d
movq 0x8(%rsp), %r14
movl (%rax,%r14,8), %edi
addl %ebp, %edi
jb 0xb24d
movl $0x4, %esi
callq 0x6ec9
testl %eax, %eax
je 0xb24d
movq 0x12d50(%rip), %rax     # 0x1ddb0
movl (%rax,%r14,8), %esi
addl %ebp, %esi
movq %rbx, %rdi
movl $0x4, %edx
callq 0x6a34
xorl %ecx, %ecx
movq %rbx, %rax
xorl %ebx, %ebx
movzbl (%rax), %edx
shll %cl, %edx
orl %edx, %ebx
addq $0x8, %rcx
incq %rax
cmpq $0x20, %rcx
jne 0xb07a
testl %ebx, %ebx
je 0xb1f3
movq 0x12d13(%rip), %r13     # 0x1ddb0
movq 0x18(%r13,%r14,8), %rdi
leal 0x1(%r12), %r15d
leaq (,%r15,8), %rax
leaq (%rax,%rax,2), %rsi
callq 0x1130
testq %rax, %rax
je 0xb278
leaq 0x18(,%r14,8), %rcx
addq %r13, %rcx
movq %rax, (%rcx)
movl %r12d, %ecx
leaq (%rcx,%rcx,2), %rcx
leaq (%rax,%rcx,8), %r12
testl %ebx, %ebx
js 0xb15d
movq 0x28(%rsp), %r13
movl (%r13), %edi
movl $0xfffffffd, %eax       # imm = 0xFFFFFFFD
subl %ebx, %eax
cmpl %edi, %eax
jb 0xb260
addl %ebx, %edi
movl $0x2, %esi
callq 0x6ec9
testl %eax, %eax
je 0xb260
movl (%r13), %eax
leal (%rbx,%rax), %edi
addl $0x2, %edi
callq 0x70d9
movq %rax, 0x8(%r12)
testq %rax, %rax
je 0xb260
addl (%r13), %ebx
leaq 0x6(%rsp), %rdi
movl %ebx, %esi
movl $0x2, %edx
callq 0x6a34
movl $0x0, (%r12)
movzwl 0x6(%rsp), %eax
movw %ax, 0x10(%r12)
movq 0x8(%r12), %rsi
movq 0x12c55(%rip), %r13     # 0x1ddb0
jmp 0xb176
movl $0x1, (%r12)
movq $0x0, 0x8(%r12)
movw %bx, 0x10(%r12)
xorl %esi, %esi
movq 0x8(%rsp), %rax
movl 0x4(%r13,%rax,8), %edx
addl %ebp, %edx
movl 0x18(%rsp), %edi
callq 0xb5ce
leaq 0x1c(%rsp), %rbx
movq %rbx, %rdx
xorl %ecx, %ecx
movl %eax, %esi
shrl %cl, %esi
movb %sil, (%rdx)
addq $0x8, %rcx
incq %rdx
cmpq $0x20, %rcx
jne 0xb195
movq 0x12c00(%rip), %rax     # 0x1ddb0
movq 0x60(%rsp), %r14
addl 0x4(%rax,%r14), %ebp
movq %rbx, %rdi
movl %ebp, %esi
movl $0x4, %edx
callq 0x6b71
movq 0x12be0(%rip), %rax     # 0x1ddb0
leal (,%r15,4), %ebp
movl 0x4(%rax,%r14), %ecx
addl %ebp, %ecx
movl %r15d, %r12d
cmpl 0x14(%rsp), %ecx
movq 0x28(%rsp), %r13
jbe 0xb036
jmp 0xb1fd
movq 0x12bb6(%rip), %rax     # 0x1ddb0
movl %r12d, %r15d
movq 0x8(%rsp), %rcx
movl %r15d, 0x20(%rax,%rcx,8)
movl %ebp, 0x8(%rax,%rcx,8)
movq 0x50(%rsp), %r14
leaq 0x40(%rsp), %r15
leaq 0x3c(%rsp), %rbp
movq 0x58(%rsp), %rcx
leal (,%rcx,4), %eax
leal (%rax,%rax,4), %ebx
movl 0x8(%r13), %eax
addl $-0x14, %eax
movl %ecx, %r12d
cmpl %eax, %ebx
jbe 0xae48
jmp 0xb240
movl %r12d, %ecx
movl %ecx, 0x12b72(%rip)     # 0x1ddb8
movl $0x1, %ebx
jmp 0xb2c9
movq 0x7da4(%rip), %rax      # 0x12ff8
movq (%rax), %rdi
leaq 0x31d4(%rip), %rsi      # 0xe432
jmp 0xb271
movq 0x7d91(%rip), %rax      # 0x12ff8
movq (%rax), %rdi
leaq 0x3201(%rip), %rsi      # 0xe472
movq 0x20(%rsp), %rdx
jmp 0xb2c0
leaq 0x2d5b(%rip), %rdi      # 0xdfda
callq 0x1160
jmp 0xb2c7
movq 0x7d6b(%rip), %rax      # 0x12ff8
movq (%rax), %rdi
leaq 0x312f(%rip), %rsi      # 0xe3c6
jmp 0xb2bd
movq 0x7d58(%rip), %rax      # 0x12ff8
movq (%rax), %rdi
leaq 0x30f4(%rip), %rsi      # 0xe39e
jmp 0xb2bd
movq 0x7d45(%rip), %rax      # 0x12ff8
movq (%rax), %rdi
leaq 0x3148(%rip), %rsi      # 0xe405
movl %r12d, %edx
xorl %eax, %eax
callq 0x10d0
xorl %ebx, %ebx
movl %ebx, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq