// File: /workspace/github_c_makefile_star2_O1/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 0x91e4
leaq 0x98(%rdi), %rcx
xorpd %xmm0, %xmm0
movupd %xmm0, 0xb8(%rdi)
movupd %xmm0, 0xa8(%rdi)
movupd %xmm0, 0x98(%rdi)
movq 0x80(%rdi), %rdx
movl %eax, %esi
notl %esi
addl %r8d, %esi
xorpd %xmm0, %xmm0
movl $0x1, %r8d
xorps %xmm1, %xmm1
movsd 0x5618(%rip), %xmm2     # 0xe6c8
movsd 0x5608(%rip), %xmm3     # 0xe6c0
xorpd %xmm5, %xmm5
movslq %esi, %r9
movss (%rdx,%r9,4), %xmm6
xorps %xmm4, %xmm4
cvtss2sd %xmm6, %xmm4
cmpq $0x1, %r8
je 0x9145
ucomiss %xmm1, %xmm6
jne 0x90d9
jnp 0x9132
ucomisd %xmm0, %xmm5
jne 0x90e1
jnp 0x9132
movaps %xmm4, %xmm6
divsd %xmm5, %xmm6
addsd %xmm2, %xmm6
mulsd %xmm3, %xmm6
xorps %xmm5, %xmm5
cvtsd2ss %xmm6, %xmm5
movss %xmm5, (%rdx,%r9,4)
ucomisd %xmm0, %xmm6
jbe 0x915f
incq 0x98(%rdi)
movsd 0xa8(%rdi), %xmm5
addsd %xmm6, %xmm5
movsd %xmm5, 0xa8(%rdi)
ucomisd 0xb8(%rdi), %xmm6
jbe 0x9145
movsd %xmm6, 0xb8(%rdi)
jmp 0x9145
ucomiss %xmm1, %xmm6
jne 0x913d
jp 0x913d
movapd %xmm5, %xmm4
movl $0xff800000, (%rdx,%r9,4) # imm = 0xFF800000
leaq (%rax,%r8), %r9
decq %r9
decq %r8
incl %esi
movaps %xmm4, %xmm5
testq %r9, %r9
jg 0x90bc
jmp 0x9192
incq 0xa0(%rdi)
movsd 0xb0(%rdi), %xmm5
addsd %xmm6, %xmm5
movsd %xmm5, 0xb0(%rdi)
movsd 0xc0(%rdi), %xmm5
ucomisd %xmm6, %xmm5
jbe 0x9145
movsd %xmm6, 0xc0(%rdi)
jmp 0x9145
movq (%rcx), %rcx
testq %rcx, %rcx
je 0x91b6
xorps %xmm0, %xmm0
cvtsi2sd %rcx, %xmm0
movsd 0xa8(%rdi), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, 0xa8(%rdi)
movq 0xa0(%rdi), %rcx
testq %rcx, %rcx
je 0x91de
xorps %xmm0, %xmm0
cvtsi2sd %rcx, %xmm0
movsd 0xb0(%rdi), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, 0xb0(%rdi)
movl %eax, 0x90(%rdi)
retq