// File: /workspace/github_c_makefile_star2_O0/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:
subq $0xd8, %rsp
movq %rdi, 0xd0(%rsp)
movl %esi, 0xcc(%rsp)
movq %rdx, 0xc0(%rsp)
movq 0xc0(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x38(%rsp)
movq 0xc0(%rsp), %rax
movq (%rax), %rdi
callq 0xfa70
movq 0x38(%rsp), %rdi
movq %rax, %rsi
subq $0x1, %rsi
callq 0x8300
movq %rax, 0xb8(%rsp)
cmpl $0x1, 0xcc(%rsp)
jle 0x117e4
movq 0xc0(%rsp), %rax
movq 0x8(%rax), %rax
movq %rax, 0x30(%rsp)
jmp 0x117ed
xorl %eax, %eax
movq %rax, 0x30(%rsp)
jmp 0x117ed
movq 0x30(%rsp), %rax
movq %rax, 0xb0(%rsp)
movq $0x0, 0xa8(%rsp)
movq 0xb8(%rsp), %rdi
leaq 0xa4(%rsp), %rsi
callq 0xe490
movq %rax, 0x98(%rsp)
cmpq $0x0, 0x98(%rsp)
jne 0x11859
callq 0x8610
movq %rax, %rdi
movq 0xb8(%rsp), %rdx
leaq 0x527f(%rip), %rsi      # 0x16ac4
movb $0x0, %al
callq 0x98c0
movq %rax, 0xa8(%rsp)
jmp 0x11d13
movl $0x0, 0x94(%rsp)
movl $0x0, 0x90(%rsp)
movl 0x90(%rsp), %eax
cmpl 0xa4(%rsp), %eax
jge 0x118c1
movq 0x98(%rsp), %rax
movslq 0x90(%rsp), %rcx
imulq $0x90, %rcx, %rcx
addq %rcx, %rax
cmpl $0x2, 0x20(%rax)
je 0x118ac
movl $0x1, 0x94(%rsp)
jmp 0x118c1
jmp 0x118ae
movl 0x90(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x90(%rsp)
jmp 0x1186f
leaq 0x5c7c(%rip), %rdi      # 0x17544
callq 0x8630
movq %rax, 0xa8(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0x88(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0x80(%rsp)
movl $0x0, 0x7c(%rsp)
movl 0x7c(%rsp), %eax
cmpl 0xa4(%rsp), %eax
jge 0x11be8
movq 0x98(%rsp), %rax
movslq 0x7c(%rsp), %rcx
imulq $0x90, %rcx, %rcx
addq %rcx, %rax
movq %rax, 0x70(%rsp)
cmpq $0x0, 0xb0(%rsp)
je 0x1198e
movq 0xb0(%rsp), %rax
movq %rax, 0x18(%rsp)
movq 0xb0(%rsp), %rdi
callq 0xfa70
movl %eax, 0x24(%rsp)
movq 0x70(%rsp), %rax
addq $0x24, %rax
movq %rax, 0x28(%rsp)
movq 0x70(%rsp), %rdi
addq $0x24, %rdi
callq 0x20e0
movq 0x18(%rsp), %rdi
movl 0x24(%rsp), %esi
movq 0x28(%rsp), %rdx
movl %eax, %ecx
movl $0x1, %r8d
callq 0xb400
cmpl $0x0, %eax
jne 0x1198c
jmp 0x11bd8
jmp 0x1198e
movq 0x70(%rsp), %rax
movsd 0x88(%rax), %xmm0
movsd %xmm0, 0x68(%rsp)
movq 0x70(%rsp), %rax
cvtsi2sdq 0x10(%rax), %xmm0
movq 0x70(%rsp), %rax
mulsd 0x18(%rax), %xmm0
movsd %xmm0, 0x60(%rsp)
cmpl $0x0, 0x94(%rsp)
je 0x119f9
movq 0x70(%rsp), %rax
cmpl $0x2, 0x20(%rax)
jne 0x119f9
movsd 0x98ef(%rip), %xmm0     # 0x1b2c8
mulsd 0x68(%rsp), %xmm0
movsd %xmm0, 0x68(%rsp)
movsd 0x98db(%rip), %xmm0     # 0x1b2c8
mulsd 0x60(%rsp), %xmm0
movsd %xmm0, 0x60(%rsp)
movsd 0x68(%rsp), %xmm0
addsd 0x88(%rsp), %xmm0
movsd %xmm0, 0x88(%rsp)
movsd 0x60(%rsp), %xmm0
addsd 0x80(%rsp), %xmm0
movsd %xmm0, 0x80(%rsp)
callq 0x8610
movq %rax, 0x58(%rsp)
movq 0x70(%rsp), %rax
movsd 0x70(%rax), %xmm0
movaps 0x35ec(%rip), %xmm1     # 0x15030
pand %xmm1, %xmm0
movsd %xmm0, 0x50(%rsp)
movq 0x58(%rsp), %rdi
movq 0x70(%rsp), %rax
movsd 0x70(%rax), %xmm0
xorps %xmm1, %xmm1
leaq 0x5099(%rip), %rsi      # 0x16b00
leaq 0x508d(%rip), %rax      # 0x16afb
ucomisd %xmm1, %xmm0
cmovaeq %rax, %rsi
callq 0x9350
movq %rax, 0x58(%rsp)
movsd 0x50(%rsp), %xmm0
movsd 0x3b1a(%rip), %xmm1     # 0x155a8
subsd %xmm1, %xmm0
movsd %xmm0, 0x50(%rsp)
movsd 0x50(%rsp), %xmm0
movsd 0x3b02(%rip), %xmm1     # 0x155a8
ucomisd %xmm1, %xmm0
jae 0x11a4e
movq 0xa8(%rsp), %rdi
movq 0x70(%rsp), %rdx
addq $0x24, %rdx
movq 0x70(%rsp), %rax
movq 0x10(%rax), %rax
movl %eax, %ecx
movq 0x70(%rsp), %rax
movsd 0x18(%rax), %xmm0
movq 0x70(%rsp), %rax
movsd 0x18(%rax), %xmm1
movq 0x70(%rsp), %rax
cvtsi2sdq 0x10(%rax), %xmm2
mulsd %xmm2, %xmm1
movq 0x70(%rsp), %rax
movsd 0x68(%rax), %xmm2
xorps %xmm3, %xmm3
leaq 0x5056(%rip), %r8       # 0x16b55
leaq 0x4656(%rip), %rax      # 0x1615c
ucomisd %xmm3, %xmm2
cmovaeq %rax, %r8
movq 0x70(%rsp), %rax
movsd 0x68(%rax), %xmm2
movq 0x70(%rsp), %rax
movsd 0x70(%rax), %xmm3
xorps %xmm4, %xmm4
leaq 0x5029(%rip), %r9       # 0x16b55
leaq 0x4629(%rip), %rax      # 0x1615c
ucomisd %xmm4, %xmm3
cmovaeq %rax, %r9
movq 0x70(%rsp), %rax
movsd 0x70(%rax), %xmm3
movq 0x58(%rsp), %r11
movq 0x70(%rsp), %rax
movsd 0x78(%rax), %xmm4
xorps %xmm5, %xmm5
leaq 0x4ff7(%rip), %r10      # 0x16b55
leaq 0x45f7(%rip), %rax      # 0x1615c
ucomisd %xmm5, %xmm4
cmovaeq %rax, %r10
movq 0x70(%rsp), %rax
movsd 0x78(%rax), %xmm4
movq 0x70(%rsp), %rax
movsd 0x80(%rax), %xmm5
xorps %xmm6, %xmm6
leaq 0x4fc7(%rip), %rax      # 0x16b55
leaq 0x45c7(%rip), %rsi      # 0x1615c
ucomisd %xmm6, %xmm5
cmovaeq %rsi, %rax
movq 0x70(%rsp), %rsi
movsd 0x80(%rsi), %xmm5
leaq 0x4f54(%rip), %rsi      # 0x16b05
movq %r11, (%rsp)
movq %r10, 0x8(%rsp)
movq %rax, 0x10(%rsp)
movb $0x6, %al
callq 0x98c0
movq %rax, 0xa8(%rsp)
movq 0x58(%rsp), %rdi
callq 0x8750
movl 0x7c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x7c(%rsp)
jmp 0x118f5
movsd 0x88(%rsp), %xmm0
subsd 0x80(%rsp), %xmm0
movsd %xmm0, 0x48(%rsp)
movsd 0x88(%rsp), %xmm0
divsd 0x80(%rsp), %xmm0
movsd 0x396e(%rip), %xmm1     # 0x15588
subsd %xmm1, %xmm0
movsd 0x395a(%rip), %xmm1     # 0x15580
mulsd %xmm1, %xmm0
movsd %xmm0, 0x40(%rsp)
movq 0xa8(%rsp), %rdi
movsd 0x88(%rsp), %xmm0
movl 0x94(%rsp), %ecx
leaq 0x41d0(%rip), %rdx      # 0x15e1f
leaq 0x4f1f(%rip), %rax      # 0x16b75
cmpl $0x0, %ecx
cmovneq %rax, %rdx
leaq 0x4ef2(%rip), %rsi      # 0x16b56
movb $0x1, %al
callq 0x98c0
movq %rax, 0xa8(%rsp)
movq 0xa8(%rsp), %rdi
movsd 0x48(%rsp), %xmm0
xorps %xmm1, %xmm1
leaq 0x4eca(%rip), %rdx      # 0x16b55
leaq 0x44ca(%rip), %rax      # 0x1615c
ucomisd %xmm1, %xmm0
cmovaq %rax, %rdx
movsd 0x48(%rsp), %xmm0
movl 0x94(%rsp), %esi
leaq 0x4171(%rip), %rcx      # 0x15e1f
leaq 0x4ec0(%rip), %rax      # 0x16b75
cmpl $0x0, %esi
cmovneq %rax, %rcx
movsd 0x40(%rsp), %xmm1
xorps %xmm2, %xmm2
leaq 0x4e89(%rip), %r8       # 0x16b55
leaq 0x4489(%rip), %rax      # 0x1615c
ucomisd %xmm2, %xmm1
cmovaq %rax, %r8
movsd 0x40(%rsp), %xmm1
leaq 0x4e8f(%rip), %rsi      # 0x16b77
movb $0x2, %al
callq 0x98c0
movq %rax, 0xa8(%rsp)
movq 0xa8(%rsp), %rdi
leaq 0x4b8a(%rip), %rsi      # 0x16890
callq 0x9350
movq %rax, 0xa8(%rsp)
movq 0x98(%rsp), %rdi
callq 0xb3f0
cmpq $0x0, 0xa8(%rsp)
je 0x11d48
movq 0xd0(%rsp), %rax
movq 0x8(%rax), %rdi
movq 0xa8(%rsp), %rsi
xorl %eax, %eax
movl %eax, %edx
callq 0xdd70
movq 0xa8(%rsp), %rdi
callq 0x8750
movq 0xb8(%rsp), %rdi
callq 0x8750
addq $0xd8, %rsp
retq
nopw (%rax,%rax)