// File: /workspace/github_c_makefile_star2_O0/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:
subq $0x78, %rsp
movq %rdi, 0x70(%rsp)
movq %rsi, 0x68(%rsp)
movq %rdx, 0x60(%rsp)
movq $0x0, 0x58(%rsp)
callq 0x8610
movq %rax, 0x50(%rsp)
movq 0x60(%rsp), %rdi
leaq 0x598c(%rip), %rsi      # 0x1577e
callq 0x2270
cmpl $0x0, %eax
jne 0xfe19
leaq 0x597b(%rip), %rax      # 0x1577e
movq %rax, 0x48(%rsp)
leaq 0x64dd(%rip), %rax      # 0x162ec
movq %rax, 0x40(%rsp)
jmp 0xff39
movq 0x60(%rsp), %rdi
leaq 0x64ca(%rip), %rsi      # 0x162ef
callq 0x2270
cmpl $0x0, %eax
jne 0xfe4c
leaq 0x64b9(%rip), %rax      # 0x162ef
movq %rax, 0x48(%rsp)
leaq 0x64b0(%rip), %rax      # 0x162f2
movq %rax, 0x40(%rsp)
jmp 0xff37
movq 0x60(%rsp), %rdi
leaq 0x649d(%rip), %rsi      # 0x162f5
callq 0x2270
cmpl $0x0, %eax
jne 0xfe7f
leaq 0x648f(%rip), %rax      # 0x162f8
movq %rax, 0x48(%rsp)
leaq 0x6487(%rip), %rax      # 0x162fc
movq %rax, 0x40(%rsp)
jmp 0xff35
movq 0x60(%rsp), %rdi
leaq 0x6475(%rip), %rsi      # 0x16300
callq 0x2270
cmpl $0x0, %eax
jne 0xfeb2
leaq 0x6467(%rip), %rax      # 0x16303
movq %rax, 0x48(%rsp)
leaq 0x58d6(%rip), %rax      # 0x1577e
movq %rax, 0x40(%rsp)
jmp 0xff33
movq 0x60(%rsp), %rdi
leaq 0x6449(%rip), %rsi      # 0x16307
callq 0x2270
cmpl $0x0, %eax
jne 0xfee2
leaq 0x6438(%rip), %rax      # 0x16307
movq %rax, 0x48(%rsp)
leaq 0x6414(%rip), %rax      # 0x162ef
movq %rax, 0x40(%rsp)
jmp 0xff31
movq 0x60(%rsp), %rdi
leaq 0x6444(%rip), %rsi      # 0x16332
callq 0x2270
cmpl $0x0, %eax
jne 0xff12
leaq 0x6433(%rip), %rax      # 0x16332
movq %rax, 0x48(%rsp)
leaq 0x63ed(%rip), %rax      # 0x162f8
movq %rax, 0x40(%rsp)
jmp 0xff2f
movq 0x50(%rsp), %rdi
leaq 0x63ec(%rip), %rsi      # 0x1630a
movb $0x0, %al
callq 0x98c0
movq %rax, 0x50(%rsp)
jmp 0x10157
jmp 0xff31
jmp 0xff33
jmp 0xff35
jmp 0xff37
jmp 0xff39
movq 0x68(%rsp), %rsi
movq 0x48(%rsp), %rdx
movq 0x40(%rsp), %rcx
movl $0x1, %edi
movl $0xe10, %r8d            # imm = 0xE10
callq 0xd200
movq %rax, 0x58(%rsp)
cmpq $0x0, 0x58(%rsp)
jne 0xff87
movq 0x50(%rsp), %rdi
movq 0x68(%rsp), %rdx
leaq 0x63bf(%rip), %rsi      # 0x16335
movb $0x0, %al
callq 0x98c0
movq %rax, 0x50(%rsp)
jmp 0x10155
movl $0x14, 0x3c(%rsp)
movl $0x2a, 0x38(%rsp)
movl 0x38(%rsp), %edi
movl 0x3c(%rsp), %esi
addl $0x8, %esi
xorl %edx, %edx
callq 0x2530
movq %rax, 0x30(%rsp)
movl $0x0, 0x2c(%rsp)
movl 0x2c(%rsp), %eax
movq 0x58(%rsp), %rcx
cmpl 0x78(%rcx), %eax
jge 0x100c0
movq 0x58(%rsp), %rax
movss 0x8c(%rax), %xmm0
movq 0x58(%rsp), %rax
subss 0x88(%rax), %xmm0
cvtss2sd %xmm0, %xmm0
movsd %xmm0, 0x20(%rsp)
movq 0x58(%rsp), %rax
movq 0x80(%rax), %rax
movslq 0x2c(%rsp), %rcx
movss (%rax,%rcx,4), %xmm0
movq 0x58(%rsp), %rax
subss 0x88(%rax), %xmm0
cvtss2sd %xmm0, %xmm0
movsd %xmm0, 0x18(%rsp)
cvtsi2sdl 0x3c(%rsp), %xmm0
movsd 0x18(%rsp), %xmm1
cvtsi2sdl 0x3c(%rsp), %xmm2
mulsd %xmm2, %xmm1
divsd 0x20(%rsp), %xmm1
subsd %xmm1, %xmm0
movsd 0x5547(%rip), %xmm1     # 0x15588
subsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, 0x14(%rsp)
cvtsi2sdl 0x2c(%rsp), %xmm0
movq 0x58(%rsp), %rax
cvtsi2sdl 0x78(%rax), %xmm1
divsd %xmm1, %xmm0
cvtsi2sdl 0x38(%rsp), %xmm1
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, 0x10(%rsp)
movl 0x14(%rsp), %eax
addl $0x4, %eax
movl %eax, 0x14(%rsp)
cmpl $0x0, 0x2c(%rsp)
jle 0x100ae
movq 0x30(%rsp), %rdi
movl 0x10(%rsp), %esi
movl 0x14(%rsp), %edx
movl 0x10(%rsp), %ecx
movl 0x3c(%rsp), %r8d
subl $0x1, %r8d
addl $0x4, %r8d
movl $0x1, %r9d
callq 0x26b0
jmp 0x100b0
movl 0x2c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x2c(%rsp)
jmp 0xffb6
movq 0x30(%rsp), %rdi
callq 0x27d0
movq %rax, 0x8(%rsp)
movq 0x30(%rsp), %rdi
callq 0x25b0
movq 0x50(%rsp), %rdi
movq 0x58(%rsp), %rax
movq 0x8(%rax), %rdx
movq 0x48(%rsp), %rcx
movq 0x58(%rsp), %rax
movss 0x88(%rax), %xmm0
cvtss2sd %xmm0, %xmm0
movq 0x58(%rsp), %rax
movss 0x8c(%rax), %xmm1
cvtss2sd %xmm1, %xmm1
leaq 0x6240(%rip), %rsi      # 0x16355
movb $0x2, %al
callq 0x98c0
movq %rax, 0x50(%rsp)
movq 0x50(%rsp), %rdi
movq 0x8(%rsp), %rsi
callq 0x93a0
movq %rax, 0x50(%rsp)
movq 0x50(%rsp), %rdi
leaq 0x674f(%rip), %rsi      # 0x16890
callq 0x9350
movq %rax, 0x50(%rsp)
movq 0x8(%rsp), %rdi
callq 0x8750
jmp 0x10157
movq 0x58(%rsp), %rdi
callq 0xd130
movq 0x70(%rsp), %rax
movq 0x8(%rax), %rdi
movq 0x50(%rsp), %rsi
xorl %eax, %eax
movl %eax, %edx
callq 0xdd70
movq 0x50(%rsp), %rdi
callq 0x8750
addq $0x78, %rsp
retq
nopw (%rax,%rax)