// File: /workspace/github_c_makefile_star2_O0/mikecat[P]x86_interpreter/pe_import.c
// Function: pe_import
int pe_import(uint32_t* eip, uint32_t regs[]) {
	uint32_t i;
	const imported_lib_info* called_lib = NULL;
	const func_info* called_func = NULL;
	uint32_t stack_remove_size;
	for (i = 0; i < imported_lib_count; i++) {
		if (imported_libs[i].iat_addr <= *eip && *eip - imported_libs[i].iat_addr < imported_libs[i].iat_size) {
			uint32_t offset = *eip - imported_libs[i].iat_addr;
			called_lib = &imported_libs[i];
			if (offset % 4 == 0) {
				called_func = &imported_libs[i].funcs[offset / 4];
			}
			break;
		}
	}
	if (called_lib == NULL) {
		fprintf(stderr, "library not found for EIP %08"PRIx32"\n", *eip);
		return -1;
	}
	stack_remove_size = pe_lib_exec(regs, called_lib->name,
		called_func->is_ord ? NULL : called_func->name,
		called_func->is_ord ? called_func->hint_or_ord : 0);
	if (stack_remove_size == PE_LIB_EXEC_FAILED) return -1;
	if (stack_remove_size == PE_LIB_EXEC_EXIT) return 0;
	/* ret */
	dmemory_read(eip, regs[ESP], 4);
	regs[ESP] += 4 + stack_remove_size;
	return 1;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq $0x0, -0x28(%rbp)
movq $0x0, -0x30(%rbp)
movl $0x0, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
cmpl 0x11478(%rip), %eax     # 0x1fe78
jae 0xeacd
movq 0x11463(%rip), %rax     # 0x1fe70
movl -0x1c(%rbp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movl 0x4(%rax), %eax
movq -0x10(%rbp), %rcx
cmpl (%rcx), %eax
ja 0xeabd
movq -0x10(%rbp), %rax
movl (%rax), %eax
movq 0x1143d(%rip), %rcx     # 0x1fe70
movl -0x1c(%rbp), %edx
imulq $0x28, %rdx, %rdx
addq %rdx, %rcx
subl 0x4(%rcx), %eax
movq 0x11429(%rip), %rcx     # 0x1fe70
movl -0x1c(%rbp), %edx
imulq $0x28, %rdx, %rdx
addq %rdx, %rcx
cmpl 0x8(%rcx), %eax
jae 0xeabd
movq -0x10(%rbp), %rax
movl (%rax), %eax
movq 0x1140d(%rip), %rcx     # 0x1fe70
movl -0x1c(%rbp), %edx
imulq $0x28, %rdx, %rdx
addq %rdx, %rcx
subl 0x4(%rcx), %eax
movl %eax, -0x38(%rbp)
movq 0x113f6(%rip), %rax     # 0x1fe70
movl -0x1c(%rbp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x28(%rbp)
movl -0x38(%rbp), %eax
andl $0x3, %eax
cmpl $0x0, %eax
jne 0xeabb
movq 0x113d6(%rip), %rax     # 0x1fe70
movl -0x1c(%rbp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq 0x18(%rax), %rax
movl -0x38(%rbp), %ecx
shrl $0x2, %ecx
movl %ecx, %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x30(%rbp)
jmp 0xeacd
jmp 0xeabf
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0xe9f7
cmpq $0x0, -0x28(%rbp)
jne 0xeafe
movq 0x6515(%rip), %rax      # 0x14ff0
movq (%rax), %rdi
movq -0x10(%rbp), %rax
movl (%rax), %edx
leaq 0x3a93(%rip), %rsi      # 0x1257e
movb $0x0, %al
callq 0x10d0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xebb8
movq -0x18(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x40(%rbp)
movq -0x30(%rbp), %rax
cmpl $0x0, (%rax)
je 0xeb23
xorl %eax, %eax
movq %rax, -0x50(%rbp)
jmp 0xeb2f
movq -0x30(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0x30(%rbp), %rax
cmpl $0x0, (%rax)
je 0xeb4d
movq -0x30(%rbp), %rax
movzwl 0x10(%rax), %eax
movl %eax, -0x5c(%rbp)
jmp 0xeb54
xorl %eax, %eax
movl %eax, -0x5c(%rbp)
jmp 0xeb54
movq -0x58(%rbp), %rdx
movq -0x40(%rbp), %rsi
movq -0x48(%rbp), %rdi
movl -0x5c(%rbp), %eax
movzwl %ax, %ecx
callq 0xefc0
movl %eax, -0x34(%rbp)
cmpl $-0x1, -0x34(%rbp)
jne 0xeb7d
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xebb8
cmpl $-0x2, -0x34(%rbp)
jne 0xeb8c
movl $0x0, -0x4(%rbp)
jmp 0xebb8
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rax
movl 0x10(%rax), %esi
movl $0x4, %edx
callq 0x7890
movl -0x34(%rbp), %ecx
addl $0x4, %ecx
movq -0x18(%rbp), %rax
addl 0x10(%rax), %ecx
movl %ecx, 0x10(%rax)
movl $0x1, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x60, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)
nopl (%rax,%rax)