// File: /workspace/github_c_makefile_star2_O0/johanDa9u[P]MiniShell/src/csapp.c
// Function: open_clientfd
int open_clientfd(char *hostname, char *port) {
    int clientfd, rc;
    struct addrinfo hints, *listp, *p;

    /* Get a list of potential server addresses */
    memset(&hints, 0, sizeof(struct addrinfo));
    hints.ai_socktype = SOCK_STREAM;  /* Open a connection */
    hints.ai_flags = AI_NUMERICSERV;  /* ... using a numeric port arg. */
    hints.ai_flags |= AI_ADDRCONFIG;  /* Recommended for connections */
    if ((rc = getaddrinfo(hostname, port, &hints, &listp)) != 0) {
        fprintf(stderr, "getaddrinfo failed (%s:%s): %s\n", hostname, port, gai_strerror(rc));
        return -2;
    }
  
    /* Walk the list for one that we can successfully connect to */
    for (p = listp; p; p = p->ai_next) {
        /* Create a socket descriptor */
        if ((clientfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) < 0) 
            continue; /* Socket failed, try the next */

        /* Connect to the server */
        if (connect(clientfd, p->ai_addr, p->ai_addrlen) != -1) 
            break; /* Success */
        if (close(clientfd) < 0) { /* Connect failed, try another */  //line:netp:openclientfd:closefd
            fprintf(stderr, "open_clientfd: close failed: %s\n", strerror(errno));
            return -1;
        } 
    } 

    /* Clean up */
    freeaddrinfo(listp);
    if (!p) /* All connects failed */
        return -1;
    else    /* The last connect succeeded */
        return clientfd;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
leaq -0x50(%rbp), %rdi
xorl %esi, %esi
movl $0x30, %edx
callq 0x21b0
movl $0x1, -0x48(%rbp)
movl $0x400, -0x50(%rbp)     # imm = 0x400
movl -0x50(%rbp), %eax
orl $0x20, %eax
movl %eax, -0x50(%rbp)
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
leaq -0x50(%rbp), %rdx
leaq -0x58(%rbp), %rcx
callq 0x2510
movl %eax, -0x20(%rbp)
cmpl $0x0, %eax
je 0x5426
movq 0x3c1a(%rip), %rax      # 0x8ff8
movq (%rax), %rax
movq %rax, -0x78(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x70(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x68(%rbp)
movl -0x20(%rbp), %edi
callq 0x2180
movq -0x78(%rbp), %rdi
movq -0x70(%rbp), %rdx
movq -0x68(%rbp), %rcx
movq %rax, %r8
leaq 0x1191(%rip), %rsi      # 0x65a4
movb $0x0, %al
callq 0x2290
movl $0xfffffffe, -0x4(%rbp) # imm = 0xFFFFFFFE
jmp 0x54f2
movq -0x58(%rbp), %rax
movq %rax, -0x60(%rbp)
cmpq $0x0, -0x60(%rbp)
je 0x54d3
movq -0x60(%rbp), %rax
movl 0x4(%rax), %edi
movq -0x60(%rbp), %rax
movl 0x8(%rax), %esi
movq -0x60(%rbp), %rax
movl 0xc(%rax), %edx
callq 0x25d0
movl %eax, -0x1c(%rbp)
cmpl $0x0, %eax
jge 0x545d
jmp 0x54c2
movl -0x1c(%rbp), %edi
movq -0x60(%rbp), %rax
movq 0x18(%rax), %rsi
movq -0x60(%rbp), %rax
movl 0x10(%rax), %edx
callq 0x24f0
cmpl $-0x1, %eax
je 0x547b
jmp 0x54d3
movl -0x1c(%rbp), %edi
callq 0x21d0
cmpl $0x0, %eax
jge 0x54c0
movq 0x3b69(%rip), %rax      # 0x8ff8
movq (%rax), %rax
movq %rax, -0x80(%rbp)
callq 0x2060
movl (%rax), %edi
callq 0x2530
movq -0x80(%rbp), %rdi
movq %rax, %rdx
leaq 0x1114(%rip), %rsi      # 0x65c4
movb $0x0, %al
callq 0x2290
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x54f2
jmp 0x54c2
movq -0x60(%rbp), %rax
movq 0x28(%rax), %rax
movq %rax, -0x60(%rbp)
jmp 0x542e
movq -0x58(%rbp), %rdi
callq 0x25c0
cmpq $0x0, -0x60(%rbp)
jne 0x54ec
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x54f2
movl -0x1c(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x80, %rsp
popq %rbp
retq
nop