// File: /workspace/github_c_makefile_star2_O2/Arlu[P]RDMA-Hello-World/setup_ib.c
// Function: get_client_dest
int get_client_dest()
{
    int sockfd, connfd, len;
    struct sockaddr_in server_address, client_address;

    // Start connection:
    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sockfd == -1)
    {
        perror("Socket creation failed!\n");
        return 1;
    }

    memset(&server_address, 0, sizeof(server_address));
    server_address.sin_family = AF_INET;
    server_address.sin_addr.s_addr = htonl(INADDR_ANY);
    server_address.sin_port = htons(PORT);

    if (bind(sockfd, (struct sockaddr*)&server_address, sizeof(server_address)))
    {
        perror("Socket bind failed!\n");
        return 1;
    }

    if (listen(sockfd, 5))
    {
        perror("Listen failed...\n");
        return 1;
    }

    len = sizeof(client_address);

    connfd = accept(sockfd, (struct sockaddr*)&client_address, &len);
    if (connfd < 0)
    {
        perror("Server accept failed!\n");
        return 1;
    }

    // Get client details:
    {
        char *buf;
        buf = (char*)malloc(sizeof(struct IBDest));
        int offset = 0;

        // Get him details.
        while (offset < sizeof(struct IBDest))
            offset += read(connfd, buf + offset, sizeof(struct IBDest) - offset);

        memcpy(&remote_dest, buf, sizeof(struct IBDest));

        // Send my details:
        memset(buf, 0, sizeof(struct IBDest));
        offset = 0;

        memcpy(buf, &local_dest, sizeof(struct IBDest));
        write(connfd, buf, sizeof(struct IBDest));
    }

    // Finish connection:
    close(sockfd);

    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
pushq $0x2
popq %rdi
pushq $0x1
popq %r14
movl %r14d, %esi
xorl %edx, %edx
callq 0x1290
cmpl $-0x1, %eax
je 0x171a
movl %eax, %ebx
leaq -0x4c(%rbp), %rsi
andq $0x0, 0x8(%rsi)
movl $0x901f0002, %eax       # imm = 0x901F0002
movq %rax, (%rsi)
pushq $0x10
popq %rdx
movl %ebx, %edi
callq 0x11c0
testl %eax, %eax
jne 0x1723
pushq $0x5
popq %rsi
movl %ebx, %edi
callq 0x1170
testl %eax, %eax
jne 0x172c
leaq -0x2c(%rbp), %rdx
movl $0x10, (%rdx)
leaq -0x3c(%rbp), %rsi
movl %ebx, %edi
callq 0x11f0
testl %eax, %eax
js 0x1735
movl %eax, %r15d
pushq $0xc
popq %r12
movq %r12, %rdi
callq 0x1160
movq %rax, %r14
xorl %eax, %eax
movslq %eax, %r13
cmpq $0xb, %r13
ja 0x16d8
leaq (%r14,%r13), %rsi
movq %r12, %rdx
subq %r13, %rdx
movl %r15d, %edi
callq 0x1110
addq %r13, %rax
jmp 0x16b8
movl 0x8(%r14), %eax
movl %eax, 0x2aa2(%rip)      # 0x4184
movq (%r14), %rax
movq %rax, 0x2a90(%rip)      # 0x417c
movq 0x2a7d(%rip), %rax      # 0x4170
movq %rax, (%r14)
movl 0x2a7c(%rip), %eax      # 0x4178
movl %eax, 0x8(%r14)
pushq $0xc
popq %rdx
movl %r15d, %edi
movq %r14, %rsi
callq 0x1080
movl %ebx, %edi
callq 0x10f0
xorl %r14d, %r14d
jmp 0x1741
leaq 0x9bf(%rip), %rdi       # 0x20e0
jmp 0x173c
leaq 0x9cf(%rip), %rdi       # 0x20f9
jmp 0x173c
leaq 0x9db(%rip), %rdi       # 0x210e
jmp 0x173c
leaq 0x9e4(%rip), %rdi       # 0x2120
callq 0x11d0
movl %r14d, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq