// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x10(%rbp)
movl $0x1, -0x5c(%rbp)
leaq -0x40(%rbp), %rdi
xorl %esi, %esi
movl $0x30, %edx
callq 0x21b0
movl $0x1, -0x38(%rbp)
movl $0x21, -0x40(%rbp)
movl -0x40(%rbp), %eax
orl $0x400, %eax            # imm = 0x400
movl %eax, -0x40(%rbp)
movq -0x10(%rbp), %rsi
xorl %eax, %eax
movl %eax, %edi
leaq -0x40(%rbp), %rdx
leaq -0x48(%rbp), %rcx
callq 0x2510
movl %eax, -0x58(%rbp)
cmpl $0x0, %eax
je 0x559f
movq 0x3a95(%rip), %rax      # 0x8ff8
movq (%rax), %rax
movq %rax, -0x70(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x68(%rbp)
movl -0x58(%rbp), %edi
callq 0x2180
movq -0x70(%rbp), %rdi
movq -0x68(%rbp), %rdx
movq %rax, %rcx
leaq 0x1059(%rip), %rsi      # 0x65e5
movb $0x0, %al
callq 0x2290
movl $0xfffffffe, -0x4(%rbp) # imm = 0xFFFFFFFE
jmp 0x56ad
movq -0x48(%rbp), %rax
movq %rax, -0x50(%rbp)
cmpq $0x0, -0x50(%rbp)
je 0x566b
movq -0x50(%rbp), %rax
movl 0x4(%rax), %edi
movq -0x50(%rbp), %rax
movl 0x8(%rax), %esi
movq -0x50(%rbp), %rax
movl 0xc(%rax), %edx
callq 0x25d0
movl %eax, -0x54(%rbp)
cmpl $0x0, %eax
jge 0x55d9
jmp 0x565a
movl -0x54(%rbp), %edi
movl $0x1, %esi
movl $0x2, %edx
leaq -0x5c(%rbp), %rcx
movl $0x4, %r8d
callq 0x20f0
movl -0x54(%rbp), %edi
movq -0x50(%rbp), %rax
movq 0x18(%rax), %rsi
movq -0x50(%rbp), %rax
movl 0x10(%rax), %edx
callq 0x23e0
cmpl $0x0, %eax
jne 0x5613
jmp 0x566b
movl -0x54(%rbp), %edi
callq 0x21d0
cmpl $0x0, %eax
jge 0x5658
movq 0x39d1(%rip), %rax      # 0x8ff8
movq (%rax), %rax
movq %rax, -0x78(%rbp)
callq 0x2060
movl (%rax), %edi
callq 0x2530
movq -0x78(%rbp), %rdi
movq %rax, %rdx
leaq 0xfbf(%rip), %rsi       # 0x6607
movb $0x0, %al
callq 0x2290
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x56ad
jmp 0x565a
movq -0x50(%rbp), %rax
movq 0x28(%rax), %rax
movq %rax, -0x50(%rbp)
jmp 0x55a7
movq -0x48(%rbp), %rdi
callq 0x25c0
cmpq $0x0, -0x50(%rbp)
jne 0x5684
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x56ad
movl -0x54(%rbp), %edi
movl $0x400, %esi            # imm = 0x400
callq 0x2380
cmpl $0x0, %eax
jge 0x56a7
movl -0x54(%rbp), %edi
callq 0x21d0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x56ad
movl -0x54(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x80, %rsp
popq %rbp
retq
nopl (%rax)