#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 {
  struct addrinfo *ai_next;
  int /*<<< orphan*/ ai_addrlen;
  int /*<<< orphan*/ ai_addr;
  int /*<<< orphan*/ ai_protocol;
  int /*<<< orphan*/ ai_socktype;
  int /*<<< orphan*/ ai_family;
};
typedef int /*<<< orphan*/ hints;

/* Variables and functions */
int /*<<< orphan*/ AF_UNSPEC;
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ bzero(struct addrinfo *, int);
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
char *gai_strerror(int);
int getaddrinfo(char const *, char const *, struct addrinfo *,
                struct addrinfo **);
int /*<<< orphan*/ printf(char *, char const *, char const *, ...);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int tcp_connect(const char *host, const char *serv) {
  int sockfd, n;
  struct addrinfo hints, *res, *p_res;
  bzero(&hints, sizeof(hints));
  hints.ai_family = AF_UNSPEC;
  hints.ai_socktype = SOCK_STREAM;
  if ((n == getaddrinfo(host, serv, &hints, &res)) != 0) {
    printf("getaddrinfo error for %s, %s: %s.\n", host, serv, gai_strerror(n));
    exit(EXIT_FAILURE);
  }
  p_res = res;
  while (NULL != res) {
    sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
    if (sockfd < 0) {
      continue;
    }
    if (0 == connect(sockfd, res->ai_addr, res->ai_addrlen)) {
      break;
    } else {
      close(sockfd);
    }
    res = res->ai_next;
  }
  if (NULL == res) {
    printf("tcp_connect error for %s, %s.\n", host, serv);
  }
  freeaddrinfo(p_res);
  return sockfd;
}


x86 assembly (clang, -O0):
<tcp_connect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x38(%rbp),%rdi
mov    $0x20,%esi
call   1060 <bzero@plt>
mov    0x2e33(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2e17(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
lea    -0x38(%rbp),%rdx
lea    -0x40(%rbp),%rcx
call   1090 <getaddrinfo@plt>
mov    %eax,%ecx
mov    -0x4c(%rbp),%eax
cmp    %ecx,%eax
sete   %al
and    $0x1,%al
movzbl %al,%eax
cmp    $0x0,%eax
je     1238 <tcp_connect+0xa8>
mov    -0x8(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x18(%rbp),%edi
call   1040 <gai_strerror@plt>
mov    -0x60(%rbp),%rsi
mov    -0x58(%rbp),%rdx
mov    %rax,%rcx
lea    0xddd(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    0x2d9f(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    -0x40(%rbp),%rax
mov    %rax,-0x48(%rbp)
xor    %eax,%eax
cmp    -0x40(%rbp),%rax
je     12b9 <tcp_connect+0x129>
mov    -0x40(%rbp),%rax
mov    0x18(%rax),%edi
mov    -0x40(%rbp),%rax
mov    0x14(%rax),%esi
mov    -0x40(%rbp),%rax
mov    0x10(%rax),%edx
call   10b0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1278 <tcp_connect+0xe8>
jmp    1240 <tcp_connect+0xb0>
mov    -0x14(%rbp),%edi
mov    -0x40(%rbp),%rax
mov    0xc(%rax),%esi
mov    -0x40(%rbp),%rax
mov    0x8(%rax),%edx
call   1080 <connect@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
jne    12a1 <tcp_connect+0x111>
jmp    12b9 <tcp_connect+0x129>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    -0x40(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x40(%rbp)
jmp    1240 <tcp_connect+0xb0>
xor    %eax,%eax
cmp    -0x40(%rbp),%rax
jne    12db <tcp_connect+0x14b>
mov    -0x8(%rbp),%rsi
mov    -0x10(%rbp),%rdx
lea    0xd4f(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x48(%rbp),%rdi
call   10a0 <freeaddrinfo@plt>
mov    -0x14(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tcp_connect>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %rsi,%r14
mov    %rdi,%r15
lea    0x8(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x20,%esi
call   1050 <bzero@plt>
mov    0x2e4f(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rsp)
mov    0x2e32(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x1c(%rsp)
mov    %rsp,%rcx
mov    %r15,%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1070 <getaddrinfo@plt>
mov    (%rsp),%rbx
test   %rbx,%rbx
je     1213 <tcp_connect+0xa3>
mov    %rbx,%rax
jmp    11d5 <tcp_connect+0x65>
nopl   0x0(%rax,%rax,1)
test   %rax,%rax
je     1213 <tcp_connect+0xa3>
mov    0x18(%rax),%edi
mov    0x10(%rax),%edx
mov    0x14(%rax),%esi
call   1090 <socket@plt>
mov    %eax,%ebp
mov    (%rsp),%rax
test   %ebp,%ebp
js     11d0 <tcp_connect+0x60>
mov    0x8(%rax),%edx
mov    0xc(%rax),%esi
mov    %ebp,%edi
call   1060 <connect@plt>
test   %rax,%rax
je     123c <tcp_connect+0xcc>
mov    %ebp,%edi
call   1040 <close@plt>
mov    (%rsp),%rax
mov    (%rax),%rax
mov    %rax,(%rsp)
jmp    11d0 <tcp_connect+0x60>
lea    0xde6(%rip),%rdi
mov    %r15,%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1030 <printf@plt>
mov    %rbx,%rdi
call   1080 <freeaddrinfo@plt>
mov    %ebp,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
cmpq   $0x0,(%rsp)
jne    1227 <tcp_connect+0xb7>
jmp    1213 <tcp_connect+0xa3>

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

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