// File: /workspace/github_c_makefile_star2_O3/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 $0x68, %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 0xb455
leaq 0x3368(%rip), %rcx      # 0xe798
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
shrl $0x3, %esi
jmp 0xb457
movq -0x11(%rdi), %rsi
jmp 0xb457
movzwl -0x5(%rdi), %esi
jmp 0xb457
movl -0x9(%rdi), %esi
jmp 0xb457
movzbl -0x3(%rdi), %esi
jmp 0xb457
xorl %esi, %esi
decq %rsi
callq 0x55a0
movq %rax, %r15
cmpl $0x2, %ebp
jl 0xb46d
movq 0x8(%rbx), %rbp
jmp 0xb46f
xorl %ebp, %ebp
leaq 0x14(%rsp), %rsi
movq %r15, %rdi
callq 0x9391
movq %rax, 0x18(%rsp)
testq %rax, %rax
je 0xb722
movslq 0x14(%rsp), %rbx
testq %rbx, %rbx
jle 0xb743
movb $0x1, 0x7(%rsp)
movq 0x18(%rsp), %rax
cmpl $0x2, 0x20(%rax)
jne 0xb4d8
movq 0x18(%rsp), %rax
addq $0xb0, %rax
movl $0x1, %edx
movq %rdx, %rcx
cmpq %rdx, %rbx
je 0xb4d0
leaq 0x1(%rcx), %rdx
cmpl $0x2, (%rax)
leaq 0x90(%rax), %rax
je 0xb4b8
cmpq %rbx, %rcx
setb 0x7(%rsp)
movq %r15, 0x48(%rsp)
movq %r14, 0x50(%rsp)
leaq 0x5251(%rip), %rdi      # 0x1073a
callq 0x56e9
movq %rax, %r14
testl %ebx, %ebx
jle 0xb776
xorl %ecx, %ecx
leaq 0x4809(%rip), %r15      # 0xfd0b
leaq 0x4807(%rip), %r13      # 0xfd10
xorpd %xmm0, %xmm0
movsd %xmm0, 0x20(%rsp)
movq %rbx, 0x30(%rsp)
movq %rcx, 0x60(%rsp)
leaq (%rcx,%rcx,8), %r12
shlq $0x4, %r12
addq 0x18(%rsp), %r12
testq %rbp, %rbp
je 0xb5ab
movzbl -0x1(%rbp), %ebx
movl %ebx, %eax
andl $0x7, %eax
cmpl $0x4, %eax
movq %r14, 0x58(%rsp)
movsd %xmm0, 0x8(%rsp)
ja 0xb574
leaq 0x325d(%rip), %rcx      # 0xe7ac
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
shrl $0x3, %ebx
jmp 0xb576
movq -0x11(%rbp), %rbx
jmp 0xb576
movzwl -0x5(%rbp), %ebx
jmp 0xb576
movl -0x9(%rbp), %ebx
jmp 0xb576
movzbl -0x3(%rbp), %ebx
jmp 0xb576
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 0x777b
testl %eax, %eax
movq 0x58(%rsp), %r14
movsd 0x8(%rsp), %xmm0
je 0xb707
xorps %xmm2, %xmm2
cvtsi2sdq 0x10(%r12), %xmm2
movsd 0x88(%r12), %xmm1
mulsd 0x18(%r12), %xmm2
cmpb $0x0, 0x7(%rsp)
je 0xb5e5
cmpl $0x2, 0x20(%r12)
jne 0xb5e5
mulsd 0x9ceb(%rip), %xmm1     # 0x152c8
mulsd 0x9ce3(%rip), %xmm2     # 0x152c8
movsd %xmm2, 0x28(%rsp)
addsd %xmm1, %xmm0
movsd %xmm0, 0x8(%rsp)
callq 0x56c6
movq %rax, %rbx
movsd 0x70(%r12), %xmm1
andps 0x2a25(%rip), %xmm1     # 0xe030
movaps %xmm1, 0x30(%rsp)
movsd 0x70(%r12), %xmm0
ucomisd 0x30c9(%rip), %xmm0     # 0xe6e8
movq %r13, %rsi
cmovaeq %r15, %rsi
movq %rbx, %rdi
callq 0x5ff1
movapd 0x30(%rsp), %xmm1
movq %rax, %rbx
addsd 0x30b1(%rip), %xmm1     # 0xe6f0
ucomisd 0x30b1(%rip), %xmm1     # 0xe6f8
jae 0xb60b
movq 0x10(%r12), %rcx
xorps %xmm1, %xmm1
cvtsi2sd %rcx, %xmm1
movsd 0x68(%r12), %xmm2
xorpd %xmm6, %xmm6
ucomisd %xmm6, %xmm2
leaq 0x46f9(%rip), %r11      # 0xfd65
movq %r11, %r8
leaq 0x3cf6(%rip), %rax      # 0xf36c
cmovaeq %rax, %r8
movsd 0x70(%r12), %xmm3
ucomisd %xmm6, %xmm3
movq %r11, %r9
cmovaeq %rax, %r9
movsd 0x20(%rsp), %xmm0
addsd 0x28(%rsp), %xmm0
movsd %xmm0, 0x20(%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 0x4635(%rip), %rsi      # 0xfd15
movb $0x6, %al
pushq %r11
pushq %r10
pushq %rbx
callq 0x6382
addq $0x20, %rsp
movq %rax, %r14
movq %rbx, %rdi
callq 0x5775
movl 0x14(%rsp), %ebx
movsd 0x8(%rsp), %xmm0
jmp 0xb70c
movq 0x30(%rsp), %rbx
movq 0x60(%rsp), %rcx
incq %rcx
movslq %ebx, %rax
cmpq %rax, %rcx
jl 0xb513
jmp 0xb784
callq 0x56c6
leaq 0x45a6(%rip), %rsi      # 0xfcd4
movq %rax, %rdi
movq %r15, %rdx
xorl %eax, %eax
callq 0x6382
movq %rax, %rbx
jmp 0xb84c
leaq 0x4ff0(%rip), %rdi      # 0x1073a
callq 0x56e9
movq %rax, %rdi
leaq 0x38d6(%rip), %rbx      # 0xf02f
xorps %xmm0, %xmm0
movsd %xmm0, 0x8(%rsp)
movsd 0x289e(%rip), %xmm0     # 0xe008
movsd %xmm0, 0x30(%rsp)
xorpd %xmm0, %xmm0
jmp 0xb7da
xorpd %xmm0, %xmm0
xorpd %xmm1, %xmm1
movsd %xmm1, 0x20(%rsp)
movapd %xmm0, %xmm3
movapd %xmm0, %xmm2
movsd 0x20(%rsp), %xmm1
divsd %xmm1, %xmm2
addsd 0x2f1a(%rip), %xmm2     # 0xe6b8
subsd %xmm1, %xmm3
movsd %xmm3, 0x8(%rsp)
mulsd 0x2f00(%rip), %xmm2     # 0xe6b0
movsd %xmm2, 0x30(%rsp)
leaq 0x45c8(%rip), %rax      # 0xfd85
leaq 0x386b(%rip), %rbx      # 0xf02f
cmpb $0x0, 0x7(%rsp)
cmovneq %rax, %rbx
movq %r14, %rdi
movq 0x50(%rsp), %r14
movq 0x48(%rsp), %r15
leaq 0x4585(%rip), %rsi      # 0xfd66
movq %rbx, %rdx
movb $0x1, %al
callq 0x6382
xorpd %xmm2, %xmm2
movsd 0x8(%rsp), %xmm0
ucomisd %xmm2, %xmm0
leaq 0x3b6c(%rip), %rcx      # 0xf36c
leaq 0x455e(%rip), %r8       # 0xfd65
movq %r8, %rdx
cmovaq %rcx, %rdx
movsd 0x30(%rsp), %xmm1
ucomisd %xmm2, %xmm1
cmovaq %rcx, %r8
leaq 0x4564(%rip), %rsi      # 0xfd87
movq %rax, %rdi
movq %rbx, %rcx
movb $0x2, %al
callq 0x6382
leaq 0x4269(%rip), %rsi      # 0xfaa0
movq %rax, %rdi
callq 0x5ff1
movq %rax, %rbx
movq 0x18(%rsp), %rdi
callq 0x2478
testq %rbx, %rbx
je 0xb85f
movq 0x8(%r14), %rdi
movq %rbx, %rsi
xorl %edx, %edx
callq 0x8f4e
movq %rbx, %rdi
callq 0x5775
movq %r15, %rdi
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x5775