// File: /workspace/github_c_makefile_star2_O3/antirez[P]stonky/stonky.c
// Function: botHandleListRequest
void botHandleListRequest(botRequest *br, sds *argv, int argc) {
    int verb = 0; /* Verbosity level. Increases for every additional
                     trailing ":" after the list name. */

    /* Remove the final ":" from the list name. */
    sds listname = sdsdup(argv[0]);
    do {
        verb++;
        sdsrange(listname,0,-2);
    } while(listname[sdslen(listname)-1] == ':');
    verb--;
    sds reply = NULL;

    if (argc == 1) {
        /* If it's just the list name, reply with a list of stocks
         * and their current price. */
        int numstocks;
        sds *stocks = dbGetStocksFromList(listname,&numstocks);
        if (stocks == NULL) {
            reply = sdscatprintf(sdsempty(),"No such list %s", listname);
        } else {
            reply = sdscatprintf(sdsempty(),"Prices for list %s:\n", listname);
            double avg = 0;
            int fetched = 0;
            for (int j = 0; j < numstocks; j++) {
                ydata *yd = getYahooData(YDATA_QUOTE,stocks[j],NULL,NULL,60);
                if (yd) {
                    fetched++;
                    avg += strtod(yd->regchange,NULL);
                }
                int format = verb > 0 ? STONKY_SHORT : STONKY_VERY_SHORT;
                reply = sdsCatPriceRequest(reply,stocks[j],yd,format);
                freeYahooData(yd);
                if (verb > 0 || (j % 2)) {
                    reply = sdscat(reply,"\n");
                } else {
                    reply = sdscat(reply," | ");
                }
            }
            if (fetched) {
                reply = sdscatprintf(reply,
                    "%d stocks. Average performance: %.2f%%.\n",
                    fetched, avg/fetched);
            }
        }
        sdsfreesplitres(stocks,numstocks);
    } else if (!strcasecmp(argv[1],"buy") && argc >= 3) {
        /* $list: buy SYMBOL [SYMBOL2 ...] [100@price] */

        /* If we have the last argument that starts with a number or
         * with '@', then it is the optional price/quantity argument. */
        int pricearg = 0;
        if (argc >= 4 && (argv[argc-1][0] == '@' ||
                          isdigit(argv[argc-1][0])))
        {
            pricearg = argc-1;
            argc--; /* Don't consider the last argument. */
        }

        /* Buy every stock listed. */
        for (int j = 2; j < argc; j++) {
            sds symbol = argv[j];
            /* Check that the symbol exists. */
            ydata *yd = getYahooData(YDATA_QUOTE,symbol,NULL,NULL,3600);
            if (yd == NULL) {
                reply = sdscatprintf(sdsempty(),
                    "Stock symbol %s not found.", symbol);
                botSendMessage(br->target,reply,0);
                sdsfree(reply);
                reply = NULL;
                continue;
            }

            /* Parse price/quantity. */
            int64_t quantity = 1;
            double price = 0;
            parseQuantityAndPrice(argv[pricearg],&quantity,&price,yd->reg);
            if (price == 0) price = yd->reg;
            freeYahooData(yd);
            yd = NULL;

            /* Ready to materialize the buy operation on the DB. */
            stockpack sp;
            if (dbBuyStocks(listname,symbol,price,quantity,&sp) == C_ERR) {
                reply = sdsnew("Error adding the stock pack.");
            } else {
                reply = sdscatprintf(sdsempty(),
                    "Now you have %d %s stocks at an average price of %.2f.",
                    (int)sp.quantity,symbol,sp.avgprice);
            }
            botSendMessage(br->target,reply,0);
            sdsfree(reply);
            reply = NULL;
        }
    } else if (!strcasecmp(argv[1],"rm-sell") && argc == 3) {
        int64_t id = strtoll(argv[2],NULL,10);
        if (id) {
            int found = sqlQuery("DELETE FROM ProfitLoss WHERE rowid=?i",id);
            reply = sdsnew(found ?
                            "History entry removed." :
                            "History entry not found.");
        } else {
            reply = sdsnew("Please specify a valid ID.");
        }
    } else if (!strcasecmp(argv[1],"sell") && (argc == 3 || argc == 4)) {
        /* $list: sell [quantity] */
        int64_t quantity = 0; /* All the stocks. */
        sds symbol = argv[2];
        double sellprice = 0;

        /* Parse the quantity@price argument if available. */
        if (argc == 4) parseQuantityAndPrice(argv[3],&quantity,&sellprice,0);

        /* Check that the symbol exists. */
        ydata *yd = getYahooData(YDATA_QUOTE,symbol,NULL,NULL,3600);
        if (yd == NULL) {
            reply = sdscatprintf(sdsempty(),
                "Stock symbol %s not found.", symbol);
            goto fmterr;
        }
        if (sellprice == 0) sellprice = yd->reg;

        stockpack sp;
        if (!dbSellStocks(listname,symbol,quantity,sellprice,yd->csym,&sp)) {
            if (sp.quantity < 0) {
                reply = sdsnew("You don't have enough stocks to sell.");
            } else {
                reply = sdsnew("Error removing from the stock pack.");
            }
        } else {
            if (sp.quantity != 0) {
                reply = sdscatprintf(sdsempty(),
                    "You are left with %d %s stocks at an average "
                    "price of %.2f.",
                    (int)sp.quantity,symbol,sp.avgprice);
            } else {
                reply = sdscatprintf(sdsempty(),
                    "You no longer own %s stocks.",symbol);
            }
        }
        freeYahooData(yd);
    } else {
        /* Otherwise we are in edit mode, with +... -... symbols. */
        for (int j = 1; j < argc; j++) {
            if (argv[j][0] == '+') {
                /* +SYMBOL */
                dbAddStockToList(listname,argv[j]+1);
            } else if (argv[j][0] == '-') {
                /* -SYMBOL. */
                dbDelStockFromList(listname,argv[j]+1,1);
            } else if (argv[j][0] == '?') {
                /* Do nothing, we want just the list of symbols. */
            } else {
                reply = sdsnew("Syntax error: use +AAPL -TWTR and so forth.");
                goto fmterr;
            }
        }

        /* Show the new composition of the list. */
        int numstocks;
        sds *stocks = dbGetStocksFromList(listname,&numstocks);
        if (stocks == NULL) {
            reply = sdscatprintf(sdsempty(),"The list %s no longer exists.",
                listname);
        } else {
            reply = sdscatprintf(sdsempty(),"The list %s is now composed of: ",
                listname);
            for (int j = 0; j < numstocks; j++) {
                reply = sdscatsds(reply,stocks[j]);
                if (j < numstocks-1) reply = sdscat(reply,", ");
            }
        }
        sdsfreesplitres(stocks,numstocks);
    }

fmterr:
    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 $0xd8, %rsp
movl %edx, %r15d
movq %rsi, %rbp
movq %rdi, 0x30(%rsp)
movq (%rsi), %rdi
callq 0x5725
movq %rax, %rbx
movl $0x1, %r13d
leaq 0x3a04(%rip), %r14      # 0xe784
movq %rbx, %rdi
xorl %esi, %esi
movq $-0x2, %rdx
callq 0x6c29
movzbl -0x1(%rbx), %ecx
movl %ecx, %edx
andl $0x7, %edx
movl $0x0, %eax
cmpl $0x4, %edx
ja 0xadca
movslq (%r14,%rdx,4), %rax
addq %r14, %rax
jmpq *%rax
shrl $0x3, %ecx
movq %rcx, %rax
jmp 0xadca
movq -0x11(%rbx), %rax
jmp 0xadca
movzwl -0x5(%rbx), %eax
jmp 0xadca
movl -0x9(%rbx), %eax
jmp 0xadca
movzbl -0x3(%rbx), %eax
decl %r13d
cmpb $0x3a, -0x1(%rbx,%rax)
je 0xad80
cmpl $0x1, %r15d
jne 0xaf4a
leaq 0x48(%rsp), %rsi
movq %rbx, %rdi
callq 0x94c5
movq %rax, %r14
callq 0x56c6
movq %r14, 0x28(%rsp)
testq %r14, %r14
je 0xb001
leaq 0x4cac(%rip), %rsi      # 0xfab4
movq %rax, %rdi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x6382
movq %rax, %r15
movslq 0x48(%rsp), %r12
testq %r12, %r12
jle 0xb01d
xorl %ecx, %ecx
testl %r13d, %r13d
sete %cl
leaq 0x44d7(%rip), %rax      # 0xf30c
leaq 0x44e9(%rip), %rdx      # 0xf325
cmovneq %rax, %rdx
movq %rdx, 0x40(%rsp)
incl %ecx
movl %ecx, 0x3c(%rsp)
xorpd %xmm0, %xmm0
movsd %xmm0, 0x10(%rsp)
xorl %r14d, %r14d
movl $0x0, 0x24(%rsp)
movq %r12, (%rsp)
movq 0x28(%rsp), %rax
movq (%rax,%r14,8), %rbp
xorl %edi, %edi
movq %rbp, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
movl $0x3c, %r8d
callq 0x88ec
testq %rax, %rax
je 0xaec7
movq %rax, %r12
incl 0x24(%rsp)
movq 0x58(%rax), %rdi
xorl %esi, %esi
callq 0x21c0
movsd 0x10(%rsp), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, 0x10(%rsp)
movq %r15, %rdi
movq %rbp, %rsi
movq %r12, %rdx
movl 0x3c(%rsp), %ecx
callq 0x9be2
movq %rax, %r15
movq %r12, %rdi
callq 0x885a
jmp 0xaedc
movq %r15, %rdi
movq 0x40(%rsp), %rsi
movq %rbp, %rdx
xorl %eax, %eax
callq 0x6382
movq %rax, %r15
testl %r13d, %r13d
leaq 0x4ae4(%rip), %rsi      # 0xf9ca
leaq 0x4e77(%rip), %rax      # 0xfd64
cmovneq %rax, %rsi
testb $0x1, %r14b
cmovneq %rax, %rsi
movq %r15, %rdi
callq 0x5ff1
movq %rax, %r15
incq %r14
movq (%rsp), %r12
cmpq %r14, %r12
jne 0xae64
movl 0x24(%rsp), %edx
testl %edx, %edx
je 0xb01d
xorps %xmm1, %xmm1
cvtsi2sd %edx, %xmm1
movsd 0x10(%rsp), %xmm0
divsd %xmm1, %xmm0
leaq 0x4b91(%rip), %rsi      # 0xfac9
movq %r15, %rdi
movb $0x1, %al
callq 0x6382
movq %rax, %r15
jmp 0xb01d
movq 0x8(%rbp), %r14
leaq 0x4b9d(%rip), %rsi      # 0xfaf2
movq %r14, %rdi
callq 0x22b0
cmpl $0x3, %r15d
jl 0xafa5
testl %eax, %eax
jne 0xafa5
cmpl $0x4, %r15d
jb 0xb118
leal -0x1(%r15), %eax
movq %rax, (%rsp)
movq (%rbp,%rax,8), %rax
movsbq (%rax), %r14
cmpq $0x40, %r14
je 0xaf9c
callq 0x22a0
movq (%rax), %rax
testb $0x8, 0x1(%rax,%r14,2)
je 0xb11e
movq (%rsp), %r15
jmp 0xb126
leaq 0x4b9e(%rip), %rsi      # 0xfb4a
movq %r14, %rdi
callq 0x22b0
cmpl $0x3, %r15d
jne 0xb02f
testl %eax, %eax
jne 0xb02f
movq 0x10(%rbp), %rdi
xorl %esi, %esi
movl $0xa, %edx
callq 0x2060
testq %rax, %rax
je 0xb329
leaq 0x4b74(%rip), %rdi      # 0xfb52
movq %rax, %rsi
xorl %eax, %eax
callq 0x82bd
testl %eax, %eax
leaq 0x4b9e(%rip), %rax      # 0xfb8f
leaq 0x4b80(%rip), %rdi      # 0xfb78
cmoveq %rax, %rdi
jmp 0xb330
leaq 0x4a9c(%rip), %rsi      # 0xfaa4
movq %rax, %rdi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x6382
movq %rax, %r15
movl 0x48(%rsp), %r12d
movq 0x28(%rsp), %rdi
movl %r12d, %esi
callq 0x7056
jmp 0xb338
leaq 0x4b17(%rip), %rsi      # 0xfb4d
movq %r14, %rdi
callq 0x22b0
leal -0x3(%r15), %ecx
cmpl $0x1, %ecx
ja 0xb259
testl %eax, %eax
jne 0xb259
xorl %eax, %eax
movq %rax, 0x18(%rsp)
movq 0x10(%rbp), %r13
movq %rax, 0x8(%rsp)
cmpl $0x4, %r15d
jne 0xb080
movq 0x18(%rbp), %rdi
leaq 0x18(%rsp), %rsi
leaq 0x8(%rsp), %rdx
xorpd %xmm0, %xmm0
callq 0xac57
xorl %edi, %edi
movq %r13, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
movl $0xe10, %r8d            # imm = 0xE10
callq 0x88ec
testq %rax, %rax
je 0xb377
movq %rax, %r14
movsd 0x8(%rsp), %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0xb0c1
jp 0xb0c1
xorps %xmm0, %xmm0
cvtss2sd 0x20(%r14), %xmm0
movsd %xmm0, 0x8(%rsp)
movl 0x18(%rsp), %edx
movq 0x70(%r14), %rcx
leaq 0x48(%rsp), %r15
movq %rbx, %rdi
movq %r13, %rsi
movq %r15, %r8
callq 0x9753
movq 0x10(%r15), %r15
testl %eax, %eax
je 0xb3b6
callq 0x56c6
testq %r15, %r15
je 0xb3c4
movsd 0x60(%rsp), %xmm0
leaq 0x4b0a(%rip), %rsi      # 0xfc0d
movq %rax, %rdi
movl %r15d, %edx
movq %r13, %rcx
movb $0x1, %al
callq 0x6382
jmp 0xb3f2
movl $0x3, %r15d
movq $0x0, (%rsp)
movl %r15d, %eax
movq %rax, 0x28(%rsp)
movl $0x2, %r12d
movq (%rbp,%r12,8), %r15
xorl %edi, %edi
movq %r15, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
movl $0xe10, %r8d            # imm = 0xE10
callq 0x88ec
testq %rax, %rax
je 0xb1fb
movq %rax, %r13
movq $0x1, 0x18(%rsp)
movq $0x0, 0x8(%rsp)
movq %rbp, %r14
movq (%rsp), %rax
movq (%rbp,%rax,8), %rdi
xorps %xmm0, %xmm0
cvtss2sd 0x20(%r13), %xmm0
leaq 0x18(%rsp), %rsi
leaq 0x8(%rsp), %rdx
callq 0xac57
movsd 0x8(%rsp), %xmm0
ucomisd 0x354b(%rip), %xmm0     # 0xe6e8
jne 0xb1aa
jp 0xb1aa
xorps %xmm0, %xmm0
cvtss2sd 0x20(%r13), %xmm0
movsd %xmm0, 0x10(%rsp)
movq %r13, %rdi
callq 0x885a
movl 0x18(%rsp), %edx
movq %rbx, %rdi
movq %r15, %rsi
movsd 0x10(%rsp), %xmm0
leaq 0x48(%rsp), %rcx
callq 0x9656
testl %eax, %eax
je 0xb219
callq 0x56c6
movl 0x58(%rsp), %edx
movsd 0x60(%rsp), %xmm0
movq %rax, %rdi
leaq 0x4924(%rip), %rsi      # 0xfb13
movq %r15, %rcx
movb $0x1, %al
callq 0x6382
jmp 0xb225
callq 0x56c6
movq %rax, %rdi
leaq 0x4242(%rip), %rsi      # 0xf44c
movq %r15, %rdx
xorl %eax, %eax
callq 0x6382
movq %rax, %r13
jmp 0xb22b
leaq 0x48d6(%rip), %rdi      # 0xfaf6
callq 0x56e9
movq %rax, %r13
movq %r14, %rbp
movq 0x30(%rsp), %rax
movq 0x8(%rax), %rdi
movq %r13, %rsi
xorl %edx, %edx
callq 0x8f4e
movq %r13, %rdi
callq 0x5775
incq %r12
cmpq %r12, 0x28(%rsp)
jne 0xb134
jmp 0xb352
cmpl $0x2, %r15d
jl 0xb2a8
movl %r15d, %r14d
movl $0x1, %r15d
movq (%rbp,%r15,8), %rsi
movzbl (%rsi), %eax
cmpl $0x3f, %eax
je 0xb2a0
cmpl $0x2d, %eax
je 0xb290
cmpl $0x2b, %eax
jne 0xb3da
incq %rsi
movq %rbx, %rdi
callq 0x9478
jmp 0xb2a0
incq %rsi
movq %rbx, %rdi
movl $0x1, %edx
callq 0x91ff
incq %r15
cmpq %r15, %r14
jne 0xb268
leaq 0x48(%rsp), %rsi
movq %rbx, %rdi
callq 0x94c5
movq %rax, %r14
callq 0x56c6
testq %r14, %r14
je 0xb392
leaq 0x49e3(%rip), %rsi      # 0xfcb0
movq %rax, %rdi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x6382
movq %rax, %r15
movslq 0x48(%rsp), %r12
testq %r12, %r12
jle 0xb3ae
movq %r15, %rdi
leal -0x1(%r12), %ebp
xorl %r13d, %r13d
leaq 0x49d4(%rip), %r15      # 0xfcd1
movq (%r14,%r13,8), %rsi
callq 0x6018
cmpq %rbp, %r13
jae 0xb316
movq %rax, %rdi
movq %r15, %rsi
callq 0x5ff1
movq %rax, %rdi
incq %r13
cmpq %r13, %r12
jne 0xb2fd
movq %rdi, %r15
jmp 0xb3ae
leaq 0x4878(%rip), %rdi      # 0xfba8
callq 0x56e9
movq %rax, %r15
testq %r15, %r15
je 0xb352
movq 0x30(%rsp), %rax
movq 0x8(%rax), %rdi
movq %r15, %rsi
xorl %edx, %edx
callq 0x8f4e
jmp 0xb355
xorl %r15d, %r15d
movq %r15, %rdi
callq 0x5775
movq %rbx, %rdi
callq 0x5775
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x56c6
leaq 0x40c9(%rip), %rsi      # 0xf44c
movq %rax, %rdi
movq %r13, %rdx
xorl %eax, %eax
callq 0x6382
jmp 0xb335
leaq 0x48f9(%rip), %rsi      # 0xfc92
movq %rax, %rdi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x6382
movq %rax, %r15
movl 0x48(%rsp), %r12d
movq %r14, %rdi
jmp 0xb022
testq %r15, %r15
js 0xb3e6
leaq 0x4827(%rip), %rdi      # 0xfbe9
jmp 0xb3ed
leaq 0x487e(%rip), %rsi      # 0xfc49
movq %rax, %rdi
movq %r13, %rdx
xorl %eax, %eax
callq 0x6382
jmp 0xb3f2
leaq 0x4885(%rip), %rdi      # 0xfc66
jmp 0xb330
leaq 0x47d6(%rip), %rdi      # 0xfbc3
callq 0x56e9
movq %rax, %r15
movq %r14, %rdi
callq 0x885a
jmp 0xb338