// File: /workspace/github_c_makefile_star2_O2/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
subq $0x18, %rsp
movq %rdx, %r12
movq %rsi, %r13
movq %rdi, %r14
callq 0x4ee9
movq %rax, %rbx
leaq 0x3734(%rip), %r15      # 0xc5f6
movq %r12, %rdi
movq %r15, %rsi
callq 0x22b0
testl %eax, %eax
je 0x8f56
leaq 0x428b(%rip), %r15      # 0xd167
movq %r12, %rdi
movq %r15, %rsi
callq 0x22b0
testl %eax, %eax
je 0x8f5f
leaq 0x427b(%rip), %rsi      # 0xd16d
movq %r12, %rdi
callq 0x22b0
testl %eax, %eax
je 0x8f68
leaq 0x4273(%rip), %rsi      # 0xd178
movq %r12, %rdi
callq 0x22b0
testl %eax, %eax
je 0x8f78
leaq 0x4267(%rip), %r15      # 0xd17f
movq %r12, %rdi
movq %r15, %rsi
callq 0x22b0
testl %eax, %eax
je 0x8f88
leaq 0x427c(%rip), %r15      # 0xd1aa
movq %r12, %rdi
movq %r15, %rsi
callq 0x22b0
testl %eax, %eax
je 0x8f91
leaq 0x423e(%rip), %rsi      # 0xd182
xorl %r12d, %r12d
movq %rbx, %rdi
xorl %eax, %eax
callq 0x5814
jmp 0x9101
leaq 0x4207(%rip), %rcx      # 0xd164
jmp 0x8f98
leaq 0x4204(%rip), %rcx      # 0xd16a
jmp 0x8f98
leaq 0x4201(%rip), %r15      # 0xd170
leaq 0x41fe(%rip), %rcx      # 0xd174
jmp 0x8f98
leaq 0x41fc(%rip), %r15      # 0xd17b
leaq 0x3670(%rip), %rcx      # 0xc5f6
jmp 0x8f98
leaq 0x41d8(%rip), %rcx      # 0xd167
jmp 0x8f98
leaq 0x41d8(%rip), %rcx      # 0xd170
pushq $0x1
popq %rdi
movq %r13, %rsi
movq %r15, %rdx
movl $0xe10, %r8d            # imm = 0xE10
callq 0x7676
testq %rax, %rax
je 0x90ea
movq %rax, %r12
movq %rbx, 0x8(%rsp)
movq %r14, 0x10(%rsp)
pushq $0x2a
popq %rdi
pushq $0x1c
popq %rsi
xorl %r14d, %r14d
xorl %edx, %edx
callq 0x257c
movq %rax, %r13
pushq $0x17
popq %rbp
pushq $0x1
popq %rbx
movslq 0x78(%r12), %rax
cmpq %rax, %r14
jge 0x907c
testq %r14, %r14
je 0x9074
movq 0x80(%r12), %rcx
movss (%rcx,%r14,4), %xmm0
movss 0x88(%r12), %xmm1
subss %xmm1, %xmm0
cvtss2sd %xmm0, %xmm0
movsd 0x33c6(%rip), %xmm3     # 0xc3e0
mulsd %xmm3, %xmm0
movss 0x8c(%r12), %xmm2
subss %xmm1, %xmm2
cvtss2sd %xmm2, %xmm1
divsd %xmm1, %xmm0
movapd %xmm3, %xmm1
subsd %xmm0, %xmm1
addsd 0x338c(%rip), %xmm1     # 0xc3d0
cvttsd2si %xmm1, %edx
addl $0x4, %edx
cvtsi2sd %r14d, %xmm0
cvtsi2sd %eax, %xmm1
divsd %xmm1, %xmm0
mulsd 0x3388(%rip), %xmm0     # 0xc3e8
cvttsd2si %xmm0, %ecx
movq %r13, %rdi
movl %ecx, %esi
movl %ebp, %r8d
movl %ebx, %r9d
callq 0x263a
incq %r14
jmp 0x8fdb
movq %r13, %rdi
callq 0x271a
movq %rax, %rbp
movq %r13, %rdi
callq 0x25c9
movq 0x8(%r12), %rdx
cvtss2sd 0x88(%r12), %xmm0
cvtss2sd 0x8c(%r12), %xmm1
leaq 0x411e(%rip), %rsi      # 0xd1cd
movq 0x8(%rsp), %rdi
movq %r15, %rcx
movb $0x2, %al
callq 0x5814
movq %rax, %rdi
movq %rbp, %rsi
callq 0x55ba
leaq 0x4638(%rip), %rsi      # 0xd708
movq %rax, %rdi
callq 0x5593
movq %rax, %rbx
movq %rbp, %rdi
callq 0x4f65
movq 0x10(%rsp), %r14
jmp 0x9104
leaq 0x40bc(%rip), %rsi      # 0xd1ad
xorl %r12d, %r12d
movq %rbx, %rdi
movq %r13, %rdx
xorl %eax, %eax
callq 0x5814
movq %rax, %rbx
movq %r12, %rdi
callq 0x75e4
movq 0x8(%r14), %rdi
movq %rbx, %rsi
xorl %edx, %edx
callq 0x7cae
movq %rbx, %rdi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x4f65