#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 *, char *);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
char *gai_strerror(int);
int getaddrinfo(char const *, 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(const 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;
  }
  freeaddrinfo(res);
  return -1;
}


x86 assembly (clang, -O0):
<open_connection>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
lea    -0x40(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1040 <memset@plt>
mov    0x2e31(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    0x2e0d(%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   1090 <getaddrinfo@plt>
mov    %eax,-0x54(%rbp)
cmp    $0x0,%eax
je     1219 <open_connection+0x89>
mov    0x2de8(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x58(%rbp)
mov    -0x54(%rbp),%edi
call   1030 <gai_strerror@plt>
mov    -0x58(%rbp),%edi
mov    %rax,%rdx
lea    0xdf6(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x48(%rbp),%rax
mov    %rax,-0x50(%rbp)
cmpq   $0x0,-0x50(%rbp)
je     12a9 <open_connection+0x119>
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   10b0 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1258 <open_connection+0xc8>
jmp    1298 <open_connection+0x108>
mov    -0x1c(%rbp),%edi
mov    -0x50(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x50(%rbp),%rax
mov    (%rax),%edx
call   1080 <connect@plt>
cmp    $0x0,%rax
jge    1284 <open_connection+0xf4>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
jmp    1298 <open_connection+0x108>
mov    -0x48(%rbp),%rdi
call   10a0 <freeaddrinfo@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12b9 <open_connection+0x129>
mov    -0x50(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x50(%rbp)
jmp    1221 <open_connection+0x91>
mov    -0x48(%rbp),%rdi
call   10a0 <freeaddrinfo@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_connection>:
push   %rbp
push   %r14
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    0x2e2f(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rsp)
mov    0x2e0a(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x1c(%rsp)
lea    0x8(%rsp),%rcx
mov    %rbp,%rdi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1090 <getaddrinfo@plt>
test   %eax,%eax
jne    1257 <open_connection+0xc7>
mov    0x8(%rsp),%rbx
mov    $0xffffffff,%r14d
test   %rbx,%rbx
jne    1209 <open_connection+0x79>
xor    %edi,%edi
jmp    1246 <open_connection+0xb6>
nopw   0x0(%rax,%rax,1)
mov    0x18(%rbx),%rbx
test   %rbx,%rbx
je     1237 <open_connection+0xa7>
mov    0x10(%rbx),%edi
mov    0x8(%rbx),%edx
mov    0xc(%rbx),%esi
call   10b0 <socket@plt>
test   %eax,%eax
js     1200 <open_connection+0x70>
mov    %eax,%ebp
mov    (%rbx),%edx
mov    0x4(%rbx),%esi
mov    %eax,%edi
call   1080 <connect@plt>
test   %rax,%rax
jns    123e <open_connection+0xae>
mov    %ebp,%edi
call   1050 <close@plt>
jmp    1200 <open_connection+0x70>
mov    0x8(%rsp),%rdi
jmp    1246 <open_connection+0xb6>
mov    0x8(%rsp),%rdi
mov    %ebp,%r14d
call   10a0 <freeaddrinfo@plt>
mov    %r14d,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2d7a(%rip),%rcx
mov    (%rcx),%ebx
mov    %eax,%edi
call   1030 <gai_strerror@plt>
lea    0xd92(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
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"
define i32 @open_connection(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*
%10 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
%11 = call i32 @memset(%struct.addrinfo* %7, i32 0, i32 32)
%12 = load i32, i32* @AF_UNSPEC
%13 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %7, i32 0, i32 4
store i32 %12, i32* %13
%14 = load i32, i32* @SOCK_STREAM
%15 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %7, 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* %7, %struct.addrinfo** %8)
store i32 %18, i32* %10
%19 = icmp ne i32 %18, 0
br i1 %19, label %20, label %26
20: ; preds = %2
%21 = load i32, i32* @stderr
%22 = load i32, i32* %10
%23 = call i8* @gai_strerror(i32 %22)
%24 = call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %23)
%25 = call i32 @exit(i32 1)
unreachable
26: ; preds = %2
%27 = load %struct.addrinfo*, %struct.addrinfo** %8
store %struct.addrinfo* %27, %struct.addrinfo** %9
br label %28
28: ; preds = %62, %26
%29 = load %struct.addrinfo*, %struct.addrinfo** %9
%30 = icmp ne %struct.addrinfo* %29, null
br i1 %30, label %31, label %66
31: ; preds = %28
%32 = load %struct.addrinfo*, %struct.addrinfo** %9
%33 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %32, i32 0, i32 4
%34 = load i32, i32* %33
%35 = load %struct.addrinfo*, %struct.addrinfo** %9
%36 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %35, i32 0, i32 3
%37 = load i32, i32* %36
%38 = load %struct.addrinfo*, %struct.addrinfo** %9
%39 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %38, i32 0, i32 2
%40 = load i32, i32* %39
%41 = call i32 @socket(i32 %34, i32 %37, i32 %40)
store i32 %41, i32* %6
%42 = load i32, i32* %6
%43 = icmp slt i32 %42, 0
br i1 %43, label %44, label %45
44: ; preds = %31
br label %62
45: ; preds = %31
%46 = load i32, i32* %6
%47 = load %struct.addrinfo*, %struct.addrinfo** %9
%48 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %47, i32 0, i32 1
%49 = load i32, i32* %48
%50 = load %struct.addrinfo*, %struct.addrinfo** %9
%51 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %50, i32 0, i32 0
%52 = load i32, i32* %51
%53 = call i64 @connect(i32 %46, i32 %49, i32 %52)
%54 = icmp slt i64 %53, 0
br i1 %54, label %55, label %58
55: ; preds = %45
%56 = load i32, i32* %6
%57 = call i32 @close(i32 %56)
br label %62
58: ; preds = %45
%59 = load %struct.addrinfo*, %struct.addrinfo** %8
%60 = call i32 @freeaddrinfo(%struct.addrinfo* %59)
%61 = load i32, i32* %6
store i32 %61, i32* %3
br label %69
62: ; preds = %55, %44
%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 %28
66: ; preds = %28
%67 = load %struct.addrinfo*, %struct.addrinfo** %8
%68 = call i32 @freeaddrinfo(%struct.addrinfo* %67)
store i32 -1, i32* %3
br label %69
69: ; preds = %66, %58
%70 = load i32, i32* %3
ret i32 %70
}

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"
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 %49, label %22
17: ; preds = %2
%18 = load i32, i32* @stderr
%19 = call i8* @gai_strerror(i32 %12)
%20 = call i32 @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, %43
%23 = phi %struct.addrinfo* [ %45, %43 ], [ %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 %43, 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 %43
41: ; preds = %32
%42 = load %struct.addrinfo*, %struct.addrinfo** %4
br label %49
43: ; preds = %22, %39
%44 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %23, i64 0, i32 5
%45 = load %struct.addrinfo*, %struct.addrinfo** %44
%46 = icmp eq %struct.addrinfo* %45, null
br i1 %46, label %47, label %22
47: ; preds = %43
%48 = load %struct.addrinfo*, %struct.addrinfo** %4
br label %49
49: ; preds = %14, %47, %41
%50 = phi %struct.addrinfo* [ %42, %41 ], [ %48, %47 ], [ null, %14 ]
%51 = phi i32 [ %30, %41 ], [ -1, %47 ], [ -1, %14 ]
%52 = call i32 @freeaddrinfo(%struct.addrinfo* %50)
ret i32 %51
}