// File: /workspace/github_c_makefile_star2_O2/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 $0x78, %rsp
movq %rdx, %rbx
movl %esi, %ebp
movq %rdi, %r15
movq (%rdx), %r14
movq %r14, %rdi
callq 0x8c9f
leaq -0x1(%rax), %rsi
movq %r14, %rdi
callq 0x4da4
movq %rax, %r14
cmpl $0x2, %ebp
jl 0x9f3e
movq 0x8(%rbx), %rbx
jmp 0x9f40
xorl %ebx, %ebx
leaq 0x4(%rsp), %rsi
movq %r14, %rdi
callq 0x8113
testq %rax, %rax
movq %rax, 0x48(%rsp)
je 0xa267
movq %rbx, 0x70(%rsp)
movq %r15, 0x60(%rsp)
movq %r14, 0x68(%rsp)
movl 0x4(%rsp), %ebx
movslq %ebx, %rdx
xorl %ecx, %ecx
testl %edx, %edx
movl $0x0, %esi
movq %rdx, 0x58(%rsp)
cmovgl %edx, %esi
leaq 0x20(%rax), %rdx
movq %rax, %r14
movq %rcx, %rax
cmpq %rcx, %rsi
je 0x9fa1
leaq 0x1(%rax), %rcx
cmpl $0x2, (%rdx)
leaq 0x90(%rdx), %rdx
je 0x9f89
movq %rax, 0x50(%rsp)
leaq 0x43f5(%rip), %rdi      # 0xe3a2
callq 0x4ef7
movq %rax, 0x18(%rsp)
xorl %ecx, %ecx
leaq 0x39b3(%rip), %r12      # 0xd973
leaq 0x39b1(%rip), %r13      # 0xd978
xorpd %xmm0, %xmm0
movsd %xmm0, 0x20(%rsp)
movsd %xmm0, 0x10(%rsp)
movq 0x70(%rsp), %rbp
movslq %ebx, %rax
cmpq %rax, %rcx
jge 0xa19a
movl %ebx, 0x30(%rsp)
movq %rcx, 0x28(%rsp)
imulq $0x90, %rcx, %r15
addq %r14, %r15
testq %rbp, %rbp
je 0xa037
movq %rbp, %rdi
callq 0x8c9f
movq %rax, %rbx
leaq 0x24(%r15), %r14
movq %r14, %rdi
callq 0x2100
movq %rbp, %rdi
movl %ebx, %esi
movq %r14, %rdx
movq 0x48(%rsp), %r14
movl %eax, %ecx
pushq $0x1
popq %r8
callq 0x65bf
testl %eax, %eax
je 0xa189
cvtsi2sdq 0x10(%r15), %xmm1
movsd 0x88(%r15), %xmm0
mulsd 0x18(%r15), %xmm1
movq 0x50(%rsp), %rax
cmpq 0x58(%rsp), %rax
jge 0xa06f
cmpl $0x2, 0x20(%r15)
jne 0xa06f
mulsd 0x9261(%rip), %xmm0     # 0x132c8
mulsd 0x9259(%rip), %xmm1     # 0x132c8
movsd %xmm1, 0x8(%rsp)
movsd 0x20(%rsp), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, 0x20(%rsp)
callq 0x4ee9
movq %rax, %rbx
movsd 0x70(%r15), %xmm1
andps 0x1fe6(%rip), %xmm1     # 0xc080
movaps %xmm1, 0x30(%rsp)
movsd 0x70(%r15), %xmm0
ucomisd 0x2353(%rip), %xmm0     # 0xc400
movq %r13, %rsi
cmovaeq %r12, %rsi
movq %rbx, %rdi
callq 0x5593
movapd 0x30(%rsp), %xmm1
movq %rax, %rbx
addsd 0x233b(%rip), %xmm1     # 0xc408
ucomisd 0x233b(%rip), %xmm1     # 0xc410
jae 0xa09a
movq 0x10(%r15), %rcx
cvtsi2sd %rcx, %xmm1
movsd 0x68(%r15), %xmm2
xorpd %xmm6, %xmm6
ucomisd %xmm6, %xmm2
leaq 0x38d8(%rip), %r11      # 0xd9cd
movq %r11, %r8
leaq 0x2ed5(%rip), %rax      # 0xcfd4
cmovaeq %rax, %r8
movsd 0x70(%r15), %xmm3
ucomisd %xmm6, %xmm3
movq %r11, %r9
cmovaeq %rax, %r9
movsd 0x10(%rsp), %xmm0
addsd 0x8(%rsp), %xmm0
movsd %xmm0, 0x10(%rsp)
movsd 0x78(%r15), %xmm4
ucomisd %xmm6, %xmm4
leaq 0x24(%r15), %rdx
movq %r11, %r10
cmovaeq %rax, %r10
movsd 0x18(%r15), %xmm0
movsd 0x80(%r15), %xmm5
ucomisd %xmm6, %xmm5
mulsd %xmm0, %xmm1
cmovaeq %rax, %r11
subq $0x8, %rsp
movq 0x20(%rsp), %rdi
leaq 0x3817(%rip), %rsi      # 0xd97d
movb $0x6, %al
pushq %r11
pushq %r10
pushq %rbx
callq 0x5814
addq $0x20, %rsp
movq %rax, 0x18(%rsp)
movq %rbx, %rdi
callq 0x4f65
movl 0x4(%rsp), %ebx
jmp 0xa18d
movl 0x30(%rsp), %ebx
movq 0x28(%rsp), %rcx
incq %rcx
jmp 0x9fdc
movq 0x50(%rsp), %rax
cmpq 0x58(%rsp), %rax
movsd 0x20(%rsp), %xmm0
movapd %xmm0, %xmm2
movsd 0x10(%rsp), %xmm1
subsd %xmm1, %xmm2
movsd %xmm2, 0x30(%rsp)
movapd %xmm0, %xmm2
divsd %xmm1, %xmm2
addsd 0x2202(%rip), %xmm2     # 0xc3d0
mulsd 0x21f2(%rip), %xmm2     # 0xc3c8
movsd %xmm2, 0x28(%rsp)
leaq 0x380a(%rip), %rax      # 0xd9ed
leaq 0x2aad(%rip), %rbx      # 0xcc97
cmovlq %rax, %rbx
leaq 0x37d9(%rip), %rsi      # 0xd9ce
movq 0x18(%rsp), %rdi
movq %rbx, %rdx
movb $0x1, %al
callq 0x5814
xorpd %xmm2, %xmm2
movsd 0x30(%rsp), %xmm0
ucomisd %xmm2, %xmm0
leaq 0x37b4(%rip), %r8       # 0xd9cd
movq %r8, %rdx
leaq 0x2db1(%rip), %rcx      # 0xcfd4
cmovaq %rcx, %rdx
movsd 0x28(%rsp), %xmm1
ucomisd %xmm2, %xmm1
cmovaq %rcx, %r8
leaq 0x37b3(%rip), %rsi      # 0xd9ef
movq %rax, %rdi
movq %rbx, %rcx
movb $0x2, %al
callq 0x5814
leaq 0x34b8(%rip), %rsi      # 0xd708
movq %rax, %rdi
callq 0x5593
movq %rax, %rbx
movq 0x68(%rsp), %r14
movq 0x60(%rsp), %r15
jmp 0xa283
callq 0x4ee9
leaq 0x36c9(%rip), %rsi      # 0xd93c
movq %rax, %rdi
movq %r14, %rdx
xorl %eax, %eax
callq 0x5814
movq %rax, %rbx
movq 0x48(%rsp), %rdi
callq 0x2478
testq %rbx, %rbx
je 0xa2a0
movq 0x8(%r15), %rdi
movq %rbx, %rsi
xorl %edx, %edx
callq 0x7cae
movq %rbx, %rdi
callq 0x4f65
movq %r14, %rdi
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x4f65