// File: /workspace/github_c_makefile_star2_O3/antirez[P]stonky/stonky.c
// Function: computeMontecarlo
void computeMontecarlo(ydata *yd, int range, int count, int period, mcres *mc) {
    int buyday, sellday;
    double total_gain = 0;
    double total_interval = 0;
    float *data = yd->ts_data;

    if (range > yd->ts_len) range = yd->ts_len;
    else if (range < yd->ts_len) data += yd->ts_len-range;
    double *gains = xmalloc(sizeof(double)*count);

    /* The period (days of difference) between buy and sell must always
     * be less than the range (total days considered). If we have a range
     * of 2 days, the maximum period must be 1 in order to buy and
     * sell in the only two days that are 1 day apart. */
    if (period >= range) period = range-1;

    for (int j = 0; j < count; j++) {
        /* We want to pick two different days, and since the API sometimes
         * return data with fields set to zero, also make sure that
         * we pick non-zero days. */
        int maxtries; /* To avoid infinite loops when picking days. */
        if (period == 0) {
            /* Random days. */
            maxtries = 10;
            do {
                buyday = rand() % range;
            } while (data[buyday] == 0 && maxtries--);

            maxtries = 10;
            do {
                sellday = rand() % range;
            } while((sellday == buyday || data[sellday] == 0) &&
                    maxtries--);

            if (buyday > sellday) {
                int t = buyday;
                buyday = sellday;
                sellday = t;
            }
        } else {
            /* Fixed days interval. Pick the first days at random, then
             * use the offset to pick the other. Note that period < range
             * since we checked at the function entry. */
            maxtries = 10;
            do {
                buyday = rand() % (range-period);
                sellday = buyday+period;
            } while (data[buyday] == 0 && data[sellday] == 0 && maxtries--);
        }

        double buy_price = data[buyday];
        double sell_price = data[sellday];

        /* Sometimes Yahoo prices are null. Don't use bad data. */
        if (buy_price == 0 || sell_price == 0) {
            gains[j] = 0;
            total_interval += sellday-buyday;
            continue;
        }

        double gain = (sell_price-buy_price)/buy_price*100;
        gains[j] = gain;
        if (DebugMode >= 3) {
            printf("Montecarlo buy (%d) %f sell (%d) %f: %f\n",
                buyday, buy_price, sellday, sell_price, gain);
        }
        total_gain += gain;
        total_interval += sellday-buyday;
    }
    mc->gain = total_gain / count;
    mc->avgper = total_interval / count;

    /* Scan the array of gains to calculate the average gain difference,
     * as long as min/max values. */
    mc->absdiff = 0;
    for (int j = 0; j < count; j++) {
        mc->absdiff += fabs(mc->gain - gains[j]);
        if (j == 0) {
            mc->mingain = gains[j];
            mc->maxgain = gains[j];
        } else {
            if (gains[j] < mc->mingain) mc->mingain = gains[j];
            if (gains[j] > mc->maxgain) mc->maxgain = gains[j];
        }
    }
    mc->absdiff /= count;
    mc->absdiffper = mc->absdiff/fabs(mc->gain)*100;
    xfree(gains);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %r8, %r13
movl %ecx, %ebp
movl %edx, %r14d
movq 0x80(%rdi), %rbx
movl 0x78(%rdi), %eax
movl %eax, %ecx
subl %esi, %ecx
jge 0x98fd
movl %eax, %r15d
jmp 0x9909
movl %esi, %r15d
jle 0x9909
movslq %ecx, %rax
leaq (%rbx,%rax,4), %rbx
movslq %r14d, %r12
leaq (,%r12,8), %rdi
callq 0x7720
movq %rax, (%rsp)
movl %r14d, %eax
leal -0x1(%r15), %r14d
cmpl %ebp, %r15d
cmovgl %ebp, %r14d
testl %r12d, %r12d
jle 0x9b8b
movq %r13, 0x28(%rsp)
movl %r15d, %ecx
subl %r14d, %ecx
movl %ecx, 0xc(%rsp)
movl %eax, 0x8(%rsp)
movl %eax, %eax
movq %rax, 0x18(%rsp)
xorpd %xmm5, %xmm5
xorl %r12d, %r12d
xorps %xmm1, %xmm1
movq %r12, 0x20(%rsp)
movaps %xmm1, 0x40(%rsp)
movapd %xmm5, 0x30(%rsp)
testl %r14d, %r14d
je 0x99c8
movl $0xfffffff6, %r12d      # imm = 0xFFFFFFF6
callq 0x20d0
cltd
idivl 0xc(%rsp)
movl %edx, %ebp
movslq %edx, %rax
movl %edx, %r13d
addl %r14d, %r13d
movss (%rbx,%rax,4), %xmm0
xorps %xmm2, %xmm2
ucomiss %xmm2, %xmm0
jne 0x9a4c
jp 0x9a4c
movslq %r13d, %rax
movss (%rbx,%rax,4), %xmm1
ucomiss %xmm2, %xmm1
jne 0x9a4c
jp 0x9a4c
leal 0x1(%r12), %eax
testl %r12d, %r12d
movl %eax, %r12d
jne 0x9973
jmp 0x9a4c
movl $0xfffffff6, %r13d      # imm = 0xFFFFFFF6
callq 0x20d0
cltd
idivl %r15d
movl %edx, %r12d
movslq %edx, %rax
movss (%rbx,%rax,4), %xmm0
ucomiss 0x4e77(%rip), %xmm0     # 0xe860
jne 0x99f9
jp 0x99f9
leal 0x1(%r13), %eax
testl %r13d, %r13d
movl %eax, %r13d
jne 0x99ce
movl $0xfffffff6, %r13d      # imm = 0xFFFFFFF6
callq 0x20d0
cltd
idivl %r15d
cmpl %r12d, %edx
jne 0x9a14
testl %r13d, %r13d
jne 0x9a2e
jmp 0x9a33
movl %edx, %ebp
movslq %edx, %rax
movss (%rbx,%rax,4), %xmm0
ucomiss 0x4e3b(%rip), %xmm0     # 0xe860
jne 0x9a36
jp 0x9a36
testl %r13d, %r13d
je 0x9a36
incl %r13d
jmp 0x99ff
movl %r12d, %ebp
cmpl %ebp, %r12d
movl %ebp, %r13d
cmovgl %r12d, %r13d
cmovll %r12d, %ebp
movslq %ebp, %rax
movss (%rbx,%rax,4), %xmm0
xorps %xmm2, %xmm2
ucomiss %xmm2, %xmm0
jne 0x9a56
jnp 0x9ac9
movslq %r13d, %rax
movss (%rbx,%rax,4), %xmm1
ucomiss %xmm2, %xmm1
jne 0x9a65
jnp 0x9ac9
cvtss2sd %xmm0, %xmm0
cvtss2sd %xmm1, %xmm1
movaps %xmm1, %xmm2
subsd %xmm0, %xmm2
divsd %xmm0, %xmm2
mulsd 0x4c30(%rip), %xmm2     # 0xe6b0
movq (%rsp), %rax
movq 0x20(%rsp), %r12
movsd %xmm2, (%rax,%r12,8)
cmpl $0x3, 0xb81a(%rip)      # 0x152b0
jl 0x9abd
leaq 0x5844(%rip), %rdi      # 0xf2e3
movl %ebp, %esi
movl %r13d, %edx
movsd %xmm2, 0x10(%rsp)
movsd 0x10(%rsp), %xmm2
movb $0x3, %al
callq 0x2030
movsd 0x10(%rsp), %xmm2
movapd 0x30(%rsp), %xmm5
addsd %xmm2, %xmm5
jmp 0x9ae0
movq (%rsp), %rax
movq 0x20(%rsp), %r12
movq $0x0, (%rax,%r12,8)
movapd 0x30(%rsp), %xmm5
subl %ebp, %r13d
xorps %xmm0, %xmm0
cvtsi2sd %r13d, %xmm0
movapd 0x40(%rsp), %xmm1
addsd %xmm0, %xmm1
incq %r12
cmpq 0x18(%rsp), %r12
jne 0x9958
movl 0x8(%rsp), %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
divsd %xmm0, %xmm5
movq 0x28(%rsp), %r13
movsd %xmm5, (%r13)
divsd %xmm0, %xmm1
movsd %xmm1, 0x28(%r13)
xorpd %xmm1, %xmm1
testl %eax, %eax
jle 0x9ba7
xorl %eax, %eax
movapd 0x44f7(%rip), %xmm2     # 0xe030
movq (%rsp), %rcx
movsd (%rcx,%rax,8), %xmm3
testq %rax, %rax
je 0x9b63
movsd 0x8(%r13), %xmm4
ucomisd %xmm3, %xmm4
jbe 0x9b59
movsd %xmm3, 0x8(%r13)
ucomisd 0x10(%r13), %xmm3
ja 0x9b69
jmp 0x9b6f
movsd %xmm3, 0x8(%r13)
movsd %xmm3, 0x10(%r13)
movapd %xmm5, %xmm4
subsd %xmm3, %xmm4
andpd %xmm2, %xmm4
addsd %xmm4, %xmm1
incq %rax
cmpq %rax, 0x18(%rsp)
jne 0x9b3d
jmp 0x9ba7
cvtsi2sd %eax, %xmm0
xorpd %xmm1, %xmm1
xorpd %xmm5, %xmm5
divsd %xmm0, %xmm5
movsd %xmm5, (%r13)
movsd %xmm5, 0x28(%r13)
divsd %xmm0, %xmm1
movsd %xmm1, 0x18(%r13)
andpd 0x4477(%rip), %xmm5     # 0xe030
divsd %xmm5, %xmm1
mulsd 0x4aeb(%rip), %xmm1     # 0xe6b0
movsd %xmm1, 0x20(%r13)
movq (%rsp), %rdi
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2478