// File: /workspace/github_c_makefile_star2_O2/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 %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movq %rdx, %rbx
movl %esi, %ebp
movq %rdi, %r12
movq (%rdx), %r14
movq %r14, %rdi
callq 0x8c9f
leaq -0x2(%rax), %rsi
movq %r14, %rdi
callq 0x4da4
movq %rax, %r14
cmpl $0x2, %ebp
jl 0xa410
movq 0x8(%rbx), %r15
jmp 0xa413
xorl %r15d, %r15d
movq %r14, %rdi
xorl %esi, %esi
callq 0x7efd
testq %rax, %rax
je 0xa665
leaq 0x3621(%rip), %rsi      # 0xda4e
leaq 0x80(%rsp), %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x711d
cmpl $0x64, %eax
jne 0xa66e
movq %r12, 0x40(%rsp)
movq %r14, 0x48(%rsp)
leaq 0x3f49(%rip), %rdi      # 0xe3a2
callq 0x4ef7
movq %rax, 0x10(%rsp)
leaq 0x80(%rsp), %r13
pushq $0x1
popq %r14
xorpd %xmm0, %xmm0
movsd %xmm0, 0x8(%rsp)
movq %r13, %rdi
callq 0x6eb5
testl %eax, %eax
je 0xa67f
movq 0x90(%rsp), %rbp
movq 0x30(%rbp), %rbx
testq %r15, %r15
je 0xa4c4
movq %r15, %rdi
callq 0x8c9f
movq %rax, %r12
movq %rbx, %rdi
callq 0x2100
movq %r15, %rdi
movl %r12d, %esi
movq %rbx, %rdx
movl %eax, %ecx
movl %r14d, %r8d
callq 0x65bf
testl %eax, %eax
je 0xa479
movq 0x8(%rbp), %rax
movq %rax, 0x68(%rsp)
movq 0x68(%rbp), %rdi
movl 0x88(%rbp), %eax
movsd 0xb8(%rbp), %xmm2
movsd 0xd8(%rbp), %xmm0
movl %eax, 0x1c(%rsp)
cvtsi2sd %eax, %xmm1
movsd %xmm1, 0x58(%rsp)
movsd %xmm0, 0x60(%rsp)
movsd %xmm2, 0x50(%rsp)
divsd %xmm2, %xmm0
addsd 0x1ec3(%rip), %xmm0     # 0xc3d0
mulsd 0x1eb3(%rip), %xmm0     # 0xc3c8
movapd %xmm0, 0x30(%rsp)
movq 0xf0(%rbp), %rax
movq %rax, 0x78(%rsp)
callq 0xa2be
movq %rax, 0x70(%rsp)
movaps 0x30(%rsp), %xmm0
andps 0x1b43(%rip), %xmm0     # 0xc080
movaps %xmm0, 0x20(%rsp)
callq 0x4ee9
movapd 0x30(%rsp), %xmm0
ucomisd 0x1eab(%rip), %xmm0     # 0xc400
movq %rax, %r12
leaq 0x355e(%rip), %rbp      # 0xdabd
leaq 0x3552(%rip), %rax      # 0xdab8
cmovaeq %rax, %rbp
movq %r12, %rdi
movq %rbp, %rsi
callq 0x5593
movq %rax, %r12
movapd 0x20(%rsp), %xmm0
addsd 0x1e82(%rip), %xmm0     # 0xc408
movapd %xmm0, 0x20(%rsp)
ucomisd 0x1e7c(%rip), %xmm0     # 0xc410
jae 0xa56a
movsd 0x60(%rsp), %xmm0
movapd %xmm0, %xmm1
subsd 0x50(%rsp), %xmm1
movsd 0x58(%rsp), %xmm3
mulsd %xmm3, %xmm1
xorpd %xmm4, %xmm4
ucomisd %xmm4, %xmm1
leaq 0x340e(%rip), %r11      # 0xd9cd
movq %r11, %r10
leaq 0x2a0b(%rip), %rax      # 0xcfd4
cmovaeq %rax, %r10
movapd 0x30(%rsp), %xmm2
ucomisd %xmm4, %xmm2
mulsd %xmm3, %xmm0
cmovaeq %rax, %r11
movq 0x10(%rsp), %rdi
leaq 0x34d7(%rip), %rsi      # 0xdac2
movq 0x68(%rsp), %rdx
movq %rbx, %rcx
movl 0x1c(%rsp), %r8d
movq 0x78(%rsp), %rbx
movq %rbx, %r9
movsd %xmm1, 0x20(%rsp)
movb $0x3, %al
movq 0x70(%rsp), %rbp
pushq %rbp
pushq %r12
pushq %r11
pushq %r10
callq 0x5814
addq $0x20, %rsp
movq %rax, 0x10(%rsp)
movq %rbp, %rdi
callq 0x4f65
movq %r12, %rdi
callq 0x4f65
cmpb $0x24, (%rbx)
jne 0xa648
movsd 0x8(%rsp), %xmm0
addsd 0x20(%rsp), %xmm0
jmp 0xa473
movsd 0x20(%rsp), %xmm1
mulsd 0x8c72(%rip), %xmm1     # 0x132c8
movsd 0x8(%rsp), %xmm0
addsd %xmm1, %xmm0
jmp 0xa473
leaq 0x33d5(%rip), %rdi      # 0xda41
jmp 0xa675
leaq 0x341f(%rip), %rdi      # 0xda94
callq 0x4ef7
movq %rax, %rbx
jmp 0xa6ce
xorpd %xmm1, %xmm1
movsd 0x8(%rsp), %xmm0
ucomisd %xmm1, %xmm0
leaq 0x3339(%rip), %rdx      # 0xd9cd
leaq 0x2939(%rip), %rax      # 0xcfd4
cmovaq %rax, %rdx
leaq 0x345d(%rip), %rsi      # 0xdb03
movq 0x10(%rsp), %rdi
movb $0x1, %al
callq 0x5814
leaq 0x304f(%rip), %rsi      # 0xd708
movq %rax, %rdi
callq 0x5593
movq %rax, %rbx
movq 0x48(%rsp), %r14
movq 0x40(%rsp), %r12
testq %rbx, %rbx
je 0xa6e2
movq 0x8(%r12), %rdi
movq %rbx, %rsi
xorl %edx, %edx
callq 0x7cae
movq %rbx, %rdi
callq 0x4f65
movq %r14, %rdi
callq 0x4f65
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq