// File: /workspace/github_c_makefile_star2_O1/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 $0xc8, %rsp
movl %edx, %r15d
movq %rsi, %r12
movq %rdi, 0x28(%rsp)
movq (%rsi), %rdi
callq 0x58cd
movq %rax, %rbx
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
leaq 0x3753(%rip), %r13      # 0xe7a8
movq %rbx, %rdi
xorl %esi, %esi
movq $-0x2, %rdx
callq 0x6e27
movzbl -0x1(%rbx), %ecx
movl %ecx, %edx
andl $0x7, %edx
movl $0x0, %eax
cmpl $0x4, %edx
ja 0xb0a0
movslq (%r13,%rdx,4), %rax
addq %r13, %rax
jmpq *%rax
shrl $0x3, %ecx
movq %rcx, %rax
jmp 0xb0a0
movq -0x11(%rbx), %rax
jmp 0xb0a0
movzwl -0x5(%rbx), %eax
jmp 0xb0a0
movl -0x9(%rbx), %eax
jmp 0xb0a0
movzbl -0x3(%rbx), %eax
incl %r14d
cmpb $0x3a, -0x1(%rbx,%rax)
je 0xb055
cmpl $0x1, %r15d
jne 0xb1bb
leaq 0x38(%rsp), %rsi
movq %rbx, %rdi
callq 0x976a
movq %rax, %r13
callq 0x5889
testq %r13, %r13
je 0xb275
leaq 0x49fb(%rip), %rsi      # 0xfad4
movq %rax, %rdi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x6581
movq %rax, %r12
cmpl $0x0, 0x38(%rsp)
jle 0xb4b8
xorl %ebp, %ebp
cmpl $0x1, %r14d
movl $0x0, %eax
adcl $0x1, %eax
movl %eax, 0x10(%rsp)
movslq 0x38(%rsp), %rax
movq %rax, 0x30(%rsp)
xorpd %xmm0, %xmm0
movsd %xmm0, 0x18(%rsp)
movl $0x0, 0xc(%rsp)
movq %r13, %r15
movq (%r13,%rbp,8), %rsi
xorl %edi, %edi
xorl %edx, %edx
xorl %ecx, %ecx
movl $0x3c, %r8d
callq 0x8b50
movq %rax, %r13
testq %rax, %rax
je 0xb162
incl 0xc(%rsp)
movq 0x58(%r13), %rdi
xorl %esi, %esi
callq 0x21b0
movsd 0x18(%rsp), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, 0x18(%rsp)
movq (%r15,%rbp,8), %rsi
movq %r12, %rdi
movq %r13, %rdx
movl 0x10(%rsp), %ecx
callq 0x9e70
movq %rax, %r12
movq %r13, %rdi
movq %r15, %r13
callq 0x8abe
movl %ebp, %eax
andl $0x1, %eax
orl %r14d, %eax
leaq 0x4bf2(%rip), %rsi      # 0xfd84
leaq 0x4851(%rip), %rax      # 0xf9ea
cmoveq %rax, %rsi
movq %r12, %rdi
callq 0x61f0
movq %rax, %r12
incq %rbp
cmpq 0x30(%rsp), %rbp
jl 0xb122
jmp 0xb4ca
movq 0x8(%r12), %r14
leaq 0x494b(%rip), %rsi      # 0xfb12
movq %r14, %rdi
callq 0x22a0
cmpl $0x3, %r15d
jl 0xb218
testl %eax, %eax
jne 0xb218
cmpl $0x4, %r15d
jl 0xb379
leal -0x1(%r15), %eax
movq %rax, 0x10(%rsp)
movq (%r12,%rax,8), %rax
movsbq (%rax), %r14
cmpq $0x40, %r14
je 0xb20e
callq 0x2290
movq (%rax), %rax
testb $0x8, 0x1(%rax,%r14,2)
je 0xb37f
movq 0x10(%rsp), %r15
jmp 0xb388
leaq 0x494b(%rip), %rsi      # 0xfb6a
movq %r14, %rdi
callq 0x22a0
cmpl $0x3, %r15d
jne 0xb28e
testl %eax, %eax
jne 0xb28e
movq 0x10(%r12), %rdi
xorl %esi, %esi
movl $0xa, %edx
callq 0x2060
testq %rax, %rax
je 0xb603
leaq 0x4920(%rip), %rdi      # 0xfb72
movq %rax, %rsi
xorl %eax, %eax
callq 0x8545
testl %eax, %eax
leaq 0x494a(%rip), %rax      # 0xfbaf
leaq 0x492c(%rip), %rdi      # 0xfb98
cmoveq %rax, %rdi
jmp 0xb60a
leaq 0x4848(%rip), %rsi      # 0xfac4
movq %rax, %rdi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x6581
jmp 0xb4f4
leaq 0x48d8(%rip), %rsi      # 0xfb6d
movq %r14, %rdi
callq 0x22a0
leal -0x3(%r15), %ecx
cmpl $0x1, %ecx
ja 0xb508
testl %eax, %eax
jne 0xb508
xorl %eax, %eax
movq %rax, 0x20(%rsp)
movq 0x10(%r12), %r13
movq %rax, (%rsp)
cmpl $0x4, %r15d
jne 0xb2de
movq 0x18(%r12), %rdi
leaq 0x20(%rsp), %rsi
movq %rsp, %rdx
xorpd %xmm0, %xmm0
callq 0xaf2c
xorl %edi, %edi
movq %r13, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
movl $0xe10, %r8d            # imm = 0xE10
callq 0x8b50
testq %rax, %rax
je 0xb617
movq %rax, %r14
movsd (%rsp), %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0xb31d
jp 0xb31d
xorps %xmm0, %xmm0
cvtss2sd 0x20(%r14), %xmm0
movsd %xmm0, (%rsp)
movl 0x20(%rsp), %edx
movsd (%rsp), %xmm0
movq 0x70(%r14), %rcx
leaq 0x38(%rsp), %r15
movq %rbx, %rdi
movq %r13, %rsi
movq %r15, %r8
callq 0x9a04
movq 0x10(%r15), %r15
testl %eax, %eax
je 0xb632
callq 0x5889
testq %r15, %r15
je 0xb668
movsd 0x50(%rsp), %xmm0
leaq 0x48c9(%rip), %rsi      # 0xfc2d
movq %rax, %rdi
movl %r15d, %edx
movq %r13, %rcx
movb $0x1, %al
callq 0x6581
jmp 0xb69e
movl $0x3, %r15d
movq $0x0, 0x10(%rsp)
cmpl $0x3, %r15d
jl 0xb4b0
movl %r15d, %eax
movq %rax, 0x18(%rsp)
movl $0x2, %r14d
movq (%r12,%r14,8), %rbp
xorl %edi, %edi
movq %rbp, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
movl $0xe10, %r8d            # imm = 0xE10
callq 0x8b50
testq %rax, %rax
je 0xb45d
movq %rax, %r13
movq $0x1, 0x20(%rsp)
movq $0x0, (%rsp)
movq 0x10(%rsp), %rax
movq (%r12,%rax,8), %rdi
xorps %xmm0, %xmm0
cvtss2sd 0x20(%r13), %xmm0
leaq 0x20(%rsp), %rsi
movq %rsp, %rdx
callq 0xaf2c
movsd (%rsp), %xmm0
ucomisd 0x32f7(%rip), %xmm0     # 0xe6f8
jne 0xb413
jp 0xb413
xorps %xmm0, %xmm0
cvtss2sd 0x20(%r13), %xmm0
movsd %xmm0, (%rsp)
movq %r13, %rdi
callq 0x8abe
movsd (%rsp), %xmm0
movl 0x20(%rsp), %edx
movq %rbx, %rdi
movq %rbp, %rsi
leaq 0x38(%rsp), %rcx
callq 0x9907
testl %eax, %eax
je 0xb478
callq 0x5889
movl 0x48(%rsp), %edx
movsd 0x50(%rsp), %xmm0
movq %rax, %rdi
leaq 0x46e2(%rip), %rsi      # 0xfb33
movq %rbp, %rcx
movb $0x1, %al
callq 0x6581
jmp 0xb484
callq 0x5889
movq %rax, %rdi
leaq 0x4000(%rip), %rsi      # 0xf46c
movq %rbp, %rdx
xorl %eax, %eax
callq 0x6581
jmp 0xb484
leaq 0x4697(%rip), %rdi      # 0xfb16
callq 0x58ac
movq %rax, %r13
movq 0x28(%rsp), %rax
movq 0x8(%rax), %rdi
movq %r13, %rsi
xorl %edx, %edx
callq 0x91e5
movq %r13, %rdi
callq 0x591d
incq %r14
cmpq %r14, 0x18(%rsp)
jne 0xb3a0
xorl %r12d, %r12d
jmp 0xb6a9
movl $0x0, 0xc(%rsp)
xorpd %xmm0, %xmm0
movsd %xmm0, 0x18(%rsp)
movl 0xc(%rsp), %edx
testl %edx, %edx
je 0xb4f7
xorps %xmm1, %xmm1
cvtsi2sd %edx, %xmm1
movsd 0x18(%rsp), %xmm0
divsd %xmm1, %xmm0
leaq 0x45ff(%rip), %rsi      # 0xfae9
movq %r12, %rdi
movb $0x1, %al
callq 0x6581
movq %rax, %r12
movl 0x38(%rsp), %esi
movq %r13, %rdi
callq 0x7258
jmp 0xb6a9
cmpl $0x2, %r15d
setge %bpl
jl 0xb573
movl %r15d, %r15d
movl $0x1, %r13d
subq %r15, %r13
movl $0x2, %r14d
movq -0x8(%r12,%r14,8), %rsi
movzbl (%rsi), %eax
cmpl $0x3f, %eax
je 0xb55c
cmpl $0x2d, %eax
je 0xb54c
cmpl $0x2b, %eax
jne 0xb67e
incq %rsi
movq %rbx, %rdi
callq 0x971d
jmp 0xb55c
incq %rsi
movq %rbx, %rdi
movl $0x1, %edx
callq 0x949a
cmpq %r15, %r14
setb %bpl
leaq (%r14,%r13), %rax
incq %rax
incq %r14
cmpq $0x2, %rax
jne 0xb524
xorl %r12d, %r12d
testb $0x1, %bpl
jne 0xb6a9
leaq 0x38(%rsp), %rsi
movq %rbx, %rdi
callq 0x976a
movq %rax, %r14
callq 0x5889
testq %r14, %r14
je 0xb640
leaq 0x472b(%rip), %rsi      # 0xfcd0
movq %rax, %rdi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x6581
movq %rax, %r15
cmpl $0x0, 0x38(%rsp)
jle 0xb657
xorl %r13d, %r13d
leaq 0x4727(%rip), %r12      # 0xfcf1
movq (%r14,%r13,8), %rsi
movq %r15, %rdi
callq 0x6217
movq %rax, %r15
movslq 0x38(%rsp), %rax
decq %rax
cmpq %rax, %r13
jge 0xb5f4
movq %r15, %rdi
movq %r12, %rsi
callq 0x61f0
movq %rax, %r15
incq %r13
movslq 0x38(%rsp), %rax
cmpq %rax, %r13
jl 0xb5ca
jmp 0xb657
leaq 0x45be(%rip), %rdi      # 0xfbc8
callq 0x58ac
movq %rax, %r12
jmp 0xb6a9
callq 0x5889
leaq 0x3e49(%rip), %rsi      # 0xf46c
movq %rax, %rdi
movq %r13, %rdx
xorl %eax, %eax
callq 0x6581
jmp 0xb60f
testq %r15, %r15
js 0xb692
leaq 0x45cb(%rip), %rdi      # 0xfc09
jmp 0xb699
leaq 0x466b(%rip), %rsi      # 0xfcb2
movq %rax, %rdi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x6581
movq %rax, %r15
movl 0x38(%rsp), %esi
movq %r14, %rdi
callq 0x7258
movq %r15, %r12
jmp 0xb6a9
leaq 0x45fa(%rip), %rsi      # 0xfc69
movq %rax, %rdi
movq %r13, %rdx
xorl %eax, %eax
callq 0x6581
jmp 0xb69e
leaq 0x4601(%rip), %rdi      # 0xfc86
callq 0x58ac
movq %rax, %r12
jmp 0xb576
leaq 0x454a(%rip), %rdi      # 0xfbe3
callq 0x58ac
movq %rax, %r12
movq %r14, %rdi
callq 0x8abe
testq %r12, %r12
je 0xb6c1
movq 0x28(%rsp), %rax
movq 0x8(%rax), %rdi
movq %r12, %rsi
xorl %edx, %edx
callq 0x91e5
movq %r12, %rdi
callq 0x591d
movq %rbx, %rdi
callq 0x591d
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq