// File: /workspace/github_c_makefile_star2_O1/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 0x5889
movq %rax, %r14
leaq 0x44da(%rip), %r15      # 0xe9ae
movq %r12, %rdi
movq %r15, %rsi
callq 0x22a0
testl %eax, %eax
je 0xa568
leaq 0x5031(%rip), %r15      # 0xf51f
movq %r12, %rdi
movq %r15, %rsi
callq 0x22a0
testl %eax, %eax
je 0xa571
leaq 0x5021(%rip), %rsi      # 0xf525
movq %r12, %rdi
callq 0x22a0
testl %eax, %eax
je 0xa57a
leaq 0x5019(%rip), %rsi      # 0xf530
movq %r12, %rdi
callq 0x22a0
testl %eax, %eax
je 0xa58a
leaq 0x500d(%rip), %r15      # 0xf537
movq %r12, %rdi
movq %r15, %rsi
callq 0x22a0
testl %eax, %eax
je 0xa59a
leaq 0x5022(%rip), %r15      # 0xf562
movq %r12, %rdi
movq %r15, %rsi
callq 0x22a0
testl %eax, %eax
je 0xa5a3
leaq 0x4fe4(%rip), %rsi      # 0xf53a
xorl %r12d, %r12d
movq %r14, %rdi
xorl %eax, %eax
callq 0x6581
jmp 0xa71c
leaq 0x4fad(%rip), %rcx      # 0xf51c
jmp 0xa5aa
leaq 0x4faa(%rip), %rcx      # 0xf522
jmp 0xa5aa
leaq 0x4fa7(%rip), %r15      # 0xf528
leaq 0x4fa4(%rip), %rcx      # 0xf52c
jmp 0xa5aa
leaq 0x4fa2(%rip), %r15      # 0xf533
leaq 0x4416(%rip), %rcx      # 0xe9ae
jmp 0xa5aa
leaq 0x4f7e(%rip), %rcx      # 0xf51f
jmp 0xa5aa
leaq 0x4f7e(%rip), %rcx      # 0xf528
movl $0x1, %edi
movq %r13, %rsi
movq %r15, %rdx
movl $0xe10, %r8d            # imm = 0xE10
callq 0x8b50
testq %rax, %rax
je 0xa705
movq %rax, %r12
movl $0x2a, %edi
movl $0x1c, %esi
xorl %edx, %edx
callq 0x25ec
movq %rax, %r13
movl 0x78(%r12), %eax
testl %eax, %eax
jle 0xa698
xorl %ebp, %ebp
testq %rbp, %rbp
je 0xa687
movq 0x80(%r12), %rcx
movss (%rcx,%rbp,4), %xmm0
movss 0x88(%r12), %xmm1
subss %xmm1, %xmm0
cvtss2sd %xmm0, %xmm0
movsd 0x40b9(%rip), %xmm3     # 0xe6d8
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 0x407c(%rip), %xmm1     # 0xe6c8
cvttsd2si %xmm1, %edx
addl $0x4, %edx
xorps %xmm0, %xmm0
cvtsi2sd %ebp, %xmm0
xorps %xmm1, %xmm1
cvtsi2sd %eax, %xmm1
divsd %xmm1, %xmm0
mulsd 0x4073(%rip), %xmm0     # 0xe6e0
cvttsd2si %xmm0, %ecx
movq %r13, %rdi
movl %ecx, %esi
movl $0x17, %r8d
movl $0x1, %r9d
callq 0x26ac
incq %rbp
movslq 0x78(%r12), %rax
cmpq %rax, %rbp
jl 0xa5ef
movq %r13, %rdi
callq 0x2779
movq %rax, %rbp
movq %r13, %rdi
callq 0x263b
movq 0x8(%r12), %rdx
xorps %xmm0, %xmm0
cvtss2sd 0x88(%r12), %xmm0
xorps %xmm1, %xmm1
cvtss2sd 0x8c(%r12), %xmm1
leaq 0x4eb4(%rip), %rsi      # 0xf585
movq %r14, %rdi
movq %r15, %rcx
movb $0x2, %al
callq 0x6581
movq %rax, %rdi
movq %rbp, %rsi
callq 0x6217
leaq 0x53d0(%rip), %rsi      # 0xfac0
movq %rax, %rdi
callq 0x61f0
movq %rax, %r14
movq %rbp, %rdi
callq 0x591d
jmp 0xa71f
leaq 0x4e59(%rip), %rsi      # 0xf565
xorl %r12d, %r12d
movq %r14, %rdi
movq %r13, %rdx
xorl %eax, %eax
callq 0x6581
movq %rax, %r14
movq %r12, %rdi
callq 0x8abe
movq 0x8(%rbx), %rdi
movq %r14, %rsi
xorl %edx, %edx
callq 0x91e5
movq %r14, %rdi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x591d