// File: /workspace/github_c_makefile_star2_O1/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 0xeb4f7
movq 0x8(%rdi), %rcx
testq %rcx, %rcx
jle 0xeb4f7
movabsq $-0x29406b2a1a85bd43, %rdx # imm = 0xD6BF94D5E57A42BD
movq %rcx, %rax
mulq %rdx
movq %rdx, %rdi
shrq $0x17, %rdi
imull $0x989680, %edi, %eax   # imm = 0x989680
movl %ecx, %r8d
subl %eax, %r8d
movabsq $0x4911bac58035d52f, %rdx # imm = 0x4911BAC58035D52F
movq %rcx, %rax
mulq %rdx
movq %rdx, %rcx
movl %r8d, 0x1c(%rsi)
shrq $0x37, %rcx
movabsq $0xae35ef4645, %rdx     # imm = 0xAE35EF4645
movq %rdi, %rax
mulq %rdx
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, %r9
imulq $0x15180, %rdx, %rax    # imm = 0x15180
movq %rdi, %r8
subq %rax, %r8
movabsq $0x123456789abce0, %rdx # imm = 0x123456789ABCE0
movq %r8, %rax
mulq %rdx
movq %rdx, %r10
imulq $0xe10, %rdx, %rdx      # imm = 0xE10
movq %r8, %rax
subq %rdx, %rax
movabsq $0x444444444444445, %r11 # imm = 0x444444444444445
mulq %r11
movl %r10d, 0x10(%rsi)
movl %edx, 0x14(%rsi)
movq %r8, %rax
mulq %r11
imull $0x3c, %edx, %r10d
movabsq $0x2b8d8f5b1, %rdx      # imm = 0x2B8D8F5B1
movq %rdi, %rax
mulq %rdx
subl %r10d, %r8d
shrl %edx
movl $0x3, %r11d
cmpq %r11, %rdx
cmovaeq %r11, %rdx
imulq $-0x8eac, %rdx, %r10    # imm = 0xFFFF7154
addq %r9, %r10
imulq $0x64, %rdx, %r9
movabsq $-0x4c927c686ebd9737, %rdx # imm = 0xB36D8397914268C9
movq %r10, %rax
imulq %rdx
addq %r10, %rdx
movq %rdx, %rax
shrq $0x3f, %rax
sarq $0xa, %rdx
addq %rax, %rdx
cmpq $0x18, %rdx
movl $0x18, %eax
cmovlq %rdx, %rax
movl %r8d, 0x18(%rsi)
imulq $-0x5b5, %rax, %rdi     # imm = 0xFA4B
addq %r10, %rdi
leaq (%r9,%rax,4), %r8
movabsq $0x2ce33e6c02ce33e7, %rdx # imm = 0x2CE33E6C02CE33E7
movq %rdi, %rax
imulq %rdx
movq %rdx, %rax
shrq $0x3f, %rax
sarq $0x6, %rdx
addq %rax, %rdx
cmpq $0x3, %rdx
cmovgeq %r11, %rdx
imulq $-0x16d, %rdx, %rax     # imm = 0xFE93
addq %r8, %rdx
imull $0x190, %ecx, %ecx      # imm = 0x190
addl %edx, %ecx
addl $0x641, %ecx            # imm = 0x641
movl %ecx, 0x4(%rsi)
movabsq $-0x7ffffffffffffffd, %rcx # imm = 0x8000000000000003
andq %rdx, %rcx
cmpq $0x3, %rcx
jne 0xeb4a1
xorl %ecx, %ecx
cmpq $0x63, %rdx
je 0xeb4a3
cmpq $0xc7, %rdx
je 0xeb4a3
cmpq $0x12b, %rdx            # imm = 0x12B
je 0xeb4a3
movb $0x1, %cl
jmp 0xeb4a3
xorl %ecx, %ecx
addq %rdi, %rax
xorl %edx, %edx
leaq 0x5a8d1(%rip), %r8      # 0x145d80
movq %rax, %rdi
movzbl (%rdx,%r8), %r10d
cmpq $0x1, %rdx
sete %r9b
andb %cl, %r9b
movzbl %r9b, %r9d
addq %r10, %r9
movq %rax, %r10
subq %r9, %r10
jl 0xeb4d7
incl 0x8(%rsi)
movq %r10, %rdi
cmpq %r9, %rax
jl 0xeb4ec
leaq 0x1(%rdx), %r9
movq %rdi, %rax
cmpq $0xa, %rdx
movq %r9, %rdx
jne 0xeb4b2
incl %edi
movl %edi, 0xc(%rsi)
movl $0x1, (%rsi)
retq