// File: /workspace/github_c_makefile_star2_O2/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
movl %ecx, %ebp
movl %edx, 0x4(%rsp)
movq 0x80(%rdi), %r14
movl 0x78(%rdi), %eax
movl %eax, %ecx
subl %esi, %ecx
movq %r8, 0x30(%rsp)
jge 0x8620
movl %eax, %r15d
jmp 0x862c
movl %esi, %r15d
jle 0x862c
movslq %ecx, %rax
leaq (%r14,%rax,4), %r14
movl 0x4(%rsp), %r13d
movslq %r13d, %r12
leaq (,%r12,8), %rdi
callq 0x6568
movq %rax, 0x8(%rsp)
leal -0x1(%r15), %ebx
cmpl %ebp, %r15d
cmovgl %ebp, %ebx
movl %r15d, %eax
subl %ebx, %eax
movl %eax, 0x14(%rsp)
xorl %ebp, %ebp
testl %r12d, %r12d
movl $0x0, %esi
cmovgl %r13d, %esi
xorpd %xmm0, %xmm0
xorpd %xmm5, %xmm5
movq %rsi, 0x20(%rsp)
cmpq %rsi, %rbp
je 0x8829
movq %rbp, 0x28(%rsp)
movsd %xmm0, 0x38(%rsp)
movapd %xmm5, 0x40(%rsp)
pushq $-0xa
popq %rax
testl %ebx, %ebx
je 0x86eb
movl %eax, %ebp
callq 0x20d0
cltd
idivl 0x14(%rsp)
movl %edx, %r12d
movslq %edx, %rax
movl %edx, %r13d
addl %ebx, %r13d
movss (%r14,%rax,4), %xmm0
xorps %xmm2, %xmm2
ucomiss %xmm2, %xmm0
jne 0x876d
jp 0x876d
movslq %r13d, %rax
movss (%r14,%rax,4), %xmm1
ucomiss %xmm2, %xmm1
jne 0x876d
jp 0x876d
leal 0x1(%rbp), %eax
testl %ebp, %ebp
movl %eax, %ebp
jne 0x8697
jmp 0x876d
movl %eax, %r12d
callq 0x20d0
cltd
idivl %r15d
movl %edx, %ebp
movslq %edx, %rax
movss (%r14,%rax,4), %xmm0
ucomiss 0x3dc7(%rip), %xmm0     # 0xc4d0
jne 0x871a
jp 0x871a
leal 0x1(%r12), %eax
testl %r12d, %r12d
movl %eax, %r12d
jne 0x86ee
pushq $-0xa
popq %r13
callq 0x20d0
cltd
idivl %r15d
cmpl %ebp, %edx
jne 0x8732
testl %r13d, %r13d
jne 0x874e
jmp 0x8753
movl %edx, %r12d
movslq %edx, %rax
movss (%r14,%rax,4), %xmm0
ucomiss 0x3d8b(%rip), %xmm0     # 0xc4d0
jne 0x8756
jp 0x8756
testl %r13d, %r13d
je 0x8756
incl %r13d
jmp 0x871e
movl %ebp, %r12d
cmpl %r12d, %ebp
movl %r12d, %r13d
cmovgl %ebp, %r13d
cmovll %ebp, %r12d
movslq %r12d, %rax
movss (%r14,%rax,4), %xmm0
xorps %xmm2, %xmm2
ucomiss %xmm2, %xmm0
jne 0x8777
jnp 0x87f1
movslq %r13d, %rax
movss (%r14,%rax,4), %xmm1
ucomiss %xmm2, %xmm1
jne 0x8787
jnp 0x87f1
cvtss2sd %xmm0, %xmm0
cvtss2sd %xmm1, %xmm1
movaps %xmm1, %xmm2
subsd %xmm0, %xmm2
divsd %xmm0, %xmm2
mulsd 0x3c26(%rip), %xmm2     # 0xc3c8
movq 0x8(%rsp), %rax
movq 0x28(%rsp), %rbp
movsd %xmm2, (%rax,%rbp,8)
cmpl $0x3, 0xaaf8(%rip)      # 0x132b0
jl 0x87e0
leaq 0x478a(%rip), %rdi      # 0xcf4b
movl %r12d, %esi
movl %r13d, %edx
movsd %xmm2, 0x18(%rsp)
movsd 0x18(%rsp), %xmm2
movb $0x3, %al
callq 0x2030
movsd 0x18(%rsp), %xmm2
movapd 0x40(%rsp), %xmm5
addsd %xmm2, %xmm5
movq 0x20(%rsp), %rsi
jmp 0x880b
movq 0x8(%rsp), %rax
movq 0x28(%rsp), %rbp
andq $0x0, (%rax,%rbp,8)
movq 0x20(%rsp), %rsi
movapd 0x40(%rsp), %xmm5
subl %r12d, %r13d
cvtsi2sd %r13d, %xmm0
movsd 0x38(%rsp), %xmm1
addsd %xmm0, %xmm1
movapd %xmm1, %xmm0
incq %rbp
jmp 0x8674
cvtsi2sdl 0x4(%rsp), %xmm1
divsd %xmm1, %xmm5
movq 0x30(%rsp), %rcx
movsd %xmm5, (%rcx)
divsd %xmm1, %xmm0
movsd %xmm0, 0x28(%rcx)
xorpd %xmm0, %xmm0
xorl %eax, %eax
movapd 0x382d(%rip), %xmm2     # 0xc080
movq 0x8(%rsp), %rdx
cmpq %rax, %rsi
je 0x88a4
movsd (%rdx,%rax,8), %xmm3
movapd %xmm5, %xmm4
subsd %xmm3, %xmm4
andpd %xmm2, %xmm4
addsd %xmm4, %xmm0
movsd %xmm0, 0x18(%rcx)
testq %rax, %rax
je 0x8895
movsd 0x8(%rcx), %xmm4
ucomisd %xmm3, %xmm4
jbe 0x888c
movsd %xmm3, 0x8(%rcx)
ucomisd 0x10(%rcx), %xmm3
ja 0x889a
jmp 0x889f
movsd %xmm3, 0x8(%rcx)
movsd %xmm3, 0x10(%rcx)
incq %rax
jmp 0x8858
divsd %xmm1, %xmm0
movsd %xmm0, 0x18(%rcx)
andpd 0x37cb(%rip), %xmm5     # 0xc080
divsd %xmm5, %xmm0
mulsd 0x3b07(%rip), %xmm0     # 0xc3c8
movsd %xmm0, 0x20(%rcx)
movq 0x8(%rsp), %rdi
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2478