// File: /workspace/github_c_makefile_star2_O3/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 0x90d1
movq %rax, %fs:-0x8
movq (%rbx), %rdi
leaq 0xc(%rsp), %r15
movq %r15, %rsi
callq 0x725c
movq %rax, %r14
movl (%r15), %ebp
movq (%rax), %r15
cmpl $0x2, %ebp
jl 0xbe81
leaq 0x3f10(%rip), %rsi      # 0xfd85
movq %r15, %rdi
callq 0x22b0
testl %eax, %eax
je 0xbeae
movzbl -0x1(%r15), %eax
movl %eax, %ecx
andl $0x7, %ecx
cmpl $0x4, %ecx
ja 0xbf5b
leaq 0x294d(%rip), %rdx      # 0xe7e8
movslq (%rdx,%rcx,4), %rsi
addq %rdx, %rsi
jmpq *%rsi
movl %eax, %edx
shrl $0x3, %edx
jmp 0xbf5d
cmpl $0x2, %ebp
jne 0xbf9c
movq 0x8(%r14), %r15
leaq 0x4024(%rip), %rsi      # 0xfee6
movq %r15, %rdi
callq 0x22b0
testl %eax, %eax
je 0xc170
leaq 0x40a5(%rip), %rsi      # 0xff7e
movq %r15, %rdi
callq 0x22b0
testl %eax, %eax
jne 0xc160
movq 0x93f0(%rip), %rdi      # 0x152e0
callq 0xb87d
movq %rax, %r15
movq 0x93e9(%rip), %r8       # 0x152e8
movq 0x93ea(%rip), %r9       # 0x152f0
movl 0x93cc(%rip), %eax      # 0x152d8
movl %eax, (%rsp)
leaq 0x406d(%rip), %rdx      # 0xff83
leaq 0x10(%rsp), %r12
movl $0x400, %esi            # imm = 0x400
movq %r12, %rdi
movq %r15, %rcx
xorl %eax, %eax
callq 0x2400
movq %r15, %rdi
callq 0x5775
movq 0x8(%rbx), %rdi
movq %r12, %rsi
jmp 0xc2dd
movq -0x11(%r15), %rdx
jmp 0xbf5d
movzwl -0x5(%r15), %edx
jmp 0xbf5d
movl -0x9(%r15), %edx
jmp 0xbf5d
movzbl -0x3(%r15), %edx
jmp 0xbf5d
xorl %edx, %edx
cmpb $0x3a, -0x1(%r15,%rdx)
jne 0xbf77
movq %rbx, %rdi
movq %r14, %rsi
movl %ebp, %edx
callq 0xad4c
jmp 0xc389
movl %ecx, %edx
cmpl $0x4, %ecx
ja 0xc06d
leaq 0x2873(%rip), %rsi      # 0xe7fc
movslq (%rsi,%rdx,4), %rdi
addq %rsi, %rdi
jmpq *%rdi
movl %eax, %esi
shrl $0x3, %esi
jmp 0xc06f
cmpl $0x3, %ebp
sete %al
movq 0x930f(%rip), %r15      # 0x152b8
testq %r15, %r15
setne %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0xc125
movq 0x8(%r14), %r12
leaq 0x3f24(%rip), %rsi      # 0xfee9
movq %r12, %rdi
callq 0x22b0
testl %eax, %eax
je 0xbfe4
leaq 0x3f16(%rip), %rsi      # 0xfeee
movq %r12, %rdi
callq 0x22b0
testl %eax, %eax
jne 0xbff8
movq 0x10(%r14), %rdi
movq %r15, %rsi
callq 0x22b0
testl %eax, %eax
je 0xc3d9
leaq 0x3f11(%rip), %rsi      # 0xff10
movq %r12, %rdi
callq 0x22b0
testl %eax, %eax
jne 0xc160
movq 0x10(%r14), %rdi
movq %r15, %rsi
callq 0x22b0
testl %eax, %eax
jne 0xc160
movq 0x8(%rbx), %rdi
leaq 0x3eee(%rip), %rsi      # 0xff1c
xorl %edx, %edx
callq 0x8f4e
leaq 0x3efb(%rip), %rdi      # 0xff37
xorl %eax, %eax
callq 0x82bd
movq 0x8(%rbx), %rdi
leaq 0x3f00(%rip), %rsi      # 0xff4e
jmp 0xc2dd
movq -0x11(%r15), %rsi
jmp 0xc06f
movzwl -0x5(%r15), %esi
jmp 0xc06f
movl -0x9(%r15), %esi
jmp 0xc06f
movzbl -0x3(%r15), %esi
jmp 0xc06f
xorl %esi, %esi
cmpb $0x3f, -0x1(%r15,%rsi)
jne 0xc098
cmpl $0x4, %ecx
ja 0xc197
leaq 0x279d(%rip), %rcx      # 0xe824
movslq (%rcx,%rdx,4), %rdx
addq %rcx, %rdx
jmpq *%rdx
shrl $0x3, %eax
jmp 0xc199
cmpl $0x2, %ebp
je 0xc0e0
cmpl $0x1, %ebp
jne 0xc113
leaq 0x3f62(%rip), %rsi      # 0x1000b
movq %r15, %rdi
callq 0x22b0
testl %eax, %eax
je 0xc1c5
leaq 0x3583(%rip), %rsi      # 0xf643
movq %r15, %rdi
callq 0x22b0
testl %eax, %eax
je 0xc2bf
movq %rbx, %rdi
movq %r15, %rsi
callq 0x9e60
jmp 0xc389
movq 0x8(%r14), %r12
movzbl -0x1(%r12), %eax
movl %eax, %ecx
andl $0x7, %ecx
movb $0x1, %r13b
cmpl $0x4, %ecx
ja 0xc219
leaq 0x270e(%rip), %rdx      # 0xe810
movslq (%rdx,%rcx,4), %rcx
addq %rdx, %rcx
jmpq *%rcx
shrl $0x3, %eax
jmp 0xc1ee
jle 0xc2d2
movq 0x8(%r14), %r12
xorl %r13d, %r13d
jmp 0xc219
testq %r15, %r15
setne %al
cmpl $0x4, %ebp
sete %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0xc160
movq 0x8(%r14), %rdi
leaq 0x3e20(%rip), %rsi      # 0xff63
callq 0x22b0
testl %eax, %eax
jne 0xc160
movq 0x18(%r14), %rdi
movq %r15, %rsi
callq 0x22b0
testl %eax, %eax
je 0xc354
movq 0x8(%rbx), %rdi
leaq 0x3e6d(%rip), %rsi      # 0xffd8
jmp 0xc2dd
movq %rbx, %rdi
callq 0xbd41
jmp 0xc389
movq -0x11(%r15), %rax
jmp 0xc199
movzwl -0x5(%r15), %eax
jmp 0xc199
movl -0x9(%r15), %eax
jmp 0xc199
movzbl -0x3(%r15), %eax
jmp 0xc199
xorl %eax, %eax
cmpb $0x3f, -0x2(%r15,%rax)
jne 0xc1b3
movq %rbx, %rdi
movl %ebp, %esi
movq %r14, %rdx
callq 0xb991
jmp 0xc389
movq %rbx, %rdi
movl %ebp, %esi
movq %r14, %rdx
callq 0xb402
jmp 0xc389
movq %rbx, %rdi
callq 0xa01a
jmp 0xc389
movq -0x11(%r12), %rax
jmp 0xc1ee
movzwl -0x5(%r12), %eax
jmp 0xc1ee
movl -0x9(%r12), %eax
jmp 0xc1ee
movzbl -0x3(%r12), %eax
cmpq $0x2, %rax
jne 0xc219
callq 0x22a0
movq (%rax), %rax
movsbq (%r12), %rcx
testb $0x8, 0x1(%rax,%rcx,2)
je 0xc219
movsbq 0x1(%r12), %rcx
testb $0x4, 0x1(%rax,%rcx,2)
jne 0xc32b
leaq 0x3df5(%rip), %rsi      # 0x10015
movq %r12, %rdi
callq 0x22b0
testl %eax, %eax
je 0xc2e9
leaq 0x3de1(%rip), %rsi      # 0x10018
movq %r12, %rdi
callq 0x22b0
testl %eax, %eax
je 0xc2e9
leaq 0x3dd5(%rip), %rsi      # 0x10023
movq %r12, %rdi
callq 0x22b0
testl %eax, %eax
je 0xc305
leaq 0x3dc2(%rip), %rsi      # 0x10027
movq %r12, %rdi
callq 0x22b0
testl %eax, %eax
je 0xc305
leaq 0x3db6(%rip), %rsi      # 0x10032
movq %r12, %rdi
callq 0x22b0
testl %eax, %eax
je 0xc33b
testb %r13b, %r13b
je 0xc2bf
leaq 0x3d9f(%rip), %rsi      # 0x10037
movq %r12, %rdi
callq 0x22b0
testl %eax, %eax
je 0xc371
leaq 0x3ccf(%rip), %rsi      # 0xff7e
movq %r12, %rdi
callq 0x22b0
testl %eax, %eax
je 0xc37e
leaq 0x337d(%rip), %rsi      # 0xf643
movq %r15, %rdi
callq 0x22b0
testl %eax, %eax
je 0xc31e
movq 0x8(%rbx), %rdi
leaq 0x4462(%rip), %rsi      # 0x1073f
xorl %edx, %edx
callq 0x8f4e
jmp 0xc389
movq %r14, %rdx
addq $0x10, %rdx
addl $-0x2, %ebp
movq %rbx, %rdi
movq %r15, %rsi
movl %ebp, %ecx
callq 0xa4b5
jmp 0xc389
movq %r14, %rdx
addq $0x10, %rdx
addl $-0x2, %ebp
movq %rbx, %rdi
movq %r15, %rsi
movl %ebp, %ecx
callq 0xa738
jmp 0xc389
movq 0x8(%rbx), %rdi
leaq 0x3d14(%rip), %rsi      # 0x1003d
jmp 0xc2dd
movq %rbx, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0xa218
jmp 0xc389
movq %r14, %rdx
addq $0x10, %rdx
addl $-0x2, %ebp
movq %rbx, %rdi
movq %r15, %rsi
movl %ebp, %ecx
callq 0xa93c
jmp 0xc389
movq 0x8(%rbx), %rdi
leaq 0x3c0c(%rip), %rsi      # 0xff6b
xorl %edx, %edx
callq 0x8f4e
movq 0x10(%r14), %rdi
callq 0x982f
jmp 0xc389
movq %rbx, %rdi
movq %r15, %rsi
callq 0xaaf6
jmp 0xc389
movq %rbx, %rdi
movq %r15, %rsi
callq 0x9eba
movq (%rbx), %rdi
callq 0x5775
movq %rbx, %rdi
callq 0x2478
movl 0xc(%rsp), %esi
movq %r14, %rdi
callq 0x7056
movq %fs:-0x8, %rdi
testq %rdi, %rdi
je 0xc3b8
callq 0x2130
movq $0x0, %fs:-0x8
xorl %eax, %eax
addq $0x418, %rsp            # imm = 0x418
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x8(%rbx), %rdi
leaq 0x3b0f(%rip), %rsi      # 0xfef3
xorl %edx, %edx
callq 0x8f4e
movl $0x5, %edi
callq 0x21a0
leaq 0x4762(%rip), %rdi      # 0x10b5e
callq 0x23d0
xorl %edi, %edi
callq 0x2300