// File: /workspace/github_c_makefile_star2_O0/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:
pushq %rbp
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x10(%rbp), %rdi
xorl %esi, %esi
movl $0x20, %edx
callq 0x19140
movq -0x8(%rbp), %rax
cmpb $0x0, (%rax)
je 0x15de84
movq -0x8(%rbp), %rax
cmpq $0x0, 0x8(%rax)
jg 0x15de89
jmp 0x15e125
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
movl $0x989680, %ecx         # imm = 0x989680
cqto
idivq %rcx
movq %rax, -0x20(%rbp)
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
movl $0x989680, %ecx         # imm = 0x989680
cqto
idivq %rcx
movl %edx, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x1c(%rax)
movq -0x20(%rbp), %rax
movabsq $0x2f0605980, %rcx      # imm = 0x2F0605980
cqto
idivq %rcx
movq %rax, -0x18(%rbp)
movq -0x20(%rbp), %rax
movabsq $0x2f0605980, %rcx      # imm = 0x2F0605980
cqto
idivq %rcx
movq %rdx, -0x28(%rbp)
movq -0x28(%rbp), %rax
movl $0x15180, %ecx          # imm = 0x15180
cqto
idivq %rcx
movq %rax, -0x30(%rbp)
movq -0x28(%rbp), %rax
movl $0x15180, %ecx          # imm = 0x15180
cqto
idivq %rcx
movq %rdx, -0x38(%rbp)
movq -0x38(%rbp), %rax
movl $0xe10, %ecx            # imm = 0xE10
cqto
idivq %rcx
movl %eax, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x10(%rax)
movq -0x38(%rbp), %rax
movl $0xe10, %ecx            # imm = 0xE10
cqto
idivq %rcx
movq %rdx, %rax
movl $0x3c, %ecx
cqto
idivq %rcx
movl %eax, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x14(%rax)
movq -0x38(%rbp), %rax
movl $0x3c, %ecx
cqto
idivq %rcx
movl %edx, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x18(%rax)
movq -0x30(%rbp), %rax
movq %rax, -0x48(%rbp)
movq $0x0, -0x40(%rbp)
movq -0x48(%rbp), %rax
movl $0x8eac, %ecx           # imm = 0x8EAC
cqto
idivq %rcx
movq %rax, -0x50(%rbp)
cmpq $0x3, -0x50(%rbp)
jle 0x15df8f
movq $0x3, -0x50(%rbp)
imulq $0x8eac, -0x50(%rbp), %rcx # imm = 0x8EAC
movq -0x48(%rbp), %rax
subq %rcx, %rax
movq %rax, -0x48(%rbp)
imulq $0x64, -0x50(%rbp), %rax
addq -0x40(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x48(%rbp), %rax
movl $0x5b5, %ecx            # imm = 0x5B5
cqto
idivq %rcx
movq %rax, -0x50(%rbp)
cmpq $0x18, -0x50(%rbp)
jle 0x15dfd0
movq $0x18, -0x50(%rbp)
imulq $0x5b5, -0x50(%rbp), %rcx # imm = 0x5B5
movq -0x48(%rbp), %rax
subq %rcx, %rax
movq %rax, -0x48(%rbp)
movq -0x50(%rbp), %rax
shlq $0x2, %rax
addq -0x40(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x48(%rbp), %rax
movl $0x16d, %ecx            # imm = 0x16D
cqto
idivq %rcx
movq %rax, -0x50(%rbp)
cmpq $0x3, -0x50(%rbp)
jle 0x15e014
movq $0x3, -0x50(%rbp)
imulq $0x16d, -0x50(%rbp), %rcx # imm = 0x16D
movq -0x48(%rbp), %rax
subq %rcx, %rax
movq %rax, -0x48(%rbp)
movq -0x50(%rbp), %rax
addq -0x40(%rbp), %rax
movq %rax, -0x40(%rbp)
imulq $0x190, -0x18(%rbp), %rax # imm = 0x190
addq $0x641, %rax            # imm = 0x641
addq -0x40(%rbp), %rax
movl %eax, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x4(%rax)
movq -0x40(%rbp), %rax
movl $0x4, %ecx
cqto
idivq %rcx
xorl %eax, %eax
cmpq $0x3, %rdx
movb %al, -0x61(%rbp)
jne 0x15e090
xorl %eax, %eax
cmpq $0x63, -0x40(%rbp)
movb %al, -0x61(%rbp)
je 0x15e090
xorl %eax, %eax
cmpq $0xc7, -0x40(%rbp)
movb %al, -0x61(%rbp)
je 0x15e090
cmpq $0x12b, -0x40(%rbp)     # imm = 0x12B
setne %al
movb %al, -0x61(%rbp)
movb -0x61(%rbp), %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x54(%rbp)
movl $0x0, -0x58(%rbp)
cmpl $0xb, -0x58(%rbp)
jge 0x15e10a
movslq -0x58(%rbp), %rcx
leaq 0x73d25(%rip), %rax     # 0x1d1dd8
movzbl (%rax,%rcx), %eax
movq %rax, -0x60(%rbp)
cmpl $0x1, -0x58(%rbp)
jne 0x15e0d3
cmpl $0x0, -0x54(%rbp)
je 0x15e0d3
movq -0x60(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x60(%rbp)
movq -0x48(%rbp), %rax
cmpq -0x60(%rbp), %rax
jl 0x15e0fb
movq -0x60(%rbp), %rcx
movq -0x48(%rbp), %rax
subq %rcx, %rax
movq %rax, -0x48(%rbp)
movq -0x10(%rbp), %rax
movl 0x8(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x8(%rax)
jmp 0x15e0fd
jmp 0x15e10a
jmp 0x15e0ff
movl -0x58(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x58(%rbp)
jmp 0x15e0a2
movq -0x48(%rbp), %rax
addq $0x1, %rax
movl %eax, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0xc(%rax)
movq -0x10(%rbp), %rax
movl $0x1, (%rax)
addq $0x70, %rsp
popq %rbp
retq
nopl (%rax,%rax)