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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
int connect(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ errx(int, char *, int /*<<< orphan*/);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
int /*<<< orphan*/ 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 open_link(const char *server, const char *port) {
  struct addrinfo hints;
  memset(&hints, 0, sizeof(hints));
  hints.ai_family = AF_INET;
  hints.ai_socktype = SOCK_STREAM;
  hints.ai_flags = 0;
  int ret;
  struct addrinfo *res;
  ret = getaddrinfo(server, port, &hints, &res);
  if (ret)
    errx(1, "getaddrinfo error %s", gai_strerror(ret));
  int sock;
  struct addrinfo *rp;
  for (rp = res; rp != NULL; rp = rp->ai_next) {
    sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
    if (sock == -1)
      continue;
    ret = connect(sock, rp->ai_addr, rp->ai_addrlen);
    if (ret == 0)
      break;
    close(sock);
    sock = -1;
  }
  freeaddrinfo(res);
  return sock;
}


x86 assembly (clang, -O0):
<open_link>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1050 <memset@plt>
mov    0x2e41(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2e25(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
lea    -0x38(%rbp),%rdx
lea    -0x48(%rbp),%rcx
call   1080 <getaddrinfo@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
je     11fd <open_link+0x7d>
mov    -0x3c(%rbp),%edi
call   1040 <gai_strerror@plt>
mov    %eax,%edx
mov    $0x1,%edi
lea    0xe08(%rip),%rsi
call   1030 <errx@plt>
mov    -0x48(%rbp),%rax
mov    %rax,-0x58(%rbp)
cmpq   $0x0,-0x58(%rbp)
je     1283 <open_link+0x103>
mov    -0x58(%rbp),%rax
mov    0x10(%rax),%edi
mov    -0x58(%rbp),%rax
mov    0xc(%rax),%esi
mov    -0x58(%rbp),%rax
mov    0x8(%rax),%edx
call   10a0 <socket@plt>
mov    %eax,-0x4c(%rbp)
cmpl   $0xffffffff,-0x4c(%rbp)
jne    123c <open_link+0xbc>
jmp    1272 <open_link+0xf2>
mov    -0x4c(%rbp),%edi
mov    -0x58(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x58(%rbp),%rax
mov    (%rax),%edx
call   1070 <connect@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jne    1263 <open_link+0xe3>
jmp    1283 <open_link+0x103>
mov    -0x4c(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4c(%rbp)
mov    -0x58(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x58(%rbp)
jmp    1205 <open_link+0x85>
mov    -0x48(%rbp),%rdi
call   1090 <freeaddrinfo@plt>
mov    -0x4c(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_link>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x38,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
lea    0x10(%rsp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1050 <memset@plt>
mov    0x2e3d(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rsp)
mov    0x2e20(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x1c(%rsp)
movq   $0x0,0x30(%rsp)
lea    0x8(%rsp),%rcx
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
call   1080 <getaddrinfo@plt>
test   %eax,%eax
je     11f8 <open_link+0x78>
mov    %eax,%edi
call   1040 <gai_strerror@plt>
lea    0xe14(%rip),%rsi
mov    $0x1,%edi
mov    %eax,%edx
call   1030 <errx@plt>
mov    0x8(%rsp),%rbx
test   %rbx,%rbx
jne    1219 <open_link+0x99>
mov    $0xffffffff,%ebp
xor    %edi,%edi
jmp    1251 <open_link+0xd1>
nopl   0x0(%rax,%rax,1)
mov    0x18(%rbx),%rbx
test   %rbx,%rbx
je     1247 <open_link+0xc7>
mov    0x10(%rbx),%edi
mov    0x8(%rbx),%edx
mov    0xc(%rbx),%esi
call   10a0 <socket@plt>
cmp    $0xffffffff,%eax
je     1210 <open_link+0x90>
mov    %eax,%ebp
mov    (%rbx),%edx
mov    0x4(%rbx),%esi
mov    %eax,%edi
call   1070 <connect@plt>
test   %eax,%eax
je     124c <open_link+0xcc>
mov    %ebp,%edi
call   1060 <close@plt>
jmp    1210 <open_link+0x90>
mov    $0xffffffff,%ebp
mov    0x8(%rsp),%rdi
call   1090 <freeaddrinfo@plt>
mov    %ebp,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i32, i32, i32, %struct.addrinfo*, i64 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [21 x i8] c"getaddrinfo error %s\00"
define i32 @open_link(i8* %0, i8* %1) {
%3 = alloca %struct.addrinfo
%4 = alloca %struct.addrinfo*
%5 = bitcast %struct.addrinfo* %3 to i8*
%6 = call i32 @memset(%struct.addrinfo* %3, i32 0, i32 40)
%7 = load i32, i32* @AF_INET
%8 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 4
store i32 %7, i32* %8
%9 = load i32, i32* @SOCK_STREAM
%10 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 3
store i32 %9, i32* %10
%11 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 6
store i64 0, i64* %11
%12 = bitcast %struct.addrinfo** %4 to i8*
%13 = call i32 @getaddrinfo(i8* %0, i8* %1, %struct.addrinfo* %3, %struct.addrinfo** %4)
%14 = icmp eq i32 %13, 0
br i1 %14, label %18, label %15
15: ; preds = %2
%16 = call i32 @gai_strerror(i32 %13)
%17 = call i32 @errx(i32 1, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i32 %16)
br label %18
18: ; preds = %15, %2
%19 = load %struct.addrinfo*, %struct.addrinfo** %4
%20 = icmp eq %struct.addrinfo* %19, null
br i1 %20, label %47, label %21
21: ; preds = %18, %40
%22 = phi %struct.addrinfo* [ %42, %40 ], [ %19, %18 ]
%23 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %22, i64 0, i32 4
%24 = load i32, i32* %23
%25 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %22, i64 0, i32 3
%26 = load i32, i32* %25
%27 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %22, i64 0, i32 2
%28 = load i32, i32* %27
%29 = call i32 @socket(i32 %24, i32 %26, i32 %28)
%30 = icmp eq i32 %29, -1
br i1 %30, label %40, label %31
31: ; preds = %21
%32 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %22, i64 0, i32 1
%33 = load i32, i32* %32
%34 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %22, i64 0, i32 0
%35 = load i32, i32* %34
%36 = call i32 @connect(i32 %29, i32 %33, i32 %35)
%37 = icmp eq i32 %36, 0
br i1 %37, label %44, label %38
38: ; preds = %31
%39 = call i32 @close(i32 %29)
br label %40
40: ; preds = %21, %38
%41 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %22, i64 0, i32 5
%42 = load %struct.addrinfo*, %struct.addrinfo** %41
%43 = icmp eq %struct.addrinfo* %42, null
br i1 %43, label %44, label %21
44: ; preds = %31, %40
%45 = phi i32 [ -1, %40 ], [ %29, %31 ]
%46 = load %struct.addrinfo*, %struct.addrinfo** %4
br label %47
47: ; preds = %44, %18
%48 = phi %struct.addrinfo* [ null, %18 ], [ %46, %44 ]
%49 = phi i32 [ -1, %18 ], [ %45, %44 ]
%50 = call i32 @freeaddrinfo(%struct.addrinfo* %48)
ret i32 %49
}