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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *, ...);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
char *gai_strerror(int);
int 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 /*<<< orphan*/ stderr;
int setup_connection(const char *host, const char *port) {
  int sockfd = 0;
  struct hostent *hostent;
  struct sockaddr_in sockaddr;
  struct addrinfo hints, *res, *ressave;
  int error;
  memset(&hints, 0, sizeof(hints));
  hints.ai_family = AF_INET;
  hints.ai_socktype = SOCK_STREAM;
  if ((error = getaddrinfo(host, port, &hints, &res))) {
    fprintf(stderr, "cannot resolve host: %s\n", gai_strerror(error));
    return 0;
  }
  ressave = res;
  do {
    sockfd = socket(AF_INET, SOCK_STREAM, res->ai_protocol);
    if (sockfd < 0)
      continue;
    if (connect(sockfd, res->ai_addr, res->ai_addrlen) == 0)
      break;
    close(sockfd);
  } while ((res = res->ai_next) != NULL);
  if (res == NULL) {
    fprintf(stderr, "connect error for %s, %s\n", host, port);
    return 0;
  }
  freeaddrinfo(ressave);
  return sockfd;
}


x86 assembly (clang, -O0):
<setup_connection>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
lea    -0x50(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1040 <memset@plt>
mov    0x2e3a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    0x2e16(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
lea    -0x50(%rbp),%rdx
lea    -0x58(%rbp),%rcx
call   1080 <getaddrinfo@plt>
mov    %eax,-0x64(%rbp)
cmp    $0x0,%eax
je     1214 <setup_connection+0x94>
mov    0x2df1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x68(%rbp)
mov    -0x64(%rbp),%edi
call   1030 <gai_strerror@plt>
mov    -0x68(%rbp),%edi
mov    %rax,%rdx
lea    0xdff(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12d3 <setup_connection+0x153>
mov    -0x58(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    0x2dc5(%rip),%rax
mov    (%rax),%edi
mov    0x2da4(%rip),%rax
mov    (%rax),%esi
mov    -0x58(%rbp),%rax
mov    0x8(%rax),%edx
call   10a0 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    124c <setup_connection+0xcc>
jmp    1278 <setup_connection+0xf8>
mov    -0x1c(%rbp),%edi
mov    -0x58(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x58(%rbp),%rax
mov    (%rax),%edx
call   1070 <connect@plt>
cmp    $0x0,%rax
jne    1270 <setup_connection+0xf0>
jmp    128e <setup_connection+0x10e>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
mov    -0x58(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x58(%rbp)
cmp    $0x0,%rax
jne    121c <setup_connection+0x9c>
cmpq   $0x0,-0x58(%rbp)
jne    12c4 <setup_connection+0x144>
mov    0x2d38(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
lea    0xd68(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12d3 <setup_connection+0x153>
mov    -0x60(%rbp),%rdi
call   1090 <freeaddrinfo@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<setup_connection>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rsi,%r14
mov    %rdi,%r12
lea    0x8(%rsp),%r15
xor    %ebp,%ebp
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1040 <memset@plt>
mov    0x2e37(%rip),%r13
mov    0x0(%r13),%eax
mov    %eax,0x24(%rsp)
mov    0x2e10(%rip),%rbx
mov    (%rbx),%eax
mov    %eax,0x20(%rsp)
mov    %rsp,%rcx
mov    %r12,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
call   1080 <getaddrinfo@plt>
test   %eax,%eax
je     1203 <setup_connection+0x83>
mov    0x2df6(%rip),%rcx
mov    (%rcx),%ebx
mov    %eax,%edi
call   1030 <gai_strerror@plt>
lea    0xe0e(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
jmp    1283 <setup_connection+0x103>
mov    (%rsp),%r15
mov    %r15,%rax
jmp    1221 <setup_connection+0xa1>
nopl   0x0(%rax)
mov    (%rsp),%rax
mov    0x10(%rax),%rax
mov    %rax,(%rsp)
test   %rax,%rax
je     1264 <setup_connection+0xe4>
mov    0x0(%r13),%edi
mov    (%rbx),%esi
mov    0x8(%rax),%edx
call   10a0 <socket@plt>
test   %eax,%eax
js     1210 <setup_connection+0x90>
mov    %eax,%ebp
mov    (%rsp),%rax
mov    (%rax),%edx
mov    0x4(%rax),%esi
mov    %ebp,%edi
call   1070 <connect@plt>
test   %rax,%rax
je     1253 <setup_connection+0xd3>
mov    %ebp,%edi
call   1050 <close@plt>
jmp    1210 <setup_connection+0x90>
cmpq   $0x0,(%rsp)
je     1264 <setup_connection+0xe4>
mov    %r15,%rdi
call   1090 <freeaddrinfo@plt>
jmp    1283 <setup_connection+0x103>
mov    0x2d6d(%rip),%rax
mov    (%rax),%edi
lea    0xda5(%rip),%rsi
xor    %ebp,%ebp
mov    %r12,%rdx
mov    %r14,%rcx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    %ebp,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i32, %struct.addrinfo*, i32, i32 }
%struct.hostent = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@stderr = global i32 0
@.str = [25 x i8] c"cannot resolve host: %s\0A\00"
@.str.1 = [26 x i8] c"connect error for %s, %s\0A\00"
define i32 @setup_connection(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_INET
%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 = icmp eq i32 %12, 0
br i1 %13, label %18, label %14
14: ; preds = %2
%15 = load i32, i32* @stderr
%16 = call i8* @gai_strerror(i32 %12)
%17 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* %16)
br label %51
18: ; preds = %2
%19 = load %struct.addrinfo*, %struct.addrinfo** %4
br label %20
20: ; preds = %38, %18
%21 = phi %struct.addrinfo* [ %41, %38 ], [ %19, %18 ]
%22 = load i32, i32* @AF_INET
%23 = load i32, i32* @SOCK_STREAM
%24 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %21, i64 0, i32 2
%25 = load i32, i32* %24
%26 = call i32 @socket(i32 %22, i32 %23, i32 %25)
%27 = icmp slt i32 %26, 0
br i1 %27, label %38, label %28
28: ; preds = %20
%29 = load %struct.addrinfo*, %struct.addrinfo** %4
%30 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %29, i64 0, i32 1
%31 = load i32, i32* %30
%32 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %29, i64 0, i32 0
%33 = load i32, i32* %32
%34 = call i64 @connect(i32 %26, i32 %31, i32 %33)
%35 = icmp eq i64 %34, 0
br i1 %35, label %43, label %36
36: ; preds = %28
%37 = call i32 @close(i32 %26)
br label %38
38: ; preds = %20, %36
%39 = load %struct.addrinfo*, %struct.addrinfo** %4
%40 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %39, i64 0, i32 3
%41 = load %struct.addrinfo*, %struct.addrinfo** %40
store %struct.addrinfo* %41, %struct.addrinfo** %4
%42 = icmp eq %struct.addrinfo* %41, null
br i1 %42, label %46, label %20
43: ; preds = %28
%44 = load %struct.addrinfo*, %struct.addrinfo** %4
%45 = icmp eq %struct.addrinfo* %44, null
br i1 %45, label %46, label %49
46: ; preds = %38, %43
%47 = load i32, i32* @stderr
%48 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %47, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i8* %0, i8* %1)
br label %51
49: ; preds = %43
%50 = call i32 @freeaddrinfo(%struct.addrinfo* %19)
br label %51
51: ; preds = %49, %46, %14
%52 = phi i32 [ 0, %14 ], [ 0, %46 ], [ %26, %49 ]
ret i32 %52
}