// File: /workspace/github_c_makefile_star2_O1/antirez[P]stonky/stonky.c
// Function: botHandleShowPortfolioRequest
void botHandleShowPortfolioRequest(botRequest *br, int argc, sds *argv) {
    /* Remove the final "?" from the list name. */
    sds listname = sdsnewlen(argv[0],sdslen(argv[0])-1);
    sds pattern = argc > 1 ? argv[1] : NULL;
    sds reply = NULL;
    int count;
    stockpack *packs = dbGetPortfolio(listname,&count);
    if (packs == NULL) {
        reply = sdscatprintf(sdsempty(),
            "There aren't bought stocks associated with the list %s",listname);
        goto cleanup;
    }

    /* Check if to convert to USD: do it only if there are also USD
     * assets. */
    int tousd = 0;
    for (int j = 0; j < count; j++) {
        if (packs[j].currency != CURRENCY_EUR) {
            tousd = 1;
            break;
        }
    }

    /* Build the reply composed of all the stocks. */
    reply = sdsnew("```\n");
    double totalvalue = 0;
    double totalpayed = 0;
    for (int j = 0; j < count; j++) {
        stockpack *pack = packs+j;

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

        double value = pack->value;
        double payed = pack->quantity * pack->avgprice;
        if (tousd && pack->currency == CURRENCY_EUR) {
            value *= EURUSD;
            payed *= EURUSD;
        }
        totalvalue += value;
        totalpayed += payed;
        sds emoji = sdsempty();

        /* One symbol for every 10% of change. */
        double gp = fabs(pack->gainperc);
        do {
            emoji = sdscat(emoji, (pack->gainperc >= 0) ? "💚":"💔");
            gp -= 10;
        } while(gp >= 10);

        reply = sdscatprintf(reply,
            "%-7s %d@%.2f = %.0f\n"
            "       %s%.2f (%s%.2f%%) %s\n"
            "       %s%.2f (%s%.2f%%) today\n\n",
            pack->symbol,
            (int)pack->quantity,
            pack->avgprice,
            pack->avgprice * pack->quantity,
            (pack->gain >= 0) ? "+" : "",
            pack->gain,
            (pack->gainperc >= 0) ? "+" : "",
            pack->gainperc,
            emoji,
            (pack->daygain >= 0) ? "+" : "",
            pack->daygain,
            (pack->daygainperc >= 0) ? "+" : "",
            pack->daygainperc);
        sdsfree(emoji);
    }
    double totalgain = totalvalue-totalpayed;
    double totalgainperc = (totalvalue/totalpayed-1)*100;
    reply = sdscatprintf(reply,"---------\nTotal value: %.2f%s\n",
                        totalvalue,
                        tousd ? "$" : "€");
    reply = sdscatprintf(reply,"Total P/L  : %s%.2f%s (%s%.2f%%)",
                        totalgain > 0 ? "+" : "", totalgain,
                        tousd ? "$" : "€",
                        totalgainperc > 0 ? "+" : "", totalgainperc);
    reply = sdscat(reply,"```");

cleanup:
    xfree(packs);
    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 $0x58, %rsp
movq %rdx, %rbx
movl %esi, %ebp
movq %rdi, %r14
movq (%rdx), %rdi
movzbl -0x1(%rdi), %esi
movl %esi, %eax
andl $0x7, %eax
cmpl $0x4, %eax
ja 0xb736
leaq 0x30ab(%rip), %rcx      # 0xe7bc
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
shrl $0x3, %esi
jmp 0xb738
movq -0x11(%rdi), %rsi
jmp 0xb738
movzwl -0x5(%rdi), %esi
jmp 0xb738
movl -0x9(%rdi), %esi
jmp 0xb738
movzbl -0x3(%rdi), %esi
jmp 0xb738
xorl %esi, %esi
decq %rsi
callq 0x576c
movq %rax, %r15
cmpl $0x2, %ebp
jl 0xb74e
movq 0x8(%rbx), %rbp
jmp 0xb750
xorl %ebp, %ebp
leaq 0x4(%rsp), %rsi
movq %r15, %rdi
callq 0x962c
movq %rax, 0x8(%rsp)
testq %rax, %rax
je 0xb9ef
movslq 0x4(%rsp), %rax
testq %rax, %rax
setg 0x3(%rsp)
jle 0xb7b6
movq 0x8(%rsp), %rcx
cmpl $0x2, 0x20(%rcx)
jne 0xb7b6
movq 0x8(%rsp), %rcx
addq $0xb0, %rcx
movl $0x1, %esi
movq %rsi, %rdx
cmpq %rsi, %rax
je 0xb7ae
leaq 0x1(%rdx), %rsi
cmpl $0x2, (%rcx)
leaq 0x90(%rcx), %rcx
je 0xb796
cmpq %rax, %rdx
setb 0x3(%rsp)
movq %r15, 0x40(%rsp)
movq %r14, 0x48(%rsp)
leaq 0x4f93(%rip), %rdi      # 0x1075a
callq 0x58ac
movq %rax, %r14
cmpl $0x0, 0x4(%rsp)
jle 0xba10
xorl %ecx, %ecx
leaq 0x4548(%rip), %r15      # 0xfd2b
leaq 0x4546(%rip), %r13      # 0xfd30
xorpd %xmm0, %xmm0
movsd %xmm0, 0x10(%rsp)
movsd %xmm0, 0x18(%rsp)
movq %rcx, 0x28(%rsp)
leaq (%rcx,%rcx,8), %r12
shlq $0x4, %r12
addq 0x8(%rsp), %r12
testq %rbp, %rbp
je 0xb881
movzbl -0x1(%rbp), %ebx
movl %ebx, %eax
andl $0x7, %eax
cmpl $0x4, %eax
movq %r14, 0x50(%rsp)
ja 0xb850
leaq 0x2fa5(%rip), %rcx      # 0xe7d0
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
shrl $0x3, %ebx
jmp 0xb852
movq -0x11(%rbp), %rbx
jmp 0xb852
movzwl -0x5(%rbp), %ebx
jmp 0xb852
movl -0x9(%rbp), %ebx
jmp 0xb852
movzbl -0x3(%rbp), %ebx
jmp 0xb852
xorl %ebx, %ebx
leaq 0x24(%r12), %r14
movq %r14, %rdi
callq 0x2100
movq %rbp, %rdi
movl %ebx, %esi
movq %r14, %rdx
movl %eax, %ecx
movl $0x1, %r8d
callq 0x79cb
testl %eax, %eax
movq 0x50(%rsp), %r14
je 0xb9d7
xorps %xmm1, %xmm1
cvtsi2sdq 0x10(%r12), %xmm1
movsd 0x88(%r12), %xmm0
mulsd 0x18(%r12), %xmm1
cmpb $0x0, 0x3(%rsp)
je 0xb8bb
cmpl $0x2, 0x20(%r12)
jne 0xb8bb
mulsd 0x9a15(%rip), %xmm0     # 0x152c8
mulsd 0x9a0d(%rip), %xmm1     # 0x152c8
movsd %xmm1, 0x20(%rsp)
movsd 0x18(%rsp), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, 0x18(%rsp)
callq 0x5889
movq %rax, %rbx
movsd 0x70(%r12), %xmm1
andps 0x2749(%rip), %xmm1     # 0xe030
movaps %xmm1, 0x30(%rsp)
movsd 0x70(%r12), %xmm0
ucomisd 0x2dfd(%rip), %xmm0     # 0xe6f8
movq %r13, %rsi
cmovaeq %r15, %rsi
movq %rbx, %rdi
callq 0x61f0
movapd 0x30(%rsp), %xmm1
movq %rax, %rbx
addsd 0x2de5(%rip), %xmm1     # 0xe700
ucomisd 0x2de5(%rip), %xmm1     # 0xe708
jae 0xb8e7
movq 0x10(%r12), %rcx
xorps %xmm1, %xmm1
cvtsi2sd %rcx, %xmm1
movsd 0x68(%r12), %xmm2
xorpd %xmm6, %xmm6
ucomisd %xmm6, %xmm2
leaq 0x443d(%rip), %r11      # 0xfd85
movq %r11, %r8
leaq 0x3a3a(%rip), %rax      # 0xf38c
cmovaeq %rax, %r8
movsd 0x70(%r12), %xmm3
ucomisd %xmm6, %xmm3
movq %r11, %r9
cmovaeq %rax, %r9
movsd 0x10(%rsp), %xmm0
addsd 0x20(%rsp), %xmm0
movsd %xmm0, 0x10(%rsp)
movsd 0x78(%r12), %xmm4
ucomisd %xmm6, %xmm4
leaq 0x24(%r12), %rdx
movq %r11, %r10
cmovaeq %rax, %r10
movsd 0x18(%r12), %xmm0
movsd 0x80(%r12), %xmm5
ucomisd %xmm6, %xmm5
mulsd %xmm0, %xmm1
cmovaeq %rax, %r11
subq $0x8, %rsp
movq %r14, %rdi
leaq 0x4379(%rip), %rsi      # 0xfd35
movb $0x6, %al
pushq %r11
pushq %r10
pushq %rbx
callq 0x6581
addq $0x20, %rsp
movq %rax, %r14
movq %rbx, %rdi
callq 0x591d
movq 0x28(%rsp), %rcx
incq %rcx
movslq 0x4(%rsp), %rax
cmpq %rax, %rcx
jl 0xb7fa
jmp 0xba20
callq 0x5889
leaq 0x42f9(%rip), %rsi      # 0xfcf4
movq %rax, %rdi
movq %r15, %rdx
xorl %eax, %eax
callq 0x6581
movq %rax, %rbx
jmp 0xbae4
xorpd %xmm0, %xmm0
movsd %xmm0, 0x18(%rsp)
movsd %xmm0, 0x10(%rsp)
movsd 0x18(%rsp), %xmm0
movapd %xmm0, %xmm3
movapd %xmm0, %xmm2
movsd 0x10(%rsp), %xmm1
divsd %xmm1, %xmm2
addsd 0x2c88(%rip), %xmm2     # 0xe6c8
subsd %xmm1, %xmm3
movsd %xmm3, 0x28(%rsp)
mulsd 0x2c6e(%rip), %xmm2     # 0xe6c0
movsd %xmm2, 0x30(%rsp)
leaq 0x4346(%rip), %rax      # 0xfda5
leaq 0x35e9(%rip), %rbx      # 0xf04f
cmpb $0x0, 0x3(%rsp)
cmovneq %rax, %rbx
leaq 0x4310(%rip), %rsi      # 0xfd86
movq %r14, %rdi
movq %rbx, %rdx
movb $0x1, %al
callq 0x6581
xorpd %xmm2, %xmm2
movsd 0x28(%rsp), %xmm0
ucomisd %xmm2, %xmm0
leaq 0x38f4(%rip), %rcx      # 0xf38c
leaq 0x42e6(%rip), %r8       # 0xfd85
movq %r8, %rdx
cmovaq %rcx, %rdx
movsd 0x30(%rsp), %xmm1
ucomisd %xmm2, %xmm1
cmovaq %rcx, %r8
leaq 0x42ec(%rip), %rsi      # 0xfda7
movq %rax, %rdi
movq %rbx, %rcx
movb $0x2, %al
callq 0x6581
leaq 0x3ff1(%rip), %rsi      # 0xfac0
movq %rax, %rdi
callq 0x61f0
movq %rax, %rbx
movq 0x48(%rsp), %r14
movq 0x40(%rsp), %r15
movq 0x8(%rsp), %rdi
callq 0x2468
testq %rbx, %rbx
je 0xbb01
movq 0x8(%r14), %rdi
movq %rbx, %rsi
xorl %edx, %edx
callq 0x91e5
movq %rbx, %rdi
callq 0x591d
movq %r15, %rdi
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x591d