// File: /workspace/github_c_makefile_star2_O3/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 $0x18, %rsp
movq %rdi, %r14
xorl %ebx, %ebx
leaq 0x4(%rsp), %rdx
movl %esi, %edi
movl $0x1, %esi
xorl %eax, %eax
callq 0x7157
testl %eax, %eax
je 0x9704
movl 0x4(%rsp), %esi
leaq 0x14(%rsp), %rbx
movq %rbx, %rdi
movl $0x4, %edx
callq 0x704d
cmpl $0x0, (%rbx)
je 0x96fa
movl %eax, %ecx
addq $0x7e90, %rcx           # imm = 0x7E90
movabsq $0xc22e4506728a, %rdx   # imm = 0xC22E4506728A
movq %rcx, %rax
mulq %rdx
leal 0x4(%rdx), %eax
movl $0xffff, %esi           # imm = 0xFFFF
movl %eax, %edi
andl %esi, %edi
imull $0x2493, %edi, %edi     # imm = 0x2493
shrl $0x10, %edi
subl %edi, %eax
andl %esi, %eax
shrl %eax
addl %edi, %eax
shrl $0x2, %eax
leal (,%rax,8), %esi
subl %esi, %eax
addl %edx, %eax
addl $0x4, %eax
movl $0x7b2, %r8d            # imm = 0x7B2
movl $0x7b1, %edi            # imm = 0x7B1
movl $0x7b2, %ebp            # imm = 0x7B2
movq %rcx, %rdx
movl %ebp, %esi
movl $0x1e13380, %r9d        # imm = 0x1E13380
testb $0x3, %sil
jne 0x9513
movl %esi, %ecx
imulq $0x51eb851f, %rcx, %rcx # imm = 0x51EB851F
movq %rcx, %r9
shrq $0x25, %r9
imull $0x64, %r9d, %r9d
movl $0x7b2, %r11d           # imm = 0x7B2
subl %r9d, %r11d
addl %esi, %r11d
movl $0x1e13380, %r10d       # imm = 0x1E13380
movl $0x1e28500, %r9d        # imm = 0x1E28500
cmpl %r8d, %r11d
je 0x94f6
movl $0x1e28500, %r10d       # imm = 0x1E28500
shrq $0x27, %rcx
imull $0x190, %ecx, %ecx      # imm = 0x190
movl $0x7b2, %r11d           # imm = 0x7B2
subl %ecx, %r11d
addl %esi, %r11d
cmpl %r8d, %r11d
cmovneq %r10, %r9
leal 0x1(%rsi), %ebp
incl %edi
movq %rdx, %rcx
subq %r9, %rcx
jae 0x94ae
movq %r14, 0x8(%rsp)
movl %edi, %ecx
imulq $0x51eb851f, %rcx, %rcx # imm = 0x51EB851F
movq %rcx, %rdi
shrq $0x27, %rdi
imull $0x190, %edi, %edi      # imm = 0x190
movl %ebp, %r8d
subl %edi, %r8d
shrq $0x25, %rcx
imull $0x64, %ecx, %ecx
movl %ebp, %edi
subl %ecx, %edi
cmpl $0x1, %edi
movl $0x24ea00, %edi         # imm = 0x24EA00
movl $0x263b80, %ecx         # imm = 0x263B80
cmoveq %rdi, %rcx
movl $0x263b80, %r9d         # imm = 0x263B80
cmpl $0x1, %r8d
cmoveq %r9, %rcx
testb $0x3, %sil
cmovneq %rdi, %rcx
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
movl $0x528, %edi            # imm = 0x528
movq %rdx, %r9
movq %r9, %rsi
incl %r14d
cmpl $0xa, %r14d
ja 0x95ac
movl $0x278d00, %r8d         # imm = 0x278D00
btl %r14d, %edi
jae 0x95a3
movq %rsi, %r9
subq %r8, %r9
jae 0x9581
jmp 0x95b4
movq %rcx, %r8
cmpl $0x1, %r14d
je 0x9599
movl $0x28de80, %r8d         # imm = 0x28DE80
jmp 0x9599
movzwl %ax, %eax
movl %eax, (%rsp)
movl %edx, %r15d
movl $0xc22e4507, %r13d      # imm = 0xC22E4507
imulq %r13, %r15
shrq $0x30, %r15
movl %esi, %r12d
imulq %r12, %r13
shrq $0x30, %r13
incl %r13d
movl 0xc7b1(%rip), %edi      # 0x15d90
movl $0x88888889, %ebx       # imm = 0x88888889
imulq %r12, %rbx
shrq $0x25, %rbx
imull $0x3c, %ebx, %eax
subl %eax, %esi
movl $0x4, %edx
callq 0x6f54
movl 0xc78f(%rip), %edi      # 0x15d90
addl $0x4, %edi
movl $0xffff, %ecx           # imm = 0xFFFF
movl %ebx, %eax
andl %ecx, %eax
imull $0x8889, %eax, %eax     # imm = 0x8889
shrl $0x15, %eax
imull $0x3c, %eax, %eax
subl %eax, %ebx
andl %ecx, %ebx
movl %ebx, %esi
movl $0x4, %edx
callq 0x6f54
movl 0xc761(%rip), %edi      # 0x15d90
addl $0x8, %edi
movl $0x91a2b3c5, %esi       # imm = 0x91A2B3C5
imulq %r12, %rsi
shrq $0x2b, %rsi
movl %esi, %eax
movl $0xffff, %ecx           # imm = 0xFFFF
andl %ecx, %eax
imull $0xaaab, %eax, %eax     # imm = 0xAAAB
shrl $0x11, %eax
andl $-0x8, %eax
leal (%rax,%rax,2), %eax
subl %eax, %esi
andl %ecx, %esi
movl $0x4, %edx
callq 0x6f54
movl 0xc725(%rip), %edi      # 0x15d90
addl $0xc, %edi
movl %r13d, %esi
movl $0x4, %edx
callq 0x6f54
movl 0xc70f(%rip), %edi      # 0x15d90
addl $0x10, %edi
movl %r14d, %esi
movl $0x4, %edx
callq 0x6f54
movl 0xc6f9(%rip), %edi      # 0x15d90
addl $0x14, %edi
addl $0xfffff893, %ebp       # imm = 0xFFFFF893
movl %ebp, %esi
movl $0x4, %edx
callq 0x6f54
movl 0xc6de(%rip), %edi      # 0x15d90
addl $0x18, %edi
movl (%rsp), %esi
movl $0x4, %edx
callq 0x6f54
movl 0xc6c8(%rip), %edi      # 0x15d90
addl $0x1c, %edi
movl %r15d, %esi
movl $0x4, %edx
callq 0x6f54
movl 0xc6b2(%rip), %edi      # 0x15d90
addl $0x20, %edi
xorl %esi, %esi
movl $0x4, %edx
callq 0x6f54
movl 0xc69d(%rip), %eax      # 0x15d90
movq 0x8(%rsp), %r14
jmp 0x96fc
xorl %eax, %eax
movl %eax, (%r14)
movl $0x1, %ebx
movl %ebx, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq