// File: /workspace/github_c_makefile_star2_O1/mikecat[P]x86_interpreter/dmem_libc_time.c
// Function: dmem_libc_localtime
int dmem_libc_localtime(uint32_t* ret, uint32_t esp) {
	uint32_t time_addr;
	uint32_t time_val;
	int ok;
	uint64_t time_left;
	uint32_t wday, year, yday, month, mday;
	if (!dmem_get_args(esp, 1, &time_addr)) return 0;

	time_val = dmem_read_uint(&ok, time_addr, 4);
	if (!ok) {
		*ret = 0;
		return 1;
	}

	/* UTC → JST */
	time_left = time_val + UINT64_C(9) * 60 * 60;
	/* 曜日を求める (1970年1月1日は木曜日) */
	wday = (4 + time_left / (UINT32_C(24) * 60 * 60)) % 7;
	/* 線形探索で年を求める */
	year = 1970;
	for (;;) {
		uint32_t year_days = 365;
		uint32_t year_seconds;
		if (year % 4 == 0 && (year % 400 == 0 || year % 100 != 0)) {
			year_days++; /* うるう年 */
		}
		year_seconds = year_days * 24 * 60 * 60;
		if (time_left >= year_seconds) {
			time_left -= year_seconds;
			year++;
		} else {
			break;
		}
	}
	yday = time_left / (UINT32_C(24) * 60 * 60);
	/* 線形探索で月を求める */
	month = 0;
	for (;;) {
		uint32_t month_days, month_seconds;
		switch (month) {
		case 1: /* 2月 */
			if (year % 4 == 0 && (year % 400 == 0 || year % 100 != 0)) {
				month_days = 29; /* うるう年 */
			} else {
				month_days = 28;
			}
			break;
		case 3: case 5: case 8: case 10: /* 4,6,9,11月 */
			month_days = 30;
			break;
		default:
			month_days = 31;
			break;
		}
		month_seconds = month_days * 24 * 60 * 60;
		if (time_left >= month_seconds) {
			time_left -= month_seconds;
			month++;
		} else {
			break;
		}
	}
	mday = time_left / (UINT32_C(24) * 60 * 60) + 1;

	dmem_write_uint(LOCALTIME_TM + 0, time_left % 60, 4); /* tm_sec */
	dmem_write_uint(LOCALTIME_TM + 4, (time_left / 60) % 60, 4); /* tm_min */
	dmem_write_uint(LOCALTIME_TM + 8, (time_left / (60 * 60)) % 24, 4); /* tm_hour */
	dmem_write_uint(LOCALTIME_TM + 12, mday, 4); /* tm_mday */
	dmem_write_uint(LOCALTIME_TM + 16, month, 4); /* tm_mon */
	dmem_write_uint(LOCALTIME_TM + 20, year - 1900, 4); /* tm_year */
	dmem_write_uint(LOCALTIME_TM + 24, wday, 4); /* tm_wday */
	dmem_write_uint(LOCALTIME_TM + 28, yday, 4); /* tm_yday */
	dmem_write_uint(LOCALTIME_TM + 32, 0, 4); /* tm_isdst */

	*ret = LOCALTIME_TM;
	return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rdi, %r14
xorl %ebx, %ebx
leaq 0xc(%rsp), %rdx
movl %esi, %edi
movl $0x1, %esi
xorl %eax, %eax
callq 0x735a
testl %eax, %eax
je 0x9f9b
movl 0xc(%rsp), %esi
leaq 0x34(%rsp), %rbx
movq %rbx, %rdi
movl $0x4, %edx
callq 0x7250
cmpl $0x0, (%rbx)
je 0x9f91
movq %r14, 0x28(%rsp)
movl %eax, %r12d
addq $0x7e90, %r12           # imm = 0x7E90
movabsq $0xc22e4506728a, %rcx   # imm = 0xC22E4506728A
movq %r12, %rax
mulq %rcx
movq %rdx, %rcx
leaq 0x4(%rdx), %rax
movabsq $0x2492492492492493, %rdx # imm = 0x2492492492492493
mulq %rdx
leal (,%rdx,8), %eax
subq %rax, %rdx
leaq (%rcx,%rdx), %rax
addq $0x4, %rax
movq %rax, 0x20(%rsp)
movl $0x7b1, %eax            # imm = 0x7B1
movl $0x7b2, %ecx            # imm = 0x7B2
movl $0x7b1, %edx            # imm = 0x7B1
movq %r12, %r15
leal 0x1(%rdx), %r9d
movl $0x1e13380, %r14d       # imm = 0x1E13380
testb $0x3, %r9b
jne 0x9d67
movl %ecx, %esi
imulq $0x51eb851f, %rsi, %rsi # imm = 0x51EB851F
movq %rsi, %rdi
shrq $0x25, %rdi
imull $0x64, %edi, %edi
movl $0x7b2, %r8d            # imm = 0x7B2
subl %edi, %r8d
addl %edx, %r8d
movl $0x1e13380, %edi        # imm = 0x1E13380
movl $0x1e28500, %r14d       # imm = 0x1E28500
cmpl %eax, %r8d
je 0x9d4b
movl $0x1e28500, %edi        # imm = 0x1E28500
shrq $0x27, %rsi
imull $0x190, %esi, %esi      # imm = 0x190
movl $0x7b2, %r8d            # imm = 0x7B2
subl %esi, %r8d
addl %r8d, %edx
cmpl %eax, %edx
cmovneq %rdi, %r14
movq %r14, %rdx
cmpq %r14, %r15
jae 0x9d71
xorl %edx, %edx
movq %r15, %r12
subq %rdx, %r12
incl %ecx
movl %r9d, %edx
cmpq %r14, %r15
jae 0x9d04
movabsq $-0x3dd1baf98d76b549, %rcx # imm = 0xC22E450672894AB7
movq %r12, %rax
mulq %rcx
shrq $0x10, %rdx
movq %rdx, 0x18(%rsp)
cmpq %r14, %r15
movl %r9d, 0x8(%rsp)
movl %r9d, %edx
sbbl $-0x1, %edx
imulq $0x51eb851f, %rdx, %rax # imm = 0x51EB851F
movq %rax, %rsi
shrq $0x27, %rsi
imull $0xfffffe70, %esi, %esi # imm = 0xFFFFFE70
shrq $0x25, %rax
imull $-0x64, %eax, %eax
addl %edx, %eax
movl $0x24ea00, %edi         # imm = 0x24EA00
movl $0x263b80, %r8d         # imm = 0x263B80
movl $0x263b80, %eax         # imm = 0x263B80
cmoveq %rdi, %rax
addl %edx, %esi
cmoveq %r8, %rax
testb $0x3, %dl
cmovneq %rdi, %rax
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
movl $0x528, %edx            # imm = 0x528
movq %r12, %rbx
incl %r13d
cmpl $0xa, %r13d
ja 0x9e2a
movl $0x278d00, %ebp         # imm = 0x278D00
btl %r13d, %edx
jae 0x9e21
cmpq %rbp, %rbx
movl $0x0, %esi
cmovaeq %rbp, %rsi
movq %rbx, %r12
subq %rsi, %r12
cmpq %rbp, %rbx
jae 0x9df1
jmp 0x9e31
movq %rax, %rbp
cmpl $0x1, %r13d
je 0x9e08
movl $0x28de80, %ebp         # imm = 0x28DE80
jmp 0x9e08
movq %r12, %rax
mulq %rcx
shrq $0x10, %rdx
leal 0x1(%rdx), %eax
movl %eax, 0x4(%rsp)
movl 0xbf48(%rip), %edi      # 0x15d90
movabsq $-0x7777777777777777, %rcx # imm = 0x8888888888888889
movq %r12, %rax
mulq %rcx
shrq $0x5, %rdx
movq %rdx, 0x10(%rsp)
imull $0x3c, %edx, %eax
movl %r12d, %esi
subl %eax, %esi
movl $0x4, %edx
callq 0x71e8
movl 0xbf17(%rip), %edi      # 0x15d90
addl $0x4, %edi
movabsq $0x888888888888889, %rcx # imm = 0x888888888888889
movq 0x10(%rsp), %rsi
movq %rsi, %rax
mulq %rcx
shrl %edx
imull $0x3c, %edx, %eax
subl %eax, %esi
movl $0x4, %edx
callq 0x71e8
movl 0xbee8(%rip), %edi      # 0x15d90
addl $0x8, %edi
shrq $0x4, %r12
movabsq $0x91a2b3c4d5e6f81, %rcx # imm = 0x91A2B3C4D5E6F81
movq %r12, %rax
mulq %rcx
movq %rdx, %rsi
shrq $0x3, %rsi
movabsq $0xaaaaaaaaaaaaaab, %rcx # imm = 0xAAAAAAAAAAAAAAB
movq %rsi, %rax
mulq %rcx
shll $0x3, %edx
leal (%rdx,%rdx,2), %eax
subl %eax, %esi
movl $0x4, %edx
callq 0x71e8
movl 0xbea2(%rip), %edi      # 0x15d90
addl $0xc, %edi
movl 0x4(%rsp), %esi
movl $0x4, %edx
callq 0x71e8
movl 0xbe8b(%rip), %edi      # 0x15d90
addl $0x10, %edi
cmpq %rbp, %rbx
sbbl $-0x1, %r13d
movl %r13d, %esi
movl $0x4, %edx
callq 0x71e8
movl 0xbe6e(%rip), %edi      # 0x15d90
addl $0x14, %edi
cmpq %r14, %r15
movl 0x8(%rsp), %esi
sbbl $-0x1, %esi
addl $0xfffff894, %esi       # imm = 0xFFFFF894
movl $0x4, %edx
callq 0x71e8
movl 0xbe4b(%rip), %edi      # 0x15d90
addl $0x18, %edi
movq 0x20(%rsp), %rsi
movl $0x4, %edx
callq 0x71e8
movl 0xbe33(%rip), %edi      # 0x15d90
addl $0x1c, %edi
movq 0x18(%rsp), %rsi
movl $0x4, %edx
callq 0x71e8
movl 0xbe1b(%rip), %edi      # 0x15d90
addl $0x20, %edi
xorl %esi, %esi
movl $0x4, %edx
callq 0x71e8
movl 0xbe06(%rip), %eax      # 0x15d90
movq 0x28(%rsp), %r14
jmp 0x9f93
xorl %eax, %eax
movl %eax, (%r14)
movl $0x1, %ebx
movl %ebx, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq