// File: /workspace/github_c_makefile_star2_O2/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, 0x18(%rsp)
movq %rdi, 0x10(%rsp)
callq 0x4ee9
movq %rax, 0x8(%rsp)
movq %r14, 0x20(%rsp)
leal -0x1(%r14), %eax
movl %eax, 0x4(%rsp)
xorl %r13d, %r13d
movl $0xfd, %ebp
xorl %r12d, %r12d
cmpl 0x20(%rsp), %r13d
jge 0x9214
movslq %r13d, %r15
movq (%rbx,%r15,8), %r14
movq %r14, %rdi
leaq 0x4061(%rip), %rsi      # 0xd1ed
callq 0x22b0
cmpl 0x4(%rsp), %r13d
je 0x91b8
testl %eax, %eax
jne 0x91b8
incl %r13d
movq 0x8(%rbx,%r15,8), %rdi
callq 0x23e0
movl %eax, %r12d
cmpl $0x2, %eax
pushq $0x1
popq %rax
cmovll %eax, %r12d
jmp 0x920c
movq %r14, %rdi
leaq 0x4032(%rip), %rsi      # 0xd1f4
callq 0x22b0
cmpl 0x4(%rsp), %r13d
je 0x9264
testl %eax, %eax
jne 0x9264
incl %r13d
movq 0x8(%rbx,%r15,8), %rdi
callq 0x23e0
movl %eax, %ebp
cmpl $0x2, %eax
pushq $0x1
popq %rax
cmovll %eax, %ebp
jle 0x920c
cvtsi2sd %ebp, %xmm0
mulsd 0x31f0(%rip), %xmm0     # 0xc3f0
divsd 0x31f0(%rip), %xmm0     # 0xc3f8
cvttsd2si %xmm0, %ebp
incl %r13d
jmp 0x9170
leaq 0x3f8f(%rip), %rdx      # 0xd1aa
leaq 0x33d4(%rip), %rcx      # 0xc5f6
pushq $0x1
popq %rdi
movq 0x18(%rsp), %r14
movq %r14, %rsi
movl $0xe10, %r8d            # imm = 0xE10
callq 0x7676
movq %rax, %r13
testq %rax, %rax
je 0x9246
cmpl %ebp, 0x78(%r13)
jge 0x9287
leaq 0x4063(%rip), %rsi      # 0xd2b0
movq 0x8(%rsp), %rdi
movq %r14, %rdx
xorl %eax, %eax
callq 0x5814
movq %rax, %rbx
jmp 0x9316
leaq 0x4040(%rip), %rsi      # 0xd2ab
movq %r14, %rdi
callq 0x22b0
testl %eax, %eax
je 0x9304
leaq 0x400e(%rip), %rdi      # 0xd290
jmp 0x930b
pushq $0xa
popq %rsi
movq %r13, %rdi
callq 0x85ab
testl %eax, %eax
je 0x934c
leaq 0x28(%rsp), %rbx
movq %r13, %rdi
movl %ebp, %esi
movl $0x3e8, %edx            # imm = 0x3E8
movl %r12d, %ecx
movq %rbx, %r8
callq 0x85f2
movsd 0x18(%rbx), %xmm1
movsd 0x10(%rbx), %xmm2
movsd (%rbx), %xmm0
movsd 0x8(%rbx), %xmm3
testl %r12d, %r12d
leaq 0x41a0(%rip), %rax      # 0xd471
leaq 0x4191(%rip), %r9       # 0xd469
cmoveq %rax, %r9
movsd 0x28(%rbx), %xmm4
leaq 0x40af(%rip), %rsi      # 0xd397
movq 0x8(%rsp), %rdi
movq %r14, %rdx
movl $0x3e8, %ecx            # imm = 0x3E8
movl %ebp, %r8d
movb $0x5, %al
callq 0x5814
jmp 0x925c
leaq 0x3eef(%rip), %rdi      # 0xd1fa
callq 0x4ef7
movq %rax, %rbx
xorl %r13d, %r13d
testq %rbx, %rbx
je 0x932e
movq 0x10(%rsp), %rax
movq 0x8(%rax), %rdi
movq %rbx, %rsi
xorl %edx, %edx
callq 0x7cae
movq %r13, %rdi
callq 0x75e4
movq %rbx, %rdi
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x4f65
leaq 0x3fc3(%rip), %rsi      # 0xd316
jmp 0x924d