#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */
struct addrinfo {
  int /*<<< orphan*/ ai_addrlen;
  int /*<<< orphan*/ ai_addr;
  int /*<<< orphan*/ ai_protocol;
  int /*<<< orphan*/ ai_socktype;
  int /*<<< orphan*/ ai_family;
  struct addrinfo *ai_next;
  int /*<<< orphan*/ ai_flags;
};

/* Variables and functions */
int /*<<< orphan*/ AI_ADDRCONFIG;
int /*<<< orphan*/ AI_NUMERICSERV;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ close(int);
int connect(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ err_sys(char *);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
scalar_t__ getaddrinfo(char const *, char const *, struct addrinfo *,
                       struct addrinfo **);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int open_clientfd(const char *hostname, const char *port) {
  int clientfd;
  struct addrinfo hints, *listp, *p;
  memset(&hints, 0, sizeof(struct addrinfo));
  hints.ai_socktype = SOCK_STREAM;
  hints.ai_flags = AI_NUMERICSERV;
  hints.ai_flags |= AI_ADDRCONFIG;
  if (getaddrinfo(hostname, port, &hints, &listp) != 0) {
    err_sys("[open_clientfd] getaddrinfo error");
  }
  for (p = listp; p; p = p->ai_next) {
    if ((clientfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) < 0) {
      err_sys("[open_clientfd] socket error");
    }
    if (connect(clientfd, p->ai_addr, p->ai_addrlen) != -1) {
      break;
    }
    if (close(clientfd) < 0) {
      err_sys("[open_clientfd] close error");
    }
  }
  freeaddrinfo(listp);
  if (!p) {
    return -1;
  } else {
    return clientfd;
  }
}


x86 assembly (clang, -O0):
<open_clientfd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
lea    -0x48(%rbp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1040 <memset@plt>
mov    0x2e41(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    0x2e55(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2e19(%rip),%rax
mov    (%rax),%eax
or     -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
lea    -0x48(%rbp),%rdx
lea    -0x50(%rbp),%rcx
call   1070 <getaddrinfo@plt>
cmp    $0x0,%rax
je     11e2 <open_clientfd+0x72>
lea    0xe23(%rip),%rdi
call   1030 <err_sys@plt>
mov    -0x50(%rbp),%rax
mov    %rax,-0x58(%rbp)
cmpq   $0x0,-0x58(%rbp)
je     127e <open_clientfd+0x10e>
mov    -0x58(%rbp),%rax
mov    0x10(%rax),%edi
mov    -0x58(%rbp),%rax
mov    0xc(%rax),%esi
mov    -0x58(%rbp),%rax
mov    0x8(%rax),%edx
call   1090 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    1227 <open_clientfd+0xb7>
lea    0xe00(%rip),%rdi
call   1030 <err_sys@plt>
mov    -0x1c(%rbp),%edi
mov    -0x58(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x58(%rbp),%rax
mov    (%rax),%edx
call   1060 <connect@plt>
cmp    $0xffffffff,%eax
je     124a <open_clientfd+0xda>
jmp    127e <open_clientfd+0x10e>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
cmp    $0x0,%rax
jge    1268 <open_clientfd+0xf8>
lea    0xddc(%rip),%rdi
call   1030 <err_sys@plt>
jmp    126d <open_clientfd+0xfd>
mov    -0x58(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x58(%rbp)
jmp    11ea <open_clientfd+0x7a>
mov    -0x50(%rbp),%rdi
call   1080 <freeaddrinfo@plt>
cmpq   $0x0,-0x58(%rbp)
jne    129e <open_clientfd+0x12e>
movl   $0xffffffff,-0x4(%rbp)
jmp    12a4 <open_clientfd+0x134>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_clientfd>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x38,%rsp
mov    %rsi,%rbx
mov    %rdi,%rbp
lea    0x10(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1040 <memset@plt>
mov    0x2e3d(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x1c(%rsp)
mov    0x2e50(%rip),%rax
mov    0x2e19(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    %ecx,0x30(%rsp)
lea    0x8(%rsp),%rcx
mov    %rbp,%rdi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1070 <getaddrinfo@plt>
test   %rax,%rax
je     11db <open_clientfd+0x6b>
lea    0xe2a(%rip),%rdi
call   1030 <err_sys@plt>
mov    0x8(%rsp),%rbx
test   %rbx,%rbx
je     124c <open_clientfd+0xdc>
lea    0xe36(%rip),%r15
lea    0xe4c(%rip),%r14
jmp    1209 <open_clientfd+0x99>
cs nopw 0x0(%rax,%rax,1)
nop
mov    0x18(%rbx),%rbx
test   %rbx,%rbx
je     1255 <open_clientfd+0xe5>
mov    0x10(%rbx),%edi
mov    0x8(%rbx),%edx
mov    0xc(%rbx),%esi
call   1090 <socket@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    1225 <open_clientfd+0xb5>
mov    %r15,%rdi
call   1030 <err_sys@plt>
mov    (%rbx),%edx
mov    0x4(%rbx),%esi
mov    %ebp,%edi
call   1060 <connect@plt>
cmp    $0xffffffff,%eax
jne    125a <open_clientfd+0xea>
mov    %ebp,%edi
call   1050 <close@plt>
test   %rax,%rax
jns    1200 <open_clientfd+0x90>
mov    %r14,%rdi
call   1030 <err_sys@plt>
jmp    1200 <open_clientfd+0x90>
mov    $0xffffffff,%ebp
xor    %edi,%edi
jmp    125f <open_clientfd+0xef>
mov    $0xffffffff,%ebp
mov    0x8(%rsp),%rdi
call   1080 <freeaddrinfo@plt>
mov    %ebp,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.addrinfo = type { i32, i32, i32, i32, i32, %struct.addrinfo*, i32 }
@SOCK_STREAM = global i32 0
@AI_NUMERICSERV = global i32 0
@AI_ADDRCONFIG = global i32 0
@.str = [34 x i8] c"[open_clientfd] getaddrinfo error\00"
@.str.1 = [29 x i8] c"[open_clientfd] socket error\00"
@.str.2 = [28 x i8] c"[open_clientfd] close error\00"
define i32 @open_clientfd(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca %struct.addrinfo
%8 = alloca %struct.addrinfo*
%9 = alloca %struct.addrinfo*
store i8* %0, i8** %4
store i8* %1, i8** %5
%10 = call i32 @memset(%struct.addrinfo* %7, i32 0, i32 40)
%11 = load i32, i32* @SOCK_STREAM
%12 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %7, i32 0, i32 3
store i32 %11, i32* %12
%13 = load i32, i32* @AI_NUMERICSERV
%14 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %7, i32 0, i32 6
store i32 %13, i32* %14
%15 = load i32, i32* @AI_ADDRCONFIG
%16 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %7, i32 0, i32 6
%17 = load i32, i32* %16
%18 = or i32 %17, %15
store i32 %18, i32* %16
%19 = load i8*, i8** %4
%20 = load i8*, i8** %5
%21 = call i64 @getaddrinfo(i8* %19, i8* %20, %struct.addrinfo* %7, %struct.addrinfo** %8)
%22 = icmp ne i64 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %2
%24 = call i32 @err_sys(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0))
br label %25
25: ; preds = %23, %2
%26 = load %struct.addrinfo*, %struct.addrinfo** %8
store %struct.addrinfo* %26, %struct.addrinfo** %9
br label %27
27: ; preds = %62, %25
%28 = load %struct.addrinfo*, %struct.addrinfo** %9
%29 = icmp ne %struct.addrinfo* %28, null
br i1 %29, label %30, label %66
30: ; preds = %27
%31 = load %struct.addrinfo*, %struct.addrinfo** %9
%32 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %31, i32 0, i32 4
%33 = load i32, i32* %32
%34 = load %struct.addrinfo*, %struct.addrinfo** %9
%35 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %34, i32 0, i32 3
%36 = load i32, i32* %35
%37 = load %struct.addrinfo*, %struct.addrinfo** %9
%38 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %37, i32 0, i32 2
%39 = load i32, i32* %38
%40 = call i32 @socket(i32 %33, i32 %36, i32 %39)
store i32 %40, i32* %6
%41 = icmp slt i32 %40, 0
br i1 %41, label %42, label %44
42: ; preds = %30
%43 = call i32 @err_sys(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
br label %44
44: ; preds = %42, %30
%45 = load i32, i32* %6
%46 = load %struct.addrinfo*, %struct.addrinfo** %9
%47 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %46, i32 0, i32 1
%48 = load i32, i32* %47
%49 = load %struct.addrinfo*, %struct.addrinfo** %9
%50 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %49, i32 0, i32 0
%51 = load i32, i32* %50
%52 = call i32 @connect(i32 %45, i32 %48, i32 %51)
%53 = icmp ne i32 %52, -1
br i1 %53, label %54, label %55
54: ; preds = %44
br label %66
55: ; preds = %44
%56 = load i32, i32* %6
%57 = call i64 @close(i32 %56)
%58 = icmp slt i64 %57, 0
br i1 %58, label %59, label %61
59: ; preds = %55
%60 = call i32 @err_sys(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
br label %61
61: ; preds = %59, %55
br label %62
62: ; preds = %61
%63 = load %struct.addrinfo*, %struct.addrinfo** %9
%64 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %63, i32 0, i32 5
%65 = load %struct.addrinfo*, %struct.addrinfo** %64
store %struct.addrinfo* %65, %struct.addrinfo** %9
br label %27
66: ; preds = %54, %27
%67 = load %struct.addrinfo*, %struct.addrinfo** %8
%68 = call i32 @freeaddrinfo(%struct.addrinfo* %67)
%69 = load %struct.addrinfo*, %struct.addrinfo** %9
%70 = icmp ne %struct.addrinfo* %69, null
br i1 %70, label %72, label %71
71: ; preds = %66
store i32 -1, i32* %3
br label %74
72: ; preds = %66
%73 = load i32, i32* %6
store i32 %73, i32* %3
br label %74
74: ; preds = %72, %71
%75 = load i32, i32* %3
ret i32 %75
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i32, i32, i32, %struct.addrinfo*, i32 }
@SOCK_STREAM = global i32 0
@AI_NUMERICSERV = global i32 0
@AI_ADDRCONFIG = global i32 0
@.str = [34 x i8] c"[open_clientfd] getaddrinfo error\00"
@.str.1 = [29 x i8] c"[open_clientfd] socket error\00"
@.str.2 = [28 x i8] c"[open_clientfd] close error\00"
define i32 @open_clientfd(i8* %0, i8* %1) {
%3 = alloca %struct.addrinfo
%4 = alloca %struct.addrinfo*
%5 = bitcast %struct.addrinfo* %3 to i8*
%6 = bitcast %struct.addrinfo** %4 to i8*
%7 = call i32 @memset(%struct.addrinfo* %3, i32 0, i32 40)
%8 = load i32, i32* @SOCK_STREAM
%9 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 3
store i32 %8, i32* %9
%10 = load i32, i32* @AI_NUMERICSERV
%11 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 6
%12 = load i32, i32* @AI_ADDRCONFIG
%13 = or i32 %12, %10
store i32 %13, i32* %11
%14 = call i64 @getaddrinfo(i8* %0, i8* %1, %struct.addrinfo* %3, %struct.addrinfo** %4)
%15 = icmp eq i64 %14, 0
br i1 %15, label %18, label %16
16: ; preds = %2
%17 = call i32 @err_sys(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0))
br label %18
18: ; preds = %16, %2
%19 = load %struct.addrinfo*, %struct.addrinfo** %4
%20 = icmp eq %struct.addrinfo* %19, null
br i1 %20, label %52, label %21
21: ; preds = %18, %45
%22 = phi %struct.addrinfo* [ %47, %45 ], [ %19, %18 ]
%23 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %22, i64 0, i32 4
%24 = load i32, i32* %23
%25 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %22, i64 0, i32 3
%26 = load i32, i32* %25
%27 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %22, i64 0, i32 2
%28 = load i32, i32* %27
%29 = call i32 @socket(i32 %24, i32 %26, i32 %28)
%30 = icmp slt i32 %29, 0
br i1 %30, label %31, label %33
31: ; preds = %21
%32 = call i32 @err_sys(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
br label %33
33: ; preds = %31, %21
%34 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %22, i64 0, i32 1
%35 = load i32, i32* %34
%36 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %22, i64 0, i32 0
%37 = load i32, i32* %36
%38 = call i32 @connect(i32 %29, i32 %35, i32 %37)
%39 = icmp eq i32 %38, -1
br i1 %39, label %40, label %49
40: ; preds = %33
%41 = call i64 @close(i32 %29)
%42 = icmp slt i64 %41, 0
br i1 %42, label %43, label %45
43: ; preds = %40
%44 = call i32 @err_sys(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
br label %45
45: ; preds = %40, %43
%46 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %22, i64 0, i32 5
%47 = load %struct.addrinfo*, %struct.addrinfo** %46
%48 = icmp eq %struct.addrinfo* %47, null
br i1 %48, label %49, label %21
49: ; preds = %33, %45
%50 = phi i32 [ -1, %45 ], [ %29, %33 ]
%51 = load %struct.addrinfo*, %struct.addrinfo** %4
br label %52
52: ; preds = %49, %18
%53 = phi %struct.addrinfo* [ null, %18 ], [ %51, %49 ]
%54 = phi i32 [ -1, %18 ], [ %50, %49 ]
%55 = call i32 @freeaddrinfo(%struct.addrinfo* %53)
ret i32 %54
}