// File: /workspace/github_c_makefile_star2_O0/gplessis[P]dotdeb-redis/src/cluster.c
// Function: getNodeByQuery
clusterNode *getNodeByQuery(client *c, struct redisCommand *cmd, robj **argv, int argc, int *hashslot, int *error_code) {
    clusterNode *n = NULL;
    robj *firstkey = NULL;
    int multiple_keys = 0;
    multiState *ms, _ms;
    multiCmd mc;
    int i, slot = 0, migrating_slot = 0, importing_slot = 0, missing_keys = 0;

    /* Set error code optimistically for the base case. */
    if (error_code) *error_code = CLUSTER_REDIR_NONE;

    /* We handle all the cases as if they were EXEC commands, so we have
     * a common code path for everything */
    if (cmd->proc == execCommand) {
        /* If CLIENT_MULTI flag is not set EXEC is just going to return an
         * error. */
        if (!(c->flags & CLIENT_MULTI)) return myself;
        ms = &c->mstate;
    } else {
        /* In order to have a single codepath create a fake Multi State
         * structure if the client is not in MULTI/EXEC state, this way
         * we have a single codepath below. */
        ms = &_ms;
        _ms.commands = &mc;
        _ms.count = 1;
        mc.argv = argv;
        mc.argc = argc;
        mc.cmd = cmd;
    }

    /* Check that all the keys are in the same hash slot, and obtain this
     * slot and the node associated. */
    for (i = 0; i < ms->count; i++) {
        struct redisCommand *mcmd;
        robj **margv;
        int margc, *keyindex, numkeys, j;

        mcmd = ms->commands[i].cmd;
        margc = ms->commands[i].argc;
        margv = ms->commands[i].argv;

        keyindex = getKeysFromCommand(mcmd,margv,margc,&numkeys);
        for (j = 0; j < numkeys; j++) {
            robj *thiskey = margv[keyindex[j]];
            int thisslot = keyHashSlot((char*)thiskey->ptr,
                                       sdslen(thiskey->ptr));

            if (firstkey == NULL) {
                /* This is the first key we see. Check what is the slot
                 * and node. */
                firstkey = thiskey;
                slot = thisslot;
                n = server.cluster->slots[slot];

                /* Error: If a slot is not served, we are in "cluster down"
                 * state. However the state is yet to be updated, so this was
                 * not trapped earlier in processCommand(). Report the same
                 * error to the client. */
                if (n == NULL) {
                    getKeysFreeResult(keyindex);
                    if (error_code)
                        *error_code = CLUSTER_REDIR_DOWN_UNBOUND;
                    return NULL;
                }

                /* If we are migrating or importing this slot, we need to check
                 * if we have all the keys in the request (the only way we
                 * can safely serve the request, otherwise we return a TRYAGAIN
                 * error). To do so we set the importing/migrating state and
                 * increment a counter for every missing key. */
                if (n == myself &&
                    server.cluster->migrating_slots_to[slot] != NULL)
                {
                    migrating_slot = 1;
                } else if (server.cluster->importing_slots_from[slot] != NULL) {
                    importing_slot = 1;
                }
            } else {
                /* If it is not the first key, make sure it is exactly
                 * the same key as the first we saw. */
                if (!equalStringObjects(firstkey,thiskey)) {
                    if (slot != thisslot) {
                        /* Error: multiple keys from different slots. */
                        getKeysFreeResult(keyindex);
                        if (error_code)
                            *error_code = CLUSTER_REDIR_CROSS_SLOT;
                        return NULL;
                    } else {
                        /* Flag this request as one with multiple different
                         * keys. */
                        multiple_keys = 1;
                    }
                }
            }

            /* Migarting / Improrting slot? Count keys we don't have. */
            if ((migrating_slot || importing_slot) &&
                lookupKeyRead(&server.db[0],thiskey) == NULL)
            {
                missing_keys++;
            }
        }
        getKeysFreeResult(keyindex);
    }

    /* No key at all in command? then we can serve the request
     * without redirections or errors in all the cases. */
    if (n == NULL) return myself;

    /* Cluster is globally down but we got keys? We can't serve the request. */
    if (server.cluster->state != CLUSTER_OK) {
        if (error_code) *error_code = CLUSTER_REDIR_DOWN_STATE;
        return NULL;
    }

    /* Return the hashslot by reference. */
    if (hashslot) *hashslot = slot;

    /* MIGRATE always works in the context of the local node if the slot
     * is open (migrating or importing state). We need to be able to freely
     * move keys among instances in this case. */
    if ((migrating_slot || importing_slot) && cmd->proc == migrateCommand)
        return myself;

    /* If we don't have all the keys and we are migrating the slot, send
     * an ASK redirection. */
    if (migrating_slot && missing_keys) {
        if (error_code) *error_code = CLUSTER_REDIR_ASK;
        return server.cluster->migrating_slots_to[slot];
    }

    /* If we are receiving the slot, and the client correctly flagged the
     * request as "ASKING", we can serve the request. However if the request
     * involves multiple keys and we don't have them all, the only option is
     * to send a TRYAGAIN error. */
    if (importing_slot &&
        (c->flags & CLIENT_ASKING || cmd->flags & CMD_ASKING))
    {
        if (multiple_keys && missing_keys) {
            if (error_code) *error_code = CLUSTER_REDIR_UNSTABLE;
            return NULL;
        } else {
            return myself;
        }
    }

    /* Handle the read-only client case reading from a slave: if this
     * node is a slave and the request is about an hash slot our master
     * is serving, we can reply without redirection. */
    if (c->flags & CLIENT_READONLY &&
        cmd->flags & CMD_READONLY &&
        nodeIsSlave(myself) &&
        myself->slaveof == n)
    {
        return myself;
    }

    /* Base case: just return the right node. However if this node is not
     * myself, set error_code to MOVED since we need to issue a rediretion. */
    if (n != myself && error_code) *error_code = CLUSTER_REDIR_MOVED;
    return n;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xe0, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movl %ecx, -0x24(%rbp)
movq %r8, -0x30(%rbp)
movq %r9, -0x38(%rbp)
movq $0x0, -0x40(%rbp)
movq $0x0, -0x48(%rbp)
movl $0x0, -0x4c(%rbp)
movl $0x0, -0x90(%rbp)
movl $0x0, -0x94(%rbp)
movl $0x0, -0x98(%rbp)
movl $0x0, -0x9c(%rbp)
cmpq $0x0, -0x38(%rbp)
je 0x86e82
movq -0x38(%rbp), %rax
movl $0x0, (%rax)
movq -0x18(%rbp), %rax
leaq -0x1127d(%rip), %rcx    # 0x75c10
cmpq %rcx, 0x8(%rax)
jne 0x86ec5
movq -0x10(%rbp), %rax
movl 0x98(%rax), %eax
andl $0x8, %eax
cmpl $0x0, %eax
jne 0x86eb5
movq 0x105cb4(%rip), %rax    # 0x18cb60
movq %rax, -0x8(%rbp)
jmp 0x8736c
movq -0x10(%rbp), %rax
addq $0x150, %rax            # imm = 0x150
movq %rax, -0x58(%rbp)
jmp 0x86ef8
leaq -0x70(%rbp), %rax
movq %rax, -0x58(%rbp)
leaq -0x88(%rbp), %rax
movq %rax, -0x70(%rbp)
movl $0x1, -0x68(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0x88(%rbp)
movl -0x24(%rbp), %eax
movl %eax, -0x80(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x78(%rbp)
movl $0x0, -0x8c(%rbp)
movl -0x8c(%rbp), %eax
movq -0x58(%rbp), %rcx
cmpl 0x8(%rcx), %eax
jge 0x871b3
movq -0x58(%rbp), %rax
movq (%rax), %rax
movslq -0x8c(%rbp), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq 0x10(%rax), %rax
movq %rax, -0xa8(%rbp)
movq -0x58(%rbp), %rax
movq (%rax), %rax
movslq -0x8c(%rbp), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
movl %eax, -0xb4(%rbp)
movq -0x58(%rbp), %rax
movq (%rax), %rax
movslq -0x8c(%rbp), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rax
movq %rax, -0xb0(%rbp)
movq -0xa8(%rbp), %rdi
movq -0xb0(%rbp), %rsi
movl -0xb4(%rbp), %edx
leaq -0xc4(%rbp), %rcx
callq 0x4cf00
movq %rax, -0xc0(%rbp)
movl $0x0, -0xc8(%rbp)
movl -0xc8(%rbp), %eax
cmpl -0xc4(%rbp), %eax
jge 0x87193
movq -0xb0(%rbp), %rax
movq -0xc0(%rbp), %rcx
movslq -0xc8(%rbp), %rdx
movslq (%rcx,%rdx,4), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0xd0(%rbp)
movq -0xd0(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0xe0(%rbp)
movq -0xd0(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x7cf60
movq -0xe0(%rbp), %rdi
movl %eax, %esi
callq 0x7df10
movl %eax, -0xd4(%rbp)
cmpq $0x0, -0x48(%rbp)
jne 0x870e7
movq -0xd0(%rbp), %rax
movq %rax, -0x48(%rbp)
movl -0xd4(%rbp), %eax
movl %eax, -0x90(%rbp)
leaq 0xf11a0(%rip), %rax     # 0x1781d8
movq 0x9b0(%rax), %rax
movslq -0x90(%rbp), %rcx
movq 0x40028(%rax,%rcx,8), %rax
movq %rax, -0x40(%rbp)
cmpq $0x0, -0x40(%rbp)
jne 0x87083
movq -0xc0(%rbp), %rdi
callq 0x4cf70
cmpq $0x0, -0x38(%rbp)
je 0x87076
movq -0x38(%rbp), %rax
movl $0x6, (%rax)
movq $0x0, -0x8(%rbp)
jmp 0x8736c
movq -0x40(%rbp), %rax
cmpq 0x105ad2(%rip), %rax    # 0x18cb60
jne 0x870b9
leaq 0xf1141(%rip), %rax     # 0x1781d8
movq 0x9b0(%rax), %rax
movslq -0x90(%rbp), %rcx
cmpq $0x0, 0x28(%rax,%rcx,8)
je 0x870b9
movl $0x1, -0x94(%rbp)
jmp 0x870e5
leaq 0xf1118(%rip), %rax     # 0x1781d8
movq 0x9b0(%rax), %rax
movslq -0x90(%rbp), %rcx
cmpq $0x0, 0x20028(%rax,%rcx,8)
je 0x870e3
movl $0x1, -0x98(%rbp)
jmp 0x870e5
jmp 0x8713f
movq -0x48(%rbp), %rdi
movq -0xd0(%rbp), %rsi
callq 0x49840
cmpl $0x0, %eax
jne 0x8713d
movl -0x90(%rbp), %eax
cmpl -0xd4(%rbp), %eax
je 0x87134
movq -0xc0(%rbp), %rdi
callq 0x4cf70
cmpq $0x0, -0x38(%rbp)
je 0x87127
movq -0x38(%rbp), %rax
movl $0x1, (%rax)
movq $0x0, -0x8(%rbp)
jmp 0x8736c
movl $0x1, -0x4c(%rbp)
jmp 0x8713d
jmp 0x8713f
cmpl $0x0, -0x94(%rbp)
jne 0x87151
cmpl $0x0, -0x98(%rbp)
je 0x8717d
leaq 0xf1080(%rip), %rax     # 0x1781d8
movq 0x28(%rax), %rdi
movq -0xd0(%rbp), %rsi
callq 0x4a6c0
cmpq $0x0, %rax
jne 0x8717d
movl -0x9c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x9c(%rbp)
jmp 0x8717f
movl -0xc8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc8(%rbp)
jmp 0x86fa3
movq -0xc0(%rbp), %rdi
callq 0x4cf70
movl -0x8c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x8c(%rbp)
jmp 0x86f02
cmpq $0x0, -0x40(%rbp)
jne 0x871ca
movq 0x10599f(%rip), %rax    # 0x18cb60
movq %rax, -0x8(%rbp)
jmp 0x8736c
leaq 0xf1007(%rip), %rax     # 0x1781d8
movq 0x9b0(%rax), %rax
cmpl $0x0, 0x10(%rax)
je 0x871fc
cmpq $0x0, -0x38(%rbp)
je 0x871ef
movq -0x38(%rbp), %rax
movl $0x5, (%rax)
movq $0x0, -0x8(%rbp)
jmp 0x8736c
cmpq $0x0, -0x30(%rbp)
je 0x8720f
movl -0x90(%rbp), %ecx
movq -0x30(%rbp), %rax
movl %ecx, (%rax)
cmpl $0x0, -0x94(%rbp)
jne 0x87221
cmpl $0x0, -0x98(%rbp)
je 0x87242
movq -0x18(%rbp), %rax
leaq -0x11ec(%rip), %rcx     # 0x86040
cmpq %rcx, 0x8(%rax)
jne 0x87242
movq 0x105927(%rip), %rax    # 0x18cb60
movq %rax, -0x8(%rbp)
jmp 0x8736c
cmpl $0x0, -0x94(%rbp)
je 0x87288
cmpl $0x0, -0x9c(%rbp)
je 0x87288
cmpq $0x0, -0x38(%rbp)
je 0x87265
movq -0x38(%rbp), %rax
movl $0x3, (%rax)
leaq 0xf0f6c(%rip), %rax     # 0x1781d8
movq 0x9b0(%rax), %rax
movslq -0x90(%rbp), %rcx
movq 0x28(%rax,%rcx,8), %rax
movq %rax, -0x8(%rbp)
jmp 0x8736c
cmpl $0x0, -0x98(%rbp)
je 0x872f0
movq -0x10(%rbp), %rax
movl 0x98(%rax), %eax
andl $0x200, %eax            # imm = 0x200
cmpl $0x0, %eax
jne 0x872b6
movq -0x18(%rbp), %rax
movl 0x20(%rax), %eax
andl $0x1000, %eax           # imm = 0x1000
cmpl $0x0, %eax
je 0x872f0
cmpl $0x0, -0x4c(%rbp)
je 0x872e3
cmpl $0x0, -0x9c(%rbp)
je 0x872e3
cmpq $0x0, -0x38(%rbp)
je 0x872d6
movq -0x38(%rbp), %rax
movl $0x2, (%rax)
movq $0x0, -0x8(%rbp)
jmp 0x8736c
movq 0x105876(%rip), %rax    # 0x18cb60
movq %rax, -0x8(%rbp)
jmp 0x8736c
movq -0x10(%rbp), %rax
movl 0x98(%rax), %eax
andl $0x20000, %eax          # imm = 0x20000
cmpl $0x0, %eax
je 0x87346
movq -0x18(%rbp), %rax
movl 0x20(%rax), %eax
andl $0x2, %eax
cmpl $0x0, %eax
je 0x87346
movq 0x105846(%rip), %rax    # 0x18cb60
movl 0x30(%rax), %eax
andl $0x2, %eax
cmpl $0x0, %eax
je 0x87346
movq 0x105834(%rip), %rax    # 0x18cb60
movq 0x850(%rax), %rax
cmpq -0x40(%rbp), %rax
jne 0x87346
movq 0x105820(%rip), %rax    # 0x18cb60
movq %rax, -0x8(%rbp)
jmp 0x8736c
movq -0x40(%rbp), %rax
cmpq 0x10580f(%rip), %rax    # 0x18cb60
je 0x87364
cmpq $0x0, -0x38(%rbp)
je 0x87364
movq -0x38(%rbp), %rax
movl $0x4, (%rax)
movq -0x40(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0xe0, %rsp
popq %rbp
retq
nopl (%rax)