// File: /workspace/github_c_makefile_star2_O0/nilyang[P]redis-tdd-annotation/src/cluster.c
// Function: clusterStartHandshake
int clusterStartHandshake(char *ip, int port) {
    clusterNode *n;
    char norm_ip[REDIS_IP_STR_LEN];
    struct sockaddr_storage sa;

    /* IP sanity check */
    // ip 合法性检查
    if (inet_pton(AF_INET,ip,
            &(((struct sockaddr_in *)&sa)->sin_addr)))
    {
        sa.ss_family = AF_INET;
    } else if (inet_pton(AF_INET6,ip,
            &(((struct sockaddr_in6 *)&sa)->sin6_addr)))
    {
        sa.ss_family = AF_INET6;
    } else {
        errno = EINVAL;
        return 0;
    }

    /* Port sanity check */
    // port 合法性检查
    if (port <= 0 || port > (65535-REDIS_CLUSTER_PORT_INCR)) {
        errno = EINVAL;
        return 0;
    }

    /* Set norm_ip as the normalized string representation of the node
     * IP address. */
    if (sa.ss_family == AF_INET)
        inet_ntop(AF_INET,
            (void*)&(((struct sockaddr_in *)&sa)->sin_addr),
            norm_ip,REDIS_IP_STR_LEN);
    else
        inet_ntop(AF_INET6,
            (void*)&(((struct sockaddr_in6 *)&sa)->sin6_addr),
            norm_ip,REDIS_IP_STR_LEN);

    // 检查节点是否已经发送握手请求，如果是的话，那么直接返回，防止出现重复握手
    if (clusterHandshakeInProgress(norm_ip,port)) {
        errno = EAGAIN;
        return 0;
    }

    /* Add the node with a random address (NULL as first argument to
     * createClusterNode()). Everything will be fixed during the
     * handskake. */
    // 对给定地址的节点设置一个随机名字
    // 当 HANDSHAKE 完成时，当前节点会取得给定地址节点的真正名字
    // 到时会用真名替换随机名
    n = createClusterNode(NULL,REDIS_NODE_HANDSHAKE|REDIS_NODE_MEET);
    memcpy(n->ip,norm_ip,sizeof(n->ip));
    n->port = port;

    // 将节点添加到集群当中
    clusterAddNode(n);

    return 1;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xd0, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq -0x10(%rbp), %rsi
leaq -0xd0(%rbp), %rdx
addq $0x4, %rdx
movl $0x2, %edi
callq 0x1f150
cmpl $0x0, %eax
je 0x6aa0b
movw $0x2, -0xd0(%rbp)
jmp 0x6aa4d
movq -0x10(%rbp), %rsi
leaq -0xd0(%rbp), %rdx
addq $0x8, %rdx
movl $0xa, %edi
callq 0x1f150
cmpl $0x0, %eax
je 0x6aa34
movw $0xa, -0xd0(%rbp)
jmp 0x6aa4b
callq 0x1f820
movl $0x16, (%rax)
movl $0x0, -0x4(%rbp)
jmp 0x6ab2a
jmp 0x6aa4d
cmpl $0x0, -0x14(%rbp)
jle 0x6aa5c
cmpl $0xd8ef, -0x14(%rbp)    # imm = 0xD8EF
jle 0x6aa73
callq 0x1f820
movl $0x16, (%rax)
movl $0x0, -0x4(%rbp)
jmp 0x6ab2a
movzwl -0xd0(%rbp), %eax
cmpl $0x2, %eax
jne 0x6aa9f
leaq -0xd0(%rbp), %rsi
addq $0x4, %rsi
leaq -0x50(%rbp), %rdx
movl $0x2, %edi
movl $0x2e, %ecx
callq 0x1fbe0
jmp 0x6aabd
leaq -0xd0(%rbp), %rsi
addq $0x8, %rsi
leaq -0x50(%rbp), %rdx
movl $0xa, %edi
movl $0x2e, %ecx
callq 0x1fbe0
leaq -0x50(%rbp), %rdi
movl -0x14(%rbp), %esi
callq 0x6a920
cmpl $0x0, %eax
je 0x6aae2
callq 0x1f820
movl $0xb, (%rax)
movl $0x0, -0x4(%rbp)
jmp 0x6ab2a
xorl %eax, %eax
movl %eax, %edi
movl $0xa0, %esi
callq 0x68a20
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rdi
addq $0x888, %rdi            # imm = 0x888
leaq -0x50(%rbp), %rsi
movl $0x2e, %edx
callq 0x1fbf0
movl -0x14(%rbp), %ecx
movq -0x20(%rbp), %rax
movl %ecx, 0x8b8(%rax)
movq -0x20(%rbp), %rdi
callq 0x68bb0
movl $0x1, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0xd0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)