// File: /workspace/github_c_makefile_star2_O2/nilyang[P]redis-tdd-annotation/src/replication.c
// Function: slaveofCommand
void slaveofCommand(redisClient *c) {
    /* SLAVEOF is not allowed in cluster mode as replication is automatically
     * configured using the current address of the master node. */
    // 不允许在集群模式中使用
    if (server.cluster_enabled) {
        addReplyError(c,"SLAVEOF not allowed in cluster mode.");
        return;
    }

    /* The special host/port combination "NO" "ONE" turns the instance
     * into a master. Otherwise the new master address is set. */
    // SLAVEOF NO ONE 让从服务器转为主服务器
    if (!strcasecmp(c->argv[1]->ptr,"no") &&
        !strcasecmp(c->argv[2]->ptr,"one")) {
        if (server.masterhost) {
            // 让服务器取消复制，成为主服务器
            replicationUnsetMaster();
            redisLog(REDIS_NOTICE,"MASTER MODE enabled (user request)");
        }
    } else {
        long port;

        // 获取端口参数
        if ((getLongFromObjectOrReply(c, c->argv[2], &port, NULL) != REDIS_OK))
            return;

        /* Check if we are already attached to the specified slave */
        // 检查输入的 host 和 port 是否服务器目前的主服务器
        // 如果是的话，向客户端返回 +OK ，不做其他动作
        if (server.masterhost && !strcasecmp(server.masterhost,c->argv[1]->ptr)
            && server.masterport == port) {
            redisLog(REDIS_NOTICE,"SLAVE OF would result into synchronization with the master we are already connected with. No operation performed.");
            addReplySds(c,sdsnew("+OK Already connected to specified master\r\n"));
            return;
        }

        /* There was no previous master or the user specified a different one,
         * we can continue. */
        // 没有前任主服务器，或者客户端指定了新的主服务器
        // 开始执行复制操作
        replicationSetMaster(c->argv[1]->ptr, port);
        redisLog(REDIS_NOTICE,"SLAVE OF %s:%d enabled (user request)",
            server.masterhost, server.masterport);
    }
    addReply(c,shared.ok);
}

// Assembly:
pushq %r15
pushq %r14
pushq %rbx
subq $0x10, %rsp
movq %rdi, %rbx
leaq 0x939d0(%rip), %r15     # 0xc6fa8
cmpl $0x0, 0x7bc(%r15)
je 0x335fa
leaq 0x5aff8(%rip), %rsi     # 0x8e5e1
movq %rbx, %rdi
addq $0x10, %rsp
popq %rbx
popq %r14
popq %r15
jmp 0x2c2da
movq 0x38(%rbx), %r14
movq 0x8(%r14), %rax
movq 0x8(%rax), %rdi
leaq 0x5aff9(%rip), %rsi     # 0x8e606
callq 0x1f2e0
movq 0x10(%r14), %r14
testl %eax, %eax
jne 0x3362e
movq 0x8(%r14), %rdi
leaq 0x61fd5(%rip), %rsi     # 0x955fa
callq 0x1f2e0
testl %eax, %eax
je 0x336a7
leaq 0x8(%rsp), %rdx
movq %rbx, %rdi
movq %r14, %rsi
xorl %ecx, %ecx
callq 0x2f672
testl %eax, %eax
jne 0x33708
movq 0x650(%r15), %rdi
movq 0x38(%rbx), %rax
movq 0x8(%rax), %rax
movq 0x8(%rax), %r14
testq %rdi, %rdi
je 0x336c9
movq %r14, %rsi
callq 0x1f2e0
movq 0x8(%rsp), %rsi
testl %eax, %eax
jne 0x336ce
movslq 0x658(%r15), %rax
cmpq %rax, %rsi
jne 0x336ce
leaq 0x5afa8(%rip), %rsi     # 0x8e62c
pushq $0x2
popq %rdi
xorl %eax, %eax
callq 0x22613
leaq 0x5b009(%rip), %rdi     # 0x8e69e
callq 0x271cd
movq %rbx, %rdi
movq %rax, %rsi
callq 0x2c20f
jmp 0x33708
cmpq $0x0, 0x650(%r15)
je 0x336f5
callq 0x33558
leaq 0x5af4c(%rip), %rsi     # 0x8e609
pushq $0x2
popq %rdi
xorl %eax, %eax
callq 0x22613
jmp 0x336f5
movq 0x8(%rsp), %rsi
movq %r14, %rdi
callq 0x334df
movq 0x650(%r15), %rdx
movl 0x658(%r15), %ecx
leaq 0x5afdf(%rip), %rsi     # 0x8e6ca
pushq $0x2
popq %rdi
xorl %eax, %eax
callq 0x22613
leaq 0x94104(%rip), %rax     # 0xc7800
movq 0x8(%rax), %rsi
movq %rbx, %rdi
callq 0x2c10d
addq $0x10, %rsp
popq %rbx
popq %r14
popq %r15
retq