#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 INET6_ADDRSTRLEN;
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 *, 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_server(char *argv[]) {
  int sockfd;
  int numbytes;
  struct addrinfo hints, *servinfo, *p;
  int rv;
  char s[INET6_ADDRSTRLEN];
  memset(&hints, 0, sizeof hints);
  hints.ai_family = AF_UNSPEC;
  hints.ai_socktype = SOCK_STREAM;
  if ((rv = getaddrinfo(argv[2], argv[3], &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("proxy: socket");
      continue;
    }
    if (connect(sockfd, p->ai_addr, p->ai_addrlen) == -1) {
      close(sockfd);
      perror("proxy: connect");
      continue;
    }
    break;
  }
  if (p == NULL) {
    fprintf(stderr, "proxy: failed to connect\n");
    return -1;
  }
  freeaddrinfo(servinfo);
  return sockfd;
}


x86 assembly (clang, -O0):
<connect_to_server>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e45(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x58(%rbp)
mov    %rax,-0x60(%rbp)
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1040 <memset@plt>
mov    0x2e19(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2df5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rsi
lea    -0x38(%rbp),%rdx
lea    -0x40(%rbp),%rcx
call   1090 <getaddrinfo@plt>
mov    %eax,-0x4c(%rbp)
cmp    $0x0,%eax
je     123c <connect_to_server+0xac>
mov    0x2dc8(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x68(%rbp)
mov    -0x4c(%rbp),%edi
call   1030 <gai_strerror@plt>
mov    -0x68(%rbp),%edi
mov    %rax,%rdx
lea    0xdde(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x64(%rbp)
jmp    131e <connect_to_server+0x18e>
mov    -0x40(%rbp),%rax
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
je     12d3 <connect_to_server+0x143>
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)
cmp    $0xffffffff,%eax
jne    1286 <connect_to_server+0xf6>
lea    0xd95(%rip),%rdi
call   1070 <perror@plt>
jmp    12c2 <connect_to_server+0x132>
mov    -0x14(%rbp),%edi
mov    -0x48(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x48(%rbp),%rax
mov    (%rax),%edx
call   1080 <connect@plt>
cmp    $0xffffffff,%eax
jne    12bd <connect_to_server+0x12d>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
lea    0xd6c(%rip),%rdi
call   1070 <perror@plt>
jmp    12c2 <connect_to_server+0x132>
jmp    12d3 <connect_to_server+0x143>
mov    -0x48(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x48(%rbp)
jmp    1244 <connect_to_server+0xb4>
cmpq   $0x0,-0x48(%rbp)
jne    1308 <connect_to_server+0x178>
mov    0x2ceb(%rip),%rax
mov    (%rax),%edi
lea    0xd40(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x64(%rbp)
jmp    131e <connect_to_server+0x18e>
mov    -0x40(%rbp),%rdi
call   10a0 <freeaddrinfo@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x64(%rbp)
mov    -0x58(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<connect_to_server>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x30,%rsp
mov    %rdi,%rbx
mov    %rsp,%r15
lea    0x10(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1040 <memset@plt>
mov    0x2e2b(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rsp)
mov    0x2e06(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x1c(%rsp)
mov    0x10(%rbx),%rdi
mov    0x18(%rbx),%rsi
lea    0x8(%rsp),%rcx
mov    %r14,%rdx
call   1090 <getaddrinfo@plt>
test   %eax,%eax
je     1216 <connect_to_server+0x86>
mov    0x2de8(%rip),%rcx
mov    (%rcx),%ebx
mov    %eax,%edi
call   1030 <gai_strerror@plt>
lea    0xe00(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0xffffffff,%ebp
jmp    1297 <connect_to_server+0x107>
mov    0x8(%rsp),%rbx
test   %rbx,%rbx
je     126f <connect_to_server+0xdf>
lea    0xdea(%rip),%r14
lea    0xdf1(%rip),%r12
mov    0x10(%rbx),%edi
mov    0x8(%rbx),%edx
mov    0xc(%rbx),%esi
call   10b0 <socket@plt>
mov    %r14,%rdi
cmp    $0xffffffff,%eax
je     1261 <connect_to_server+0xd1>
mov    %eax,%ebp
mov    (%rbx),%edx
mov    0x4(%rbx),%esi
mov    %eax,%edi
call   1080 <connect@plt>
cmp    $0xffffffff,%eax
jne    128d <connect_to_server+0xfd>
mov    %ebp,%edi
call   1050 <close@plt>
mov    %r12,%rdi
call   1070 <perror@plt>
mov    0x18(%rbx),%rbx
test   %rbx,%rbx
jne    122e <connect_to_server+0x9e>
mov    0x2d62(%rip),%rax
mov    (%rax),%edi
lea    0xdaf(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0xffffffff,%ebp
jmp    1297 <connect_to_server+0x107>
mov    0x8(%rsp),%rdi
call   10a0 <freeaddrinfo@plt>
mov    %r15,%rsp
mov    %ebp,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

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