// File: /workspace/github_c_makefile_star2_O3/antirez[P]stonky/stonky.c
// Function: botHandleChartRequest
void botHandleChartRequest(botRequest *br, sds symbol, sds range) {
    ydata *yd = NULL;
    sds reply = sdsempty();

    /* Select the Yahoo chart API parameters according to the
     * requested interval. */
    /* api_range and api_internal defaults to 1d and 5m respectively */
    char *api_range, *api_interval;
    if (!strcasecmp(range,"1d")) {
        api_range = "1d";
        api_interval = "5m";
    } else if (!strcasecmp(range,"5d")) {
        api_range = "5d";
        api_interval = "1h";
    } else if (!strcasecmp(range,"1m")) {
        api_range = "1mo";
        api_interval = "90m";
    } else if (!strcasecmp(range,"6m")) {
        api_range = "6mo";
        api_interval = "1d";
    } else if (!strcasecmp(range,"1y")) {
        api_range = "1y";
        api_interval = "5d";
    } else if (!strcasecmp(range,"5y")) {
        api_range = "5y";
        api_interval = "1mo";
    } else {
        reply = sdscatprintf(reply,"Invalid chart range. Use 1d|5d|1m|6m|1y|5y");
        goto fmterr;
    }

    yd = getYahooData(YDATA_TS,symbol,api_range,api_interval,3600);
    if (yd == NULL) {
        reply = sdscatprintf(reply,"Can't fetch chart data for '%s'",symbol);
    } else {
        int height = 20;
        int width = 42;
        canvas *canv = createCanvas(width,height+8,0);
        for (int j = 0; j < yd->ts_len; j++) {
            double range = yd->ts_max - yd->ts_min;
            double this = yd->ts_data[j] - yd->ts_min;
            int y = height-(this*height/range)-1;
            int x = (double)j/yd->ts_len*width;
            y += 4;
            if (j > 0) {
                drawLine(canv,x,y,x,height-1+4,1);
            }
        }
        sds graph = renderCanvas(canv);
        freeCanvas(canv);
        reply = sdscatprintf(reply,
                            "%s %s | min %.02f max %.02f\n```",
                            yd->symbol, api_range, yd->ts_min, yd->ts_max);
        reply = sdscatsds(reply,graph);
        reply = sdscat(reply,"```");
        sdsfree(graph);
    }

fmterr:
    freeYahooData(yd);
    botSendMessage(br->target,reply,0);
    sdsfree(reply);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdx, %r12
movq %rsi, %r13
movq %rdi, %rbx
callq 0x56c6
movq %rax, %r14
leaq 0x4753(%rip), %r15      # 0xe98e
movq %r12, %rdi
movq %r15, %rsi
callq 0x22b0
testl %eax, %eax
je 0xa2cf
leaq 0x52aa(%rip), %r15      # 0xf4ff
movq %r12, %rdi
movq %r15, %rsi
callq 0x22b0
testl %eax, %eax
je 0xa2d8
leaq 0x529a(%rip), %rsi      # 0xf505
movq %r12, %rdi
callq 0x22b0
testl %eax, %eax
je 0xa2e1
leaq 0x5292(%rip), %rsi      # 0xf510
movq %r12, %rdi
callq 0x22b0
testl %eax, %eax
je 0xa2f1
leaq 0x5286(%rip), %r15      # 0xf517
movq %r12, %rdi
movq %r15, %rsi
callq 0x22b0
testl %eax, %eax
je 0xa301
leaq 0x529b(%rip), %r15      # 0xf542
movq %r12, %rdi
movq %r15, %rsi
callq 0x22b0
testl %eax, %eax
je 0xa30a
leaq 0x525d(%rip), %rsi      # 0xf51a
xorl %r12d, %r12d
movq %r14, %rdi
xorl %eax, %eax
callq 0x6382
jmp 0xa486
leaq 0x5226(%rip), %rcx      # 0xf4fc
jmp 0xa311
leaq 0x5223(%rip), %rcx      # 0xf502
jmp 0xa311
leaq 0x5220(%rip), %r15      # 0xf508
leaq 0x521d(%rip), %rcx      # 0xf50c
jmp 0xa311
leaq 0x521b(%rip), %r15      # 0xf513
leaq 0x468f(%rip), %rcx      # 0xe98e
jmp 0xa311
leaq 0x51f7(%rip), %rcx      # 0xf4ff
jmp 0xa311
leaq 0x51f7(%rip), %rcx      # 0xf508
movl $0x1, %edi
movq %r13, %rsi
movq %r15, %rdx
movl $0xe10, %r8d            # imm = 0xE10
callq 0x88ec
testq %rax, %rax
je 0xa46f
movq %rax, %r12
movl $0x2a, %edi
movl $0x1c, %esi
xorl %edx, %edx
callq 0x25fc
movq %rax, %r13
movl 0x78(%r12), %eax
testl %eax, %eax
jle 0xa402
xorl %ebp, %ebp
testq %rbp, %rbp
je 0xa3f3
movq 0x80(%r12), %rcx
movss (%rcx,%rbp,4), %xmm0
movss 0x88(%r12), %xmm1
subss %xmm1, %xmm0
cvtss2sd %xmm0, %xmm0
movsd 0x4342(%rip), %xmm3     # 0xe6c8
mulsd %xmm3, %xmm0
movss 0x8c(%r12), %xmm2
subss %xmm1, %xmm2
xorps %xmm1, %xmm1
cvtss2sd %xmm2, %xmm1
divsd %xmm1, %xmm0
movapd %xmm3, %xmm1
subsd %xmm0, %xmm1
addsd 0x4305(%rip), %xmm1     # 0xe6b8
cvttsd2si %xmm1, %edx
addl $0x4, %edx
xorps %xmm0, %xmm0
cvtsi2sd %ebp, %xmm0
xorps %xmm1, %xmm1
cvtsi2sd %eax, %xmm1
divsd %xmm1, %xmm0
mulsd 0x42fc(%rip), %xmm0     # 0xe6d0
cvttsd2si %xmm0, %ecx
movq %r13, %rdi
movl %ecx, %esi
movl $0x17, %r8d
movl $0x1, %r9d
callq 0x26bc
movl 0x78(%r12), %eax
incq %rbp
movslq %eax, %rcx
cmpq %rcx, %rbp
jl 0xa356
movq %r13, %rdi
callq 0x2789
movq %rax, %rbp
movq %r13, %rdi
callq 0x264b
movq 0x8(%r12), %rdx
xorps %xmm0, %xmm0
cvtss2sd 0x88(%r12), %xmm0
xorps %xmm1, %xmm1
cvtss2sd 0x8c(%r12), %xmm1
leaq 0x512a(%rip), %rsi      # 0xf565
movq %r14, %rdi
movq %r15, %rcx
movb $0x2, %al
callq 0x6382
movq %rax, %rdi
movq %rbp, %rsi
callq 0x6018
leaq 0x5646(%rip), %rsi      # 0xfaa0
movq %rax, %rdi
callq 0x5ff1
movq %rax, %r14
movq %rbp, %rdi
callq 0x5775
jmp 0xa489
leaq 0x50cf(%rip), %rsi      # 0xf545
xorl %r12d, %r12d
movq %r14, %rdi
movq %r13, %rdx
xorl %eax, %eax
callq 0x6382
movq %rax, %r14
movq %r12, %rdi
callq 0x885a
movq 0x8(%rbx), %rdi
movq %r14, %rsi
xorl %edx, %edx
callq 0x8f4e
movq %r14, %rdi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x5775