// File: /workspace/github_c_makefile_star2_O0/mikecat[P]x86_interpreter/pe_libs.c
// Function: exec_kernel32
static uint32_t exec_kernel32(uint32_t regs[], const char* func_name) {
	if (strcmp(func_name, "SetUnhandledExceptionFilter") == 0) {
		/* 無視 */
		regs[EAX] = 0;
		return 4;
	} else if (strcmp(func_name, "GetModuleHandleA") == 0) {
		regs[EAX] = 0;
		return 0;
	} else if (strcmp(func_name, "ExitProcess") == 0) {
		return PE_LIB_EXEC_EXIT;
	} else if (strcmp(func_name, "SetErrorMode") == 0) {
		/* 無視 */
		regs[EAX] = 0;
		return 4;
	} else if (strcmp(func_name, "GetFileAttributesA") == 0) {
		regs[EAX] = -1;
		return 4;
	} else if (strcmp(func_name, "GetLastError") == 0) {
		regs[EAX] = 0;
		return 0;
	} else if (strcmp(func_name, "GetSystemTimeAsFileTime") == 0) {
		uint32_t ptr;
		if (dmem_get_args(regs[ESP], 1, &ptr) && dmemory_is_allocated(ptr, 8)) {
			time_t time_raw;
			struct tm *time_data;
			int year, uruu_num;
			uint64_t result;
			time_raw = time(NULL);
			time_data = gmtime(&time_raw);
			year = time_data->tm_year + 1900;
			/* 1601年からyear年の前年までの閏年の数を計算する */
			/* 388は1年から1600年における閏年の数 */
			uruu_num = ((year - 1) / 4) - ((year - 1) / 100) + ((year - 1) / 400) - 388;
			/* 1601年1月1日からyear年1月1日の日数を計算する */
			result = 365 * (year - 1601) + uruu_num;
			/* year年1月1日から今日までの日数を足す */
			result += time_data->tm_yday;
			/* それを秒数に変換する */
			result *= UINT64_C(60) * 60 * 24;
			/* 今日の0時から現在時刻までの秒数を足す */
			result += 60 * ((UINT64_C(60) * time_data->tm_hour) + time_data->tm_min) + time_data->tm_sec;
			/* 秒数を「100ナノ秒」数に変換する */
			result *= UINT64_C(10000000);
			/* 結果を書き込む */
			dmem_write_uint(ptr, (uint32_t)result, 4);
			dmem_write_uint(ptr + 4, (uint32_t)(result >> 32), 4);
		}
		return 4;
	} else if (strcmp(func_name, "GetCurrentProcessId") == 0) {
		regs[EAX] = 1;
		return 0;
	} else if (strcmp(func_name, "GetCurrentThreadId") == 0) {
		regs[EAX] = 1;
		return 0;
	} else if (strcmp(func_name, "GetTickCount") == 0) {
		regs[EAX] = 0;
		return 0;
	} else if (strcmp(func_name, "QueryPerformanceCounter") == 0) {
		uint32_t outptr;
		if (!dmem_get_args(regs[ESP], 1, &outptr) || !dmemory_is_allocated(outptr, 8)) {
			regs[EAX] = 0; /* 失敗 */
		} else {
			dmem_write_uint(outptr, 0, 4);
			dmem_write_uint(outptr + 4, 0, 4);
			regs[EAX] = 1;
		}
		return 4;
	} else {
		fprintf(stderr, "unimplemented function %s() in kernel32.dll called.\n", func_name);
		return PE_LIB_EXEC_FAILED;
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq -0x18(%rbp), %rdi
leaq 0x2e96(%rip), %rsi      # 0x12cb1
callq 0x10c0
cmpl $0x0, %eax
jne 0xfe3b
movq -0x10(%rbp), %rax
movl $0x0, (%rax)
movl $0x4, -0x4(%rbp)
jmp 0x10192
movq -0x18(%rbp), %rdi
leaq 0x2e87(%rip), %rsi      # 0x12ccd
callq 0x10c0
cmpl $0x0, %eax
jne 0xfe66
movq -0x10(%rbp), %rax
movl $0x0, (%rax)
movl $0x0, -0x4(%rbp)
jmp 0x10192
movq -0x18(%rbp), %rdi
leaq 0x2e6d(%rip), %rsi      # 0x12cde
callq 0x10c0
cmpl $0x0, %eax
jne 0xfe87
movl $0xfffffffe, -0x4(%rbp) # imm = 0xFFFFFFFE
jmp 0x10192
movq -0x18(%rbp), %rdi
leaq 0x2e58(%rip), %rsi      # 0x12cea
callq 0x10c0
cmpl $0x0, %eax
jne 0xfeb2
movq -0x10(%rbp), %rax
movl $0x0, (%rax)
movl $0x4, -0x4(%rbp)
jmp 0x10192
movq -0x18(%rbp), %rdi
leaq 0x2e3a(%rip), %rsi      # 0x12cf7
callq 0x10c0
cmpl $0x0, %eax
jne 0xfedd
movq -0x10(%rbp), %rax
movl $0xffffffff, (%rax)     # imm = 0xFFFFFFFF
movl $0x4, -0x4(%rbp)
jmp 0x10192
movq -0x18(%rbp), %rdi
leaq 0x2e22(%rip), %rsi      # 0x12d0a
callq 0x10c0
cmpl $0x0, %eax
jne 0xff08
movq -0x10(%rbp), %rax
movl $0x0, (%rax)
movl $0x0, -0x4(%rbp)
jmp 0x10192
movq -0x18(%rbp), %rdi
leaq 0x2e04(%rip), %rsi      # 0x12d17
callq 0x10c0
cmpl $0x0, %eax
jne 0x1006b
movq -0x10(%rbp), %rax
movl 0x10(%rax), %edi
movl $0x1, %esi
leaq -0x1c(%rbp), %rdx
movb $0x0, %al
callq 0x8280
cmpl $0x0, %eax
je 0x1005f
movl -0x1c(%rbp), %edi
movl $0x8, %esi
callq 0x7f60
cmpl $0x0, %eax
je 0x1005f
xorl %eax, %eax
movl %eax, %edi
callq 0x10f0
movq %rax, -0x28(%rbp)
leaq -0x28(%rbp), %rdi
callq 0x1180
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
movl 0x14(%rax), %eax
addl $0x76c, %eax            # imm = 0x76C
movl %eax, -0x34(%rbp)
movl -0x34(%rbp), %eax
subl $0x1, %eax
movl $0x4, %ecx
cltd
idivl %ecx
movl %eax, -0x4c(%rbp)
movl -0x34(%rbp), %eax
subl $0x1, %eax
movl $0x64, %ecx
cltd
idivl %ecx
movl %eax, %ecx
movl -0x4c(%rbp), %eax
subl %ecx, %eax
movl %eax, -0x48(%rbp)
movl -0x34(%rbp), %eax
subl $0x1, %eax
movl $0x190, %ecx            # imm = 0x190
cltd
idivl %ecx
movl %eax, %ecx
movl -0x48(%rbp), %eax
addl %ecx, %eax
subl $0x184, %eax            # imm = 0x184
movl %eax, -0x38(%rbp)
movl -0x34(%rbp), %eax
subl $0x641, %eax            # imm = 0x641
imull $0x16d, %eax, %eax      # imm = 0x16D
addl -0x38(%rbp), %eax
cltq
movq %rax, -0x40(%rbp)
movq -0x30(%rbp), %rax
movslq 0x1c(%rax), %rax
addq -0x40(%rbp), %rax
movq %rax, -0x40(%rbp)
imulq $0x15180, -0x40(%rbp), %rax # imm = 0x15180
movq %rax, -0x40(%rbp)
movq -0x30(%rbp), %rax
movslq 0x8(%rax), %rax
imulq $0x3c, %rax, %rax
movq -0x30(%rbp), %rcx
movslq 0x4(%rcx), %rcx
addq %rcx, %rax
imulq $0x3c, %rax, %rax
movq -0x30(%rbp), %rcx
movslq (%rcx), %rcx
addq %rcx, %rax
addq -0x40(%rbp), %rax
movq %rax, -0x40(%rbp)
imulq $0x989680, -0x40(%rbp), %rax # imm = 0x989680
movq %rax, -0x40(%rbp)
movl -0x1c(%rbp), %edi
movq -0x40(%rbp), %rax
movl %eax, %esi
movl $0x4, %edx
callq 0x8070
movl -0x1c(%rbp), %edi
addl $0x4, %edi
movq -0x40(%rbp), %rax
shrq $0x20, %rax
movl %eax, %esi
movl $0x4, %edx
callq 0x8070
movl $0x4, -0x4(%rbp)
jmp 0x10192
movq -0x18(%rbp), %rdi
leaq 0x2cb9(%rip), %rsi      # 0x12d2f
callq 0x10c0
cmpl $0x0, %eax
jne 0x10096
movq -0x10(%rbp), %rax
movl $0x1, (%rax)
movl $0x0, -0x4(%rbp)
jmp 0x10192
movq -0x18(%rbp), %rdi
leaq 0x2ca2(%rip), %rsi      # 0x12d43
callq 0x10c0
cmpl $0x0, %eax
jne 0x100c1
movq -0x10(%rbp), %rax
movl $0x1, (%rax)
movl $0x0, -0x4(%rbp)
jmp 0x10192
movq -0x18(%rbp), %rdi
leaq 0x2c8a(%rip), %rsi      # 0x12d56
callq 0x10c0
cmpl $0x0, %eax
jne 0x100ec
movq -0x10(%rbp), %rax
movl $0x0, (%rax)
movl $0x0, -0x4(%rbp)
jmp 0x10192
movq -0x18(%rbp), %rdi
leaq 0x2c6c(%rip), %rsi      # 0x12d63
callq 0x10c0
cmpl $0x0, %eax
jne 0x1016f
movq -0x10(%rbp), %rax
movl 0x10(%rax), %edi
movl $0x1, %esi
leaq -0x44(%rbp), %rdx
movb $0x0, %al
callq 0x8280
cmpl $0x0, %eax
je 0x1012f
movl -0x44(%rbp), %edi
movl $0x8, %esi
callq 0x7f60
cmpl $0x0, %eax
jne 0x1013b
movq -0x10(%rbp), %rax
movl $0x0, (%rax)
jmp 0x10166
movl -0x44(%rbp), %edi
xorl %esi, %esi
movl $0x4, %edx
callq 0x8070
movl -0x44(%rbp), %edi
addl $0x4, %edi
xorl %esi, %esi
movl $0x4, %edx
callq 0x8070
movq -0x10(%rbp), %rax
movl $0x1, (%rax)
movl $0x4, -0x4(%rbp)
jmp 0x10192
movq 0x4e7a(%rip), %rax      # 0x14ff0
movq (%rax), %rdi
movq -0x18(%rbp), %rdx
leaq 0x2bf7(%rip), %rsi      # 0x12d7b
movb $0x0, %al
callq 0x10d0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl -0x4(%rbp), %eax
addq $0x50, %rsp
popq %rbp
retq
nopl (%rax,%rax)