// File: /workspace/github_c_makefile_star2_O1/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 $0x68, %rsp
movl %ecx, %ebp
movl %edx, 0xc(%rsp)
movq 0x80(%rdi), %r14
movl 0x78(%rdi), %eax
movl %eax, %ecx
subl %esi, %ecx
movq %r8, 0x38(%rsp)
jge 0x9ba3
movl %eax, %r15d
jmp 0x9baf
movl %esi, %r15d
jle 0x9baf
movslq %ecx, %rax
leaq (%r14,%rax,4), %r14
movl 0xc(%rsp), %r13d
movslq %r13d, %r12
leaq (,%r12,8), %rdi
callq 0x7970
movq %rax, 0x10(%rsp)
leal -0x1(%r15), %ebx
cmpl %ebp, %r15d
cmovgl %ebp, %ebx
movl %r13d, %edi
testl %r12d, %r12d
jle 0x9d9d
movl %r15d, %eax
subl %ebx, %eax
movl %eax, 0x1c(%rsp)
xorpd %xmm5, %xmm5
xorl %ebp, %ebp
xorps %xmm1, %xmm1
movq %rdi, 0x28(%rsp)
movq %rbp, 0x30(%rsp)
movaps %xmm1, 0x50(%rsp)
movapd %xmm5, 0x40(%rsp)
testl %ebx, %ebx
je 0x9c5c
movl $0xfffffff6, %ebp       # imm = 0xFFFFFFF6
callq 0x20d0
cltd
idivl 0x1c(%rsp)
movl %edx, %r12d
movslq %edx, %rax
movl %edx, %r13d
addl %ebx, %r13d
movss (%r14,%rax,4), %xmm0
xorps %xmm1, %xmm1
ucomiss %xmm1, %xmm0
jne 0x9cd0
jp 0x9cd0
movslq %r13d, %rax
movss (%r14,%rax,4), %xmm0
ucomiss %xmm1, %xmm0
jne 0x9cd0
jp 0x9cd0
leal 0x1(%rbp), %eax
testl %ebp, %ebp
movl %eax, %ebp
jne 0x9c0f
jmp 0x9cd0
movl $0xfffffff6, %r12d      # imm = 0xFFFFFFF6
callq 0x20d0
cltd
idivl %r15d
movl %edx, %ebp
movslq %edx, %rax
movss (%r14,%rax,4), %xmm0
ucomiss 0x4c03(%rip), %xmm0     # 0xe880
jne 0x9c8e
jp 0x9c8e
leal 0x1(%r12), %eax
testl %r12d, %r12d
movl %eax, %r12d
jne 0x9c62
movl $0xfffffff6, %r13d      # imm = 0xFFFFFFF6
callq 0x20d0
cltd
idivl %r15d
movl %edx, %r12d
cmpl %ebp, %edx
je 0x9cb8
movslq %r12d, %rax
movss (%r14,%rax,4), %xmm0
ucomiss 0x4bcc(%rip), %xmm0     # 0xe880
jne 0x9cc2
jp 0x9cc2
testl %r13d, %r13d
je 0x9cc2
incl %r13d
jmp 0x9c94
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 0x9ce3
jnp 0x9d5d
movslq %r13d, %rax
movss (%r14,%rax,4), %xmm1
ucomiss %xmm2, %xmm1
jne 0x9cf3
jnp 0x9d5d
cvtss2sd %xmm0, %xmm0
cvtss2sd %xmm1, %xmm1
movaps %xmm1, %xmm2
subsd %xmm0, %xmm2
divsd %xmm0, %xmm2
mulsd 0x49b2(%rip), %xmm2     # 0xe6c0
movq 0x10(%rsp), %rax
movq 0x30(%rsp), %rbp
movsd %xmm2, (%rax,%rbp,8)
cmpl $0x3, 0xb58c(%rip)      # 0x152b0
jl 0x9d4c
leaq 0x55d6(%rip), %rdi      # 0xf303
movl %r12d, %esi
movl %r13d, %edx
movsd %xmm2, 0x20(%rsp)
movsd 0x20(%rsp), %xmm2
movb $0x3, %al
callq 0x2030
movsd 0x20(%rsp), %xmm2
movapd 0x40(%rsp), %xmm5
addsd %xmm2, %xmm5
movq 0x28(%rsp), %rdi
jmp 0x9d7a
movq 0x10(%rsp), %rax
movq 0x30(%rsp), %rbp
movq $0x0, (%rax,%rbp,8)
movq 0x28(%rsp), %rdi
movapd 0x40(%rsp), %xmm5
subl %r12d, %r13d
xorps %xmm0, %xmm0
cvtsi2sd %r13d, %xmm0
movapd 0x50(%rsp), %xmm1
addsd %xmm0, %xmm1
incq %rbp
cmpq %rdi, %rbp
jne 0x9bf6
jmp 0x9da4
xorps %xmm1, %xmm1
xorpd %xmm5, %xmm5
movl 0xc(%rsp), %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
divsd %xmm0, %xmm5
movq 0x38(%rsp), %rdx
movsd %xmm5, (%rdx)
divsd %xmm0, %xmm1
movsd %xmm1, 0x28(%rdx)
movq $0x0, 0x18(%rdx)
testl %eax, %eax
jle 0x9e31
leaq 0x10(%rdx), %rax
movsd 0x18(%rdx), %xmm1
xorl %ecx, %ecx
movapd 0x424c(%rip), %xmm2     # 0xe030
movq 0x10(%rsp), %rsi
movsd (%rsi,%rcx,8), %xmm3
testq %rcx, %rcx
je 0x9e0b
movsd 0x8(%rdx), %xmm4
ucomisd %xmm3, %xmm4
jbe 0x9e03
movsd %xmm3, 0x8(%rdx)
ucomisd (%rax), %xmm3
ja 0x9e10
jmp 0x9e14
movsd %xmm3, 0x8(%rdx)
movsd %xmm3, (%rax)
movapd %xmm5, %xmm4
subsd %xmm3, %xmm4
andpd %xmm2, %xmm4
addsd %xmm4, %xmm1
incq %rcx
cmpq %rcx, %rdi
jne 0x9de9
movsd %xmm1, 0x18(%rdx)
movsd 0x18(%rdx), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, 0x18(%rdx)
andpd 0x41e9(%rip), %xmm5     # 0xe030
divsd %xmm5, %xmm1
mulsd 0x486d(%rip), %xmm1     # 0xe6c0
movsd %xmm1, 0x20(%rdx)
movq 0x10(%rsp), %rdi
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2468