// File: /workspace/github_c_makefile_star2_O0/nilyang[P]redis-tdd-annotation/src/cluster.c
// Function: clusterBuildMessageHdr
void clusterBuildMessageHdr(clusterMsg *hdr, int type) {
    int totlen = 0;
    uint64_t offset;
    clusterNode *master;

    /* If this node is a master, we send its slots bitmap and configEpoch.
     *
     * 如果这是一个主节点，那么发送该节点的槽 bitmap 和配置纪元。
     *
     * If this node is a slave we send the master's information instead (the
     * node is flagged as slave so the receiver knows that it is NOT really
     * in charge for this slots.
     * 如果这是一个从节点，
     * 那么发送这个节点的主节点的槽 bitmap 和配置纪元。
     *
     * 因为接收信息的节点通过标识可以知道这个节点是一个从节点，
     * 所以接收信息的节点不会将从节点错认作是主节点。
     */
    master = (nodeIsSlave(myself) && myself->slaveof) ?
              myself->slaveof : myself;

    // 清零信息头
    memset(hdr,0,sizeof(*hdr));

    hdr->sig[0] = 'R';
    hdr->sig[1] = 'C';
    hdr->sig[2] = 'm';
    hdr->sig[3] = 'b';

    // 设置信息类型
    hdr->type = htons(type);

    // 设置信息发送者
    memcpy(hdr->sender,myself->name,REDIS_CLUSTER_NAMELEN);

    // 设置当前节点负责的槽
    memcpy(hdr->myslots,master->slots,sizeof(hdr->myslots));

    // 清零 slaveof 域
    memset(hdr->slaveof,0,REDIS_CLUSTER_NAMELEN);

    // 如果节点是从节点的话，那么设置 slaveof 域
    if (myself->slaveof != NULL)
        memcpy(hdr->slaveof,myself->slaveof->name, REDIS_CLUSTER_NAMELEN);

    // 设置端口号
    hdr->port = htons(server.port);

    // 设置标识
    hdr->flags = htons(myself->flags);

    // 设置状态
    hdr->state = server.cluster->state;

    /* Set the currentEpoch and configEpochs. */
    // 设置集群当前配置纪元
    hdr->currentEpoch = htonu64(server.cluster->currentEpoch);
    // 设置主节点当前配置纪元
    hdr->configEpoch = htonu64(master->configEpoch);

    /* Set the replication offset. */
    // 设置复制偏移量
    if (nodeIsSlave(myself))
        offset = replicationGetSlaveOffset();
    else
        offset = server.master_repl_offset;
    hdr->offset = htonu64(offset);

    /* Set the message flags. */
    if (nodeIsMaster(myself) && server.cluster->mf_end)
        hdr->mflags[0] |= CLUSTERMSG_FLAG0_PAUSED;

    /* Compute the message length for certain messages. For other messages
     * this is up to the caller. */
    // 计算信息的长度
    if (type == CLUSTERMSG_TYPE_FAIL) {
        totlen = sizeof(clusterMsg)-sizeof(union clusterMsgData);
        totlen += sizeof(clusterMsgDataFail);
    } else if (type == CLUSTERMSG_TYPE_UPDATE) {
        totlen = sizeof(clusterMsg)-sizeof(union clusterMsgData);
        totlen += sizeof(clusterMsgDataUpdate);
    }

    // 设置信息的长度
    hdr->totlen = htonl(totlen);
    /* For PING, PONG, and MEET, fixing the totlen field is up to the caller. */
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x30, %rsp
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movl $0x0, -0x10(%rbp)
movq 0xbb23b(%rip), %rax     # 0x127f78
movl 0x30(%rax), %eax
andl $0x2, %eax
cmpl $0x0, %eax
je 0x6cd6d
movq 0xbb229(%rip), %rax     # 0x127f78
cmpq $0x0, 0x850(%rax)
je 0x6cd6d
movq 0xbb218(%rip), %rax     # 0x127f78
movq 0x850(%rax), %rax
movq %rax, -0x28(%rbp)
jmp 0x6cd78
movq 0xbb204(%rip), %rax     # 0x127f78
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
movq %rax, -0x20(%rbp)
movq -0x8(%rbp), %rdi
xorl %esi, %esi
movl $0x10d0, %edx           # imm = 0x10D0
callq 0x1f110
movq -0x8(%rbp), %rax
movb $0x52, (%rax)
movq -0x8(%rbp), %rax
movb $0x43, 0x1(%rax)
movq -0x8(%rbp), %rax
movb $0x6d, 0x2(%rax)
movq -0x8(%rbp), %rax
movb $0x62, 0x3(%rax)
movl -0xc(%rbp), %eax
movzwl %ax, %edi
callq 0x1f270
movw %ax, %cx
movq -0x8(%rbp), %rax
movw %cx, 0xc(%rax)
movq -0x8(%rbp), %rdi
addq $0x28, %rdi
movq 0xbb1a4(%rip), %rsi     # 0x127f78
addq $0x8, %rsi
movl $0x28, %edx
callq 0x1fbf0
movq -0x8(%rbp), %rdi
addq $0x50, %rdi
movq -0x20(%rbp), %rsi
addq $0x40, %rsi
movl $0x800, %edx            # imm = 0x800
callq 0x1fbf0
movq -0x8(%rbp), %rdi
addq $0x850, %rdi            # imm = 0x850
xorl %esi, %esi
movl $0x28, %edx
callq 0x1f110
movq 0xbb15e(%rip), %rax     # 0x127f78
cmpq $0x0, 0x850(%rax)
je 0x6ce4b
movq -0x8(%rbp), %rdi
addq $0x850, %rdi            # imm = 0x850
movq 0xbb142(%rip), %rax     # 0x127f78
movq 0x850(%rax), %rsi
addq $0x8, %rsi
movl $0x28, %edx
callq 0x1fbf0
leaq 0xa6aa6(%rip), %rax     # 0x1138f8
movl 0x88(%rax), %eax
movzwl %ax, %edi
callq 0x1f270
movw %ax, %cx
movq -0x8(%rbp), %rax
movw %cx, 0x898(%rax)
movq 0xbb103(%rip), %rax     # 0x127f78
movl 0x30(%rax), %eax
movzwl %ax, %edi
callq 0x1f270
movw %ax, %cx
movq -0x8(%rbp), %rax
movw %cx, 0x89a(%rax)
leaq 0xa6a63(%rip), %rax     # 0x1138f8
movq 0x7d0(%rax), %rax
movl 0x10(%rax), %eax
movb %al, %cl
movq -0x8(%rbp), %rax
movb %cl, 0x89c(%rax)
leaq 0xa6a46(%rip), %rax     # 0x1138f8
movq 0x7d0(%rax), %rax
movq 0x8(%rax), %rdi
callq 0x724a0
movq %rax, %rcx
movq -0x8(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x20(%rbp), %rax
movq 0x38(%rax), %rdi
callq 0x724a0
movq %rax, %rcx
movq -0x8(%rbp), %rax
movq %rcx, 0x18(%rax)
movq 0xbb08c(%rip), %rax     # 0x127f78
movl 0x30(%rax), %eax
andl $0x2, %eax
cmpl $0x0, %eax
je 0x6cf02
callq 0x42960
movq %rax, -0x18(%rbp)
jmp 0x6cf14
leaq 0xa69ef(%rip), %rax     # 0x1138f8
movq 0x5f0(%rax), %rax
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rdi
callq 0x724a0
movq %rax, %rcx
movq -0x8(%rbp), %rax
movq %rcx, 0x20(%rax)
movq 0xbb049(%rip), %rax     # 0x127f78
movl 0x30(%rax), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x6cf66
leaq 0xa69b7(%rip), %rax     # 0x1138f8
movq 0x7d0(%rax), %rax
cmpq $0x0, 0x60050(%rax)
je 0x6cf66
movq -0x8(%rbp), %rax
movzbl 0x89d(%rax), %ecx
orl $0x1, %ecx
movb %cl, 0x89d(%rax)
cmpl $0x3, -0xc(%rbp)
jne 0x6cf80
movl $0x8a0, -0x10(%rbp)     # imm = 0x8A0
movslq -0x10(%rbp), %rax
addq $0x28, %rax
movl %eax, -0x10(%rbp)
jmp 0x6cf9c
cmpl $0x7, -0xc(%rbp)
jne 0x6cf9a
movl $0x8a0, -0x10(%rbp)     # imm = 0x8A0
movslq -0x10(%rbp), %rax
addq $0x830, %rax            # imm = 0x830
movl %eax, -0x10(%rbp)
jmp 0x6cf9c
movl -0x10(%rbp), %edi
callq 0x1fab0
movl %eax, %ecx
movq -0x8(%rbp), %rax
movl %ecx, 0x4(%rax)
addq $0x30, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)