// File: /workspace/github_c_makefile_star2_O3/antirez[P]stonky/stonky.c
// Function: yahooDataToPriceChanges
void yahooDataToPriceChanges(ydata *yd, int range) {
    if (range >= yd->ts_len) range = yd->ts_len-1;
    if (range <= 0) return;

    /* Intialize the result set. */
    yd->pdays = 0;
    yd->ldays = 0;
    yd->avgp = 0;
    yd->avgl = 0;
    yd->maxp = 0;
    yd->maxl = 0;

    double pval = 0; /* Previous sample value. */
    /* Analyze from the first to the last day in the range. */
    for (int j = range; j >= 0; j--) {
        double val = yd->ts_data[yd->ts_len-j-1];
        if (j == range) { /* First iteration. */
            pval = val;
            continue;
        } else {
            /* Handle broken data: the Yahoo API returns zero samples for
             * certain data points of less known stocks. */
            if (val == 0 || pval == 0) {
                if (val != 0) pval = val;
                yd->ts_data[yd->ts_len-j-1] = -1.0/0;
                continue;
            }
        }

        /* Compute the PL percentage between the previous and current
         * day of trading. */
        double pl = ((val/pval)-1)*100;
        yd->ts_data[yd->ts_len-j-1] = pl;
        if (pl > 0) {
            yd->pdays++;
            yd->avgp += pl;
            if (pl > yd->maxp) yd->maxp = pl;
        } else {
            yd->ldays++;
            yd->avgl += pl;
            if (pl < yd->maxl) yd->maxl = pl;
        }
        pval = val;
    }
    if (yd->pdays) yd->avgp /= yd->pdays;
    if (yd->ldays) yd->avgl /= yd->ldays;
    yd->ts_clen = range;
}

// Assembly:
movl 0x78(%rdi), %r8d
leal -0x1(%r8), %eax
cmpl %esi, %r8d
cmovgl %esi, %eax
testl %eax, %eax
jle 0x8f4d
xorpd %xmm0, %xmm0
movupd %xmm0, 0xb8(%rdi)
movupd %xmm0, 0xa8(%rdi)
movupd %xmm0, 0x98(%rdi)
movq 0x80(%rdi), %rcx
movl %eax, %edx
notl %edx
addl %r8d, %edx
xorpd %xmm1, %xmm1
movl $0x1, %r9d
xorl %esi, %esi
xorps %xmm3, %xmm3
movsd 0x58a1(%rip), %xmm4     # 0xe6b8
movsd 0x5891(%rip), %xmm5     # 0xe6b0
xorpd %xmm6, %xmm6
xorpd %xmm2, %xmm2
xorpd %xmm7, %xmm7
xorpd %xmm0, %xmm0
xorl %r8d, %r8d
xorpd %xmm9, %xmm9
movslq %edx, %r10
movss (%rcx,%r10,4), %xmm10
xorps %xmm8, %xmm8
cvtss2sd %xmm10, %xmm8
cmpq $0x1, %r9
je 0x8ecc
ucomiss %xmm3, %xmm10
jne 0x8e57
jnp 0x8eb7
ucomisd %xmm1, %xmm9
jne 0x8e60
jnp 0x8eb7
movaps %xmm8, %xmm10
divsd %xmm9, %xmm10
addsd %xmm4, %xmm10
mulsd %xmm5, %xmm10
xorps %xmm9, %xmm9
cvtsd2ss %xmm10, %xmm9
movss %xmm9, (%rcx,%r10,4)
ucomisd %xmm1, %xmm10
jbe 0x8ee7
incq %rsi
movq %rsi, 0x98(%rdi)
addsd %xmm10, %xmm2
movsd %xmm2, 0xa8(%rdi)
ucomisd %xmm6, %xmm10
jbe 0x8ecc
movsd %xmm10, 0xb8(%rdi)
movapd %xmm10, %xmm6
jmp 0x8ecc
ucomiss %xmm3, %xmm10
jne 0x8ec4
jp 0x8ec4
movapd %xmm9, %xmm8
movl $0xff800000, (%rcx,%r10,4) # imm = 0xFF800000
leaq (%rax,%r9), %r10
decq %r10
decq %r9
incl %edx
movaps %xmm8, %xmm9
testq %r10, %r10
jg 0x8e37
jmp 0x8f15
incq %r8
movq %r8, 0xa0(%rdi)
addsd %xmm10, %xmm0
movsd %xmm0, 0xb0(%rdi)
ucomisd %xmm10, %xmm7
jbe 0x8ecc
movsd %xmm10, 0xc0(%rdi)
movapd %xmm10, %xmm7
jmp 0x8ecc
testq %rsi, %rsi
je 0x8f2e
xorps %xmm1, %xmm1
cvtsi2sd %rsi, %xmm1
divsd %xmm1, %xmm2
movsd %xmm2, 0xa8(%rdi)
testq %r8, %r8
je 0x8f47
xorps %xmm1, %xmm1
cvtsi2sd %r8, %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, 0xb0(%rdi)
movl %eax, 0x90(%rdi)
retq