// File: /workspace/github_c_makefile_star2_O0/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:
subq $0x78, %rsp
movq %rdi, 0x70(%rsp)
movl %esi, 0x6c(%rsp)
movl %edx, 0x68(%rsp)
movl %ecx, 0x64(%rsp)
movq %r8, 0x58(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0x48(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0x40(%rsp)
movq 0x70(%rsp), %rax
movq 0x80(%rax), %rax
movq %rax, 0x38(%rsp)
movl 0x6c(%rsp), %eax
movq 0x70(%rsp), %rcx
cmpl 0x78(%rcx), %eax
jle 0xed69
movq 0x70(%rsp), %rax
movl 0x78(%rax), %eax
movl %eax, 0x6c(%rsp)
jmp 0xed99
movl 0x6c(%rsp), %eax
movq 0x70(%rsp), %rcx
cmpl 0x78(%rcx), %eax
jge 0xed97
movq 0x70(%rsp), %rax
movl 0x78(%rax), %ecx
subl 0x6c(%rsp), %ecx
movq 0x38(%rsp), %rax
movslq %ecx, %rcx
shlq $0x2, %rcx
addq %rcx, %rax
movq %rax, 0x38(%rsp)
jmp 0xed99
movslq 0x68(%rsp), %rdi
shlq $0x3, %rdi
callq 0xb350
movq %rax, 0x30(%rsp)
movl 0x64(%rsp), %eax
cmpl 0x6c(%rsp), %eax
jl 0xedc1
movl 0x6c(%rsp), %eax
subl $0x1, %eax
movl %eax, 0x64(%rsp)
movl $0x0, 0x2c(%rsp)
movl 0x2c(%rsp), %eax
cmpl 0x68(%rsp), %eax
jge 0xf06b
cmpl $0x0, 0x64(%rsp)
jne 0xeebb
movl $0xa, 0x28(%rsp)
callq 0x20b0
cltd
idivl 0x6c(%rsp)
movl %edx, 0x54(%rsp)
movq 0x38(%rsp), %rax
movslq 0x54(%rsp), %rcx
movss (%rax,%rcx,4), %xmm0
xorl %eax, %eax
xorps %xmm1, %xmm1
ucomiss %xmm1, %xmm0
movb %al, 0x3(%rsp)
jne 0xee2e
jp 0xee2e
movl 0x28(%rsp), %eax
movl %eax, %ecx
addl $-0x1, %ecx
movl %ecx, 0x28(%rsp)
cmpl $0x0, %eax
setne %al
movb %al, 0x3(%rsp)
movb 0x3(%rsp), %al
testb $0x1, %al
jne 0xedea
movl $0xa, 0x28(%rsp)
callq 0x20b0
cltd
idivl 0x6c(%rsp)
movl %edx, 0x50(%rsp)
movl 0x50(%rsp), %eax
cmpl 0x54(%rsp), %eax
je 0xee75
movq 0x38(%rsp), %rax
movslq 0x50(%rsp), %rcx
movss (%rax,%rcx,4), %xmm0
xorl %eax, %eax
xorps %xmm1, %xmm1
ucomiss %xmm1, %xmm0
movb %al, 0x2(%rsp)
jne 0xee8c
jp 0xee8c
movl 0x28(%rsp), %eax
movl %eax, %ecx
addl $-0x1, %ecx
movl %ecx, 0x28(%rsp)
cmpl $0x0, %eax
setne %al
movb %al, 0x2(%rsp)
movb 0x2(%rsp), %al
testb $0x1, %al
jne 0xee3e
movl 0x54(%rsp), %eax
cmpl 0x50(%rsp), %eax
jle 0xeeb6
movl 0x54(%rsp), %eax
movl %eax, 0x24(%rsp)
movl 0x50(%rsp), %eax
movl %eax, 0x54(%rsp)
movl 0x24(%rsp), %eax
movl %eax, 0x50(%rsp)
jmp 0xef42
movl $0xa, 0x28(%rsp)
callq 0x20b0
movl 0x6c(%rsp), %ecx
subl 0x64(%rsp), %ecx
cltd
idivl %ecx
movl %edx, 0x54(%rsp)
movl 0x54(%rsp), %eax
addl 0x64(%rsp), %eax
movl %eax, 0x50(%rsp)
movq 0x38(%rsp), %rax
movslq 0x54(%rsp), %rcx
movss (%rax,%rcx,4), %xmm0
xorl %eax, %eax
xorps %xmm1, %xmm1
ucomiss %xmm1, %xmm0
movb %al, 0x1(%rsp)
jne 0xef38
jp 0xef38
movq 0x38(%rsp), %rax
movslq 0x50(%rsp), %rcx
movss (%rax,%rcx,4), %xmm0
xorl %eax, %eax
xorps %xmm1, %xmm1
ucomiss %xmm1, %xmm0
movb %al, 0x1(%rsp)
jne 0xef38
jp 0xef38
movl 0x28(%rsp), %eax
movl %eax, %ecx
addl $-0x1, %ecx
movl %ecx, 0x28(%rsp)
cmpl $0x0, %eax
setne %al
movb %al, 0x1(%rsp)
movb 0x1(%rsp), %al
testb $0x1, %al
jne 0xeec3
jmp 0xef42
movq 0x38(%rsp), %rax
movslq 0x54(%rsp), %rcx
movss (%rax,%rcx,4), %xmm0
cvtss2sd %xmm0, %xmm0
movsd %xmm0, 0x18(%rsp)
movq 0x38(%rsp), %rax
movslq 0x50(%rsp), %rcx
movss (%rax,%rcx,4), %xmm0
cvtss2sd %xmm0, %xmm0
movsd %xmm0, 0x10(%rsp)
movsd 0x18(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0xef87
jp 0xef87
jmp 0xef98
movsd 0x10(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0xefc7
jp 0xefc7
movq 0x30(%rsp), %rax
movslq 0x2c(%rsp), %rcx
xorps %xmm0, %xmm0
movsd %xmm0, (%rax,%rcx,8)
movl 0x50(%rsp), %eax
subl 0x54(%rsp), %eax
cvtsi2sd %eax, %xmm0
addsd 0x40(%rsp), %xmm0
movsd %xmm0, 0x40(%rsp)
jmp 0xf05b
movsd 0x10(%rsp), %xmm0
subsd 0x18(%rsp), %xmm0
divsd 0x18(%rsp), %xmm0
movsd 0x659f(%rip), %xmm1     # 0x15580
mulsd %xmm1, %xmm0
movsd %xmm0, 0x8(%rsp)
movsd 0x8(%rsp), %xmm0
movq 0x30(%rsp), %rax
movslq 0x2c(%rsp), %rcx
movsd %xmm0, (%rax,%rcx,8)
cmpl $0x3, 0xc2a9(%rip)      # 0x1b2b0
jl 0xf031
movl 0x54(%rsp), %esi
movsd 0x18(%rsp), %xmm0
movl 0x50(%rsp), %edx
movsd 0x10(%rsp), %xmm1
movsd 0x8(%rsp), %xmm2
leaq 0x70a9(%rip), %rdi      # 0x160d3
movb $0x3, %al
callq 0x2030
movsd 0x8(%rsp), %xmm0
addsd 0x48(%rsp), %xmm0
movsd %xmm0, 0x48(%rsp)
movl 0x50(%rsp), %eax
subl 0x54(%rsp), %eax
cvtsi2sd %eax, %xmm0
addsd 0x40(%rsp), %xmm0
movsd %xmm0, 0x40(%rsp)
movl 0x2c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x2c(%rsp)
jmp 0xedc9
movsd 0x48(%rsp), %xmm0
cvtsi2sdl 0x68(%rsp), %xmm1
divsd %xmm1, %xmm0
movq 0x58(%rsp), %rax
movsd %xmm0, (%rax)
movsd 0x40(%rsp), %xmm0
cvtsi2sdl 0x68(%rsp), %xmm1
divsd %xmm1, %xmm0
movq 0x58(%rsp), %rax
movsd %xmm0, 0x28(%rax)
movq 0x58(%rsp), %rax
xorps %xmm0, %xmm0
movsd %xmm0, 0x18(%rax)
movl $0x0, 0x4(%rsp)
movl 0x4(%rsp), %eax
cmpl 0x68(%rsp), %eax
jge 0xf1ae
movq 0x58(%rsp), %rax
movsd (%rax), %xmm0
movq 0x30(%rsp), %rax
movslq 0x4(%rsp), %rcx
subsd (%rax,%rcx,8), %xmm0
movaps 0x5f50(%rip), %xmm1     # 0x15030
pand %xmm1, %xmm0
movq 0x58(%rsp), %rax
addsd 0x18(%rax), %xmm0
movsd %xmm0, 0x18(%rax)
cmpl $0x0, 0x4(%rsp)
jne 0xf12e
movq 0x30(%rsp), %rax
movslq 0x4(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
movq 0x58(%rsp), %rax
movsd %xmm0, 0x8(%rax)
movq 0x30(%rsp), %rax
movslq 0x4(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
movq 0x58(%rsp), %rax
movsd %xmm0, 0x10(%rax)
jmp 0xf19c
movq 0x30(%rsp), %rax
movslq 0x4(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm1
movq 0x58(%rsp), %rax
movsd 0x8(%rax), %xmm0
ucomisd %xmm1, %xmm0
jbe 0xf166
movq 0x30(%rsp), %rax
movslq 0x4(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
movq 0x58(%rsp), %rax
movsd %xmm0, 0x8(%rax)
movq 0x30(%rsp), %rax
movslq 0x4(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
movq 0x58(%rsp), %rax
ucomisd 0x10(%rax), %xmm0
jbe 0xf19a
movq 0x30(%rsp), %rax
movslq 0x4(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
movq 0x58(%rsp), %rax
movsd %xmm0, 0x10(%rax)
jmp 0xf19c
jmp 0xf19e
movl 0x4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x4(%rsp)
jmp 0xf0b3
cvtsi2sdl 0x68(%rsp), %xmm1
movq 0x58(%rsp), %rax
movsd 0x18(%rax), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, 0x18(%rax)
movq 0x58(%rsp), %rax
movsd 0x18(%rax), %xmm0
movq 0x58(%rsp), %rax
movsd (%rax), %xmm1
movaps 0x5e4f(%rip), %xmm2     # 0x15030
pand %xmm2, %xmm1
divsd %xmm1, %xmm0
movsd 0x638f(%rip), %xmm1     # 0x15580
mulsd %xmm1, %xmm0
movq 0x58(%rsp), %rax
movsd %xmm0, 0x20(%rax)
movq 0x30(%rsp), %rdi
callq 0xb3f0
addq $0x78, %rsp
retq
nop