// File: /workspace/github_c_makefile_star2_O3/johanDa9u[P]MiniShell/src/csapp.c
// Function: open_listenfd
int open_listenfd(char *port) 
{
    struct addrinfo hints, *listp, *p;
    int listenfd, rc, optval=1;

    /* Get a list of potential server addresses */
    memset(&hints, 0, sizeof(struct addrinfo));
    hints.ai_socktype = SOCK_STREAM;             /* Accept connections */
    hints.ai_flags = AI_PASSIVE | AI_ADDRCONFIG; /* ... on any IP address */
    hints.ai_flags |= AI_NUMERICSERV;            /* ... using port number */
    if ((rc = getaddrinfo(NULL, port, &hints, &listp)) != 0) {
        fprintf(stderr, "getaddrinfo failed (port %s): %s\n", port, gai_strerror(rc));
        return -2;
    }

    /* Walk the list for one that we can bind to */
    for (p = listp; p; p = p->ai_next) {
        /* Create a socket descriptor */
        if ((listenfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) < 0) 
            continue;  /* Socket failed, try the next */

        /* Eliminates "Address already in use" error from bind */
        setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR,    //line:netp:csapp:setsockopt
                   (const void *)&optval , sizeof(int));

        /* Bind the descriptor to the address */
        if (bind(listenfd, p->ai_addr, p->ai_addrlen) == 0)
            break; /* Success */
        if (close(listenfd) < 0) { /* Bind failed, try the next */
            fprintf(stderr, "open_listenfd close failed: %s\n", strerror(errno));
            return -1;
        }
    }


    /* Clean up */
    freeaddrinfo(listp);
    if (!p) /* No address worked */
        return -1;

    /* Make it a listening socket ready to accept connection requests */
    if (listen(listenfd, LISTENQ) < 0) {
        close(listenfd);
	return -1;
    }
    return listenfd;
}

// Assembly:
pushq %r15
pushq %r14
pushq %rbx
subq $0x40, %rsp
movq %rdi, %rbx
movl $0x1, %eax
movl %eax, 0xc(%rsp)
xorps %xmm0, %xmm0
leaq 0x10(%rsp), %rdx
movaps %xmm0, (%rdx)
movaps %xmm0, 0x10(%rdx)
movaps %xmm0, 0x20(%rdx)
movl %eax, 0x8(%rdx)
movl $0x421, (%rdx)          # imm = 0x421
movq %rsp, %rcx
xorl %edi, %edi
movq %rbx, %rsi
callq 0x2510
testl %eax, %eax
jne 0x425e
movq (%rsp), %r15
testq %r15, %r15
je 0x4224
leaq 0xc(%rsp), %r14
movl 0x4(%r15), %edi
movl 0x8(%r15), %esi
movl 0xc(%r15), %edx
callq 0x25d0
testl %eax, %eax
js 0x4215
movl %eax, %ebx
movl %eax, %edi
movl $0x1, %esi
movl $0x2, %edx
movq %r14, %rcx
movl $0x4, %r8d
callq 0x2100
movq 0x18(%r15), %rsi
movl 0x10(%r15), %edx
movl %ebx, %edi
callq 0x23e0
testl %eax, %eax
je 0x423c
movl %ebx, %edi
callq 0x21d0
testl %eax, %eax
js 0x428d
movq 0x28(%r15), %r15
testq %r15, %r15
jne 0x41c6
movq (%rsp), %rdi
jmp 0x4226
xorl %edi, %edi
callq 0x25c0
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
movl %ebx, %eax
addq $0x40, %rsp
popq %rbx
popq %r14
popq %r15
retq
movq (%rsp), %rdi
callq 0x25c0
movl %ebx, %edi
movl $0x400, %esi            # imm = 0x400
callq 0x2380
testl %eax, %eax
jns 0x4230
movl %ebx, %edi
callq 0x21d0
jmp 0x422b
movq 0x3d93(%rip), %rcx      # 0x7ff8
movq (%rcx), %r14
movl %eax, %edi
callq 0x2190
leaq 0x135b(%rip), %rsi      # 0x55d1
movq %r14, %rdi
movq %rbx, %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0x2290
movl $0xfffffffe, %ebx       # imm = 0xFFFFFFFE
jmp 0x4230
callq 0x26b9
jmp 0x422b