// File: /workspace/github_c_makefile_star2_O2/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, %r13d
movq %rsi, %r12
movq %rdi, 0x38(%rsp)
movq (%rsi), %rdi
callq 0x4f18
movq %rax, %rbx
pushq $-0x1
popq %r14
pushq $-0x2
popq %r15
movq %rbx, %rdi
xorl %esi, %esi
movq %r15, %rdx
callq 0x5c53
movq %rbx, %rdi
callq 0x8c9f
incl %r14d
cmpb $0x3a, -0x1(%rbx,%rax)
je 0x98fe
cmpl $0x1, %r13d
jne 0x9a36
leaq 0x48(%rsp), %rsi
movq %rbx, %rdi
callq 0x8238
movq %rax, %r15
callq 0x4ee9
movq %r15, 0x30(%rsp)
testq %r15, %r15
je 0x9b1e
leaq 0x3dcb(%rip), %rsi      # 0xd71c
xorl %r13d, %r13d
movq %rax, %rdi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x5814
movq %rax, %r15
movl 0x48(%rsp), %eax
cmpl $0x1, %r14d
movl $0x0, %ecx
adcl $0x1, %ecx
movl %ecx, 0x44(%rsp)
testl %eax, %eax
movl $0x0, %ecx
movl %eax, 0x28(%rsp)
cmovgl %eax, %ecx
movq %rcx, 0x10(%rsp)
xorpd %xmm0, %xmm0
movsd %xmm0, 0x8(%rsp)
movl $0x0, 0x2c(%rsp)
cmpq %r13, 0x10(%rsp)
je 0x9a93
movq 0x30(%rsp), %rax
movq (%rax,%r13,8), %rbp
xorl %edi, %edi
movq %rbp, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
pushq $0x3c
popq %r8
callq 0x7676
movq %rax, %r12
testq %rax, %rax
je 0x99eb
incl 0x2c(%rsp)
movq 0x58(%r12), %rdi
xorl %esi, %esi
callq 0x21c0
movsd 0x8(%rsp), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, 0x8(%rsp)
movq %r15, %rdi
movq %rbp, %rsi
movq %r12, %rdx
movl 0x44(%rsp), %ecx
callq 0x88de
movq %rax, %r15
movq %r12, %rdi
callq 0x75e4
movl %r13d, %eax
andl $0x1, %eax
orl %r14d, %eax
leaq 0x3fb4(%rip), %rsi      # 0xd9cc
leaq 0x3c13(%rip), %rax      # 0xd632
cmoveq %rax, %rsi
movq %r15, %rdi
callq 0x5593
movq %rax, %r15
incq %r13
jmp 0x999d
movq 0x8(%r12), %r15
leaq 0x3d18(%rip), %rsi      # 0xd75a
movq %r15, %rdi
callq 0x22b0
cmpl $0x3, %r13d
jl 0x9ac3
testl %eax, %eax
jne 0x9ac3
cmpl $0x4, %r13d
jb 0x9c31
leal -0x1(%r13), %eax
movq %rax, 0x10(%rsp)
movq (%r12,%rax,8), %rax
movsbq (%rax), %r14
cmpq $0x40, %r14
je 0x9a89
callq 0x22a0
movq (%rax), %rax
testb $0x8, 0x1(%rax,%r14,2)
je 0x9c35
movq 0x10(%rsp), %r13
jmp 0x9c3e
movl 0x2c(%rsp), %edx
testl %edx, %edx
je 0x9abd
cvtsi2sd %edx, %xmm1
movsd 0x8(%rsp), %xmm0
divsd %xmm1, %xmm0
leaq 0x3c81(%rip), %rsi      # 0xd731
movq %r15, %rdi
movb $0x1, %al
callq 0x5814
movq %rax, %r15
movl 0x28(%rsp), %esi
jmp 0x9b39
leaq 0x3ce8(%rip), %rsi      # 0xd7b2
movq %r15, %rdi
callq 0x22b0
cmpl $0x3, %r13d
jne 0x9b48
testl %eax, %eax
jne 0x9b48
movq 0x10(%r12), %rdi
pushq $0xa
popq %rdx
xorl %esi, %esi
callq 0x2060
testq %rax, %rax
je 0x9e2a
leaq 0x3cbf(%rip), %rdi      # 0xd7ba
movq %rax, %rsi
xorl %eax, %eax
callq 0x7083
testl %eax, %eax
leaq 0x3ce9(%rip), %rax      # 0xd7f7
leaq 0x3ccb(%rip), %rdi      # 0xd7e0
cmoveq %rax, %rdi
jmp 0x9e7f
leaq 0x3be7(%rip), %rsi      # 0xd70c
movq %rax, %rdi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x5814
movq %rax, %r15
movl 0x48(%rsp), %esi
movq 0x30(%rsp), %rdi
callq 0x5f72
jmp 0x9e87
leaq 0x3c66(%rip), %rsi      # 0xd7b5
movq %r15, %rdi
callq 0x22b0
leal -0x3(%r13), %ecx
cmpl $0x1, %ecx
ja 0x9d5f
testl %eax, %eax
jne 0x9d5f
andq $0x0, 0x20(%rsp)
movq 0x10(%r12), %r15
andq $0x0, 0x18(%rsp)
cmpl $0x4, %r13d
jne 0x9b9b
movq 0x18(%r12), %rdi
leaq 0x20(%rsp), %rsi
leaq 0x18(%rsp), %rdx
xorpd %xmm0, %xmm0
callq 0x981b
xorl %edi, %edi
movq %r15, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
movl $0xe10, %r8d            # imm = 0xE10
callq 0x7676
testq %rax, %rax
je 0x9e3a
movq %rax, %r14
movsd 0x18(%rsp), %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x9bd9
jp 0x9bd9
cvtss2sd 0x20(%r14), %xmm0
movsd %xmm0, 0x18(%rsp)
movl 0x20(%rsp), %edx
movq 0x70(%r14), %rcx
leaq 0x48(%rsp), %r12
movq %rbx, %rdi
movq %r15, %rsi
movq %r12, %r8
callq 0x8486
movq 0x10(%r12), %r12
testl %eax, %eax
je 0x9ec6
callq 0x4ee9
testq %r12, %r12
je 0x9ed4
movsd 0x60(%rsp), %xmm0
leaq 0x3c59(%rip), %rsi      # 0xd875
movq %rax, %rdi
movl %r12d, %edx
movq %r15, %rcx
movb $0x1, %al
callq 0x5814
jmp 0x9ef6
pushq $0x3
popq %r13
movq $0x0, 0x10(%rsp)
movl %r13d, %eax
movq %rax, 0x30(%rsp)
pushq $0x2
popq %r14
xorl %r15d, %r15d
cmpq 0x30(%rsp), %r14
jae 0x9ea4
movq (%r12,%r14,8), %rbp
xorl %edi, %edi
movq %rbp, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
movl $0xe10, %r8d            # imm = 0xE10
callq 0x7676
testq %rax, %rax
je 0x9d12
movq %rax, %r13
andq $0x0, 0x18(%rsp)
movq $0x1, 0x20(%rsp)
movq 0x10(%rsp), %rax
movq (%r12,%rax,8), %rdi
cvtss2sd 0x20(%r13), %xmm0
leaq 0x20(%rsp), %rsi
leaq 0x18(%rsp), %rdx
callq 0x981b
movsd 0x18(%rsp), %xmm0
ucomisd 0x2749(%rip), %xmm0     # 0xc400
jne 0x9cc1
jp 0x9cc1
cvtss2sd 0x20(%r13), %xmm0
movsd %xmm0, 0x8(%rsp)
movq %r13, %rdi
callq 0x75e4
movl 0x20(%rsp), %edx
movq %rbx, %rdi
movq %rbp, %rsi
movsd 0x8(%rsp), %xmm0
leaq 0x48(%rsp), %rcx
callq 0x8394
testl %eax, %eax
je 0x9d2d
callq 0x4ee9
movl 0x58(%rsp), %edx
movsd 0x60(%rsp), %xmm0
movq %rax, %rdi
leaq 0x3a75(%rip), %rsi      # 0xd77b
movq %rbp, %rcx
movb $0x1, %al
callq 0x5814
jmp 0x9d39
callq 0x4ee9
movq %rax, %rdi
leaq 0x3393(%rip), %rsi      # 0xd0b4
movq %rbp, %rdx
xorl %eax, %eax
callq 0x5814
jmp 0x9d39
leaq 0x3a2a(%rip), %rdi      # 0xd75e
callq 0x4ef7
movq %rax, %r13
movq 0x38(%rsp), %rax
movq 0x8(%rax), %rdi
movq %r13, %rsi
xorl %edx, %edx
callq 0x7cae
movq %r13, %rdi
callq 0x4f65
incq %r14
jmp 0x9c4d
movslq %r13d, %r14
pushq $0x1
popq %r15
pushq $0x1
popq %rbp
cmpq %r14, %r15
jge 0x9da7
movq (%r12,%r15,8), %rsi
movzbl (%rsi), %eax
cmpl $0x3f, %eax
je 0x9da2
cmpl $0x2d, %eax
je 0x9d95
cmpl $0x2b, %eax
jne 0x9e78
incq %rsi
movq %rbx, %rdi
callq 0x81ed
jmp 0x9da2
incq %rsi
movq %rbx, %rdi
movl %ebp, %edx
callq 0x7f8f
incq %r15
jmp 0x9d69
leaq 0x48(%rsp), %rsi
movq %rbx, %rdi
callq 0x8238
movq %rax, %r14
callq 0x4ee9
testq %r14, %r14
je 0x9e55
leaq 0x3b4c(%rip), %rsi      # 0xd918
xorl %ebp, %ebp
movq %rax, %rdi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x5814
movq %rax, %r15
movslq 0x48(%rsp), %rax
movq %rax, %r12
decq %r12
testl %eax, %eax
movl $0x0, %r13d
movq %rax, 0x8(%rsp)
cmovgl %eax, %r13d
cmpq %rbp, %r13
je 0x9e33
movq (%r14,%rbp,8), %rsi
movq %r15, %rdi
callq 0x55ba
movq %rax, %r15
cmpq %r12, %rbp
jge 0x9e25
movq %r15, %rdi
leaq 0x3b1c(%rip), %rsi      # 0xd939
callq 0x5593
movq %rax, %r15
incq %rbp
jmp 0x9dfa
leaq 0x39df(%rip), %rdi      # 0xd810
jmp 0x9e7f
movq 0x8(%rsp), %rsi
jmp 0x9e70
callq 0x4ee9
leaq 0x326e(%rip), %rsi      # 0xd0b4
movq %rax, %rdi
movq %r15, %rdx
xorl %eax, %eax
callq 0x5814
jmp 0x9e84
leaq 0x3a9e(%rip), %rsi      # 0xd8fa
movq %rax, %rdi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x5814
movq %rax, %r15
movl 0x48(%rsp), %esi
movq %r14, %rdi
jmp 0x9b3e
leaq 0x3a4f(%rip), %rdi      # 0xd8ce
callq 0x4ef7
movq %rax, %r15
testq %r15, %r15
je 0x9ea1
movq 0x38(%rsp), %rax
movq 0x8(%rax), %rdi
movq %r15, %rsi
xorl %edx, %edx
callq 0x7cae
jmp 0x9ea4
xorl %r15d, %r15d
movq %r15, %rdi
callq 0x4f65
movq %rbx, %rdi
callq 0x4f65
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
testq %r12, %r12
js 0x9eea
leaq 0x397f(%rip), %rdi      # 0xd851
jmp 0x9ef1
leaq 0x39d6(%rip), %rsi      # 0xd8b1
movq %rax, %rdi
movq %r15, %rdx
xorl %eax, %eax
callq 0x5814
jmp 0x9ef6
leaq 0x393a(%rip), %rdi      # 0xd82b
callq 0x4ef7
movq %rax, %r15
movq %r14, %rdi
callq 0x75e4
jmp 0x9e87