// File: /workspace/github_c_makefile_star2_O1/zhou-yuxin[P]redis-aof-no-cow/src/cluster.c
// Function: clusterUpdateSlotsConfigWith
void clusterUpdateSlotsConfigWith(clusterNode *sender, uint64_t senderConfigEpoch, unsigned char *slots) {
    int j;
    clusterNode *curmaster, *newmaster = NULL;
    /* The dirty slots list is a list of slots for which we lose the ownership
     * while having still keys inside. This usually happens after a failover
     * or after a manual cluster reconfiguration operated by the admin.
     *
     * If the update message is not able to demote a master to slave (in this
     * case we'll resync with the master updating the whole key space), we
     * need to delete all the keys in the slots we lost ownership. */
    uint16_t dirty_slots[CLUSTER_SLOTS];
    int dirty_slots_count = 0;

    /* Here we set curmaster to this node or the node this node
     * replicates to if it's a slave. In the for loop we are
     * interested to check if slots are taken away from curmaster. */
    curmaster = nodeIsMaster(myself) ? myself : myself->slaveof;

    if (sender == myself) {
        serverLog(LL_WARNING,"Discarding UPDATE message about myself.");
        return;
    }

    for (j = 0; j < CLUSTER_SLOTS; j++) {
        if (bitmapTestBit(slots,j)) {
            /* The slot is already bound to the sender of this message. */
            if (server.cluster->slots[j] == sender) continue;

            /* The slot is in importing state, it should be modified only
             * manually via redis-trib (example: a resharding is in progress
             * and the migrating side slot was already closed and is advertising
             * a new config. We still want the slot to be closed manually). */
            if (server.cluster->importing_slots_from[j]) continue;

            /* We rebind the slot to the new node claiming it if:
             * 1) The slot was unassigned or the new node claims it with a
             *    greater configEpoch.
             * 2) We are not currently importing the slot. */
            if (server.cluster->slots[j] == NULL ||
                server.cluster->slots[j]->configEpoch < senderConfigEpoch)
            {
                /* Was this slot mine, and still contains keys? Mark it as
                 * a dirty slot. */
                if (server.cluster->slots[j] == myself &&
                    countKeysInSlot(j) &&
                    sender != myself)
                {
                    dirty_slots[dirty_slots_count] = j;
                    dirty_slots_count++;
                }

                if (server.cluster->slots[j] == curmaster)
                    newmaster = sender;
                clusterDelSlot(j);
                clusterAddSlot(sender,j);
                clusterDoBeforeSleep(CLUSTER_TODO_SAVE_CONFIG|
                                     CLUSTER_TODO_UPDATE_STATE|
                                     CLUSTER_TODO_FSYNC_CONFIG);
            }
        }
    }

    /* If at least one slot was reassigned from a node to another node
     * with a greater configEpoch, it is possible that:
     * 1) We are a master left without slots. This means that we were
     *    failed over and we should turn into a replica of the new
     *    master.
     * 2) We are a slave and our master is left without slots. We need
     *    to replicate to the new slots owner. */
    if (newmaster && curmaster->numslots == 0) {
        serverLog(LL_WARNING,
            "Configuration change detected. Reconfiguring myself "
            "as a replica of %.40s", sender->name);
        clusterSetMaster(sender);
        clusterDoBeforeSleep(CLUSTER_TODO_SAVE_CONFIG|
                             CLUSTER_TODO_UPDATE_STATE|
                             CLUSTER_TODO_FSYNC_CONFIG);
    } else if (dirty_slots_count) {
        /* If we are here, we received an update message which removed
         * ownership for certain slots we still have keys about, but still
         * we are serving some slots, so this master node was not demoted to
         * a slave.
         *
         * In order to maintain a consistent state between keys and slots
         * we need to remove all the keys from the slots we lost. */
        for (j = 0; j < dirty_slots_count; j++)
            delKeysInSlot(dirty_slots[j]);
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x8018, %rsp           # imm = 0x8018
movq %rdx, %r14
movq %rsi, 0x8(%rsp)
movq %rdi, %rbx
movq 0xb007a(%rip), %rax     # 0x11b1d0
testb $0x1, 0x30(%rax)
movq %rax, %rbp
jne 0x6b166
movq 0x850(%rax), %rbp
cmpq %rbx, %rax
je 0x6b29e
xorl %r12d, %r12d
leaq 0x9b6df(%rip), %r13     # 0x106858
movq $0x0, (%rsp)
xorl %r15d, %r15d
movl %r12d, %eax
shrl $0x3, %eax
movzbl (%r14,%rax), %eax
movl %r12d, %ecx
andl $0x7, %ecx
btl %ecx, %eax
jae 0x6b25e
movq 0x9a0(%r13), %rcx
movq 0x40028(%rcx,%r12,8), %rax
cmpq %rbx, %rax
je 0x6b25e
cmpq $0x0, 0x20028(%rcx,%r12,8)
jne 0x6b25e
testq %rax, %rax
je 0x6b1d9
movq 0x8(%rsp), %rcx
cmpq %rcx, 0x38(%rax)
jae 0x6b25e
cmpq 0xafff0(%rip), %rax     # 0x11b1d0
jne 0x6b209
movl %r12d, %edi
callq 0x46fe3
testl %eax, %eax
je 0x6b209
cmpq %rbx, 0xaffdb(%rip)     # 0x11b1d0
je 0x6b209
movq (%rsp), %rax
cltq
movw %r12w, 0x10(%rsp,%rax,2)
incl %eax
movq %rax, (%rsp)
movq 0x9a0(%r13), %rax
cmpq %rbp, 0x40028(%rax,%r12,8)
cmoveq %rbx, %r15
movl %r12d, %edi
callq 0x69fe5
movq 0x9a0(%r13), %rax
cmpq $0x0, 0x40028(%rax,%r12,8)
jne 0x6b250
movq %rbx, %rdi
movl %r12d, %esi
callq 0x6dccc
movq 0x9a0(%r13), %rax
movq %rbx, 0x40028(%rax,%r12,8)
movq 0x9a0(%r13), %rax
orl $0xe, 0x60080(%rax)
incq %r12
cmpq $0x4000, %r12           # imm = 0x4000
jne 0x6b184
testq %r15, %r15
je 0x6b27c
cmpl $0x0, 0x840(%rbp)
je 0x6b2c2
movq (%rsp), %rax
testl %eax, %eax
jle 0x6b2ef
movl %eax, %ebx
xorl %r14d, %r14d
movzwl 0x10(%rsp,%r14,2), %edi
callq 0x46f43
incq %r14
cmpq %r14, %rbx
jne 0x6b289
jmp 0x6b2ef
leaq 0x5f9af(%rip), %rsi     # 0xcac54
movl $0x3, %edi
xorl %eax, %eax
addq $0x8018, %rsp           # imm = 0x8018
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x32ea6
leaq 0x8(%rbx), %rdx
leaq 0x5f9af(%rip), %rsi     # 0xcac7c
movl $0x3, %edi
xorl %eax, %eax
callq 0x32ea6
movq %rbx, %rdi
callq 0x6b31e
movq 0x9a0(%r13), %rax
orl $0xe, 0x60080(%rax)
addq $0x8018, %rsp           # imm = 0x8018
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq