// File: /workspace/github_c_makefile_star2_O1/zhou-yuxin[P]redis-aof-no-cow/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.
     * 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. */
    master = (nodeIsSlave(myself) && myself->slaveof) ?
              myself->slaveof : myself;

    memset(hdr,0,sizeof(*hdr));
    hdr->ver = htons(CLUSTER_PROTO_VER);
    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,CLUSTER_NAMELEN);

    memcpy(hdr->myslots,master->slots,sizeof(hdr->myslots));
    memset(hdr->slaveof,0,CLUSTER_NAMELEN);
    if (myself->slaveof != NULL)
        memcpy(hdr->slaveof,myself->slaveof->name, 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
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
movl %esi, %ebp
movq %rdi, %rbx
movq 0xaeb4e(%rip), %rax     # 0x11b1d0
testb $0x2, 0x30(%rax)
je 0x6c694
movq 0x850(%rax), %r14
testq %r14, %r14
jne 0x6c697
movq %rax, %r14
movl $0x10d0, %edx           # imm = 0x10D0
movq %rbx, %rdi
xorl %esi, %esi
callq 0x26110
movl $0x626d4352, (%rbx)     # imm = 0x626D4352
movl %ebp, %eax
rolw $0x8, %ax
movw %ax, 0xc(%rbx)
movq 0xaeb13(%rip), %r12     # 0x11b1d0
movups 0x8(%r12), %xmm0
movups 0x18(%r12), %xmm1
movups %xmm0, 0x28(%rbx)
movups %xmm1, 0x38(%rbx)
movq 0x28(%r12), %rax
movq %rax, 0x48(%rbx)
leaq 0x50(%rbx), %rdi
leaq 0x40(%r14), %rsi
movl $0x800, %edx            # imm = 0x800
callq 0x26c60
xorps %xmm0, %xmm0
movups %xmm0, 0x850(%rbx)
movups %xmm0, 0x860(%rbx)
movq $0x0, 0x870(%rbx)
movq 0x850(%r12), %rax
testq %rax, %rax
je 0x6c733
leaq 0x850(%rbx), %rcx
movq 0x28(%rax), %rdx
movq %rdx, 0x20(%rcx)
movups 0x8(%rax), %xmm0
movups 0x18(%rax), %xmm1
movups %xmm1, 0x10(%rcx)
movups %xmm0, (%rcx)
leaq 0x9a11e(%rip), %r15     # 0x106858
movzwl 0xa0(%r15), %eax
rolw $0x8, %ax
movw %ax, 0x898(%rbx)
movzwl 0x30(%r12), %eax
rolw $0x8, %ax
movw %ax, 0x89a(%rbx)
movq 0x9a0(%r15), %rax
movb 0x10(%rax), %cl
movb %cl, 0x89c(%rbx)
movq 0x8(%rax), %rdi
callq 0x70c51
movq %rax, 0x10(%rbx)
movq 0x38(%r14), %rdi
callq 0x70c51
movq %rax, 0x18(%rbx)
movq 0xaea41(%rip), %rax     # 0x11b1d0
testb $0x2, 0x30(%rax)
jne 0x6c79e
movq 0x7b0(%r15), %rdi
jmp 0x6c7a6
callq 0x4a7d1
movq %rax, %rdi
callq 0x70c51
movq %rax, 0x20(%rbx)
movq 0xaea1a(%rip), %rax     # 0x11b1d0
testb $0x1, 0x30(%rax)
je 0x6c7d4
movq 0x9a0(%r15), %rax
cmpq $0x0, 0x60058(%rax)
je 0x6c7d4
orb $0x1, 0x89d(%rbx)
xorl %eax, %eax
cmpl $0x7, %ebp
movl $0x10d0, %ecx           # imm = 0x10D0
cmovnel %eax, %ecx
cmpl $0x3, %ebp
movl $0x8c8, %eax            # imm = 0x8C8
cmovnel %ecx, %eax
bswapl %eax
movl %eax, 0x4(%rbx)
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq