// File: /workspace/github_c_makefile_star2_O2/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 0x7cad
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
xorl %r8d, %r8d
xorps %xmm2, %xmm2
movsd 0x4849(%rip), %xmm4     # 0xc3d0
movsd 0x4839(%rip), %xmm5     # 0xc3c8
xorpd %xmm6, %xmm6
xorpd %xmm7, %xmm7
xorpd %xmm0, %xmm0
xorl %esi, %esi
xorpd %xmm3, %xmm3
xorl %r9d, %r9d
xorpd %xmm9, %xmm9
movq %rax, %r10
addq %r8, %r10
js 0x7c7b
movslq %edx, %r10
movss (%rcx,%r10,4), %xmm10
cvtss2sd %xmm10, %xmm8
subq $0x1, %r8
jb 0x7c42
ucomiss %xmm2, %xmm10
jne 0x7bd1
jnp 0x7c2d
ucomisd %xmm1, %xmm9
jne 0x7bda
jnp 0x7c2d
movaps %xmm8, %xmm10
divsd %xmm9, %xmm10
addsd %xmm4, %xmm10
mulsd %xmm5, %xmm10
cvtsd2ss %xmm10, %xmm9
movss %xmm9, (%rcx,%r10,4)
ucomisd %xmm1, %xmm10
jbe 0x7c4d
incq %r9
movq %r9, 0x98(%rdi)
addsd %xmm10, %xmm3
movsd %xmm3, 0xa8(%rdi)
ucomisd %xmm6, %xmm10
jbe 0x7c42
movsd %xmm10, 0xb8(%rdi)
movapd %xmm10, %xmm6
jmp 0x7c42
ucomiss %xmm2, %xmm10
jne 0x7c3a
jp 0x7c3a
movapd %xmm9, %xmm8
movl $0xff800000, (%rcx,%r10,4) # imm = 0xFF800000
incl %edx
movaps %xmm8, %xmm9
jmp 0x7ba9
incq %rsi
movq %rsi, 0xa0(%rdi)
addsd %xmm10, %xmm0
movsd %xmm0, 0xb0(%rdi)
ucomisd %xmm10, %xmm7
jbe 0x7c42
movsd %xmm10, 0xc0(%rdi)
movapd %xmm10, %xmm7
jmp 0x7c42
testq %r9, %r9
je 0x7c91
cvtsi2sd %r9, %xmm1
divsd %xmm1, %xmm3
movsd %xmm3, 0xa8(%rdi)
testq %rsi, %rsi
je 0x7ca7
cvtsi2sd %rsi, %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, 0xb0(%rdi)
movl %eax, 0x90(%rdi)
retq