// File: /workspace/github_c_makefile_star2_O1/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 0x94b70(%rip), %r14     # 0xc9fa8
cmpl $0x0, 0x7bc(%r14)
je 0x3545a
leaq 0x63164(%rip), %rsi     # 0x985ad
movq %rbx, %rdi
addq $0x10, %rsp
popq %rbx
popq %r14
popq %r15
jmp 0x2d9d7
movq 0x38(%rbx), %r15
movq 0x8(%r15), %rax
movq 0x8(%rax), %rdi
leaq 0x63165(%rip), %rsi     # 0x985d2
callq 0x1f2e0
testl %eax, %eax
jne 0x35492
movq 0x10(%r15), %rax
movq 0x8(%rax), %rdi
leaq 0x6a191(%rip), %rsi     # 0x9f616
callq 0x1f2e0
testl %eax, %eax
je 0x35540
movq 0x10(%r15), %rsi
leaq 0x8(%rsp), %rdx
movq %rbx, %rdi
xorl %ecx, %ecx
callq 0x310df
testl %eax, %eax
jne 0x35575
movq 0x650(%r14), %rdi
testq %rdi, %rdi
je 0x35508
movq 0x38(%rbx), %rax
movq 0x8(%rax), %rax
movq 0x8(%rax), %rsi
callq 0x1f2e0
testl %eax, %eax
jne 0x35508
movslq 0x658(%r14), %rax
cmpq %rax, 0x8(%rsp)
jne 0x35508
leaq 0x63115(%rip), %rsi     # 0x985f8
movl $0x2, %edi
xorl %eax, %eax
callq 0x23064
leaq 0x63174(%rip), %rdi     # 0x9866a
callq 0x281da
movq %rbx, %rdi
movq %rax, %rsi
callq 0x2d894
jmp 0x35575
movq 0x38(%rbx), %rax
movq 0x8(%rax), %rax
movq 0x8(%rax), %rdi
movl 0x8(%rsp), %esi
callq 0x35339
movq 0x650(%r14), %rdx
movl 0x658(%r14), %ecx
leaq 0x63164(%rip), %rsi     # 0x98696
movl $0x2, %edi
xorl %eax, %eax
callq 0x23064
jmp 0x35562
cmpq $0x0, 0x650(%r14)
je 0x35562
callq 0x353b2
leaq 0x6307f(%rip), %rsi     # 0x985d5
movl $0x2, %edi
xorl %eax, %eax
callq 0x23064
leaq 0x95297(%rip), %rax     # 0xca800
movq 0x8(%rax), %rsi
movq %rbx, %rdi
callq 0x2d6f0
addq $0x10, %rsp
popq %rbx
popq %r14
popq %r15
retq