// File: /workspace/github_c_makefile_star2_O3/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:
testq %r9, %r9
je 0x6caf0
movl $0x0, (%r9)
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movq 0x8(%rsi), %rax
cmpq 0x90404(%rip), %rax     # 0xfcf10
je 0x6cdad
movq %r8, 0x30(%rsp)
movq %rdi, 0x38(%rsp)
movq %r9, 0x18(%rsp)
leaq 0x80(%rsp), %rax
leaq 0x78(%rsp), %rdi
movq %rdi, 0x50(%rsp)
movq %rax, (%rdi)
leaq 0x70(%rsp), %rdi
movq %rdi, 0x48(%rsp)
movl $0x1, (%rdi)
movq %rdx, (%rax)
movl %ecx, 0x8(%rax)
movq %rsi, 0x40(%rsp)
movq %rsi, 0x10(%rax)
xorl %ecx, %ecx
movq $0x0, 0x58(%rsp)
movq $0x0, 0x28(%rsp)
movl $0x0, 0xc(%rsp)
movl $0x0, 0x10(%rsp)
movl $0x0, 0x8(%rsp)
xorl %ebx, %ebx
xorl %r14d, %r14d
movq 0x50(%rsp), %rax
movq (%rax), %rax
movq %rcx, 0x60(%rsp)
leaq (%rcx,%rcx,2), %rcx
movq 0x10(%rax,%rcx,8), %rdi
movl 0x8(%rax,%rcx,8), %edx
movq (%rax,%rcx,8), %rsi
movq %rsi, 0x68(%rsp)
leaq 0x14(%rsp), %rcx
callq 0x43778
movq %rax, 0x20(%rsp)
cmpl $0x0, 0x14(%rsp)
jle 0x6cd1f
xorl %r13d, %r13d
movl %ebx, %r15d
movl %r14d, %ebp
movq 0x20(%rsp), %rax
movslq (%rax,%r13,4), %rax
movq 0x68(%rsp), %rcx
movq (%rcx,%rax,8), %r12
movq 0x8(%r12), %rdi
movzbl -0x1(%rdi), %esi
movl %esi, %eax
andl $0x7, %eax
cmpl $0x4, %eax
ja 0x6cc22
leaq 0x5a9fc(%rip), %rcx     # 0xc75f4
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
movq 0x28(%rsp), %rbx
jmpq *%rax
shrl $0x3, %esi
jmp 0x6cc29
movq -0x11(%rdi), %rsi
jmp 0x6cc29
movzwl -0x5(%rdi), %esi
jmp 0x6cc29
movl -0x9(%rdi), %esi
jmp 0x6cc29
movzbl -0x3(%rdi), %esi
jmp 0x6cc29
xorl %esi, %esi
movq 0x28(%rsp), %rbx
callq 0x66701
movl %eax, %r14d
testq %rbx, %rbx
je 0x6cc4a
movq %rbx, %rdi
movq %r12, %rsi
callq 0x41236
testl %eax, %eax
je 0x6cc9c
movl %ebp, %r14d
jmp 0x6ccb0
leaq 0x94c57(%rip), %rax     # 0x1018a8
movq 0x9b0(%rax), %rax
movl %r14d, %ecx
movq 0x40028(%rax,%rcx,8), %rdx
testq %rdx, %rdx
je 0x6cd9c
movq %rdx, 0x58(%rsp)
cmpq 0xa95b8(%rip), %rdx     # 0x116230
je 0x6cd07
cmpq $0x0, 0x20028(%rax,%rcx,8)
movl 0x8(%rsp), %eax
movl $0x1, %ecx
cmovnel %ecx, %eax
movl %eax, 0x8(%rsp)
movq %r12, %rbx
jmp 0x6ccb0
movl $0x1, 0xc(%rsp)
cmpl %r14d, %ebp
movl %ebp, %r14d
jne 0x6cdf8
movq %rbx, 0x28(%rsp)
xorl %ebx, %ebx
movl %r15d, %ebp
movl %r15d, %eax
orl 0x8(%rsp), %eax
je 0x6cce6
leaq 0x94bde(%rip), %rax     # 0x1018a8
movq 0x28(%rax), %rdi
movq %r12, %rsi
callq 0x41acf
cmpq $0x1, %rax
adcl $0x0, 0x10(%rsp)
movl %ebp, %r15d
movl %ebp, %ebx
jmp 0x6ccf1
movl $0x0, 0x8(%rsp)
movl %ebx, %r15d
incq %r13
movslq 0x14(%rsp), %rax
movl %r14d, %ebp
cmpq %rax, %r13
jl 0x6cbcc
jmp 0x6cd1f
cmpq $0x0, 0x28(%rax,%rcx,8)
je 0x6cc7e
movq %r12, 0x28(%rsp)
movl $0x1, %ebp
jmp 0x6ccc3
movq 0x20(%rsp), %rdi
callq 0x43787
movq 0x60(%rsp), %rcx
incq %rcx
movq 0x48(%rsp), %rax
movslq (%rax), %rax
cmpq %rax, %rcx
jl 0x6cb86
movl 0x10(%rsp), %r11d
testl %r11d, %r11d
setne %al
cmpl $0x0, 0xc(%rsp)
setne %cl
movq 0x58(%rsp), %r10
testq %r10, %r10
movq 0x18(%rsp), %rdi
movq 0x40(%rsp), %r8
movq 0x38(%rsp), %r9
movq 0x30(%rsp), %rsi
je 0x6cece
leaq 0x94b2a(%rip), %rdx     # 0x1018a8
movq 0x9b0(%rdx), %rdx
cmpl $0x0, 0x10(%rdx)
je 0x6ce1a
testq %rdi, %rdi
je 0x6ce13
movl $0x5, (%rdi)
jmp 0x6ce13
movq 0x20(%rsp), %rdi
callq 0x43787
movl $0x6, %eax
jmp 0x6ce07
testb $0x8, 0x98(%rdi)
je 0x6cece
cmpl $0x0, 0x158(%rdi)
jle 0x6cece
movq %r8, 0x30(%rsp)
movq %rsi, 0x40(%rsp)
movq %r9, 0x18(%rsp)
leaq 0x150(%rdi), %rax
movq %rax, 0x50(%rsp)
movq %rdi, 0x38(%rsp)
leaq 0x158(%rdi), %rax
movq %rax, 0x48(%rsp)
jmp 0x6cb55
movq 0x20(%rsp), %rdi
callq 0x43787
movl $0x1, %eax
movq 0x18(%rsp), %rcx
testq %rcx, %rcx
je 0x6ce13
movl %eax, (%rcx)
xorl %eax, %eax
jmp 0x6ced5
testq %rsi, %rsi
je 0x6ce22
movl %r14d, (%rsi)
movl %ebx, %esi
orl 0x8(%rsp), %esi
je 0x6ce71
leaq -0xee4(%rip), %rsi      # 0x6bf4d
cmpq %rsi, 0x8(%r8)
je 0x6cece
testl %ebx, %ebx
je 0x6ce59
testl %r11d, %r11d
je 0x6ce59
testq %rdi, %rdi
je 0x6ce4f
movl $0x3, (%rdi)
movl %r14d, %eax
movq 0x28(%rdx,%rax,8), %rax
jmp 0x6ced5
cmpl $0x0, 0x8(%rsp)
je 0x6ce71
testb $0x2, 0x99(%r9)
jne 0x6ceb6
testb $0x10, 0x21(%r8)
jne 0x6ceb6
testb $0x2, 0x9a(%r9)
jne 0x6ce84
movq 0xa93ae(%rip), %rax     # 0x116230
jmp 0x6cea1
testb $0x2, 0x20(%r8)
movq 0xa93a0(%rip), %rax     # 0x116230
je 0x6cea1
testb $0x2, 0x30(%rax)
je 0x6cea1
cmpq %r10, 0x850(%rax)
je 0x6ced5
testq %rdi, %rdi
je 0x6ceb1
cmpq %rax, %r10
je 0x6ceb1
movl $0x4, (%rdi)
movq %r10, %rax
jmp 0x6ced5
andb %al, %cl
je 0x6cece
testq %rdi, %rdi
je 0x6ce13
movl $0x2, (%rdi)
jmp 0x6ce13
movq 0xa935b(%rip), %rax     # 0x116230
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq