// File: /workspace/github_c_makefile_star2_O0/antirez[P]stonky/stonky.c
// Function: botHandleShowProfitLossRequest
void botHandleShowProfitLossRequest(botRequest *br, int argc, sds *argv) {
    /* Remove the final "??" from the list name. */
    sds listname = sdsnewlen(argv[0],sdslen(argv[0])-2);
    sds pattern = argc > 1 ? argv[1] : NULL;
    sds reply = NULL;
    int64_t listid = dbGetListID(listname,0);
    if (listid == 0) {
        reply = sdsnew("No such list");
        goto cleanup;
    }

    sqlRow row;
    if (sqlSelect(&row,"SELECT rowid,* FROM ProfitLoss WHERE listid=?i "
                       "ORDER BY selltime DESC", listid) != SQLITE_ROW)
    {
        reply = sdsnew("No sells history for this portfolio");
        goto cleanup;
    }

    /* Build the reply with the history of sells. */
    double total = 0;
    reply = sdsnew("```\n");
    while(sqlNextRow(&row)) {
        const char *symbol = row.col[1].s;

        /* Filter for pattern if any. */
        if (pattern) {
            if (!strmatch(pattern,sdslen(pattern),symbol,strlen(symbol),1))
                continue;
        }

        int64_t id = row.col[0].i;
        time_t selltime = row.col[3].i;
        int quantity = row.col[4].i;
        double buyprice = row.col[5].d;
        double sellprice = row.col[6].d;
        double diff = (sellprice-buyprice)*quantity;
        double diffperc = ((sellprice/buyprice)-1)*100;
        const char *csym = row.col[7].s;
        sds ago = sdsTimeAgo(selltime);

        /* One symbol for every 10% of change. */
        double aux = fabs(diffperc);
        sds emoji = sdsempty();
        do {
            emoji = sdscat(emoji, (diffperc >= 0) ? "🍀":"🍄");
            aux -= 10;
        } while(aux >= 10);

        reply = sdscatprintf(reply,
            "[%lld] %-7s: %d sold at %.2f%s (P/L %s%.2f %s%.2f%% %s), %s ago\n",
            (long long)id,
            symbol, quantity, sellprice*quantity, csym,
            (diff >= 0) ? "+" : "", diff,
            (diffperc >= 0) ? "+" : "", diffperc,
            emoji, ago);
        sdsfree(ago);
        sdsfree(emoji);

        /* Calculate the total P/L of the portfolio. */
        if (csym[0] == '$')
            total += diff;
        else
            total += diff * EURUSD;
    }
    reply = sdscatprintf(reply,"Portfolio performance: %s%.2f USD\n",
                         total > 0 ? "+" : "", total);
    reply = sdscat(reply,"```");

cleanup:
    if (reply) botSendMessage(br->target,reply,0);
    sdsfree(reply);
    sdsfree(listname);
}

// Assembly:
pushq %rbx
subq $0x100, %rsp            # imm = 0x100
movq %rdi, 0xf8(%rsp)
movl %esi, 0xf4(%rsp)
movq %rdx, 0xe8(%rsp)
movq 0xe8(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x40(%rsp)
movq 0xe8(%rsp), %rax
movq (%rax), %rdi
callq 0xfa70
movq 0x40(%rsp), %rdi
movq %rax, %rsi
subq $0x2, %rsi
callq 0x8300
movq %rax, 0xe0(%rsp)
cmpl $0x1, 0xf4(%rsp)
jle 0x11ff5
movq 0xe8(%rsp), %rax
movq 0x8(%rax), %rax
movq %rax, 0x38(%rsp)
jmp 0x11ffe
xorl %eax, %eax
movq %rax, 0x38(%rsp)
jmp 0x11ffe
movq 0x38(%rsp), %rax
movq %rax, 0xd8(%rsp)
movq $0x0, 0xd0(%rsp)
movq 0xe0(%rsp), %rdi
xorl %esi, %esi
callq 0xe0d0
movq %rax, 0xc8(%rsp)
cmpq $0x0, 0xc8(%rsp)
jne 0x12052
leaq 0x4b89(%rip), %rdi      # 0x16bc9
callq 0x8630
movq %rax, 0xd0(%rsp)
jmp 0x12421
movq 0xc8(%rsp), %rdx
leaq 0xb0(%rsp), %rdi
leaq 0x4b6d(%rip), %rsi      # 0x16bd6
movb $0x0, %al
callq 0xca80
cmpl $0x64, %eax
je 0x1208e
leaq 0x4ba0(%rip), %rdi      # 0x16c1c
callq 0x8630
movq %rax, 0xd0(%rsp)
jmp 0x12421
xorps %xmm0, %xmm0
movsd %xmm0, 0xa8(%rsp)
leaq 0x54a3(%rip), %rdi      # 0x17544
callq 0x8630
movq %rax, 0xd0(%rsp)
leaq 0xb0(%rsp), %rdi
callq 0xc5b0
cmpl $0x0, %eax
je 0x123bc
movq 0xc0(%rsp), %rax
movq 0x30(%rax), %rax
movq %rax, 0xa0(%rsp)
cmpq $0x0, 0xd8(%rsp)
je 0x12142
movq 0xd8(%rsp), %rax
movq %rax, 0x20(%rsp)
movq 0xd8(%rsp), %rdi
callq 0xfa70
movl %eax, 0x2c(%rsp)
movq 0xa0(%rsp), %rax
movq %rax, 0x30(%rsp)
movq 0xa0(%rsp), %rdi
callq 0x20e0
movq 0x20(%rsp), %rdi
movl 0x2c(%rsp), %esi
movq 0x30(%rsp), %rdx
movl %eax, %ecx
movl $0x1, %r8d
callq 0xb400
cmpl $0x0, %eax
jne 0x12140
jmp 0x120ae
jmp 0x12142
movq 0xc0(%rsp), %rax
movq 0x8(%rax), %rax
movq %rax, 0x98(%rsp)
movq 0xc0(%rsp), %rax
movq 0x68(%rax), %rax
movq %rax, 0x90(%rsp)
movq 0xc0(%rsp), %rax
movq 0x88(%rax), %rax
movl %eax, 0x8c(%rsp)
movq 0xc0(%rsp), %rax
movsd 0xb8(%rax), %xmm0
movsd %xmm0, 0x80(%rsp)
movq 0xc0(%rsp), %rax
movsd 0xd8(%rax), %xmm0
movsd %xmm0, 0x78(%rsp)
movsd 0x78(%rsp), %xmm0
subsd 0x80(%rsp), %xmm0
cvtsi2sdl 0x8c(%rsp), %xmm1
mulsd %xmm1, %xmm0
movsd %xmm0, 0x70(%rsp)
movsd 0x78(%rsp), %xmm0
divsd 0x80(%rsp), %xmm0
movsd 0x33a0(%rip), %xmm1     # 0x15588
subsd %xmm1, %xmm0
movsd 0x338c(%rip), %xmm1     # 0x15580
mulsd %xmm1, %xmm0
movsd %xmm0, 0x68(%rsp)
movq 0xc0(%rsp), %rax
movq 0xf0(%rax), %rax
movq %rax, 0x60(%rsp)
movq 0x90(%rsp), %rdi
callq 0x11d70
movq %rax, 0x58(%rsp)
movsd 0x68(%rsp), %xmm0
movaps 0x2dff(%rip), %xmm1     # 0x15030
pand %xmm1, %xmm0
movsd %xmm0, 0x50(%rsp)
callq 0x8610
movq %rax, 0x48(%rsp)
movq 0x48(%rsp), %rdi
movsd 0x68(%rsp), %xmm0
xorps %xmm1, %xmm1
leaq 0x49eb(%rip), %rsi      # 0x16c45
leaq 0x49df(%rip), %rax      # 0x16c40
ucomisd %xmm1, %xmm0
cmovaeq %rax, %rsi
callq 0x9350
movq %rax, 0x48(%rsp)
movsd 0x50(%rsp), %xmm0
movsd 0x3327(%rip), %xmm1     # 0x155a8
subsd %xmm1, %xmm0
movsd %xmm0, 0x50(%rsp)
movsd 0x50(%rsp), %xmm0
movsd 0x330f(%rip), %xmm1     # 0x155a8
ucomisd %xmm1, %xmm0
jae 0x12245
movq 0xd0(%rsp), %rdi
movq 0x98(%rsp), %rdx
movq 0xa0(%rsp), %rcx
movl 0x8c(%rsp), %r8d
movsd 0x78(%rsp), %xmm0
cvtsi2sdl 0x8c(%rsp), %xmm1
mulsd %xmm1, %xmm0
movq 0x60(%rsp), %r9
movsd 0x70(%rsp), %xmm1
xorps %xmm2, %xmm2
leaq 0x486e(%rip), %rbx      # 0x16b55
leaq 0x3e6e(%rip), %rax      # 0x1615c
ucomisd %xmm2, %xmm1
cmovaeq %rax, %rbx
movsd 0x70(%rsp), %xmm1
movsd 0x68(%rsp), %xmm2
xorps %xmm3, %xmm3
leaq 0x4849(%rip), %r11      # 0x16b55
leaq 0x3e49(%rip), %rax      # 0x1615c
ucomisd %xmm3, %xmm2
cmovaeq %rax, %r11
movsd 0x68(%rsp), %xmm2
movq 0x48(%rsp), %r10
movq 0x58(%rsp), %rax
leaq 0x4918(%rip), %rsi      # 0x16c4a
movq %rbx, (%rsp)
movq %r11, 0x8(%rsp)
movq %r10, 0x10(%rsp)
movq %rax, 0x18(%rsp)
movb $0x3, %al
callq 0x98c0
movq %rax, 0xd0(%rsp)
movq 0x58(%rsp), %rdi
callq 0x8750
movq 0x48(%rsp), %rdi
callq 0x8750
movq 0x60(%rsp), %rax
movsbl (%rax), %eax
cmpl $0x24, %eax
jne 0x1238f
movsd 0x70(%rsp), %xmm0
addsd 0xa8(%rsp), %xmm0
movsd %xmm0, 0xa8(%rsp)
jmp 0x123b7
movsd 0x70(%rsp), %xmm0
movsd 0x8f2b(%rip), %xmm2     # 0x1b2c8
movsd 0xa8(%rsp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0xa8(%rsp)
jmp 0x120ae
movq 0xd0(%rsp), %rdi
movsd 0xa8(%rsp), %xmm0
xorps %xmm1, %xmm1
leaq 0x477e(%rip), %rdx      # 0x16b55
leaq 0x3d7e(%rip), %rax      # 0x1615c
ucomisd %xmm1, %xmm0
cmovaq %rax, %rdx
movsd 0xa8(%rsp), %xmm0
leaq 0x4895(%rip), %rsi      # 0x16c8b
movb $0x1, %al
callq 0x98c0
movq %rax, 0xd0(%rsp)
movq 0xd0(%rsp), %rdi
leaq 0x447c(%rip), %rsi      # 0x16890
callq 0x9350
movq %rax, 0xd0(%rsp)
cmpq $0x0, 0xd0(%rsp)
je 0x12449
movq 0xf8(%rsp), %rax
movq 0x8(%rax), %rdi
movq 0xd0(%rsp), %rsi
xorl %eax, %eax
movl %eax, %edx
callq 0xdd70
movq 0xd0(%rsp), %rdi
callq 0x8750
movq 0xe0(%rsp), %rdi
callq 0x8750
addq $0x100, %rsp            # imm = 0x100
popq %rbx
retq
nopl (%rax)