// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movl -0x14(%rbp), %edi
movl $0x1, %esi
leaq -0x18(%rbp), %rdx
movb $0x0, %al
callq 0x8280
cmpl $0x0, %eax
jne 0xbad3
movl $0x0, -0x4(%rbp)
jmp 0xbdd0
movl -0x18(%rbp), %esi
leaq -0x20(%rbp), %rdi
movl $0x4, %edx
callq 0x8110
movl %eax, -0x1c(%rbp)
cmpl $0x0, -0x20(%rbp)
jne 0xbb03
movq -0x10(%rbp), %rax
movl $0x0, (%rax)
movl $0x1, -0x4(%rbp)
jmp 0xbdd0
movl -0x1c(%rbp), %eax
addq $0x7e90, %rax           # imm = 0x7E90
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
movl $0x15180, %ecx          # imm = 0x15180
xorl %edx, %edx
divq %rcx
addq $0x4, %rax
movl $0x7, %ecx
xorl %edx, %edx
divq %rcx
movl %edx, %eax
movl %eax, -0x2c(%rbp)
movl $0x7b2, -0x30(%rbp)     # imm = 0x7B2
movl $0x16d, -0x40(%rbp)     # imm = 0x16D
movl -0x30(%rbp), %eax
andl $0x3, %eax
cmpl $0x0, %eax
jne 0xbb75
movl -0x30(%rbp), %eax
movl $0x190, %ecx            # imm = 0x190
xorl %edx, %edx
divl %ecx
cmpl $0x0, %edx
je 0xbb6c
movl -0x30(%rbp), %eax
movl $0x64, %ecx
xorl %edx, %edx
divl %ecx
cmpl $0x0, %edx
je 0xbb75
movl -0x40(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x40(%rbp)
imull $0x18, -0x40(%rbp), %eax
imull $0x3c, %eax, %eax
imull $0x3c, %eax, %eax
movl %eax, -0x44(%rbp)
movq -0x28(%rbp), %rax
movl -0x44(%rbp), %ecx
cmpq %rcx, %rax
jb 0xbba9
movl -0x44(%rbp), %eax
movl %eax, %ecx
movq -0x28(%rbp), %rax
subq %rcx, %rax
movq %rax, -0x28(%rbp)
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0xbbab
jmp 0xbbad
jmp 0xbb38
movq -0x28(%rbp), %rax
movl $0x15180, %ecx          # imm = 0x15180
xorl %edx, %edx
divq %rcx
movl %eax, -0x34(%rbp)
movl $0x0, -0x38(%rbp)
movl -0x38(%rbp), %eax
movl %eax, -0x50(%rbp)
subl $0x1, %eax
je 0xbbfa
jmp 0xbbd2
movl -0x50(%rbp), %eax
subl $0x3, %eax
je 0xbc39
jmp 0xbbdc
movl -0x50(%rbp), %eax
subl $0x5, %eax
je 0xbc39
jmp 0xbbe6
movl -0x50(%rbp), %eax
subl $0x8, %eax
je 0xbc39
jmp 0xbbf0
movl -0x50(%rbp), %eax
subl $0xa, %eax
je 0xbc39
jmp 0xbc42
movl -0x30(%rbp), %eax
andl $0x3, %eax
cmpl $0x0, %eax
jne 0xbc30
movl -0x30(%rbp), %eax
movl $0x190, %ecx            # imm = 0x190
xorl %edx, %edx
divl %ecx
cmpl $0x0, %edx
je 0xbc27
movl -0x30(%rbp), %eax
movl $0x64, %ecx
xorl %edx, %edx
divl %ecx
cmpl $0x0, %edx
je 0xbc30
movl $0x1d, -0x48(%rbp)
jmp 0xbc37
movl $0x1c, -0x48(%rbp)
jmp 0xbc49
movl $0x1e, -0x48(%rbp)
jmp 0xbc49
movl $0x1f, -0x48(%rbp)
imull $0x18, -0x48(%rbp), %eax
imull $0x3c, %eax, %eax
imull $0x3c, %eax, %eax
movl %eax, -0x4c(%rbp)
movq -0x28(%rbp), %rax
movl -0x4c(%rbp), %ecx
cmpq %rcx, %rax
jb 0xbc7d
movl -0x4c(%rbp), %eax
movl %eax, %ecx
movq -0x28(%rbp), %rax
subq %rcx, %rax
movq %rax, -0x28(%rbp)
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0xbc7f
jmp 0xbc84
jmp 0xbbc5
movq -0x28(%rbp), %rax
movl $0x15180, %ecx          # imm = 0x15180
xorl %edx, %edx
divq %rcx
addq $0x1, %rax
movl %eax, -0x3c(%rbp)
movl 0xc1b1(%rip), %edi      # 0x17e50
addl $0x0, %edi
addl $0x0, %edi
movq -0x28(%rbp), %rax
movl $0x3c, %ecx
xorl %edx, %edx
divq %rcx
movl %edx, %esi
movl $0x4, %edx
callq 0x8070
movl 0xc18b(%rip), %edi      # 0x17e50
addl $0x0, %edi
addl $0x4, %edi
movq -0x28(%rbp), %rax
movl $0x3c, %ecx
xorl %edx, %edx
divq %rcx
movl $0x3c, %ecx
xorl %edx, %edx
divq %rcx
movl %edx, %esi
movl $0x4, %edx
callq 0x8070
movl 0xc15b(%rip), %edi      # 0x17e50
addl $0x0, %edi
addl $0x8, %edi
movq -0x28(%rbp), %rax
movl $0xe10, %ecx            # imm = 0xE10
xorl %edx, %edx
divq %rcx
movl $0x18, %ecx
xorl %edx, %edx
divq %rcx
movl %edx, %esi
movl $0x4, %edx
callq 0x8070
movl 0xc12b(%rip), %edi      # 0x17e50
addl $0x0, %edi
addl $0xc, %edi
movl -0x3c(%rbp), %esi
movl $0x4, %edx
callq 0x8070
movl 0xc112(%rip), %edi      # 0x17e50
addl $0x0, %edi
addl $0x10, %edi
movl -0x38(%rbp), %esi
movl $0x4, %edx
callq 0x8070
movl 0xc0f9(%rip), %edi      # 0x17e50
addl $0x0, %edi
addl $0x14, %edi
movl -0x30(%rbp), %esi
subl $0x76c, %esi            # imm = 0x76C
movl $0x4, %edx
callq 0x8070
movl 0xc0da(%rip), %edi      # 0x17e50
addl $0x0, %edi
addl $0x18, %edi
movl -0x2c(%rbp), %esi
movl $0x4, %edx
callq 0x8070
movl 0xc0c1(%rip), %edi      # 0x17e50
addl $0x0, %edi
addl $0x1c, %edi
movl -0x34(%rbp), %esi
movl $0x4, %edx
callq 0x8070
movl 0xc0a8(%rip), %edi      # 0x17e50
addl $0x0, %edi
addl $0x20, %edi
xorl %esi, %esi
movl $0x4, %edx
callq 0x8070
movl 0xc090(%rip), %ecx      # 0x17e50
addl $0x0, %ecx
movq -0x10(%rbp), %rax
movl %ecx, (%rax)
movl $0x1, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x50, %rsp
popq %rbp
retq
nopl (%rax)