// File: /workspace/github_c_makefile_star2_O1/antirez[P]stonky/stonky.c
// Function: botProcessUpdates
int64_t botProcessUpdates(int64_t offset, int timeout) {
    char *options[6];
    int res;

    options[0] = "offset";
    options[1] = sdsfromlonglong(offset+1);
    options[2] = "timeout";
    options[3] = sdsfromlonglong(timeout);
    options[4] = "allowed_updates";
    options[5] = "message";
    sds body = makeBotRequest("getUpdates",&res,options,3);
    sdsfree(options[1]);
    sdsfree(options[3]);

    /* Parse the JSON in order to extract the message info. */
    cJSON *json = cJSON_Parse(body);
    cJSON *result = cJSON_Select(json,".result:a");
    if (result == NULL) goto fmterr;
    /* Process the array of updates. */
    cJSON *update;
    cJSON_ArrayForEach(update,result) {
        cJSON *update_id = cJSON_Select(update,".update_id:n");
        if (update_id == NULL) continue;
        int64_t thisoff = (int64_t) update_id->valuedouble;
        if (thisoff > offset) offset = thisoff;

        /* The actual message may be stored in .message or .channel_post
         * depending on the fact this is a private or group message,
         * or, instead, a channel post. */
        cJSON *msg = cJSON_Select(update,".message");
        if (!msg) msg = cJSON_Select(update,".channel_post");
        if (!msg) continue;

        cJSON *chatid = cJSON_Select(msg,".chat.id:n");
        if (chatid == NULL) continue;
        int64_t target = (int64_t) chatid->valuedouble;
        cJSON *chattype = cJSON_Select(msg,".chat.type:s");
        if (chattype != NULL) {
            if (!strcasecmp(chattype->valuestring,"group")) {
                botUpdateActiveChannels(target);
            }
        }
        cJSON *date = cJSON_Select(msg,".date:n");
        if (date == NULL) continue;
        time_t timestamp = date->valuedouble;
        cJSON *text = cJSON_Select(msg,".text:s");
        if (text == NULL) continue;
        if (VerboseMode) printf(".text: %s\n", text->valuestring);

        /* Sanity check the request before starting the thread:
         * validate that is a request that is really targeting our bot. */
        char *s = text->valuestring;
        if (s[0] != '$') continue;
        if (time(NULL)-timestamp > 60*5) continue;

        /* Spawn a thread that will handle the request. */
        botStats.queries++;
        sds request = sdsnew(text->valuestring+1);
        botRequest *bt = createBotRequest();
        bt->request = request;
        bt->target = target;
        pthread_t tid;
        if (pthread_create(&tid,NULL,botHandleRequest,bt) != 0) {
            freeBotRequest(bt);
            continue;
        }
        if (VerboseMode)
            printf("Starting thread to serve: \"%s\"\n",bt->request);
    }

fmterr:
    cJSON_Delete(json);
    sdsfree(body);
    return offset;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movl %esi, %ebp
movq %rdi, %rbx
leaq 0x400a(%rip), %rax      # 0x10792
leaq 0x20(%rsp), %r14
movq %rax, (%r14)
incq %rdi
callq 0x646e
movq %rax, %r15
movq %rax, 0x8(%r14)
leaq 0x3ff3(%rip), %rax      # 0x10799
movq %rax, 0x10(%r14)
movslq %ebp, %rdi
callq 0x646e
movq %rax, %r12
movq %rax, 0x18(%r14)
leaq 0x3fe1(%rip), %rax      # 0x107a1
movq %rax, 0x20(%r14)
leaq 0x4009(%rip), %rax      # 0x107d4
movq %rax, 0x28(%r14)
leaq 0x3fdb(%rip), %rdi      # 0x107b1
leaq 0x14(%rsp), %rsi
movq %r14, %rdx
movl $0x3, %ecx
callq 0x8a44
movq %rax, %r14
movq %r15, %rdi
callq 0x591d
movq %r12, %rdi
callq 0x591d
movq %r14, 0x8(%rsp)
movq %r14, %rdi
callq 0x31ce
leaq 0x3fad(%rip), %rsi      # 0x107bc
movq %rax, (%rsp)
movq %rax, %rdi
xorl %eax, %eax
callq 0x7cc5
testq %rax, %rax
je 0xc9f7
movq 0x10(%rax), %r15
testq %r15, %r15
je 0xc9f7
leaq 0x3f8c(%rip), %r12      # 0x107c6
leaq 0x3f92(%rip), %r13      # 0x107d3
movq %r15, %rdi
movq %r12, %rsi
xorl %eax, %eax
callq 0x7cc5
testq %rax, %rax
je 0xc9c4
cvttsd2si 0x30(%rax), %rbp
cmpq %rbp, %rbx
cmovgq %rbx, %rbp
movq %r15, %rdi
movq %r13, %rsi
xorl %eax, %eax
callq 0x7cc5
movq %rax, %r14
testq %rax, %rax
jne 0xc88d
movq %r15, %rdi
leaq 0x3f59(%rip), %rsi      # 0x107dc
xorl %eax, %eax
callq 0x7cc5
movq %rax, %r14
testq %r14, %r14
je 0xc9c7
movq %r14, %rdi
leaq 0x3f4a(%rip), %rsi      # 0x107ea
xorl %eax, %eax
callq 0x7cc5
testq %rax, %rax
je 0xc9c7
cvttsd2si 0x30(%rax), %rbx
movq %r14, %rdi
leaq 0x3f35(%rip), %rsi      # 0x107f5
xorl %eax, %eax
callq 0x7cc5
testq %rax, %rax
je 0xc8e8
movq 0x20(%rax), %rdi
leaq 0x3f2b(%rip), %rsi      # 0x10802
callq 0x22a0
testl %eax, %eax
jne 0xc8e8
movq %rbx, %rdi
callq 0xc6d0
movq %r14, %rdi
leaq 0x3f16(%rip), %rsi      # 0x10808
xorl %eax, %eax
callq 0x7cc5
testq %rax, %rax
je 0xc9c7
movq %r13, %r12
cvttsd2si 0x30(%rax), %r13
movq %r14, %rdi
leaq 0x3efb(%rip), %rsi      # 0x10810
xorl %eax, %eax
callq 0x7cc5
testq %rax, %rax
je 0xc9b8
movq %rax, %r14
cmpl $0x0, 0x8985(%rip)      # 0x152b4
je 0xc943
movq 0x20(%r14), %rsi
leaq 0x3edc(%rip), %rdi      # 0x10818
xorl %eax, %eax
callq 0x2030
movq 0x20(%r14), %rax
cmpb $0x24, (%rax)
jne 0xc9b8
xorl %edi, %edi
callq 0x2200
subq %r13, %rax
cmpq $0x12c, %rax            # imm = 0x12C
jg 0xc9b8
incq 0x8983(%rip)            # 0x152e8
movq 0x20(%r14), %rdi
incq %rdi
callq 0x58ac
movq %rax, %r13
movl $0x10, %edi
callq 0x2470
movq %rax, %r14
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movq %r13, (%rax)
movq %rbx, 0x8(%rax)
leaq 0x18(%rsp), %rdi
xorl %esi, %esi
leaq -0x8b4(%rip), %rdx      # 0xc0e8
movq %rax, %rcx
callq 0x2440
testl %eax, %eax
je 0xc9d8
movq (%r14), %rdi
callq 0x591d
movq %r14, %rdi
callq 0x2468
movq %r12, %r13
leaq 0x3e04(%rip), %r12      # 0x107c6
jmp 0xc9c7
movq %rbx, %rbp
movq (%r15), %r15
movq %rbp, %rbx
testq %r15, %r15
jne 0xc841
jmp 0xc9fa
cmpl $0x0, 0x88d5(%rip)      # 0x152b4
movq %r12, %r13
je 0xc9bb
movq (%r14), %rsi
leaq 0x3e35(%rip), %rdi      # 0x10823
xorl %eax, %eax
callq 0x2030
jmp 0xc9bb
movq %rbx, %rbp
movq (%rsp), %rdi
callq 0x2ad2
movq 0x8(%rsp), %rdi
callq 0x591d
movq %rbp, %rax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq