// File: /workspace/github_c_makefile_star2_O1/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 %r12
pushq %rbx
subq $0x40, %rsp
movq %rdi, %rbx
movl $0x1, %eax
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), %rcx
xorl %edi, %edi
movq %rbx, %rsi
callq 0x2510
testl %eax, %eax
jne 0x42b6
movq 0x8(%rsp), %r12
testq %r12, %r12
sete %r15b
je 0x4284
leaq 0x4(%rsp), %r14
movl 0x4(%r12), %edi
movl 0x8(%r12), %esi
movl 0xc(%r12), %edx
callq 0x25d0
movl %eax, %ebx
testl %eax, %eax
js 0x4274
movl %ebx, %edi
movl $0x1, %esi
movl $0x2, %edx
movq %r14, %rcx
movl $0x4, %r8d
callq 0x2100
movq 0x18(%r12), %rsi
movl 0x10(%r12), %edx
movl %ebx, %edi
callq 0x23e0
testl %eax, %eax
je 0x4284
movl %ebx, %edi
callq 0x21d0
testl %eax, %eax
js 0x42f2
movq 0x28(%r12), %r12
testq %r12, %r12
sete %r15b
jne 0x4220
jmp 0x4284
movq 0x8(%rsp), %rdi
callq 0x25c0
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
testb $0x1, %r15b
jne 0x42e3
movl %ebx, %edi
movl $0x400, %esi            # imm = 0x400
callq 0x2380
testl %eax, %eax
js 0x42ad
movl %ebx, %ebp
jmp 0x42e3
movl %ebx, %edi
callq 0x21d0
jmp 0x42e3
movq 0x3d3b(%rip), %rcx      # 0x7ff8
movq (%rcx), %r14
movl %eax, %edi
callq 0x2190
leaq 0x1308(%rip), %rsi      # 0x55d6
movq %r14, %rdi
movq %rbx, %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0x2290
movl $0xfffffffe, %ebp       # imm = 0xFFFFFFFE
movl %ebp, %eax
addq $0x40, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
callq 0x26b9
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
jmp 0x42e3