// File: /workspace/github_c_makefile_star2_O1/antirez[P]stonky/stonky.c
// Function: botHandleVolatilityRequest
void botHandleVolatilityRequest(botRequest *br, sds symbol, sds *argv, int argc) {
    sds reply = sdsempty();
    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],"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 vol [range <days>]`\n"
                "The default range is one year (253 open market days).");
            goto cleanup;
        } else {
            reply = sdsnew("Syntax error. Try $AAPL vol 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;
    }

    yahooDataToPriceChanges(yd,range);
    range = yd->ts_clen;
    long totdays = yd->pdays + yd->ldays;
    reply = sdscatprintf(reply,
        "%s volatility report:\n"
        "```\n"
        "Reported profits %-3ld times (%.2f%%)\n"
        "Reported loss    %-3ld times (%.2f%%)\n"
        "Average profit   %.2f%%\n"
        "Average loss     %.2f%%\n"
        "Max     profit   %.2f%%\n"
        "Max     loss     %.2f%%"
        "```\n"
        "Data from last %d days (adjusted) range.",
        symbol,
        (long)yd->pdays, (double)yd->pdays/totdays*100,
        (long)yd->ldays, (double)yd->ldays/totdays*100,
        yd->avgp, yd->avgl, yd->maxp, yd->maxl, range);

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 $0x38, %rsp
movl %ecx, %r13d
movq %rdx, %r12
movq %rsi, 0x20(%rsp)
movq %rdi, 0x28(%rsp)
callq 0x5889
movq %rax, 0x10(%rsp)
testl %r13d, %r13d
setg %bl
jle 0xaae7
leal -0x1(%r13), %eax
movl %eax, 0x1c(%rsp)
xorl %r14d, %r14d
movl $0xfd, 0xc(%rsp)
movq %r12, 0x30(%rsp)
movslq %r14d, %rbp
movq (%r12,%rbp,8), %r15
movq %r15, %rdi
leaq 0x4b68(%rip), %rsi      # 0xf5ac
callq 0x22a0
testl %eax, %eax
sete %al
cmpl 0x1c(%rsp), %ebp
movq %r12, %rcx
setne %r12b
andb %al, %r12b
cmpb $0x1, %r12b
jne 0xaa9f
leal 0x1(%rbp), %r14d
movq 0x8(%rcx,%rbp,8), %rdi
callq 0x23d0
cmpl $0x2, %eax
movl $0x1, %ecx
cmovll %ecx, %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
mulsd 0x3c5e(%rip), %xmm0     # 0xe6e8
divsd 0x3c5e(%rip), %xmm0     # 0xe6f0
cvttsd2si %xmm0, %ecx
cmovlel %eax, %ecx
movl %ecx, 0xc(%rsp)
jmp 0xaacc
movq %r15, %rdi
leaq 0x4bba(%rip), %rsi      # 0xf663
callq 0x22a0
testl %eax, %eax
je 0xaabb
leaq 0x4dce(%rip), %rdi      # 0xf887
jmp 0xaac2
leaq 0x4d72(%rip), %rdi      # 0xf834
callq 0x58ac
movq %rax, 0x10(%rsp)
testb %r12b, %r12b
je 0xaaef
incl %r14d
cmpl %r13d, %r14d
setl %bl
movq 0x30(%rsp), %r12
jl 0xaa33
jmp 0xaaef
movl $0xfd, 0xc(%rsp)
testb $0x1, %bl
je 0xab00
xorl %ebx, %ebx
movq 0x10(%rsp), %r14
jmp 0xabdb
leaq 0x4a5b(%rip), %rdx      # 0xf562
leaq 0x3ea0(%rip), %rcx      # 0xe9ae
movl $0x1, %edi
movq 0x20(%rsp), %r15
movq %r15, %rsi
movl $0xe10, %r8d            # imm = 0xE10
callq 0x8b50
movq %rax, %rbx
testq %rax, %rax
movq 0x10(%rsp), %r14
je 0xab3c
movl 0xc(%rsp), %eax
cmpl %eax, 0x78(%rbx)
jge 0xab55
leaq 0x4b25(%rip), %rsi      # 0xf668
movq %r14, %rdi
movq %r15, %rdx
xorl %eax, %eax
callq 0x6581
jmp 0xabd8
movq %rbx, %rdi
movl 0xc(%rsp), %esi
callq 0x9054
movq 0x98(%rbx), %rcx
movq 0xa0(%rbx), %r8
leaq (%r8,%rcx), %rax
xorps %xmm0, %xmm0
cvtsi2sd %rcx, %xmm0
cvtsi2sd %rax, %xmm2
movl 0x90(%rbx), %r9d
divsd %xmm2, %xmm0
movsd 0x3b2d(%rip), %xmm3     # 0xe6c0
mulsd %xmm3, %xmm0
cvtsi2sd %r8, %xmm1
divsd %xmm2, %xmm1
mulsd %xmm3, %xmm1
movsd 0xa8(%rbx), %xmm2
movsd 0xb0(%rbx), %xmm3
movsd 0xb8(%rbx), %xmm4
movsd 0xc0(%rbx), %xmm5
leaq 0x4cdd(%rip), %rsi      # 0xf8a8
movq %r14, %rdi
movq %r15, %rdx
movb $0x6, %al
callq 0x6581
movq %rax, %r14
testq %r14, %r14
je 0xabf3
movq 0x28(%rsp), %rax
movq 0x8(%rax), %rdi
movq %r14, %rsi
xorl %edx, %edx
callq 0x91e5
movq %rbx, %rdi
callq 0x8abe
movq %r14, %rdi
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x591d