#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;
};
typedef int /*<<< orphan*/ hints;

/* Variables and functions */
int /*<<< orphan*/ AF_UNSPEC;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
int connect(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
int getaddrinfo(char *, char *, struct addrinfo *, struct addrinfo **);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int tcp_connect(char *address, char *port) {
  int status, sockfd;
  struct addrinfo hints, *servinfo, *p;
  memset(&hints, 0, sizeof(hints));
  hints.ai_family = AF_UNSPEC;
  hints.ai_socktype = SOCK_STREAM;
  status = getaddrinfo(address, port, &hints, &servinfo);
  if (status != 0) {
    printf("\ntcpconnect: getaddrinfo failed");
    freeaddrinfo(servinfo);
    return -1;
  }
  for (p = servinfo; p != NULL; p = p->ai_next) {
    sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol);
    if (sockfd == -1) {
      printf("\ntcpconnect: socket failed");
      continue;
    }
    status = connect(sockfd, p->ai_addr, p->ai_addrlen);
    if (status == -1) {
      close(sockfd);
      printf("\ntcpconnect: connect failed");
      continue;
    }
    break;
  }
  if (p == NULL) {
    printf("\ntcpconnect: could not get a valid socket file descriptof");
    freeaddrinfo(servinfo);
    return -1;
  }
  freeaddrinfo(servinfo);
  return sockfd;
}


x86 assembly (clang, -O0):
<tcp_connect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
lea    -0x40(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1040 <memset@plt>
mov    0x2e51(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    0x2e35(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
lea    -0x40(%rbp),%rdx
lea    -0x48(%rbp),%rcx
call   1070 <getaddrinfo@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     11eb <tcp_connect+0x7b>
lea    0xe2f(%rip),%rdi
call   1030 <printf@plt>
mov    -0x48(%rbp),%rdi
call   1080 <freeaddrinfo@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c2 <tcp_connect+0x152>
mov    -0x48(%rbp),%rax
mov    %rax,-0x50(%rbp)
cmpq   $0x0,-0x50(%rbp)
je     1287 <tcp_connect+0x117>
mov    -0x50(%rbp),%rax
mov    0x10(%rax),%edi
mov    -0x50(%rbp),%rax
mov    0xc(%rax),%esi
mov    -0x50(%rbp),%rax
mov    0x8(%rax),%edx
call   1090 <socket@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    1236 <tcp_connect+0xc6>
lea    0xdf4(%rip),%rdi
call   1030 <printf@plt>
jmp    1276 <tcp_connect+0x106>
mov    -0x20(%rbp),%edi
mov    -0x50(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x50(%rbp),%rax
mov    (%rax),%edx
call   1060 <connect@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    1271 <tcp_connect+0x101>
mov    -0x20(%rbp),%edi
call   1050 <close@plt>
lea    0xdd4(%rip),%rdi
call   1030 <printf@plt>
jmp    1276 <tcp_connect+0x106>
jmp    1287 <tcp_connect+0x117>
mov    -0x50(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x50(%rbp)
jmp    11f3 <tcp_connect+0x83>
cmpq   $0x0,-0x50(%rbp)
jne    12b3 <tcp_connect+0x143>
lea    0xdbe(%rip),%rdi
call   1030 <printf@plt>
mov    -0x48(%rbp),%rdi
call   1080 <freeaddrinfo@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c2 <tcp_connect+0x152>
mov    -0x48(%rbp),%rdi
call   1080 <freeaddrinfo@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tcp_connect>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x30,%rsp
mov    %rsi,%rbx
mov    %rdi,%rbp
lea    0x10(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1040 <memset@plt>
mov    0x2e4b(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rsp)
mov    0x2e2e(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x1c(%rsp)
lea    0x8(%rsp),%rcx
mov    %rbp,%rdi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1070 <getaddrinfo@plt>
test   %eax,%eax
je     11f4 <tcp_connect+0x84>
lea    0xe32(%rip),%r14
mov    %r14,%rdi
call   1030 <printf@plt>
mov    $0xffffffff,%ebp
mov    0x8(%rsp),%rdi
call   1080 <freeaddrinfo@plt>
mov    %ebp,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x8(%rsp),%rbx
lea    0xe57(%rip),%r14
test   %rbx,%rbx
je     11ce <tcp_connect+0x5e>
lea    0xe14(%rip),%r15
lea    0xe28(%rip),%r12
jmp    1238 <tcp_connect+0xc8>
cs nopw 0x0(%rax,%rax,1)
nop
mov    %ebp,%edi
call   1050 <close@plt>
mov    %r12,%rdi
call   1030 <printf@plt>
mov    0x18(%rbx),%rbx
test   %rbx,%rbx
je     11ce <tcp_connect+0x5e>
mov    0x10(%rbx),%edi
mov    0x8(%rbx),%edx
mov    0xc(%rbx),%esi
call   1090 <socket@plt>
mov    %r15,%rdi
cmp    $0xffffffff,%eax
je     122a <tcp_connect+0xba>
mov    %eax,%ebp
mov    (%rbx),%edx
mov    0x4(%rbx),%esi
mov    %eax,%edi
call   1060 <connect@plt>
cmp    $0xffffffff,%eax
je     1220 <tcp_connect+0xb0>
jmp    11db <tcp_connect+0x6b>

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

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i32, i32, i32, %struct.addrinfo* }
@AF_UNSPEC = global i32 0
@SOCK_STREAM = global i32 0
@.str = [32 x i8] c"\0Atcpconnect: getaddrinfo failed\00"
@.str.1 = [27 x i8] c"\0Atcpconnect: socket failed\00"
@.str.2 = [28 x i8] c"\0Atcpconnect: connect failed\00"
@.str.3 = [58 x i8] c"\0Atcpconnect: could not get a valid socket file descriptof\00"
define i32 @tcp_connect(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 32)
%8 = load i32, i32* @AF_UNSPEC
%9 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 4
store i32 %8, i32* %9
%10 = load i32, i32* @SOCK_STREAM
%11 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 3
store i32 %10, i32* %11
%12 = call i32 @getaddrinfo(i8* %0, i8* %1, %struct.addrinfo* %3, %struct.addrinfo** %4)
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %42
14: ; preds = %2
%15 = load %struct.addrinfo*, %struct.addrinfo** %4
%16 = icmp eq %struct.addrinfo* %15, null
br i1 %16, label %42, label %17
17: ; preds = %14, %36
%18 = phi %struct.addrinfo* [ %40, %36 ], [ %15, %14 ]
%19 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %18, i64 0, i32 4
%20 = load i32, i32* %19
%21 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %18, i64 0, i32 3
%22 = load i32, i32* %21
%23 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %18, i64 0, i32 2
%24 = load i32, i32* %23
%25 = call i32 @socket(i32 %20, i32 %22, i32 %24)
%26 = icmp eq i32 %25, -1
br i1 %26, label %36, label %27
27: ; preds = %17
%28 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %18, i64 0, i32 1
%29 = load i32, i32* %28
%30 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %18, i64 0, i32 0
%31 = load i32, i32* %30
%32 = call i32 @connect(i32 %25, i32 %29, i32 %31)
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %45
34: ; preds = %27
%35 = call i32 @close(i32 %25)
br label %36
36: ; preds = %17, %34
%37 = phi i8* [ getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), %34 ], [ getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), %17 ]
%38 = call i32 @printf(i8* dereferenceable(1) %37)
%39 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %18, i64 0, i32 5
%40 = load %struct.addrinfo*, %struct.addrinfo** %39
%41 = icmp eq %struct.addrinfo* %40, null
br i1 %41, label %42, label %17
42: ; preds = %36, %14, %2
%43 = phi i8* [ getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), %2 ], [ getelementptr inbounds ([58 x i8], [58 x i8]* @.str.3, i64 0, i64 0), %14 ], [ getelementptr inbounds ([58 x i8], [58 x i8]* @.str.3, i64 0, i64 0), %36 ]
%44 = call i32 @printf(i8* dereferenceable(1) %43)
br label %45
45: ; preds = %27, %42
%46 = phi i32 [ -1, %42 ], [ %25, %27 ]
%47 = load %struct.addrinfo*, %struct.addrinfo** %4
%48 = call i32 @freeaddrinfo(%struct.addrinfo* %47)
ret i32 %46
}