// File: /workspace/github_c_makefile_star2_O2/Paxa[P]fast_excel/libxlsxwriter/src/utility.c
// Function: lxw_datetime_to_excel_date_epoch
double
lxw_datetime_to_excel_date_epoch(lxw_datetime *datetime, uint8_t date_1904)
{
    int year = datetime->year;
    int month = datetime->month;
    int day = datetime->day;
    int hour = datetime->hour;
    int min = datetime->min;
    double sec = datetime->sec;
    double seconds;
    int epoch = date_1904 ? 1904 : 1900;
    int offset = date_1904 ? 4 : 0;
    int norm = 300;
    int range;
    /* Set month days and check for leap year. */
    int mdays[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
    int leap = 0;
    int days = 0;
    int i;

    /* For times without dates set the default date for the epoch. */
    if (!year) {
        if (!date_1904) {
            year = 1899;
            month = 12;
            day = 31;
        }
        else {
            year = 1904;
            month = 1;
            day = 1;
        }
    }

    /* Convert the Excel seconds to a fraction of the seconds in 24 hours. */
    seconds = (hour * 60 * 60 + min * 60 + sec) / (24 * 60 * 60.0);

    /* Special cases for Excel dates in the 1900 epoch. */
    if (!date_1904) {
        /* Excel 1900 epoch. */
        if (year == 1899 && month == 12 && day == 31)
            return seconds;

        /* Excel 1900 epoch. */
        if (year == 1900 && month == 1 && day == 0)
            return seconds;

        /* Excel false leapday */
        if (year == 1900 && month == 2 && day == 29)
            return 60 + seconds;
    }

    /* We calculate the date by calculating the number of days since the */
    /* epoch and adjust for the number of leap days. We calculate the */
    /* number of leap days by normalizing the year in relation to the */
    /* epoch. Thus the year 2000 becomes 100 for 4-year and 100-year */
    /* leapdays and 400 for 400-year leapdays. */
    range = year - epoch;

    if (year % 4 == 0 && (year % 100 > 0 || year % 400 == 0)) {
        leap = 1;
        mdays[2] = 29;
    }

    /*
     * Calculate the serial date by accumulating the number of days
     * since the epoch.
     */

    /* Add days for previous months. */
    for (i = 0; i < month; i++) {
        days += mdays[i];
    }
    /* Add days for current month. */
    days += day;
    /* Add days for all previous years.  */
    days += range * 365;
    /* Add 4 year leapdays. */
    days += range / 4;
    /* Remove 100 year leapdays. */
    days -= (range + offset) / 100;
    /* Add 400 year leapdays. */
    days += (range + offset + norm) / 400;
    /* Remove leap days already counted. */
    days -= leap;

    /* Adjust for Excel erroneously treating 1900 as a leap year. */
    if (!date_1904 && days > 59)
        days++;

    return days + seconds;
}

// Assembly:
movl (%rdi), %ecx
xorl %r9d, %r9d
testl %esi, %esi
setne %r9b
movl $0x1f, %r8d
movl $0x1, %eax
cmovnel %eax, %r8d
movl $0xc, %r10d
cmovnel %eax, %r10d
testl %ecx, %ecx
je 0x2354e
movl 0x8(%rdi), %r8d
movl 0x4(%rdi), %r10d
jmp 0x23558
leal (%r9,%r9,4), %ecx
addl $0x76b, %ecx            # imm = 0x76B
pushq %rbp
pushq %rbx
imull $0xe10, 0xc(%rdi), %eax # imm = 0xE10
imull $0x3c, 0x10(%rdi), %edx
addl %eax, %edx
cvtsi2sd %edx, %xmm0
addsd 0x18(%rdi), %xmm0
xorl %eax, %eax
movaps 0x1ed37(%rip), %xmm1    # 0x422b0
movaps %xmm1, -0x18(%rsp)
movaps 0x1ed1b(%rip), %xmm1    # 0x422a0
movaps %xmm1, -0x28(%rsp)
movapd 0x1ecfe(%rip), %xmm1    # 0x42290
movapd %xmm1, -0x38(%rsp)
movl $0x1f, -0x8(%rsp)
divsd 0x1e780(%rip), %xmm0    # 0x41d28
testl %esi, %esi
sete %dl
je 0x23675
movb %dl, %al
leal -0x770(,%rax,4), %ebp
xorl %r11d, %r11d
movl $0x0, %ebx
testb $0x3, %cl
jne 0x235f9
movl $0x64, %edi
movl %ecx, %eax
cltd
idivl %edi
movl %edx, %edi
movl $0x190, %ebx            # imm = 0x190
movl %ecx, %eax
cltd
idivl %ebx
testl %edi, %edi
jg 0x235ec
movl $0x0, %ebx
testl %edx, %edx
jne 0x235f9
movl $0x1d, -0x30(%rsp)
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
shll $0x2, %r9d
addl %ebp, %ecx
testl %r10d, %r10d
cmovlel %r11d, %r10d
xorl %eax, %eax
cmpq %rax, %r10
je 0x23617
addl -0x38(%rsp,%rax,4), %r11d
incq %rax
jmp 0x23608
testb %sil, %sil
sete %r10b
imull $0x16d, %ecx, %ebp      # imm = 0x16D
movl $0x4, %esi
movl %ecx, %eax
cltd
idivl %esi
movl %eax, %esi
leal (%rcx,%r9), %eax
movl $0xffffff9c, %edi       # imm = 0xFFFFFF9C
cltd
idivl %edi
movl %eax, %edi
leal (%rcx,%r9), %eax
addl $0x12c, %eax            # imm = 0x12C
movl $0x190, %ecx            # imm = 0x190
cltd
idivl %ecx
addl %r8d, %ebp
addl %esi, %ebp
addl %edi, %ebp
addl %ebx, %eax
addl %ebp, %eax
addl %r11d, %eax
cmpl $0x3c, %eax
setge %cl
andb %r10b, %cl
movzbl %cl, %ecx
addl %eax, %ecx
cvtsi2sd %ecx, %xmm1
addsd %xmm1, %xmm0
jmp 0x236cb
movl %ecx, %edi
xorl $0x76b, %edi            # imm = 0x76B
movl %r10d, %r11d
xorl $0xc, %r11d
orl %edi, %r11d
movl %r8d, %edi
xorl $0x1f, %edi
orl %r11d, %edi
je 0x236cb
movl %ecx, %edi
xorl $0x76c, %edi            # imm = 0x76C
movl %r10d, %r11d
xorl $0x1, %r11d
orl %edi, %r11d
orl %r8d, %r11d
je 0x236cb
movl %r10d, %r11d
xorl $0x2, %r11d
orl %r11d, %edi
movl %r8d, %r11d
xorl $0x1d, %r11d
orl %edi, %r11d
jne 0x235b3
addsd 0x1e665(%rip), %xmm0    # 0x41d30
popq %rbx
popq %rbp
retq