// File: /workspace/github_c_makefile_star2_O1/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 0xbca5
leaq 0x2b64(%rip), %rcx      # 0xe7e4
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
shrl $0x3, %esi
jmp 0xbca7
movq -0x11(%rdi), %rsi
jmp 0xbca7
movzwl -0x5(%rdi), %esi
jmp 0xbca7
movl -0x9(%rdi), %esi
jmp 0xbca7
movzbl -0x3(%rdi), %esi
jmp 0xbca7
xorl %esi, %esi
addq $-0x2, %rsi
callq 0x576c
cmpl $0x2, %ebp
jl 0xbcbb
movq 0x8(%rbx), %r12
jmp 0xbcbe
xorl %r12d, %r12d
leaq 0x3257(%rip), %rdi      # 0xef1c
movq %rax, 0x50(%rsp)
movq %rax, %rsi
xorl %eax, %eax
callq 0x8702
testq %rax, %rax
je 0xbf61
leaq 0x4122(%rip), %rsi      # 0xfe06
leaq 0x80(%rsp), %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x85df
cmpl $0x64, %eax
jne 0xbf6a
leaq 0x4a54(%rip), %rdi      # 0x1075a
callq 0x58ac
movq %rax, %rbp
leaq 0x80(%rsp), %rdi
callq 0x836f
xorpd %xmm0, %xmm0
testl %eax, %eax
je 0xbf78
movsd %xmm0, 0x8(%rsp)
movq %r12, 0x28(%rsp)
movq 0x90(%rsp), %r15
movq 0x30(%r15), %rbx
testq %r12, %r12
je 0xbdb1
movzbl -0x1(%r12), %r14d
movl %r14d, %eax
andl $0x7, %eax
cmpl $0x4, %eax
ja 0xbd88
leaq 0x2a9d(%rip), %rcx      # 0xe7f8
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
shrl $0x3, %r14d
jmp 0xbd8b
movq -0x11(%r12), %r14
jmp 0xbd8b
movzwl -0x5(%r12), %r14d
jmp 0xbd8b
movl -0x9(%r12), %r14d
jmp 0xbd8b
movzbl -0x3(%r12), %r14d
jmp 0xbd8b
xorl %r14d, %r14d
movq %rbx, %rdi
callq 0x2100
movq %r12, %rdi
movl %r14d, %esi
movq %rbx, %rdx
movl %eax, %ecx
movl $0x1, %r8d
callq 0x79cb
testl %eax, %eax
je 0xbf4a
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 0x28c6(%rip), %xmm0     # 0xe6c8
mulsd 0x28b6(%rip), %xmm0     # 0xe6c0
movapd %xmm0, 0x30(%rsp)
movq 0xf0(%r15), %r14
callq 0xbb1f
movq %rax, %r13
movaps 0x30(%rsp), %xmm0
andps 0x2205(%rip), %xmm0     # 0xe030
movaps %xmm0, 0x10(%rsp)
callq 0x5889
movapd 0x30(%rsp), %xmm0
ucomisd 0x28b5(%rip), %xmm0     # 0xe6f8
movq %rax, %r15
leaq 0x4028(%rip), %rbp      # 0xfe75
leaq 0x401c(%rip), %rax      # 0xfe70
cmovaeq %rax, %rbp
movq %r15, %rdi
movq %rbp, %rsi
callq 0x61f0
movq %rax, %r15
movapd 0x10(%rsp), %xmm0
addsd 0x288c(%rip), %xmm0     # 0xe700
movapd %xmm0, 0x10(%rsp)
ucomisd 0x2886(%rip), %xmm0     # 0xe708
jae 0xbe58
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 0x3ed8(%rip), %r11      # 0xfd85
movq %r11, %r10
leaq 0x34d5(%rip), %rax      # 0xf38c
cmovaeq %rax, %r10
movapd 0x30(%rsp), %xmm2
ucomisd %xmm4, %xmm2
mulsd %xmm3, %xmm0
cmovaeq %rax, %r11
movq 0x78(%rsp), %rdi
leaq 0x3fa1(%rip), %rsi      # 0xfe7a
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 0x6581
addq $0x20, %rsp
movq %rax, %rbp
movq %r13, %rdi
callq 0x591d
movq %r15, %rdi
callq 0x591d
cmpb $0x24, (%r14)
jne 0xbf27
movsd 0x8(%rsp), %xmm0
addsd 0x10(%rsp), %xmm0
jmp 0xbf3f
movsd 0x10(%rsp), %xmm1
mulsd 0x9393(%rip), %xmm1     # 0x152c8
movsd 0x8(%rsp), %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x8(%rsp)
movq 0x28(%rsp), %r12
leaq 0x80(%rsp), %rdi
callq 0x836f
testl %eax, %eax
jne 0xbd32
jmp 0xbf7e
leaq 0x3e91(%rip), %rdi      # 0xfdf9
jmp 0xbf71
leaq 0x3edb(%rip), %rdi      # 0xfe4c
callq 0x58ac
jmp 0xbfbe
movsd %xmm0, 0x8(%rsp)
movsd 0x8(%rsp), %xmm0
ucomisd 0x276c(%rip), %xmm0     # 0xe6f8
leaq 0x33f9(%rip), %rax      # 0xf38c
leaq 0x3deb(%rip), %rdx      # 0xfd85
cmovaq %rax, %rdx
leaq 0x3f16(%rip), %rsi      # 0xfebb
movq %rbp, %rdi
movb $0x1, %al
callq 0x6581
leaq 0x3b0a(%rip), %rsi      # 0xfac0
movq %rax, %rdi
callq 0x61f0
movq %rax, %rbx
testq %rax, %rax
je 0xbfd9
movq 0x48(%rsp), %rax
movq 0x8(%rax), %rdi
movq %rbx, %rsi
xorl %edx, %edx
callq 0x91e5
movq %rbx, %rdi
callq 0x591d
movq 0x50(%rsp), %rdi
callq 0x591d
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq