// File: /workspace/github_c_makefile_star2_O3/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 $0x18, %rsp
movl %ecx, %r13d
movq %rdx, %r12
movq %rsi, %r14
movq %rdi, %rbx
callq 0x56c6
movq %rax, %r15
testl %r13d, %r13d
jle 0xa7f3
movq %r15, 0x8(%rsp)
movq %r14, 0x10(%rsp)
movq %rbx, (%rsp)
leal -0x1(%r13), %r14d
xorl %r15d, %r15d
leaq 0x4e0d(%rip), %rbp      # 0xf58c
movq (%r12,%r15,8), %rbx
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x22b0
cmpq %r15, %r14
je 0xa841
testl %eax, %eax
jne 0xa841
addq $0x2, %r15
cmpl %r13d, %r15d
jl 0xa77f
addl $-0x2, %r15d
shlq $0x3, %r15
orq $0x8, %r15
movq (%r12,%r15), %rdi
callq 0x23e0
cmpl $0x2, %eax
movl $0x1, %ecx
cmovgel %eax, %ecx
cvtsi2sd %ecx, %xmm0
mulsd 0x3f04(%rip), %xmm0     # 0xe6d8
divsd 0x3f04(%rip), %xmm0     # 0xe6e0
cvttsd2si %xmm0, %ebp
cmovlel %ecx, %ebp
movq (%rsp), %rbx
movq 0x10(%rsp), %r14
movq 0x8(%rsp), %r15
jmp 0xa7f8
movl $0xfd, %ebp
leaq 0x4d43(%rip), %rdx      # 0xf542
leaq 0x4188(%rip), %rcx      # 0xe98e
movl $0x1, %edi
movq %r14, %rsi
movl $0xe10, %r8d            # imm = 0xE10
callq 0x88ec
movq %rax, %r12
testq %rax, %rax
je 0xa828
cmpl %ebp, 0x78(%r12)
jge 0xa864
leaq 0x4e19(%rip), %rsi      # 0xf648
movq %r15, %rdi
movq %r14, %rdx
xorl %eax, %eax
callq 0x6382
jmp 0xa8f0
leaq 0x4dfb(%rip), %rsi      # 0xf643
movq %rbx, %rdi
callq 0x22b0
testl %eax, %eax
je 0xa8f5
leaq 0x5008(%rip), %rdi      # 0xf867
jmp 0xa8fc
movq %r12, %rdi
movl %ebp, %esi
callq 0x8dc0
movq 0x98(%r12), %rcx
movq 0xa0(%r12), %r8
leaq (%r8,%rcx), %rax
xorps %xmm0, %xmm0
cvtsi2sd %rcx, %xmm0
cvtsi2sd %rax, %xmm2
movl 0x90(%r12), %r9d
divsd %xmm2, %xmm0
movsd 0x3e0d(%rip), %xmm3     # 0xe6b0
mulsd %xmm3, %xmm0
cvtsi2sd %r8, %xmm1
divsd %xmm2, %xmm1
mulsd %xmm3, %xmm1
movsd 0xa8(%r12), %xmm2
movsd 0xb0(%r12), %xmm3
movsd 0xb8(%r12), %xmm4
movsd 0xc0(%r12), %xmm5
leaq 0x4fa5(%rip), %rsi      # 0xf888
movq %r15, %rdi
movq %r14, %rdx
movb $0x6, %al
callq 0x6382
movq %rax, %r14
jmp 0xa90b
leaq 0x4f18(%rip), %rdi      # 0xf814
callq 0x56e9
movq %rax, %r14
xorl %r12d, %r12d
movq (%rsp), %rbx
testq %r14, %r14
je 0xa91e
movq 0x8(%rbx), %rdi
movq %r14, %rsi
xorl %edx, %edx
callq 0x8f4e
movq %r12, %rdi
callq 0x885a
movq %r14, %rdi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x5775