// File: /workspace/github_c_makefile_star2_O0/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:
subq $0x78, %rsp
movq %rdi, 0x70(%rsp)
movq %rsi, 0x68(%rsp)
movq %rdx, 0x60(%rsp)
movl %ecx, 0x5c(%rsp)
callq 0x8610
movq %rax, 0x50(%rsp)
movl $0x0, 0x4c(%rsp)
movl $0xfd, 0x48(%rsp)
movq $0x0, 0x40(%rsp)
movl $0x0, 0x3c(%rsp)
movl 0x3c(%rsp), %eax
cmpl 0x5c(%rsp), %eax
jge 0x1032c
movl 0x5c(%rsp), %eax
subl 0x3c(%rsp), %eax
subl $0x1, %eax
movl %eax, 0x38(%rsp)
movq 0x60(%rsp), %rax
movslq 0x3c(%rsp), %rcx
movq (%rax,%rcx,8), %rdi
leaq 0x6171(%rip), %rsi      # 0x16375
callq 0x2270
cmpl $0x0, %eax
jne 0x10249
cmpl $0x0, 0x38(%rsp)
je 0x10249
movq 0x60(%rsp), %rax
movl 0x3c(%rsp), %ecx
addl $0x1, %ecx
movl %ecx, 0x3c(%rsp)
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rdi
callq 0x2380
movl %eax, 0x4c(%rsp)
cmpl $0x0, 0x4c(%rsp)
jg 0x10244
movl $0x1, 0x4c(%rsp)
jmp 0x1031a
movq 0x60(%rsp), %rax
movslq 0x3c(%rsp), %rcx
movq (%rax,%rcx,8), %rdi
leaq 0x611e(%rip), %rsi      # 0x1637c
callq 0x2270
cmpl $0x0, %eax
jne 0x102cd
cmpl $0x0, 0x38(%rsp)
je 0x102cd
movq 0x60(%rsp), %rax
movl 0x3c(%rsp), %ecx
addl $0x1, %ecx
movl %ecx, 0x3c(%rsp)
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rdi
callq 0x2380
movl %eax, 0x48(%rsp)
cmpl $0x0, 0x48(%rsp)
jg 0x1029e
movl $0x1, 0x48(%rsp)
cmpl $0x2, 0x48(%rsp)
jle 0x102cb
cvtsi2sdl 0x48(%rsp), %xmm0
movsd 0x52ed(%rip), %xmm1     # 0x155a0
mulsd %xmm1, %xmm0
movsd 0x52d9(%rip), %xmm1     # 0x15598
divsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, 0x48(%rsp)
jmp 0x10318
movq 0x60(%rsp), %rax
movslq 0x3c(%rsp), %rcx
movq (%rax,%rcx,8), %rdi
leaq 0x6151(%rip), %rsi      # 0x16433
callq 0x2270
cmpl $0x0, %eax
jne 0x10302
leaq 0x608f(%rip), %rdi      # 0x16382
callq 0x8630
movq %rax, 0x50(%rsp)
jmp 0x1043c
leaq 0x610f(%rip), %rdi      # 0x16418
callq 0x8630
movq %rax, 0x50(%rsp)
jmp 0x1043c
jmp 0x1031a
jmp 0x1031c
movl 0x3c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x3c(%rsp)
jmp 0x101d2
movq 0x68(%rsp), %rsi
movl $0x1, %edi
leaq 0x5ff5(%rip), %rdx      # 0x16332
leaq 0x543a(%rip), %rcx      # 0x1577e
movl $0xe10, %r8d            # imm = 0xE10
callq 0xd200
movq %rax, 0x40(%rsp)
cmpq $0x0, 0x40(%rsp)
je 0x1036a
movq 0x40(%rsp), %rax
movl 0x78(%rax), %eax
cmpl 0x48(%rsp), %eax
jge 0x1038c
movq 0x50(%rsp), %rdi
movq 0x68(%rsp), %rdx
leaq 0x60bd(%rip), %rsi      # 0x16438
movb $0x0, %al
callq 0x98c0
movq %rax, 0x50(%rsp)
jmp 0x1043c
movq 0x40(%rsp), %rdi
movl $0xa, %esi
callq 0xec80
cmpl $0x0, %eax
jne 0x103bf
movq 0x50(%rsp), %rdi
movq 0x68(%rsp), %rdx
leaq 0x60ed(%rip), %rsi      # 0x1649e
movb $0x0, %al
callq 0x98c0
movq %rax, 0x50(%rsp)
jmp 0x1043c
movl $0x3e8, 0x34(%rsp)      # imm = 0x3E8
movq 0x40(%rsp), %rdi
movl 0x48(%rsp), %esi
movl 0x34(%rsp), %edx
movl 0x4c(%rsp), %ecx
movq %rsp, %r8
callq 0xed10
movq 0x50(%rsp), %rdi
movq 0x68(%rsp), %rdx
movsd (%rsp), %xmm0
movsd 0x18(%rsp), %xmm1
movsd 0x10(%rsp), %xmm2
movsd 0x8(%rsp), %xmm3
movl 0x34(%rsp), %ecx
movl 0x48(%rsp), %r8d
movl 0x4c(%rsp), %esi
leaq 0x61e4(%rip), %r9       # 0x165f9
leaq 0x61d5(%rip), %rax      # 0x165f1
cmpl $0x0, %esi
cmovneq %rax, %r9
movsd 0x28(%rsp), %xmm4
leaq 0x60ef(%rip), %rsi      # 0x1651f
movb $0x5, %al
callq 0x98c0
movq %rax, 0x50(%rsp)
cmpq $0x0, 0x50(%rsp)
je 0x1045b
movq 0x70(%rsp), %rax
movq 0x8(%rax), %rdi
movq 0x50(%rsp), %rsi
xorl %eax, %eax
movl %eax, %edx
callq 0xdd70
movq 0x40(%rsp), %rdi
callq 0xd130
movq 0x50(%rsp), %rdi
callq 0x8750
addq $0x78, %rsp
retq
nopw %cs:(%rax,%rax)