// File: /workspace/github_c_makefile_star2_O3/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 0x42e0(%rip), %rax      # 0x10772
leaq 0x20(%rsp), %r14
movq %rax, (%r14)
incq %rdi
callq 0x626f
movq %rax, %r15
movq %rax, 0x8(%r14)
leaq 0x42c9(%rip), %rax      # 0x10779
movq %rax, 0x10(%r14)
movslq %ebp, %rdi
callq 0x626f
movq %rax, %r12
movq %rax, 0x18(%r14)
leaq 0x42b7(%rip), %rax      # 0x10781
movq %rax, 0x20(%r14)
leaq 0x42df(%rip), %rax      # 0x107b4
movq %rax, 0x28(%r14)
leaq 0x42b1(%rip), %rdi      # 0x10791
leaq 0x14(%rsp), %rsi
movq %r14, %rdx
movl $0x3, %ecx
callq 0x87e0
movq %rax, %r14
movq %r15, %rdi
callq 0x5775
movq %r12, %rdi
callq 0x5775
movq %r14, 0x8(%rsp)
movq %r14, %rdi
callq 0x3163
leaq 0x4283(%rip), %rsi      # 0x1079c
movq %rax, (%rsp)
movq %rax, %rdi
xorl %eax, %eax
callq 0x7a6f
testq %rax, %rax
je 0xc6ff
movq 0x10(%rax), %r15
testq %r15, %r15
je 0xc6ff
leaq 0x4262(%rip), %r12      # 0x107a6
leaq 0x4268(%rip), %r13      # 0x107b3
movq %r15, %rdi
movq %r12, %rsi
xorl %eax, %eax
callq 0x7a6f
testq %rax, %rax
je 0xc6cb
cvttsd2si 0x30(%rax), %r14
cmpq %r14, %rbx
cmovgq %rbx, %r14
movq %r15, %rdi
movq %r13, %rsi
xorl %eax, %eax
callq 0x7a6f
testq %rax, %rax
jne 0xc59a
movq %r15, %rdi
leaq 0x4232(%rip), %rsi      # 0x107bc
xorl %eax, %eax
callq 0x7a6f
testq %rax, %rax
je 0xc6ce
movq %rax, %rbp
movq %rax, %rdi
leaq 0x4223(%rip), %rsi      # 0x107ca
xorl %eax, %eax
callq 0x7a6f
testq %rax, %rax
je 0xc6ce
cvttsd2si 0x30(%rax), %rbx
movq %rbp, %rdi
leaq 0x420e(%rip), %rsi      # 0x107d5
xorl %eax, %eax
callq 0x7a6f
testq %rax, %rax
je 0xc5ef
movq 0x20(%rax), %rdi
leaq 0x4204(%rip), %rsi      # 0x107e2
callq 0x22b0
testl %eax, %eax
jne 0xc5ef
movq %rbx, %rdi
callq 0xc408
movq %rbp, %rdi
leaq 0x41ef(%rip), %rsi      # 0x107e8
xorl %eax, %eax
callq 0x7a6f
testq %rax, %rax
je 0xc6ce
movq %r13, %r12
cvttsd2si 0x30(%rax), %r13
movq %rbp, %rdi
leaq 0x41d4(%rip), %rsi      # 0x107f0
xorl %eax, %eax
callq 0x7a6f
testq %rax, %rax
je 0xc6bf
movq %rax, %rbp
cmpl $0x0, 0x8c7e(%rip)      # 0x152b4
je 0xc64a
movq 0x20(%rbp), %rsi
leaq 0x41b5(%rip), %rdi      # 0x107f8
xorl %eax, %eax
callq 0x2030
movq 0x20(%rbp), %rax
cmpb $0x24, (%rax)
jne 0xc6bf
xorl %edi, %edi
callq 0x2210
subq %r13, %rax
cmpq $0x12c, %rax            # imm = 0x12C
jg 0xc6bf
incq 0x8c7c(%rip)            # 0x152e8
movq 0x20(%rbp), %rdi
incq %rdi
callq 0x56e9
movq %rax, %r13
movl $0x1, %edi
movl $0x10, %esi
callq 0x2190
movq %rax, %rbp
movq %r13, (%rax)
movq %rbx, 0x8(%rax)
leaq 0x18(%rsp), %rdi
xorl %esi, %esi
leaq -0x876(%rip), %rdx      # 0xbe2c
movq %rax, %rcx
callq 0x2450
testl %eax, %eax
je 0xc6df
movq (%rbp), %rdi
callq 0x5775
movq %rbp, %rdi
callq 0x2478
movq %r12, %r13
leaq 0x40dd(%rip), %r12      # 0x107a6
jmp 0xc6ce
movq %rbx, %r14
movq (%r15), %r15
movq %r14, %rbx
testq %r15, %r15
jne 0xc54b
jmp 0xc702
cmpl $0x0, 0x8bce(%rip)      # 0x152b4
movq %r12, %r13
je 0xc6c2
movq (%rbp), %rsi
leaq 0x410d(%rip), %rdi      # 0x10803
xorl %eax, %eax
callq 0x2030
jmp 0xc6c2
movq %rbx, %r14
movq (%rsp), %rdi
callq 0x2aa8
movq 0x8(%rsp), %rdi
callq 0x5775
movq %r14, %rax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq