// File: /workspace/github_c_makefile_star2_O3/jsummers[P]deark/src/deark-util.c
// Function: de_gmtime
void de_gmtime(const struct de_timestamp *ts, struct de_struct_tm *tm2)
{
	// Let's define an "eon" to be a 400-year period. Eons begin at the start
	// of the year 1601, 2001, 2401, etc.
	static const i64 secs_per_eon = 12622780800LL;
	i64 eon;
	i64 secs_since_start_of_1601;
	i64 secs_since_start_of_eon;
	i64 days_since_start_of_eon;
	i64 secs_since_start_of_day;
	i64 yr_tmp; // years, since start of eon, accounted for so far
	i64 days_tmp; // number of days not accounted for in yr_tmp
	i64 count;
	int is_leapyear;
	int k;

	de_zeromem(tm2, sizeof(struct de_struct_tm));
	if(!ts->is_valid || ts->ts_FILETIME<=0) {
		return;
	}

	secs_since_start_of_1601 = ts->ts_FILETIME / 10000000;
	tm2->tm_subsec = ts->ts_FILETIME % 10000000;
	eon = secs_since_start_of_1601 / secs_per_eon;
	secs_since_start_of_eon = secs_since_start_of_1601 % secs_per_eon;
	days_since_start_of_eon = secs_since_start_of_eon / 86400;
	secs_since_start_of_day = secs_since_start_of_eon % 86400;
	tm2->tm_hour = (int)(secs_since_start_of_day / 3600);
	tm2->tm_min = (int)((secs_since_start_of_day % 3600)/60);
	tm2->tm_sec = (int)(secs_since_start_of_day % 60);

	days_tmp = days_since_start_of_eon;
	yr_tmp = 0;

	// The first 3 100-year periods in this eon have
	// 100*365 + 24 days each.
	count = days_tmp / (100*365 + 24);
	if(count>3) count = 3;
	days_tmp -= (100*365 + 24)*count;
	yr_tmp += 100*count;

	// The first 24 4-year periods in this 100-year period have
	// 1 leap day each.
	count = days_tmp / (4*365 + 1);
	if(count>24) count = 24;
	days_tmp -= (4*365 + 1)*count;
	yr_tmp += 4*count;

	// The first 3 years in this 4-year period are not leap years.
	count = days_tmp / 365;
	if(count>3) count = 3;
	days_tmp -= 365*count;
	yr_tmp += count;

	tm2->tm_fullyear = (int)(1601 + eon*400 + yr_tmp);
	is_leapyear = ((yr_tmp%4)==3 &&
		yr_tmp!=99 && yr_tmp!=199 && yr_tmp!=299);

	for(k=0; k<11; k++) {
		static const u8 days_in_month[11] = // (Don't need December)
			{ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30 };
		i64 days_in_this_month = (i64)days_in_month[k];
		if(k==1 && is_leapyear) days_in_this_month++;
		if(days_tmp >= days_in_this_month) {
			days_tmp -= days_in_this_month;
			tm2->tm_mon++;
		}
		else {
			break;
		}
	}

	tm2->tm_mday = (int)(1+days_tmp);
	tm2->is_valid = 1;
}

// Assembly:
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rsi)
movups %xmm0, (%rsi)
cmpb $0x0, (%rdi)
je 0xeaf45
movq 0x8(%rdi), %rcx
testq %rcx, %rcx
jle 0xeaf45
movabsq $-0x29406b2a1a85bd43, %rdx # imm = 0xD6BF94D5E57A42BD
movq %rcx, %rax
mulq %rdx
movq %rdx, %rdi
shrq $0x17, %rdi
imull $0x989680, %edi, %eax   # imm = 0x989680
movl %ecx, %edx
subl %eax, %edx
movl %edx, 0x1c(%rsi)
movabsq $0x4911bac58035d52f, %rdx # imm = 0x4911BAC58035D52F
movq %rcx, %rax
mulq %rdx
movq %rdx, %rcx
movabsq $0xae35ef4645, %rdx     # imm = 0xAE35EF4645
movq %rdi, %rax
mulq %rdx
shrq $0x37, %rcx
shrl $0x9, %edx
movabsq $0x2f0605980, %rax      # imm = 0x2F0605980
imulq %rdx, %rax
subq %rax, %rdi
movabsq $0xc22e4506728a, %rdx   # imm = 0xC22E4506728A
movq %rdi, %rax
mulq %rdx
movq %rdx, %r8
imulq $0x15180, %rdx, %rax    # imm = 0x15180
movq %rdi, %rdx
subq %rax, %rdx
imulq $0x123457, %rdx, %rax   # imm = 0x123457
shrq $0x20, %rax
movl %eax, 0x10(%rsi)
imull $0xe10, %eax, %eax      # imm = 0xE10
movl %edx, %r9d
subl %eax, %r9d
imull $0x889, %r9d, %eax      # imm = 0x889
shrl $0x11, %eax
movl %eax, 0x14(%rsi)
imulq $0x4444445, %rdx, %rax  # imm = 0x4444445
shrq $0x20, %rax
imull $0x3c, %eax, %eax
subl %eax, %edx
movl %edx, 0x18(%rsi)
movabsq $0x2b8d8f5b1, %rdx      # imm = 0x2B8D8F5B1
movq %rdi, %rax
mulq %rdx
shrl %edx
cmpq $0x3, %rdx
movl $0x3, %eax
cmovbq %rdx, %rax
imulq $-0x8eac, %rax, %rdi    # imm = 0xFFFF7154
addq %r8, %rdi
imulq $0x64, %rax, %rax
imulq $0x166db073, %rdi, %rdx # imm = 0x166DB073
movq %rdx, %r8
shrq $0x3f, %r8
shrq $0x20, %rdx
sarl $0x7, %edx
addl %r8d, %edx
cmpl $0x18, %edx
movl $0x18, %r8d
cmovll %edx, %r8d
movslq %r8d, %r8
imulq $-0x5b5, %r8, %rdx      # imm = 0xFA4B
addq %rdi, %rdx
leaq (%rax,%r8,4), %rdi
movslq %edx, %rax
imulq $-0x4c73064f, %rax, %r8 # imm = 0xB38CF9B1
shrq $0x20, %r8
addl %r8d, %eax
movl %eax, %r8d
shrl $0x1f, %r8d
sarl $0x8, %eax
addl %r8d, %eax
cmpl $0x3, %eax
movl $0x3, %r8d
cmovll %eax, %r8d
movslq %r8d, %r8
imulq $-0x16d, %r8, %rax      # imm = 0xFE93
addq %r8, %rdi
imull $0x190, %ecx, %ecx      # imm = 0x190
addl %edi, %ecx
addl $0x641, %ecx            # imm = 0x641
movl %ecx, 0x4(%rsi)
movabsq $-0x7ffffffffffffffd, %rcx # imm = 0x8000000000000003
andq %rdi, %rcx
cmpq $0x3, %rcx
jne 0xeaefc
xorl %ecx, %ecx
cmpq $0x63, %rdi
je 0xeaefe
cmpq $0xc7, %rdi
je 0xeaefe
cmpq $0x12b, %rdi            # imm = 0x12B
je 0xeaefe
movb $0x1, %cl
jmp 0xeaefe
xorl %ecx, %ecx
addq %rdx, %rax
xorl %edx, %edx
leaq 0x5b3e6(%rip), %rdi     # 0x1462f0
movq %rax, %r8
movzbl (%rdx,%rdi), %r9d
cmpq $0x1, %rdx
sete %r10b
andb %cl, %r10b
movzbl %r10b, %r10d
addq %r9, %r10
subq %r10, %rax
jl 0xeaf38
incq %rdx
movl %edx, 0x8(%rsi)
movq %rax, %r8
cmpq $0xb, %rdx
jne 0xeaf0a
incl %r8d
movl %r8d, 0xc(%rsi)
movl $0x1, (%rsi)
retq