// File: /workspace/github_c_makefile_star2_O3/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 0x94742(%rip), %r15     # 0xc9fa8
cmpl $0x0, 0x7bc(%r15)
je 0x35888
leaq 0x62da6(%rip), %rsi     # 0x9861d
movq %rbx, %rdi
addq $0x10, %rsp
popq %rbx
popq %r14
popq %r15
jmp 0x2dc63
movq 0x38(%rbx), %r14
movq 0x8(%r14), %rax
movq 0x8(%rax), %rdi
leaq 0x62da7(%rip), %rsi     # 0x98642
callq 0x1f2e0
movq 0x10(%r14), %r14
testl %eax, %eax
jne 0x358bc
movq 0x8(%r14), %rdi
leaq 0x69dd3(%rip), %rsi     # 0x9f686
callq 0x1f2e0
testl %eax, %eax
je 0x35937
leaq 0x8(%rsp), %rdx
movq %rbx, %rdi
movq %r14, %rsi
xorl %ecx, %ecx
callq 0x313b2
testl %eax, %eax
jne 0x3599c
movq 0x650(%r15), %rdi
movq 0x38(%rbx), %rax
movq 0x8(%rax), %rax
movq 0x8(%rax), %r14
testq %rdi, %rdi
je 0x3595b
movq %r14, %rsi
callq 0x1f2e0
movq 0x8(%rsp), %rsi
testl %eax, %eax
jne 0x35960
movslq 0x658(%r15), %rax
cmpq %rax, %rsi
jne 0x35960
leaq 0x62d56(%rip), %rsi     # 0x98668
movl $0x2, %edi
xorl %eax, %eax
callq 0x23223
leaq 0x62db5(%rip), %rdi     # 0x986da
callq 0x28486
movq %rbx, %rdi
movq %rax, %rsi
callq 0x2db20
jmp 0x3599c
cmpq $0x0, 0x650(%r15)
je 0x35989
callq 0x357e0
leaq 0x62cf8(%rip), %rsi     # 0x98645
movl $0x2, %edi
xorl %eax, %eax
callq 0x23223
jmp 0x35989
movq 0x8(%rsp), %rsi
movq %r14, %rdi
callq 0x35767
movq 0x650(%r15), %rdx
movl 0x658(%r15), %ecx
leaq 0x62d89(%rip), %rsi     # 0x98706
movl $0x2, %edi
xorl %eax, %eax
callq 0x23223
leaq 0x94e70(%rip), %rax     # 0xca800
movq 0x8(%rax), %rsi
movq %rbx, %rdi
callq 0x2d97c
addq $0x10, %rsp
popq %rbx
popq %r14
popq %r15
retq