#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);
int connect(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
char *gai_strerror(int);
int getaddrinfo(char *, char const *, struct addrinfo *, struct addrinfo **);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int connect_to(const char *host, const char *port, int *sockfd) {
  struct addrinfo hints, *servinfo, *p;
  int rv;
  memset(&hints, 0, sizeof hints);
  hints.ai_family = AF_UNSPEC;
  hints.ai_socktype = SOCK_STREAM;
  if ((rv = getaddrinfo("127.0.0.1", port, &hints, &servinfo)) != 0) {
    fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
    return -1;
  }
  for (p = servinfo; p != NULL; p = p->ai_next) {
    if ((*sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) ==
        -1) {
      perror("client: socket");
      continue;
    }
    if (connect(*sockfd, p->ai_addr, p->ai_addrlen) == -1) {
      close(*sockfd);
      perror("client: connect");
      continue;
    }
    break;
  }
  if (p == NULL) {
    fprintf(stderr, "client: failed to connect\n");
    return -1;
  }
  freeaddrinfo(servinfo);
  return 0;
}


x86 assembly (clang, -O0):
<connect_to>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
lea    -0x40(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1040 <memset@plt>
mov    0x2e2d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    0x2e09(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x18(%rbp),%rsi
lea    0xe29(%rip),%rdi
lea    -0x40(%rbp),%rdx
lea    -0x48(%rbp),%rcx
call   1090 <getaddrinfo@plt>
mov    %eax,-0x54(%rbp)
cmp    $0x0,%eax
je     1224 <connect_to+0x94>
mov    0x2de1(%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    0xdf9(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1302 <connect_to+0x172>
mov    -0x48(%rbp),%rax
mov    %rax,-0x50(%rbp)
cmpq   $0x0,-0x50(%rbp)
je     12c4 <connect_to+0x134>
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    -0x20(%rbp),%rcx
mov    %eax,(%rcx)
cmp    $0xffffffff,%eax
jne    1271 <connect_to+0xe1>
lea    0xdb4(%rip),%rdi
call   1070 <perror@plt>
jmp    12b3 <connect_to+0x123>
mov    -0x20(%rbp),%rax
mov    (%rax),%edi
mov    -0x50(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x50(%rbp),%rax
mov    (%rax),%edx
call   1080 <connect@plt>
cmp    $0xffffffff,%eax
jne    12ae <connect_to+0x11e>
mov    -0x20(%rbp),%rax
mov    (%rax),%edi
call   1050 <close@plt>
lea    0xd86(%rip),%rdi
call   1070 <perror@plt>
jmp    12b3 <connect_to+0x123>
jmp    12c4 <connect_to+0x134>
mov    -0x50(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x50(%rbp)
jmp    122c <connect_to+0x9c>
cmpq   $0x0,-0x50(%rbp)
jne    12f2 <connect_to+0x162>
mov    0x2d02(%rip),%rax
mov    (%rax),%edi
lea    0xd5b(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1302 <connect_to+0x172>
mov    -0x48(%rbp),%rdi
call   10a0 <freeaddrinfo@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<connect_to>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rdx,%r14
mov    %rsi,%rbx
lea    0x8(%rsp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1040 <memset@plt>
mov    0x2e2c(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
mov    0x2e07(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
lea    0xe2a(%rip),%rdi
mov    %rsp,%rcx
mov    %rbx,%rsi
mov    %r15,%rdx
call   1090 <getaddrinfo@plt>
test   %eax,%eax
je     1215 <connect_to+0x85>
mov    0x2de9(%rip),%rcx
mov    (%rcx),%ebx
mov    %eax,%edi
call   1030 <gai_strerror@plt>
lea    0xe0b(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0xffffffff,%eax
jmp    1298 <connect_to+0x108>
mov    (%rsp),%rbx
test   %rbx,%rbx
je     126f <connect_to+0xdf>
lea    0xdf6(%rip),%r15
lea    0xdfe(%rip),%r12
mov    0x10(%rbx),%edi
mov    0x8(%rbx),%edx
mov    0xc(%rbx),%esi
call   10b0 <socket@plt>
mov    %eax,(%r14)
mov    %r15,%rdi
cmp    $0xffffffff,%eax
je     1261 <connect_to+0xd1>
mov    (%rbx),%edx
mov    0x4(%rbx),%esi
mov    %eax,%edi
call   1080 <connect@plt>
cmp    $0xffffffff,%eax
jne    128d <connect_to+0xfd>
mov    (%r14),%edi
call   1050 <close@plt>
mov    %r12,%rdi
call   1070 <perror@plt>
mov    0x18(%rbx),%rbx
test   %rbx,%rbx
jne    122c <connect_to+0x9c>
mov    0x2d62(%rip),%rax
mov    (%rax),%edi
lea    0xdbb(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0xffffffff,%eax
jmp    1298 <connect_to+0x108>
mov    (%rsp),%rdi
call   10a0 <freeaddrinfo@plt>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i32, i32, i32, %struct.addrinfo* }
@AF_UNSPEC = global i32 0
@SOCK_STREAM = global i32 0
@.str = [10 x i8] c"127.0.0.1\00"
@stderr = global i32 0
@.str.1 = [17 x i8] c"getaddrinfo: %s\0A\00"
@.str.2 = [15 x i8] c"client: socket\00"
@.str.3 = [16 x i8] c"client: connect\00"
@.str.4 = [27 x i8] c"client: failed to connect\0A\00"
define i32 @connect_to(i8* nocapture readnone %0, i8* %1, i32* nocapture %2) {
%4 = alloca %struct.addrinfo
%5 = alloca %struct.addrinfo*
%6 = bitcast %struct.addrinfo* %4 to i8*
%7 = bitcast %struct.addrinfo** %5 to i8*
%8 = call i32 @memset(%struct.addrinfo* %4, i32 0, i32 32)
%9 = load i32, i32* @AF_UNSPEC
%10 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 4
store i32 %9, i32* %10
%11 = load i32, i32* @SOCK_STREAM
%12 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 3
store i32 %11, i32* %12
%13 = call i32 @getaddrinfo(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* %1, %struct.addrinfo* %4, %struct.addrinfo** %5)
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %3
%16 = load %struct.addrinfo*, %struct.addrinfo** %5
%17 = icmp eq %struct.addrinfo* %16, null
br i1 %17, label %48, label %22
18: ; preds = %3
%19 = load i32, i32* @stderr
%20 = call i8* @gai_strerror(i32 %13)
%21 = call i32 (i32, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %20)
br label %54
22: ; preds = %15, %42
%23 = phi %struct.addrinfo* [ %46, %42 ], [ %16, %15 ]
%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)
store i32 %30, i32* %2
%31 = icmp eq i32 %30, -1
br i1 %31, label %42, 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 i32 @connect(i32 %30, i32 %34, i32 %36)
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %51
39: ; preds = %32
%40 = load i32, i32* %2
%41 = call i32 @close(i32 %40)
br label %42
42: ; preds = %22, %39
%43 = phi i8* [ getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), %39 ], [ getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), %22 ]
%44 = call i32 @perror(i8* %43)
%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 = %42, %15
%49 = load i32, i32* @stderr
%50 = call i32 (i32, i8*, ...) @fprintf(i32 %49, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.4, i64 0, i64 0))
br label %54
51: ; preds = %32
%52 = load %struct.addrinfo*, %struct.addrinfo** %5
%53 = call i32 @freeaddrinfo(%struct.addrinfo* %52)
br label %54
54: ; preds = %51, %48, %18
%55 = phi i32 [ -1, %18 ], [ -1, %48 ], [ 0, %51 ]
ret i32 %55
}