// File: /workspace/github_c_makefile_star2_O2/antirez[P]stonky/stonky.c
// Function: botHandleRequest
void *botHandleRequest(void *arg) {
    DbHandle = dbInit(0);
    botRequest *br = arg;

    /* Parse the request as a command composed of arguments. */
    int argc;
    sds *argv = sdssplitargs(br->request,&argc);

    if (argc >= 2 && !strcasecmp(argv[0],"$")) {
        if (argc == 2 && !strcasecmp(argv[1],"ls")) {
            /* $$ ls */
            botHandleLsRequest(br);
        } else if (argc == 3 &&
                   AdminPass != NULL &&
                   (!strcasecmp(argv[1],"quit") ||
                    !strcasecmp(argv[1],"exit")) &&
                   !strcasecmp(argv[2],AdminPass))
        {
            /* $$ quit <password> */
            botSendMessage(br->target, "Exiting in 5 seconds. Bye...",0);
            sleep(5); /* Wait for the main thread to likely acknowledge
                         the processing of this message, otherwise the
                         bot will quit again since it will fetch $$ quit
                         again at the next restart. */
            printf("Exiting by user request.\n");
            exit(0);
        } else if (argc == 3 &&
                   AdminPass != NULL &&
                   !strcasecmp(argv[1],"flush-cache") &&
                   !strcasecmp(argv[2],AdminPass))
        {
            /* $$ flush-cache <password> */
            botSendMessage(br->target, "Deleting cached entries...",0);
            sqlQuery("DELETE * FROM KeyValue");
            botSendMessage(br->target, "Cache flushing done.",0);
        } else if (argc == 4 &&
                   AdminPass != NULL &&
                   !strcasecmp(argv[1],"destroy") &&
                   !strcasecmp(argv[3],AdminPass))
        {
            /* $$ destroy <listname> <password> */
            botSendMessage(br->target, "Destroying list...",0);
            dbDestroyList(argv[2]);
        } else if (argc == 2 && !strcasecmp(argv[1],"info")) {
            /* $$ info */
            char buf[1024];
            sds ago = sdsTimeAgo(botStats.start_time);

            snprintf(buf,sizeof(buf),
                "Hey, a few info about me.\n"
                "uptime: %s\n"
                "queries: %llu\n"
                "scanned: %llu\n"
                "active-channels: %d",
                ago,
                (unsigned long long)botStats.queries,
                (unsigned long long)botStats.scanned,
                (int)ActiveChannelsCount);
            sdsfree(ago);
            botSendMessage(br->target,buf,0);
        } else {
            botSendMessage(br->target,
                "Invalid control command or bad password, try $HELP",0);
        }
    } else if (argv[0][sdslen(argv[0])-1] == ':') {
        /* $list: [+... -...] */
        botHandleListRequest(br,argv,argc);
    } else if (argv[0][sdslen(argv[0])-1] == '?') {
        /* $list? [pattern] and $list?? [pattern] */
        if (argv[0][sdslen(argv[0])-2] != '?') {
            botHandleShowPortfolioRequest(br,argc,argv);
        } else {
            botHandleShowProfitLossRequest(br,argc,argv);
        }
    } else if (argc == 1 && !strcasecmp(argv[0],"bigmovers")) {
        /* $bigmovers -- best and worse among stocks in defined lists. */
        botHandleBigMoversRequest(br);
    } else if (argc == 1 && strcasecmp(argv[0],"help")) {
        /* $AAPL */
        botHandlePriceRequest(br,argv[0]);
    } else if (argc == 2 && sdslen(argv[1]) == 2 &&
               isdigit(argv[1][0]) && isalpha(argv[1][1]))
    {
        /* $AAPL 5d */
        botHandleChartRequest(br,argv[0],argv[1]);
    } else if (argc >= 2 && (!strcasecmp(argv[1],"mc") ||
                             !strcasecmp(argv[1],"montecarlo")))
    {
        /* $AAPL mc | montecarlo [options] */
        botHandleMontecarloRequest(br,argv[0],argv+2,argc-2);
    } else if (argc >= 2 && (!strcasecmp(argv[1],"vol") ||
                             !strcasecmp(argv[1],"volatility")))
    {
        /* $AAPL vol | volatility [options] */
        botHandleVolatilityRequest(br,argv[0],argv+2,argc-2);
    } else if (argc >= 2 && !strcasecmp(argv[1],"last")) {
        /* $AAPL last [count] */
        botHandleLastChangesRequest(br,argv[0],argv+2,argc-2);
    } else if (argc == 2 && !strcasecmp(argv[1],"trend")) {
        /* $AAPL trend. */
        botHandleTrendRequest(br,argv[0]);
    } else if (argc == 2 && !strcasecmp(argv[1],"info")) {
        /* $AAPL info. */
        botHandleInfoRequest(br,argv[0]);
    } else if (argc >= 1 && !strcasecmp(argv[0],"help")) {
        /* $HELP */
        botSendMessage(br->target,
"```\n"
"$AAPL                    | Show price for AAPL.\n"
"$AAPL 1d|5d|1m|6m|1y|5y  | Show AAPL price chart for period.\n"
"$AAPL info               | Show general info for the company.\n"
"$mylist: +VMW +AAPL -KO  | Modify the list.\n"
"$mylist:                 | Ask prices of whole list.\n"
"$mylist::                | Like $mylist: but more verbose.\n"
"$mylist: ?               | Show just stock names.\n"
"$AAPL mc                 | Montecarlo simulation.\n"
"$AAPL mc range 60        | Specify Montecarlo range.\n"
"$APPL mc period 5        | Specify Sell/Buy fixed period.\n"
"$APPL trend              | Montecarlo trend analysis.\n"
"$AAPL last [numdays]     | Last N price %% changes (default 10).\n"
"$AAPL vol                | Volatility analysis.\n"
"$AAPL vol range 60       | Volatility anal. last 60 market days.\n"
"$bigmovers               | Best and worse of today.\n"
"$mylist: buy AAPL 10@50  | Add 10 AAPL stocks at 50$ each.\n"
"$mylist: buy AAPL 20     | Add 20 AAPL stocks at current price.\n"
"$mylist: buy AAPL        | Add 1 AAPL stocks at current price.\n"
"$mylist: buy AAPL 10000$ | Add AAPL stocks you can buy with 10k$.\n"
"$mylist: buy V T 500$    | Add AT&T and VISA stocks for 5k$ each.\n"
"$mylist: sell AAPL 10@35 | Sell 10 AAPL stocks at 35$ each.\n"
"$mylist: sell AAPL 10    | Sell 10 AAPL stocks at current price.\n"
"$mylist: sell AAPL       | Sell all AAPL stocks at current price.\n"
"$mylist: rm-sell <id>    | Remove sell with specified ID.\n"
"$mylist? [pattern]       | Show portfolio associated with mylist.\n"
"$mylist?? [pattern]      | Show portfolio profit and loss history.\n"
"$$ ls                    | Show all the available lists.\n"
"$$ info                  | Stop bot internal stats.\n"
"$$ quit <password>       | Stop the bot process.\n"
"$$ destroy <name> <pass> | Destroy the list, portfolio, P/Ls.\n"
"$help                    | Show this help.\n"
"```\n",0);
    } else {
        botSendMessage(br->target,
            "Sorry, I can't understand your request. Try $HELP.",0);
    }

    freeBotRequest(br);
    sdsfreesplitres(argv,argc);
    dbClose();
    return NULL;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x418, %rsp            # imm = 0x418
movq %rdi, %rbx
xorl %edi, %edi
callq 0x7e27
movq %rax, %fs:-0x8
movq (%rbx), %rdi
leaq 0xc(%rsp), %r15
movq %r15, %rsi
callq 0x613c
movq %rax, %r14
movl (%r15), %ebp
movq (%rax), %r15
cmpl $0x2, %ebp
jl 0xa819
leaq 0x31e4(%rip), %rsi      # 0xd9ed
movq %r15, %rdi
callq 0x22b0
testl %eax, %eax
je 0xa8b1
movq %r15, %rdi
callq 0x8c9f
movzbl -0x1(%r15,%rax), %ecx
cmpl $0x3f, %ecx
je 0xa843
cmpl $0x3a, %ecx
jne 0xa861
movq %rbx, %rdi
movq %r14, %rsi
movl %ebp, %edx
callq 0x98cf
jmp 0xab52
cmpb $0x3f, -0x2(%r15,%rax)
jne 0xa985
movq %rbx, %rdi
movl %ebp, %esi
movq %r14, %rdx
callq 0xa3d2
jmp 0xab52
cmpl $0x2, %ebp
je 0xa997
cmpl $0x1, %ebp
jne 0xa9e0
leaq 0x33f9(%rip), %rsi      # 0xdc73
movq %r15, %rdi
callq 0x22b0
testl %eax, %eax
je 0xab89
leaq 0x2a1a(%rip), %rsi      # 0xd2ab
movq %r15, %rdi
callq 0x22b0
testl %eax, %eax
je 0xaa93
movq %rbx, %rdi
movq %r15, %rsi
callq 0x8b08
jmp 0xab52
cmpl $0x2, %ebp
jne 0xa8ce
movq 0x8(%r14), %rdi
leaq 0x328d(%rip), %rsi      # 0xdb4e
callq 0x22b0
testl %eax, %eax
je 0xab7f
cmpl $0x3, %ebp
sete %al
movq 0x89dd(%rip), %r15      # 0x132b8
testq %r15, %r15
setne %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0xaaba
movq 0x8(%r14), %r12
leaq 0x325a(%rip), %rsi      # 0xdb51
movq %r12, %rdi
callq 0x22b0
testl %eax, %eax
je 0xa916
leaq 0x324c(%rip), %rsi      # 0xdb56
movq %r12, %rdi
callq 0x22b0
testl %eax, %eax
jne 0xa92a
movq 0x10(%r14), %rdi
movq %r15, %rsi
callq 0x22b0
testl %eax, %eax
je 0xac4d
leaq 0x3247(%rip), %rsi      # 0xdb78
movq %r12, %rdi
callq 0x22b0
testl %eax, %eax
jne 0xab40
movq 0x10(%r14), %rdi
movq %r15, %rsi
callq 0x22b0
testl %eax, %eax
jne 0xab40
movq 0x8(%rbx), %rdi
leaq 0x3224(%rip), %rsi      # 0xdb84
xorl %edx, %edx
callq 0x7cae
leaq 0x3231(%rip), %rdi      # 0xdb9f
xorl %eax, %eax
callq 0x7083
movq 0x8(%rbx), %rdi
leaq 0x3236(%rip), %rsi      # 0xdbb6
jmp 0xab4b
movq %rbx, %rdi
movl %ebp, %esi
movq %r14, %rdx
callq 0x9f03
jmp 0xab52
movq 0x8(%r14), %r12
movq %r12, %rdi
callq 0x8c9f
movb $0x1, %r13b
cmpq $0x2, %rax
jne 0xa9ed
callq 0x22a0
movq (%rax), %rax
movsbq (%r12), %rcx
testb $0x8, 0x1(%rax,%rcx,2)
je 0xa9ed
movsbq 0x1(%r12), %rcx
testb $0x4, 0x1(%rax,%rcx,2)
je 0xa9ed
movq %rbx, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x8e9c
jmp 0xab52
jle 0xaaaa
movq 0x8(%r14), %r12
xorl %r13d, %r13d
leaq 0x3289(%rip), %rsi      # 0xdc7d
movq %r12, %rdi
callq 0x22b0
testl %eax, %eax
je 0xab0e
leaq 0x3275(%rip), %rsi      # 0xdc80
movq %r12, %rdi
callq 0x22b0
testl %eax, %eax
je 0xab0e
leaq 0x3269(%rip), %rsi      # 0xdc8b
movq %r12, %rdi
callq 0x22b0
testl %eax, %eax
je 0xab93
leaq 0x3256(%rip), %rsi      # 0xdc8f
movq %r12, %rdi
callq 0x22b0
testl %eax, %eax
je 0xab93
leaq 0x324a(%rip), %rsi      # 0xdc9a
movq %r12, %rdi
callq 0x22b0
testl %eax, %eax
je 0xac11
testb %r13b, %r13b
je 0xaa93
leaq 0x3233(%rip), %rsi      # 0xdc9f
movq %r12, %rdi
callq 0x22b0
testl %eax, %eax
je 0xac2d
leaq 0x3163(%rip), %rsi      # 0xdbe6
movq %r12, %rdi
callq 0x22b0
testl %eax, %eax
je 0xac3d
leaq 0x2811(%rip), %rsi      # 0xd2ab
movq %r15, %rdi
callq 0x22b0
testl %eax, %eax
je 0xabac
movq 0x8(%rbx), %rdi
leaq 0x38f2(%rip), %rsi      # 0xe3a7
jmp 0xab4b
testq %r15, %r15
setne %al
cmpl $0x4, %ebp
sete %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0xab27
movq 0x8(%r14), %rdi
leaq 0x30f3(%rip), %rsi      # 0xdbcb
callq 0x22b0
testl %eax, %eax
jne 0xab40
movq 0x18(%r14), %rdi
movq %r15, %rsi
callq 0x22b0
testl %eax, %eax
jne 0xab40
movq 0x8(%rbx), %rdi
leaq 0x30d7(%rip), %rsi      # 0xdbd3
xorl %edx, %edx
callq 0x7cae
movq 0x10(%r14), %rdi
callq 0x8554
jmp 0xab52
movq %r14, %rdx
addq $0x10, %rdx
addl $-0x2, %ebp
movq %rbx, %rdi
movq %r15, %rsi
movl %ebp, %ecx
callq 0x9130
jmp 0xab52
cmpl $0x2, %ebp
jne 0xab40
movq 0x8(%r14), %rdi
leaq 0x30af(%rip), %rsi      # 0xdbe6
callq 0x22b0
testl %eax, %eax
je 0xabb9
movq 0x8(%rbx), %rdi
leaq 0x30f5(%rip), %rsi      # 0xdc40
xorl %edx, %edx
callq 0x7cae
movq %rbx, %rdi
callq 0x7d80
movl 0xc(%rsp), %esi
movq %r14, %rdi
callq 0x5f72
callq 0x7eda
xorl %eax, %eax
addq $0x418, %rsp            # imm = 0x418
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rbx, %rdi
callq 0xa704
jmp 0xab52
movq %rbx, %rdi
callq 0x8cd7
jmp 0xab52
movq %r14, %rdx
addq $0x10, %rdx
addl $-0x2, %ebp
movq %rbx, %rdi
movq %r15, %rsi
movl %ebp, %ecx
callq 0x9358
jmp 0xab52
movq 0x8(%rbx), %rdi
leaq 0x30ee(%rip), %rsi      # 0xdca5
jmp 0xab4b
movq 0x8720(%rip), %rdi      # 0x132e0
callq 0xa2be
movq %rax, %r15
movq 0x8719(%rip), %r8       # 0x132e8
movq 0x871a(%rip), %r9       # 0x132f0
movl 0x86fc(%rip), %eax      # 0x132d8
movl %eax, (%rsp)
leaq 0x3005(%rip), %rdx      # 0xdbeb
leaq 0x10(%rsp), %r12
movl $0x400, %esi            # imm = 0x400
movq %r12, %rdi
movq %r15, %rcx
xorl %eax, %eax
callq 0x2400
movq %r15, %rdi
callq 0x4f65
movq 0x8(%rbx), %rdi
movq %r12, %rsi
jmp 0xab4b
movq %r14, %rdx
addq $0x10, %rdx
addl $-0x2, %ebp
movq %rbx, %rdi
movq %r15, %rsi
movl %ebp, %ecx
callq 0x9531
jmp 0xab52
movq %rbx, %rdi
movq %r15, %rsi
callq 0x96c7
jmp 0xab52
movq %rbx, %rdi
movq %r15, %rsi
callq 0x8b60
jmp 0xab52
movq 0x8(%rbx), %rdi
leaq 0x2f03(%rip), %rsi      # 0xdb5b
xorl %edx, %edx
callq 0x7cae
pushq $0x5
popq %rdi
callq 0x21a0
leaq 0x3b58(%rip), %rdi      # 0xe7c6
callq 0x23d0
xorl %edi, %edi
callq 0x2300