// File: /workspace/github_c_makefile_star2_O1/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 0x936c
movq %rax, %fs:-0x8
movq (%rbx), %rdi
leaq 0xc(%rsp), %r15
movq %r15, %rsi
callq 0x745e
movq %rax, %r14
movl (%r15), %ebp
cmpl $0x2, %ebp
jl 0xc13a
movq (%r14), %rdi
leaq 0x3c74(%rip), %rsi      # 0xfda5
callq 0x22a0
testl %eax, %eax
je 0xc16a
movq (%r14), %r15
movzbl -0x1(%r15), %eax
movl %eax, %ecx
andl $0x7, %ecx
cmpl $0x4, %ecx
ja 0xc291
leaq 0x26b5(%rip), %rdx      # 0xe80c
movslq (%rdx,%rcx,4), %rsi
addq %rdx, %rsi
jmpq *%rsi
movl %eax, %edx
shrl $0x3, %edx
jmp 0xc293
cmpl $0x2, %ebp
jne 0xc187
movq 0x8(%r14), %rdi
leaq 0x3d8c(%rip), %rsi      # 0xff06
callq 0x22a0
testl %eax, %eax
je 0xc396
cmpl $0x3, %ebp
sete %al
movq 0x9124(%rip), %r15      # 0x152b8
testq %r15, %r15
setne %r13b
andb %al, %r13b
cmpb $0x1, %r13b
jne 0xc20f
movq 0x8(%r14), %r12
leaq 0x3d5a(%rip), %rsi      # 0xff09
movq %r12, %rdi
callq 0x22a0
testl %eax, %eax
je 0xc1ce
leaq 0x3d4c(%rip), %rsi      # 0xff0e
movq %r12, %rdi
callq 0x22a0
testl %eax, %eax
jne 0xc1e2
movq 0x10(%r14), %rdi
movq %r15, %rsi
callq 0x22a0
testl %eax, %eax
je 0xc6a1
testb %r13b, %r13b
je 0xc20f
movq 0x8(%r14), %rdi
leaq 0x3d3e(%rip), %rsi      # 0xff30
callq 0x22a0
testl %eax, %eax
jne 0xc20f
movq 0x10(%r14), %rdi
movq %r15, %rsi
callq 0x22a0
testl %eax, %eax
je 0xc5f9
testq %r15, %r15
setne %al
cmpl $0x4, %ebp
sete %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0xc24a
movq 0x8(%r14), %rdi
leaq 0x3d56(%rip), %rsi      # 0xff83
callq 0x22a0
testl %eax, %eax
jne 0xc24a
movq 0x18(%r14), %rdi
movq %r15, %rsi
callq 0x22a0
testl %eax, %eax
je 0xc5b3
cmpl $0x2, %ebp
jne 0xc267
movq 0x8(%r14), %rdi
leaq 0x3d44(%rip), %rsi      # 0xff9e
callq 0x22a0
testl %eax, %eax
je 0xc3eb
movq 0x8(%rbx), %rdi
leaq 0x3d86(%rip), %rsi      # 0xfff8
jmp 0xc624
movq -0x11(%r15), %rdx
jmp 0xc293
movzwl -0x5(%r15), %edx
jmp 0xc293
movl -0x9(%r15), %edx
jmp 0xc293
movzbl -0x3(%r15), %edx
jmp 0xc293
xorl %edx, %edx
cmpb $0x3a, -0x1(%r15,%rdx)
jne 0xc2ad
movq %rbx, %rdi
movq %r14, %rsi
movl %ebp, %edx
callq 0xb021
jmp 0xc62b
movl %ecx, %edx
cmpl $0x4, %ecx
ja 0xc2e5
leaq 0x2565(%rip), %rsi      # 0xe820
movslq (%rsi,%rdx,4), %rdi
addq %rsi, %rdi
jmpq *%rdi
movl %eax, %esi
shrl $0x3, %esi
jmp 0xc2e7
movq -0x11(%r15), %rsi
jmp 0xc2e7
movzwl -0x5(%r15), %esi
jmp 0xc2e7
movl -0x9(%r15), %esi
jmp 0xc2e7
movzbl -0x3(%r15), %esi
jmp 0xc2e7
xorl %esi, %esi
cmpb $0x3f, -0x1(%r15,%rsi)
jne 0xc310
cmpl $0x4, %ecx
ja 0xc3bd
leaq 0x2549(%rip), %rcx      # 0xe848
movslq (%rcx,%rdx,4), %rdx
addq %rcx, %rdx
jmpq *%rdx
shrl $0x3, %eax
jmp 0xc3bf
cmpl $0x1, %ebp
jne 0xc350
leaq 0x3d0f(%rip), %rsi      # 0x1002b
movq %r15, %rdi
callq 0x22a0
testl %eax, %eax
je 0xc389
cmpl $0x1, %ebp
jne 0xc350
leaq 0x332f(%rip), %rsi      # 0xf663
movq %r15, %rdi
callq 0x22a0
testl %eax, %eax
je 0xc350
movq %rbx, %rdi
movq %r15, %rsi
callq 0xa0ee
jmp 0xc62b
cmpl $0x2, %ebp
jne 0xc48e
movq 0x8(%r14), %r12
movzbl -0x1(%r12), %eax
movl %eax, %ecx
andl $0x7, %ecx
cmpl $0x4, %ecx
ja 0xc461
leaq 0x24bc(%rip), %rdx      # 0xe834
movslq (%rdx,%rcx,4), %rcx
addq %rdx, %rcx
jmpq *%rcx
shrl $0x3, %eax
jmp 0xc463
movq %rbx, %rdi
callq 0xa2c0
jmp 0xc62b
movq %rbx, %rdi
callq 0xbffd
jmp 0xc62b
movq -0x11(%r15), %rax
jmp 0xc3bf
movzwl -0x5(%r15), %eax
jmp 0xc3bf
movl -0x9(%r15), %eax
jmp 0xc3bf
movzbl -0x3(%r15), %eax
jmp 0xc3bf
xorl %eax, %eax
cmpb $0x3f, -0x2(%r15,%rax)
jne 0xc3d9
movq %rbx, %rdi
movl %ebp, %esi
movq %r14, %rdx
callq 0xbc4d
jmp 0xc62b
movq %rbx, %rdi
movl %ebp, %esi
movq %r14, %rdx
callq 0xb6e3
jmp 0xc62b
movq 0x8eee(%rip), %rdi      # 0x152e0
callq 0xbb1f
movq %rax, %r15
movq 0x8ee7(%rip), %r8       # 0x152e8
movq 0x8ee8(%rip), %r9       # 0x152f0
movl 0x8eca(%rip), %eax      # 0x152d8
movl %eax, (%rsp)
leaq 0x3b8b(%rip), %rdx      # 0xffa3
leaq 0x10(%rsp), %r12
movl $0x400, %esi            # imm = 0x400
movq %r12, %rdi
movq %r15, %rcx
xorl %eax, %eax
callq 0x23f0
movq %r15, %rdi
callq 0x591d
movq 0x8(%rbx), %rdi
movq %r12, %rsi
jmp 0xc624
movq -0x11(%r12), %rax
jmp 0xc463
movzwl -0x5(%r12), %eax
jmp 0xc463
movl -0x9(%r12), %eax
jmp 0xc463
movzbl -0x3(%r12), %eax
jmp 0xc463
xorl %eax, %eax
cmpq $0x2, %rax
jne 0xc48e
callq 0x2290
movq (%rax), %rax
movsbq (%r12), %rcx
testb $0x8, 0x1(%rax,%rcx,2)
je 0xc48e
movsbq 0x1(%r12), %rcx
testb $0x4, 0x1(%rax,%rcx,2)
jne 0xc5d0
cmpl $0x2, %ebp
jl 0xc51d
movq 0x8(%r14), %r12
leaq 0x3b93(%rip), %rsi      # 0x10035
movq %r12, %rdi
callq 0x22a0
testl %eax, %eax
je 0xc57a
leaq 0x3b7f(%rip), %rsi      # 0x10038
movq %r12, %rdi
callq 0x22a0
testl %eax, %eax
je 0xc57a
cmpl $0x2, %ebp
jl 0xc51d
movq 0x8(%r14), %r12
leaq 0x3b6a(%rip), %rsi      # 0x10043
movq %r12, %rdi
callq 0x22a0
testl %eax, %eax
je 0xc5e0
leaq 0x3b57(%rip), %rsi      # 0x10047
movq %r12, %rdi
callq 0x22a0
testl %eax, %eax
je 0xc5e0
cmpl $0x2, %ebp
jl 0xc51d
movq 0x8(%r14), %rdi
leaq 0x3b42(%rip), %rsi      # 0x10052
callq 0x22a0
testl %eax, %eax
je 0xc688
cmpl $0x2, %ebp
jne 0xc553
movq 0x8(%r14), %rdi
leaq 0x3b2a(%rip), %rsi      # 0x10057
callq 0x22a0
testl %eax, %eax
je 0xc596
cmpl $0x2, %ebp
jne 0xc553
movq 0x8(%r14), %rdi
leaq 0x3a58(%rip), %rsi      # 0xff9e
callq 0x22a0
testl %eax, %eax
je 0xc67b
testl %ebp, %ebp
jle 0xc56a
leaq 0x3105(%rip), %rsi      # 0xf663
movq %r15, %rdi
callq 0x22a0
testl %eax, %eax
je 0xc5a6
movq 0x8(%rbx), %rdi
leaq 0x41ea(%rip), %rsi      # 0x1075f
jmp 0xc624
movq %r14, %rdx
addq $0x10, %rdx
addl $-0x2, %ebp
movq %rbx, %rdi
movq %r15, %rsi
movl %ebp, %ecx
callq 0xa74b
jmp 0xc62b
movq %rbx, %rdi
movq %r15, %rsi
callq 0xadcb
jmp 0xc62b
movq 0x8(%rbx), %rdi
leaq 0x3aac(%rip), %rsi      # 0x1005d
jmp 0xc624
movq 0x8(%rbx), %rdi
leaq 0x39cd(%rip), %rsi      # 0xff8b
xorl %edx, %edx
callq 0x91e5
movq 0x10(%r14), %rdi
callq 0x9ad2
jmp 0xc62b
movq %rbx, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0xa4b1
jmp 0xc62b
movq %r14, %rdx
addq $0x10, %rdx
addl $-0x2, %ebp
movq %rbx, %rdi
movq %r15, %rsi
movl %ebp, %ecx
callq 0xa9e7
jmp 0xc62b
movq 0x8(%rbx), %rdi
leaq 0x3938(%rip), %rsi      # 0xff3c
xorl %edx, %edx
callq 0x91e5
leaq 0x3945(%rip), %rdi      # 0xff57
xorl %eax, %eax
callq 0x8545
movq 0x8(%rbx), %rdi
leaq 0x394a(%rip), %rsi      # 0xff6e
xorl %edx, %edx
callq 0x91e5
movq (%rbx), %rdi
callq 0x591d
movq %rbx, %rdi
callq 0x2468
movl 0xc(%rsp), %esi
movq %r14, %rdi
callq 0x7258
movq %fs:-0x8, %rdi
testq %rdi, %rdi
je 0xc65a
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 %rbx, %rdi
movq %r15, %rsi
callq 0xa148
jmp 0xc62b
movq %r14, %rdx
addq $0x10, %rdx
addl $-0x2, %ebp
movq %rbx, %rdi
movq %r15, %rsi
movl %ebp, %ecx
callq 0xac11
jmp 0xc62b
movq 0x8(%rbx), %rdi
leaq 0x3867(%rip), %rsi      # 0xff13
xorl %edx, %edx
callq 0x91e5
movl $0x5, %edi
callq 0x2190
leaq 0x44ba(%rip), %rdi      # 0x10b7e
callq 0x23c0
xorl %edi, %edi
callq 0x22f0