// File: /workspace/github_c_makefile_star2_O2/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 $0x28, %rsp
movq %rdi, %r14
xorl %ebx, %ebx
pushq $0x1
popq %rbp
leaq 0x14(%rsp), %rdx
movl %esi, %edi
movl %ebp, %esi
xorl %eax, %eax
callq 0x626c
testl %eax, %eax
je 0x85aa
movl 0x14(%rsp), %esi
leaq 0x24(%rsp), %rbx
pushq $0x4
popq %rdx
movq %rbx, %rdi
callq 0x616c
cmpl $0x0, (%rbx)
je 0x85a3
movq %r14, 0x18(%rsp)
movl %eax, %r9d
addq $0x7e90, %r9            # imm = 0x7E90
movl $0x15180, %ecx          # imm = 0x15180
movq %r9, %rax
xorl %edx, %edx
divq %rcx
addl $0x4, %eax
movw $0x7, %cx
xorl %edx, %edx
divw %cx
movl %edx, %ecx
movl $0x7b2, %r11d           # imm = 0x7B2
movl $0x7b1, %r8d            # imm = 0x7B1
pushq $0x64
popq %r10
movl $0x190, %ebx            # imm = 0x190
movl $0x7b2, %r14d           # imm = 0x7B2
movq %r9, %rsi
movl %r14d, %edi
movl %r14d, %eax
xorl %edx, %edx
divl %r10d
movl %eax, %r9d
movl %r14d, %eax
xorl %edx, %edx
divl %ebx
movl $0x1e13380, %edx        # imm = 0x1E13380
testb $0x3, %dil
jne 0x83f8
imull $0x64, %r9d, %edx
movl $0x7b2, %r9d            # imm = 0x7B2
subl %edx, %r9d
addl %edi, %r9d
movl $0x1e13380, %edx        # imm = 0x1E13380
cmpl %r11d, %r9d
je 0x83dc
movl $0x1e28500, %edx        # imm = 0x1E28500
imull $0x190, %eax, %eax      # imm = 0x190
movl $0x7b2, %r9d            # imm = 0x7B2
subl %eax, %r9d
addl %edi, %r9d
cmpl %r11d, %r9d
jne 0x83f8
movl $0x1e28500, %edx        # imm = 0x1E28500
leal 0x1(%rdi), %r14d
incl %r8d
movq %rsi, %r9
subq %rdx, %r9
jae 0x839a
movl $0x190, %r9d            # imm = 0x190
movl %r8d, %eax
xorl %edx, %edx
divl %r9d
imull $0x190, %eax, %eax      # imm = 0x190
movl %r14d, %r9d
subl %eax, %r9d
movl %r8d, %eax
xorl %edx, %edx
divl %r10d
imull $0x64, %eax, %eax
movl %r14d, %edx
subl %eax, %edx
cmpl $0x1, %edx
movl $0x24ea00, %edx         # imm = 0x24EA00
movl $0x263b80, %eax         # imm = 0x263B80
cmoveq %rdx, %rax
movl $0x263b80, %r8d         # imm = 0x263B80
cmpl $0x1, %r9d
cmoveq %r8, %rax
testb $0x3, %dil
cmovneq %rdx, %rax
pushq $-0x1
popq %r15
movl $0x528, %edx            # imm = 0x528
movq %rsi, %r8
movq %r8, %rbx
incl %r15d
cmpl $0xa, %r15d
ja 0x848e
movl $0x278d00, %edi         # imm = 0x278D00
btl %r15d, %edx
jae 0x8485
movq %rbx, %r8
subq %rdi, %r8
jae 0x8464
jmp 0x8495
movq %rax, %rdi
cmpl $0x1, %r15d
je 0x847b
movl $0x28de80, %edi         # imm = 0x28DE80
jmp 0x847b
movzwl %cx, %eax
movl %eax, 0xc(%rsp)
movl $0x15180, %ecx          # imm = 0x15180
movl %esi, %eax
xorl %edx, %edx
divl %ecx
movl %eax, 0x10(%rsp)
movl %ebx, %eax
xorl %edx, %edx
divl %ecx
leal 0x1(%rax), %r12d
movl 0xc8d5(%rip), %edi      # 0x14d90
pushq $0x3c
popq %rcx
movl %ebx, %eax
xorl %edx, %edx
divl %ecx
movl %eax, %r13d
pushq $0x4
popq %rbp
movl %edx, %esi
movl %ebp, %edx
callq 0x6110
movl 0xc8b7(%rip), %edi      # 0x14d90
addl $0x4, %edi
movw $0x3c, %cx
movl %r13d, %eax
xorl %edx, %edx
divw %cx
movzwl %dx, %esi
movl %ebp, %edx
callq 0x6110
movl 0xc898(%rip), %edi      # 0x14d90
addl $0x8, %edi
movl $0xe10, %ecx            # imm = 0xE10
movl %ebx, %eax
xorl %edx, %edx
divl %ecx
movw $0x18, %cx
xorl %edx, %edx
divw %cx
movzwl %dx, %esi
movl %ebp, %edx
callq 0x6110
movl 0xc871(%rip), %edi      # 0x14d90
addl $0xc, %edi
movl %r12d, %esi
movl %ebp, %edx
callq 0x6110
movl 0xc85e(%rip), %edi      # 0x14d90
addl $0x10, %edi
movl %r15d, %esi
movl %ebp, %edx
callq 0x6110
movl 0xc84b(%rip), %edi      # 0x14d90
addl $0x14, %edi
addl $0xfffff893, %r14d      # imm = 0xFFFFF893
movl %r14d, %esi
movl %ebp, %edx
callq 0x6110
movl 0xc831(%rip), %edi      # 0x14d90
addl $0x18, %edi
movl 0xc(%rsp), %esi
movl %ebp, %edx
callq 0x6110
movl 0xc81d(%rip), %edi      # 0x14d90
addl $0x1c, %edi
movl 0x10(%rsp), %esi
movl %ebp, %edx
callq 0x6110
movl 0xc809(%rip), %edi      # 0x14d90
addl $0x20, %edi
xorl %esi, %esi
movl %ebp, %edx
callq 0x6110
movl 0xc7f7(%rip), %eax      # 0x14d90
movq 0x18(%rsp), %r14
pushq $0x1
popq %rbp
jmp 0x85a5
xorl %eax, %eax
movl %eax, (%r14)
movl %ebp, %ebx
movl %ebx, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq