// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x90, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movl %edx, -0x18(%rbp)
movl %ecx, -0x1c(%rbp)
movq -0x10(%rbp), %rax
movl 0xc(%rax), %eax
movl %eax, -0x80(%rbp)
movq -0x10(%rbp), %rax
cmpl $0x0, 0x10(%rax)
jbe 0xe23e
movq -0x10(%rbp), %rax
movl 0x10(%rax), %eax
subl $0x1, %eax
movl %eax, -0x84(%rbp)
jmp 0xe248
xorl %eax, %eax
movl %eax, -0x84(%rbp)
jmp 0xe248
movl -0x80(%rbp), %eax
movl -0x84(%rbp), %ecx
addl %ecx, %eax
movl %eax, -0x20(%rbp)
movl -0x14(%rbp), %edi
movl -0x18(%rbp), %esi
movl -0x1c(%rbp), %edx
callq 0xebd0
cmpl $0x0, %eax
jne 0xe275
movl $0x0, -0x4(%rbp)
jmp 0xe923
movq 0x11bf4(%rip), %rdi     # 0x1fe70
callq 0x1030
movq $0x0, 0x11be4(%rip) # 0x1fe70
movl $0x0, 0x11be2(%rip)     # 0x1fe78
movq -0x10(%rbp), %rax
cmpl $0x14, 0x8(%rax)
jb 0xe91c
movl $0x0, -0x24(%rbp)
imull $0x14, -0x24(%rbp), %eax
movq -0x10(%rbp), %rcx
movl 0x8(%rcx), %ecx
subl $0x14, %ecx
cmpl %ecx, %eax
ja 0xe913
movq -0x10(%rbp), %rax
movl 0x4(%rax), %eax
imull $0x14, -0x24(%rbp), %ecx
addl %ecx, %eax
movl %eax, -0x28(%rbp)
movl $0x1, -0x4c(%rbp)
movl -0x28(%rbp), %edi
movl $0x14, %esi
callq 0x7f60
cmpl $0x0, %eax
jne 0xe311
movq 0x6cff(%rip), %rax      # 0x14ff0
movq (%rax), %rdi
movl -0x24(%rbp), %edx
leaq 0x4170(%rip), %rsi      # 0x1246e
movb $0x0, %al
callq 0x10d0
movl $0x0, -0x4(%rbp)
jmp 0xe923
leaq -0x40(%rbp), %rdi
movl -0x28(%rbp), %esi
movl $0x14, %edx
callq 0x7890
movl $0x0, -0x48(%rbp)
cmpl $0x14, -0x48(%rbp)
jae 0xe350
movl -0x48(%rbp), %eax
movzbl -0x40(%rbp,%rax), %eax
cmpl $0x0, %eax
je 0xe343
movl $0x0, -0x4c(%rbp)
jmp 0xe345
movl -0x48(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x48(%rbp)
jmp 0xe329
cmpl $0x0, -0x4c(%rbp)
je 0xe35b
jmp 0xe913
movq 0x11b0e(%rip), %rdi     # 0x1fe70
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, %eax
imulq $0x28, %rax, %rsi
callq 0x1140
movq %rax, -0x58(%rbp)
cmpq $0x0, -0x58(%rbp)
jne 0xe396
leaq 0x3d11(%rip), %rdi      # 0x12096
callq 0x1170
movl $0x0, -0x4(%rbp)
jmp 0xe923
movq -0x58(%rbp), %rax
movq %rax, 0x11acf(%rip)     # 0x1fe70
leaq -0x40(%rbp), %rdi
movl $0x4, %esi
callq 0xe930
movl %eax, %ecx
movq 0x11ab8(%rip), %rax     # 0x1fe70
movl -0x24(%rbp), %edx
imulq $0x28, %rdx, %rdx
addq %rdx, %rax
movl %ecx, (%rax)
leaq -0x40(%rbp), %rdi
addq $0x10, %rdi
movl $0x4, %esi
callq 0xe930
movl %eax, %ecx
movq 0x11a91(%rip), %rax     # 0x1fe70
movl -0x24(%rbp), %edx
imulq $0x28, %rdx, %rdx
addq %rdx, %rax
movl %ecx, 0x4(%rax)
leaq -0x40(%rbp), %rdi
addq $0xc, %rdi
movl $0x4, %esi
callq 0xe930
movl %eax, -0x44(%rbp)
movq -0x10(%rbp), %rcx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
subl (%rcx), %eax
movq 0x11a5d(%rip), %rcx     # 0x1fe70
movl -0x24(%rbp), %edx
imulq $0x28, %rdx, %rdx
addq %rdx, %rcx
cmpl (%rcx), %eax
jb 0xe452
movq -0x10(%rbp), %rcx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
subl (%rcx), %eax
movq 0x11a3d(%rip), %rcx     # 0x1fe70
movl -0x24(%rbp), %edx
imulq $0x28, %rdx, %rdx
addq %rdx, %rcx
cmpl 0x4(%rcx), %eax
jb 0xe452
movq -0x10(%rbp), %rcx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
subl (%rcx), %eax
cmpl -0x44(%rbp), %eax
jae 0xe479
movq 0x6b97(%rip), %rax      # 0x14ff0
movq (%rax), %rdi
movl -0x24(%rbp), %edx
leaq 0x4030(%rip), %rsi      # 0x12496
movb $0x0, %al
callq 0x10d0
movl $0x0, -0x4(%rbp)
jmp 0xe923
movq 0x119f0(%rip), %rax     # 0x1fe70
movl -0x24(%rbp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
cmpl $0x0, (%rax)
setne %al
andb $0x1, %al
movzbl %al, %ecx
movq 0x119d4(%rip), %rax     # 0x1fe70
movl -0x24(%rbp), %edx
imulq $0x28, %rdx, %rdx
addq %rdx, %rax
movl %ecx, 0xc(%rax)
movq -0x10(%rbp), %rax
movl (%rax), %ecx
movq 0x119ba(%rip), %rax     # 0x1fe70
movl -0x24(%rbp), %edx
imulq $0x28, %rdx, %rdx
addq %rdx, %rax
addl (%rax), %ecx
movl %ecx, (%rax)
movq -0x10(%rbp), %rax
movl (%rax), %ecx
movq 0x1199f(%rip), %rax     # 0x1fe70
movl -0x24(%rbp), %edx
imulq $0x28, %rdx, %rdx
addq %rdx, %rax
addl 0x4(%rax), %ecx
movl %ecx, 0x4(%rax)
movq 0x11988(%rip), %rax     # 0x1fe70
movl -0x24(%rbp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movl $0x0, 0x8(%rax)
movq -0x10(%rbp), %rax
movl (%rax), %eax
addl -0x44(%rbp), %eax
movl %eax, -0x44(%rbp)
movl -0x44(%rbp), %edi
callq 0x81d0
movq %rax, %rcx
movq 0x11959(%rip), %rax     # 0x1fe70
movl -0x24(%rbp), %edx
imulq $0x28, %rdx, %rdx
addq %rdx, %rax
movq %rcx, 0x10(%rax)
movq 0x11944(%rip), %rax     # 0x1fe70
movl -0x24(%rbp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
cmpq $0x0, 0x10(%rax)
jne 0xe564
movq 0x6aac(%rip), %rax      # 0x14ff0
movq (%rax), %rdi
movl -0x24(%rbp), %edx
leaq 0x3f84(%rip), %rsi      # 0x124d5
movb $0x0, %al
callq 0x10d0
movl $0x0, -0x4(%rbp)
jmp 0xe923
movq 0x11905(%rip), %rax     # 0x1fe70
movl -0x24(%rbp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq 0x10(%rax), %rdi
callq 0xee10
movl %eax, -0x5c(%rbp)
movq 0x118e8(%rip), %rax     # 0x1fe70
movl -0x24(%rbp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
cmpl $0x0, 0xc(%rax)
jne 0xe5bf
movq 0x118d1(%rip), %rax     # 0x1fe70
movl -0x24(%rbp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movl 0x4(%rax), %ecx
movq 0x118bd(%rip), %rax     # 0x1fe70
movl -0x24(%rbp), %edx
imulq $0x28, %rdx, %rdx
addq %rdx, %rax
movl %ecx, (%rax)
movq 0x118aa(%rip), %rax     # 0x1fe70
movl -0x24(%rbp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq $0x0, 0x18(%rax)
movq 0x11891(%rip), %rax     # 0x1fe70
movl -0x24(%rbp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movl $0x0, 0x20(%rax)
cmpl $0x3, -0x20(%rbp)
jb 0xe903
movl $0x0, -0x48(%rbp)
movq 0x11868(%rip), %rax     # 0x1fe70
movl -0x24(%rbp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movl 0x4(%rax), %eax
movl -0x48(%rbp), %ecx
shll $0x2, %ecx
addl %ecx, %eax
movl -0x20(%rbp), %ecx
subl $0x3, %ecx
cmpl %ecx, %eax
ja 0xe8d2
movq 0x1183e(%rip), %rcx     # 0x1fe70
movl -0x24(%rbp), %eax
imulq $0x28, %rax, %rax
addq %rax, %rcx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
subl (%rcx), %eax
movl -0x48(%rbp), %ecx
shll $0x2, %ecx
cmpl %ecx, %eax
jb 0xe677
movq 0x1181c(%rip), %rax     # 0x1fe70
movl -0x24(%rbp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movl (%rax), %edi
movl -0x48(%rbp), %eax
shll $0x2, %eax
addl %eax, %edi
movl $0x4, %esi
callq 0x7f60
cmpl $0x0, %eax
jne 0xe69e
movq 0x6972(%rip), %rax      # 0x14ff0
movq (%rax), %rdi
movl -0x24(%rbp), %edx
leaq 0x3e77(%rip), %rsi      # 0x12502
movb $0x0, %al
callq 0x10d0
movl $0x0, -0x4(%rbp)
jmp 0xe923
leaq -0x60(%rbp), %rdi
movq 0x117c7(%rip), %rax     # 0x1fe70
movl -0x24(%rbp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movl (%rax), %esi
movl -0x48(%rbp), %eax
shll $0x2, %eax
addl %eax, %esi
movl $0x4, %edx
callq 0x7890
leaq -0x60(%rbp), %rdi
movl $0x4, %esi
callq 0xe930
movl %eax, -0x64(%rbp)
cmpl $0x0, -0x64(%rbp)
jne 0xe6e3
jmp 0xe8d2
movq 0x11786(%rip), %rax     # 0x1fe70
movl -0x24(%rbp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq 0x18(%rax), %rdi
movl -0x48(%rbp), %eax
addl $0x1, %eax
movl %eax, %eax
imulq $0x18, %rax, %rsi
callq 0x1140
movq %rax, -0x70(%rbp)
cmpq $0x0, -0x70(%rbp)
jne 0xe72c
leaq 0x397b(%rip), %rdi      # 0x12096
callq 0x1170
movl $0x0, -0x4(%rbp)
jmp 0xe923
movq -0x70(%rbp), %rcx
movq 0x11739(%rip), %rax     # 0x1fe70
movl -0x24(%rbp), %edx
imulq $0x28, %rdx, %rdx
addq %rdx, %rax
movq %rcx, 0x18(%rax)
movq 0x11724(%rip), %rax     # 0x1fe70
movl -0x24(%rbp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq 0x18(%rax), %rax
movl -0x48(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x78(%rbp)
movl -0x64(%rbp), %eax
andl $0x80000000, %eax       # imm = 0x80000000
cmpl $0x0, %eax
je 0xe7a3
movq -0x78(%rbp), %rax
movl $0x1, (%rax)
movq -0x78(%rbp), %rax
movq $0x0, 0x8(%rax)
movl -0x64(%rbp), %eax
andl $0xffff, %eax           # imm = 0xFFFF
movw %ax, %cx
movq -0x78(%rbp), %rax
movw %cx, 0x10(%rax)
jmp 0xe850
movq -0x10(%rbp), %rcx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
subl (%rcx), %eax
movl -0x64(%rbp), %ecx
addl $0x2, %ecx
cmpl %ecx, %eax
jb 0xe7ef
movq -0x10(%rbp), %rax
movl (%rax), %edi
addl -0x64(%rbp), %edi
movl $0x2, %esi
callq 0x7f60
cmpl $0x0, %eax
je 0xe7ef
movq -0x10(%rbp), %rax
movl (%rax), %edi
addl -0x64(%rbp), %edi
addl $0x2, %edi
callq 0x81d0
movq -0x78(%rbp), %rcx
movq %rax, 0x8(%rcx)
cmpq $0x0, %rax
jne 0xe816
movq 0x67fa(%rip), %rax      # 0x14ff0
movq (%rax), %rdi
movl -0x24(%rbp), %edx
leaq 0x3d3f(%rip), %rsi      # 0x12542
movb $0x0, %al
callq 0x10d0
movl $0x0, -0x4(%rbp)
jmp 0xe923
leaq -0x7a(%rbp), %rdi
movq -0x10(%rbp), %rax
movl (%rax), %esi
addl -0x64(%rbp), %esi
movl $0x2, %edx
callq 0x7890
movq -0x78(%rbp), %rax
movl $0x0, (%rax)
leaq -0x7a(%rbp), %rdi
movl $0x2, %esi
callq 0xe930
movw %ax, %cx
movq -0x78(%rbp), %rax
movw %cx, 0x10(%rax)
leaq -0x60(%rbp), %rax
movq %rax, -0x90(%rbp)
movl -0x5c(%rbp), %edi
movq -0x78(%rbp), %rax
movq 0x8(%rax), %rsi
movq 0x11603(%rip), %rax     # 0x1fe70
movl -0x24(%rbp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movl 0x4(%rax), %edx
movl -0x48(%rbp), %eax
shll $0x2, %eax
addl %eax, %edx
callq 0xeee0
movq -0x90(%rbp), %rdi
movl %eax, %esi
movl $0x4, %edx
callq 0xe980
leaq -0x60(%rbp), %rdi
movq 0x115cb(%rip), %rax     # 0x1fe70
movl -0x24(%rbp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movl 0x4(%rax), %esi
movl -0x48(%rbp), %eax
shll $0x2, %eax
addl %eax, %esi
movl $0x4, %edx
callq 0x7ad0
movl -0x48(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x48(%rbp)
jmp 0xe601
movl -0x48(%rbp), %ecx
movq 0x11594(%rip), %rax     # 0x1fe70
movl -0x24(%rbp), %edx
imulq $0x28, %rdx, %rdx
addq %rdx, %rax
movl %ecx, 0x20(%rax)
movl -0x48(%rbp), %ecx
shll $0x2, %ecx
movq 0x1157a(%rip), %rax     # 0x1fe70
movl -0x24(%rbp), %edx
imulq $0x28, %rdx, %rdx
addq %rdx, %rax
movl %ecx, 0x8(%rax)
jmp 0xe905
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0xe2ab
movl -0x24(%rbp), %eax
movl %eax, 0x1155c(%rip)     # 0x1fe78
movl $0x1, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x90, %rsp
popq %rbp
retq
nop