// File: /workspace/github_c_makefile_star2_O2/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 %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rdi, %r14
pushq $0x1
popq %rax
movl %eax, 0x4(%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
leaq 0x8(%rsp), %rbx
xorl %edi, %edi
movq %r14, %rsi
movq %rbx, %rcx
callq 0x2510
testl %eax, %eax
jne 0x3fa7
pushq $0x1
popq %r14
pushq $0x2
popq %r15
leaq 0x4(%rsp), %r12
pushq $0x4
popq %r13
movq (%rbx), %rbx
testq %rbx, %rbx
je 0x3f76
movl 0x4(%rbx), %edi
movl 0x8(%rbx), %esi
movl 0xc(%rbx), %edx
callq 0x25d0
movl %eax, %ebp
testl %eax, %eax
js 0x3f70
movl %ebp, %edi
movl %r14d, %esi
movl %r15d, %edx
movq %r12, %rcx
movl %r13d, %r8d
callq 0x2100
movq 0x18(%rbx), %rsi
movl 0x10(%rbx), %edx
movl %ebp, %edi
callq 0x23e0
testl %eax, %eax
je 0x3f76
movl %ebp, %edi
callq 0x21d0
testl %eax, %eax
js 0x3fd3
addq $0x28, %rbx
jmp 0x3f24
movq 0x8(%rsp), %rdi
callq 0x25c0
pushq $-0x1
popq %r14
testq %rbx, %rbx
je 0x4001
movl %ebp, %edi
movl $0x400, %esi            # imm = 0x400
callq 0x2380
testl %eax, %eax
js 0x3f9e
movl %ebp, %r14d
jmp 0x4001
movl %ebp, %edi
callq 0x21d0
jmp 0x4001
movq 0x404a(%rip), %rcx      # 0x7ff8
movq (%rcx), %rbx
movl %eax, %edi
callq 0x2190
leaq 0x1612(%rip), %rsi      # 0x55d1
movq %rbx, %rdi
movq %r14, %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0x2290
pushq $-0x2
jmp 0x3fff
movq 0x401e(%rip), %rax      # 0x7ff8
movq (%rax), %rbx
callq 0x2070
movl (%rax), %edi
callq 0x2530
leaq 0x1603(%rip), %rsi      # 0x55f3
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x2290
pushq $-0x1
popq %r14
movl %r14d, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq