// File: /workspace/github_c_makefile_star2_O3/antirez[P]stonky/stonky.c
// Function: botHandleMontecarloRequest
void botHandleMontecarloRequest(botRequest *br, sds symbol, sds *argv, int argc) {
    sds reply = sdsempty();
    int period = 0;
    int range = 253; /* Markets are open a bit more than 253 days per year. */
    ydata *yd = NULL;

    /* Parse arguments. */
    for (int j = 0; j < argc; j++) {
        int moreargs = argc-j-1;
        if (!strcasecmp(argv[j],"period") && moreargs) {
            period = atoi(argv[++j]);
            if (period <= 0) period = 1;
        } else if (!strcasecmp(argv[j],"range") && moreargs) {
            range = atoi(argv[++j]);
            if (range <= 0) range = 1;
            if (range > 2) {
                /* Adjust for the amount of open market days in 1 year. */
                range = (double)range*5/7.2;
            }
        } else if (!strcasecmp(argv[j],"help")) {
            reply = sdsnew(
                "`$SYMBOL mc [period <days>] [range <days>]`\n"
                "A period of 0 (default) means to use a random period "
                "between buy and sell. The default range is 365 days.");
            goto cleanup;
        } else {
            reply = sdsnew("Syntax error. Try $AAPL mc help");
            goto cleanup;
        }
    }

    /* Fetch the data. Sometimes we'll not obtain enough data points. */
    yd = getYahooData(YDATA_TS,symbol,"5y","1d",3600);
    if (yd == NULL || yd->ts_len < range) {
        reply = sdscatprintf(reply,
            "Can't fetch historical data for '%s', use the range option to "
            "limit the amount of history to analyze.",
                             symbol);
        goto cleanup;
    }

    /* Some other times, there will be too many data points with NULL
     * inside. */
    if (checkYahooTimeSeriesValidity(yd,10) == C_ERR) {
        reply = sdscatprintf(reply,
                    "Historical data for '%s' has more than 10%% of invalid "
                    "data points. Doing the analysis would be unreliable. "
                    "Blame Yahoo Finance.", symbol);
        goto cleanup;
    }

    int count = 1000; /* Number of experiments to perform in the
                         Montecarlo simulation. */

    mcres mc;
    computeMontecarlo(yd,range,count,period,&mc);
    reply = sdscatprintf(reply,
        "Random buying/selling '%s' simulation report:\n"
        "Average gain/loss: %.2f%% (+/-%.2f%%).\n"
        "Best outcome : %.2f%%.\n"
        "Worst outcome: %.2f%%.\n"
        "%d experiments within %d days (adjusted) range using %s "
        "interval of %.2f days.",
        symbol, mc.gain, mc.absdiff, mc.maxgain, mc.mingain, count, range,
        period ? "a fixed" : "an average",
        mc.avgper);

cleanup:
    if (reply) botSendMessage(br->target,reply,0);
    freeYahooData(yd);
    sdsfree(reply);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movl %ecx, %r15d
movq %rdx, %rbx
movq %rsi, 0x20(%rsp)
movq %rdi, 0x18(%rsp)
callq 0x56c6
movq %rax, 0x10(%rsp)
testl %r15d, %r15d
jle 0xa5a8
leal -0x1(%r15), %eax
movl %eax, 0xc(%rsp)
movl $0xfd, %r12d
movl $0x0, 0x8(%rsp)
xorl %r13d, %r13d
movslq %r13d, %rbp
movq (%rbx,%rbp,8), %r14
movq %r14, %rdi
leaq 0x5075(%rip), %rsi      # 0xf585
callq 0x22b0
cmpl 0xc(%rsp), %ebp
je 0xa53d
testl %eax, %eax
jne 0xa53d
incl %r13d
movq 0x8(%rbx,%rbp,8), %rdi
callq 0x23e0
cmpl $0x2, %eax
movl $0x1, %ecx
cmovll %ecx, %eax
movl %eax, 0x8(%rsp)
jmp 0xa59a
movq %r14, %rdi
leaq 0x5045(%rip), %rsi      # 0xf58c
callq 0x22b0
cmpl 0xc(%rsp), %r13d
je 0xa638
testl %eax, %eax
jne 0xa638
incl %r13d
movq 0x8(%rbx,%rbp,8), %rdi
callq 0x23e0
movl %eax, %r12d
cmpl $0x2, %eax
movl $0x1, %eax
cmovll %eax, %r12d
jle 0xa59a
xorps %xmm0, %xmm0
cvtsi2sd %r12d, %xmm0
mulsd 0x414b(%rip), %xmm0     # 0xe6d8
divsd 0x414b(%rip), %xmm0     # 0xe6e0
cvttsd2si %xmm0, %r12d
incl %r13d
cmpl %r15d, %r13d
jl 0xa4ff
jmp 0xa5b6
movl $0x0, 0x8(%rsp)
movl $0xfd, %r12d
leaq 0x4f85(%rip), %rdx      # 0xf542
leaq 0x43ca(%rip), %rcx      # 0xe98e
movl $0x1, %edi
movq 0x20(%rsp), %r14
movq %r14, %rsi
movl $0xe10, %r8d            # imm = 0xE10
callq 0x88ec
movq %rax, %r13
testq %rax, %rax
je 0xa5ed
movl 0x78(%r13), %eax
cmpl %r12d, %eax
jge 0xa60b
leaq 0x5054(%rip), %rsi      # 0xf648
movq 0x10(%rsp), %rdi
movq %r14, %rdx
xorl %eax, %eax
callq 0x6382
movq %rax, %rbx
jmp 0xa702
testl %eax, %eax
jle 0xa65b
movq 0x80(%r13), %rdx
xorl %esi, %esi
xorpd %xmm0, %xmm0
xorl %ecx, %ecx
movss (%rdx,%rsi,4), %xmm1
cmpeqss %xmm0, %xmm1
movd %xmm1, %edi
subl %edi, %ecx
incq %rsi
cmpq %rsi, %rax
jne 0xa61e
jmp 0xa65d
leaq 0x5004(%rip), %rsi      # 0xf643
movq %r14, %rdi
callq 0x22b0
testl %eax, %eax
je 0xa6f0
leaq 0x4fd2(%rip), %rdi      # 0xf628
jmp 0xa6f7
xorl %ecx, %ecx
cltq
imulq $0x66666667, %rax, %rax # imm = 0x66666667
movq %rax, %rdx
shrq $0x3f, %rdx
sarq $0x22, %rax
addl %edx, %eax
cmpl %eax, %ecx
jle 0xa683
leaq 0x5030(%rip), %rsi      # 0xf6ae
jmp 0xa5f4
leaq 0x28(%rsp), %rbx
movq %r13, %rdi
movl %r12d, %esi
movl $0x3e8, %edx            # imm = 0x3E8
movl 0x8(%rsp), %ebp
movl %ebp, %ecx
movq %rbx, %r8
callq 0x98d2
movsd 0x18(%rbx), %xmm1
movsd 0x10(%rbx), %xmm2
movsd (%rbx), %xmm0
movsd 0x8(%rbx), %xmm3
testl %ebp, %ebp
leaq 0x514c(%rip), %rax      # 0xf809
leaq 0x513d(%rip), %r9       # 0xf801
cmoveq %rax, %r9
movsd 0x28(%rbx), %xmm4
leaq 0x505b(%rip), %rsi      # 0xf72f
movq 0x10(%rsp), %rdi
movq %r14, %rdx
movl $0x3e8, %ecx            # imm = 0x3E8
movl %r12d, %r8d
movb $0x5, %al
callq 0x6382
jmp 0xa603
leaq 0x4e9b(%rip), %rdi      # 0xf592
callq 0x56e9
movq %rax, %rbx
xorl %r13d, %r13d
testq %rbx, %rbx
je 0xa71a
movq 0x18(%rsp), %rax
movq 0x8(%rax), %rdi
movq %rbx, %rsi
xorl %edx, %edx
callq 0x8f4e
movq %r13, %rdi
callq 0x885a
movq %rbx, %rdi
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x5775