// File: /workspace/github_c_makefile_star2_O1/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, %r14d
movq %rdx, %rbx
movq %rsi, 0x10(%rsp)
movq %rdi, 0x18(%rsp)
callq 0x5889
movq %rax, 0x8(%rsp)
testl %r14d, %r14d
setg %r15b
jle 0xa878
movq %r14, 0x20(%rsp)
leal -0x1(%r14), %ebp
movl $0xfd, (%rsp)
movl $0x0, 0x4(%rsp)
xorl %r13d, %r13d
movslq %r13d, %r12
movq (%rbx,%r12,8), %r14
movq %r14, %rdi
leaq 0x4df9(%rip), %rsi      # 0xf5a5
callq 0x22a0
cmpl %ebp, %r12d
je 0xa7dd
testl %eax, %eax
jne 0xa7dd
incl %r13d
movq 0x8(%rbx,%r12,8), %rdi
callq 0x23d0
cmpl $0x2, %eax
movl $0x1, %ecx
cmovll %ecx, %eax
movl %eax, 0x4(%rsp)
movb $0x1, %cl
jmp 0xa860
movq %r14, %rdi
leaq 0x4dc5(%rip), %rsi      # 0xf5ac
callq 0x22a0
cmpl %ebp, %r13d
je 0xa831
testl %eax, %eax
jne 0xa831
incl %r13d
movq 0x8(%rbx,%r12,8), %rdi
callq 0x23d0
movb $0x1, %cl
cmpl $0x2, %eax
movl $0x1, %edx
cmovll %edx, %eax
jle 0xa82c
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
mulsd 0x3ec8(%rip), %xmm0     # 0xe6e8
divsd 0x3ec8(%rip), %xmm0     # 0xe6f0
cvttsd2si %xmm0, %eax
movl %eax, (%rsp)
jmp 0xa860
movq %r14, %rdi
leaq 0x4e28(%rip), %rsi      # 0xf663
callq 0x22a0
testl %eax, %eax
je 0xa84d
leaq 0x4dfd(%rip), %rdi      # 0xf648
jmp 0xa854
leaq 0x4d5e(%rip), %rdi      # 0xf5b2
callq 0x58ac
movq %rax, 0x8(%rsp)
xorl %ecx, %ecx
testb %cl, %cl
je 0xa887
incl %r13d
cmpl 0x20(%rsp), %r13d
setl %r15b
jl 0xa79b
jmp 0xa887
movl $0x0, 0x4(%rsp)
movl $0xfd, (%rsp)
testb $0x1, %r15b
je 0xa896
xorl %ebx, %ebx
movq 0x8(%rsp), %r14
jmp 0xa8ea
leaq 0x4cc5(%rip), %rdx      # 0xf562
leaq 0x410a(%rip), %rcx      # 0xe9ae
movl $0x1, %edi
movq 0x10(%rsp), %r12
movq %r12, %rsi
movl $0xe10, %r8d            # imm = 0xE10
callq 0x8b50
movq %rax, %rbx
testq %rax, %rax
movq 0x8(%rsp), %r15
movl (%rsp), %ebp
je 0xa8d3
movl 0x78(%rbx), %eax
cmpl %ebp, %eax
jge 0xa920
leaq 0x4d8e(%rip), %rsi      # 0xf668
movq %r15, %rdi
movq %r12, %rdx
xorl %eax, %eax
callq 0x6581
movq %rax, %r14
testq %r14, %r14
je 0xa902
movq 0x18(%rsp), %rax
movq 0x8(%rax), %rdi
movq %r14, %rsi
xorl %edx, %edx
callq 0x91e5
movq %rbx, %rdi
callq 0x8abe
movq %r14, %rdi
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x591d
testl %eax, %eax
jle 0xa94d
movq 0x80(%rbx), %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 0xa933
jmp 0xa94f
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 0xa975
leaq 0x4d5e(%rip), %rsi      # 0xf6ce
jmp 0xa8da
leaq 0x28(%rsp), %r14
movq %rbx, %rdi
movl %ebp, %esi
movl $0x3e8, %edx            # imm = 0x3E8
movl 0x4(%rsp), %r13d
movl %r13d, %ecx
movq %r14, %r8
callq 0x9b75
movsd 0x18(%r14), %xmm1
movsd 0x10(%r14), %xmm2
movsd (%r14), %xmm0
movsd 0x8(%r14), %xmm3
testl %r13d, %r13d
leaq 0x4e74(%rip), %rax      # 0xf829
leaq 0x4e65(%rip), %r9       # 0xf821
cmoveq %rax, %r9
movsd 0x28(%r14), %xmm4
leaq 0x4d82(%rip), %rsi      # 0xf74f
movq %r15, %rdi
movq %r12, %rdx
movl $0x3e8, %ecx            # imm = 0x3E8
movl %ebp, %r8d
movb $0x5, %al
callq 0x6581
jmp 0xa8e7