// File: /workspace/github_c_makefile_star2_O3/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, 0x48(%rsp)
movq (%rdx), %rdi
movzbl -0x1(%rdi), %esi
movl %esi, %eax
andl $0x7, %eax
cmpl $0x4, %eax
ja 0xb9e9
leaq 0x2dfc(%rip), %rcx      # 0xe7c0
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
shrl $0x3, %esi
jmp 0xb9eb
movq -0x11(%rdi), %rsi
jmp 0xb9eb
movzwl -0x5(%rdi), %esi
jmp 0xb9eb
movl -0x9(%rdi), %esi
jmp 0xb9eb
movzbl -0x3(%rdi), %esi
jmp 0xb9eb
xorl %esi, %esi
addq $-0x2, %rsi
callq 0x55a0
cmpl $0x2, %ebp
jl 0xb9ff
movq 0x8(%rbx), %r12
jmp 0xba02
xorl %r12d, %r12d
leaq 0x34f3(%rip), %rdi      # 0xeefc
movq %rax, 0x50(%rsp)
movq %rax, %rsi
xorl %eax, %eax
callq 0x847a
testq %rax, %rax
je 0xbca5
leaq 0x43be(%rip), %rsi      # 0xfde6
leaq 0x80(%rsp), %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x8357
cmpl $0x64, %eax
jne 0xbcae
leaq 0x4cf0(%rip), %rdi      # 0x1073a
callq 0x56e9
movq %rax, %rbp
leaq 0x80(%rsp), %rdi
callq 0x80e3
xorpd %xmm0, %xmm0
testl %eax, %eax
je 0xbcbc
movsd %xmm0, 0x8(%rsp)
movq %r12, 0x28(%rsp)
movq 0x90(%rsp), %r15
movq 0x30(%r15), %rbx
testq %r12, %r12
je 0xbaf5
movzbl -0x1(%r12), %r14d
movl %r14d, %eax
andl $0x7, %eax
cmpl $0x4, %eax
ja 0xbacc
leaq 0x2d35(%rip), %rcx      # 0xe7d4
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
shrl $0x3, %r14d
jmp 0xbacf
movq -0x11(%r12), %r14
jmp 0xbacf
movzwl -0x5(%r12), %r14d
jmp 0xbacf
movl -0x9(%r12), %r14d
jmp 0xbacf
movzbl -0x3(%r12), %r14d
jmp 0xbacf
xorl %r14d, %r14d
movq %rbx, %rdi
callq 0x2100
movq %r12, %rdi
movl %r14d, %esi
movq %rbx, %rdx
movl %eax, %ecx
movl $0x1, %r8d
callq 0x777b
testl %eax, %eax
je 0xbc8e
movq %rbp, 0x78(%rsp)
movq 0x8(%r15), %rax
movq %rax, 0x70(%rsp)
movq 0x68(%r15), %rdi
movl 0x88(%r15), %r12d
movsd 0xb8(%r15), %xmm2
movsd 0xd8(%r15), %xmm0
xorps %xmm1, %xmm1
cvtsi2sd %r12d, %xmm1
movsd %xmm1, 0x60(%rsp)
movsd %xmm0, 0x68(%rsp)
movsd %xmm2, 0x58(%rsp)
divsd %xmm2, %xmm0
addsd 0x2b72(%rip), %xmm0     # 0xe6b8
mulsd 0x2b62(%rip), %xmm0     # 0xe6b0
movapd %xmm0, 0x30(%rsp)
movq 0xf0(%r15), %r14
callq 0xb87d
movq %rax, %r13
movaps 0x30(%rsp), %xmm0
andps 0x24c1(%rip), %xmm0     # 0xe030
movaps %xmm0, 0x10(%rsp)
callq 0x56c6
movapd 0x30(%rsp), %xmm0
ucomisd 0x2b61(%rip), %xmm0     # 0xe6e8
movq %rax, %r15
leaq 0x42c4(%rip), %rbp      # 0xfe55
leaq 0x42b8(%rip), %rax      # 0xfe50
cmovaeq %rax, %rbp
movq %r15, %rdi
movq %rbp, %rsi
callq 0x5ff1
movq %rax, %r15
movapd 0x10(%rsp), %xmm0
addsd 0x2b38(%rip), %xmm0     # 0xe6f0
movapd %xmm0, 0x10(%rsp)
ucomisd 0x2b32(%rip), %xmm0     # 0xe6f8
jae 0xbb9c
movsd 0x68(%rsp), %xmm0
movapd %xmm0, %xmm1
subsd 0x58(%rsp), %xmm1
movsd 0x60(%rsp), %xmm3
mulsd %xmm3, %xmm1
xorpd %xmm4, %xmm4
ucomisd %xmm4, %xmm1
leaq 0x4174(%rip), %r11      # 0xfd65
movq %r11, %r10
leaq 0x3771(%rip), %rax      # 0xf36c
cmovaeq %rax, %r10
movapd 0x30(%rsp), %xmm2
ucomisd %xmm4, %xmm2
mulsd %xmm3, %xmm0
cmovaeq %rax, %r11
movq 0x78(%rsp), %rdi
leaq 0x423d(%rip), %rsi      # 0xfe5a
movq 0x70(%rsp), %rdx
movq %rbx, %rcx
movl %r12d, %r8d
movq %r14, %r9
movsd %xmm1, 0x10(%rsp)
movb $0x3, %al
pushq %r13
pushq %r15
pushq %r11
pushq %r10
callq 0x6382
addq $0x20, %rsp
movq %rax, %rbp
movq %r13, %rdi
callq 0x5775
movq %r15, %rdi
callq 0x5775
cmpb $0x24, (%r14)
jne 0xbc6b
movsd 0x8(%rsp), %xmm0
addsd 0x10(%rsp), %xmm0
jmp 0xbc83
movsd 0x10(%rsp), %xmm1
mulsd 0x964f(%rip), %xmm1     # 0x152c8
movsd 0x8(%rsp), %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x8(%rsp)
movq 0x28(%rsp), %r12
leaq 0x80(%rsp), %rdi
callq 0x80e3
testl %eax, %eax
jne 0xba76
jmp 0xbcc2
leaq 0x412d(%rip), %rdi      # 0xfdd9
jmp 0xbcb5
leaq 0x4177(%rip), %rdi      # 0xfe2c
callq 0x56e9
jmp 0xbd02
movsd %xmm0, 0x8(%rsp)
movsd 0x8(%rsp), %xmm0
ucomisd 0x2a18(%rip), %xmm0     # 0xe6e8
leaq 0x3695(%rip), %rax      # 0xf36c
leaq 0x4087(%rip), %rdx      # 0xfd65
cmovaq %rax, %rdx
leaq 0x41b2(%rip), %rsi      # 0xfe9b
movq %rbp, %rdi
movb $0x1, %al
callq 0x6382
leaq 0x3da6(%rip), %rsi      # 0xfaa0
movq %rax, %rdi
callq 0x5ff1
movq %rax, %rbx
testq %rax, %rax
je 0xbd1d
movq 0x48(%rsp), %rax
movq 0x8(%rax), %rdi
movq %rbx, %rsi
xorl %edx, %edx
callq 0x8f4e
movq %rbx, %rdi
callq 0x5775
movq 0x50(%rsp), %rdi
callq 0x5775
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq