// File: /workspace/github_c_makefile_star2_O2/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, %ebx
movq %rdi, %r13
leaq 0x36dd(%rip), %rax      # 0xe3da
leaq 0x20(%rsp), %r14
movq %rax, (%r14)
incq %rdi
callq 0x570c
movq %rax, %r15
movq %rax, 0x8(%r14)
leaq 0x36c6(%rip), %rax      # 0xe3e1
movq %rax, 0x10(%r14)
movslq %ebx, %rdi
callq 0x570c
movq %rax, %rbx
movq %rax, 0x18(%r14)
leaq 0x36b4(%rip), %rax      # 0xe3e9
movq %rax, 0x20(%r14)
leaq 0x36dc(%rip), %rax      # 0xe41c
movq %rax, 0x28(%r14)
leaq 0x36ae(%rip), %rdi      # 0xe3f9
leaq 0x14(%rsp), %rsi
pushq $0x3
popq %rcx
movq %r14, %rdx
callq 0x756c
movq %rax, %r14
movq %r15, %rdi
callq 0x4f65
movq %rbx, %rdi
callq 0x4f65
movq %r14, %rdi
callq 0x3185
movq %rax, %rbx
leaq 0x3684(%rip), %rsi      # 0xe404
movq %rax, %rdi
xorl %eax, %eax
callq 0x689e
testq %rax, %rax
je 0xaf50
movq %rax, %r15
movq %rbx, (%rsp)
movq %r14, 0x8(%rsp)
addq $0x10, %r15
movq (%r15), %r15
testq %r15, %r15
je 0xaf47
movq %r15, %rdi
leaq 0x3655(%rip), %rsi      # 0xe40e
xorl %eax, %eax
callq 0x689e
testq %rax, %rax
je 0xada3
cvttsd2si 0x30(%rax), %rbp
cmpq %rbp, %r13
cmovgq %r13, %rbp
movq %r15, %rdi
leaq 0x363f(%rip), %rsi      # 0xe41b
xorl %eax, %eax
callq 0x689e
testq %rax, %rax
jne 0xae01
movq %r15, %rdi
leaq 0x3632(%rip), %rsi      # 0xe424
xorl %eax, %eax
callq 0x689e
movq %rbp, %r13
testq %rax, %rax
je 0xada3
movq %rax, %rbx
movq %rax, %rdi
leaq 0x3624(%rip), %rsi      # 0xe432
xorl %eax, %eax
callq 0x689e
movq %rbp, %r13
testq %rax, %rax
je 0xada3
cvttsd2si 0x30(%rax), %r14
movq %rbx, %rdi
leaq 0x3610(%rip), %rsi      # 0xe43d
xorl %eax, %eax
callq 0x689e
testq %rax, %rax
je 0xae55
movq 0x20(%rax), %rdi
leaq 0x3606(%rip), %rsi      # 0xe44a
callq 0x22b0
testl %eax, %eax
jne 0xae55
movq %r14, %rdi
callq 0xac7a
movq %rbx, %rdi
leaq 0x35f1(%rip), %rsi      # 0xe450
xorl %eax, %eax
callq 0x689e
movq %rbp, %r13
testq %rax, %rax
je 0xada3
cvttsd2si 0x30(%rax), %r12
movq %rbx, %rdi
leaq 0x35d6(%rip), %rsi      # 0xe458
xorl %eax, %eax
callq 0x689e
movq %rbp, %r13
testq %rax, %rax
je 0xada3
movq %rax, %rbx
cmpl $0x0, 0x8415(%rip)      # 0x132b4
je 0xaeb3
movq 0x20(%rbx), %rsi
leaq 0x35b4(%rip), %rdi      # 0xe460
xorl %eax, %eax
callq 0x2030
movq 0x20(%rbx), %rax
cmpb $0x24, (%rax)
movq %rbp, %r13
jne 0xada3
xorl %edi, %edi
callq 0x2210
subq %r12, %rax
movq %rbp, %r13
cmpq $0x12c, %rax            # imm = 0x12C
jg 0xada3
incq 0x8405(%rip)            # 0x132e8
movq 0x20(%rbx), %rdi
incq %rdi
callq 0x4ef7
movq %rax, %r13
callq 0x7d95
movq %rax, %rbx
movq %r13, (%rax)
movq %r14, 0x8(%rax)
leaq 0x18(%rsp), %rdi
xorl %esi, %esi
leaq -0x74f(%rip), %rdx      # 0xa7c0
movq %rax, %rcx
callq 0x2450
testl %eax, %eax
je 0xaf25
movq %rbx, %rdi
callq 0x7d80
jmp 0xaf3f
cmpl $0x0, 0x8388(%rip)      # 0x132b4
je 0xaf3f
movq (%rbx), %rsi
leaq 0x3533(%rip), %rdi      # 0xe46b
xorl %eax, %eax
callq 0x2030
movq %rbp, %r13
jmp 0xada3
movq 0x8(%rsp), %r14
movq (%rsp), %rbx
movq %rbx, %rdi
callq 0x29c2
movq %r14, %rdi
callq 0x4f65
movq %r13, %rax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq