#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;
};

/* Variables and functions */
int /*<<< orphan*/ AF_UNSPEC;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
char *gai_strerror(int);
int getaddrinfo(char *, char *, struct addrinfo *, struct addrinfo **);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int open_connection(char *host, char *service) {
  int sock;
  struct addrinfo hints, *res, *ai;
  int err;
  memset(&hints, 0, sizeof(struct addrinfo));
  hints.ai_family = AF_UNSPEC;
  hints.ai_socktype = SOCK_STREAM;
  if ((err = getaddrinfo(host, service, &hints, &res)) != 0) {
    fprintf(stderr, "getaddrinfo(3): %s\n", gai_strerror(err));
    exit(1);
  }
  for (ai = res; ai; ai = ai->ai_next) {
    sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
    if (sock < 0) {
      continue;
    }
    if (connect(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
      close(sock);
      continue;
    }
    freeaddrinfo(res);
    return sock;
  }
  fprintf(stderr, "socket(2)/connect(2) failed");
  freeaddrinfo(res);
  exit(1);
}


x86 assembly (clang, -O0):
<open_connection>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1040 <memset@plt>
mov    0x2e31(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2e0d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
lea    -0x38(%rbp),%rdx
lea    -0x40(%rbp),%rcx
call   1090 <getaddrinfo@plt>
mov    %eax,-0x4c(%rbp)
cmp    $0x0,%eax
je     121b <open_connection+0x8b>
mov    0x2de8(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x50(%rbp)
mov    -0x4c(%rbp),%edi
call   1030 <gai_strerror@plt>
mov    -0x50(%rbp),%edi
mov    %rax,%rdx
lea    0xdf6(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x40(%rbp),%rax
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
je     12a9 <open_connection+0x119>
mov    -0x48(%rbp),%rax
mov    0x10(%rax),%edi
mov    -0x48(%rbp),%rax
mov    0xc(%rax),%esi
mov    -0x48(%rbp),%rax
mov    0x8(%rax),%edx
call   10b0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    125a <open_connection+0xca>
jmp    1298 <open_connection+0x108>
mov    -0x14(%rbp),%edi
mov    -0x48(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x48(%rbp),%rax
mov    (%rax),%edx
call   1080 <connect@plt>
cmp    $0x0,%rax
jge    1286 <open_connection+0xf6>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
jmp    1298 <open_connection+0x108>
mov    -0x40(%rbp),%rdi
call   10a0 <freeaddrinfo@plt>
mov    -0x14(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret
mov    -0x48(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x48(%rbp)
jmp    1223 <open_connection+0x93>
mov    0x2d28(%rip),%rax
mov    (%rax),%edi
lea    0xd5b(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x40(%rbp),%rdi
call   10a0 <freeaddrinfo@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

x86 assembly (clang, -O2):
<open_connection>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
lea    0x8(%rsp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1040 <memset@plt>
mov    0x2e2d(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
mov    0x2e08(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
mov    %rsp,%rcx
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
call   1090 <getaddrinfo@plt>
test   %eax,%eax
jne    1267 <open_connection+0xd7>
mov    (%rsp),%rbx
jmp    11f4 <open_connection+0x64>
nopl   (%rax)
mov    0x18(%rbx),%rbx
test   %rbx,%rbx
je     123d <open_connection+0xad>
mov    0x10(%rbx),%edi
mov    0x8(%rbx),%edx
mov    0xc(%rbx),%esi
call   10b0 <socket@plt>
test   %eax,%eax
js     11f0 <open_connection+0x60>
mov    %eax,%ebp
mov    (%rbx),%edx
mov    0x4(%rbx),%esi
mov    %eax,%edi
call   1080 <connect@plt>
test   %rax,%rax
jns    1227 <open_connection+0x97>
mov    %ebp,%edi
call   1050 <close@plt>
jmp    11f0 <open_connection+0x60>
mov    (%rsp),%rdi
call   10a0 <freeaddrinfo@plt>
mov    %ebp,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d94(%rip),%rax
mov    (%rax),%edi
lea    0xdc7(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    (%rsp),%rdi
call   10a0 <freeaddrinfo@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    0x2d6a(%rip),%rcx
mov    (%rcx),%ebx
mov    %eax,%edi
call   1030 <gai_strerror@plt>
lea    0xd82(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

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

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