#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;
  scalar_t__ ai_protocol;
  int /*<<< orphan*/ ai_socktype;
  int /*<<< orphan*/ ai_family;
  struct addrinfo *ai_next;
  scalar_t__ ai_flags;
};

/* Variables and functions */
int /*<<< orphan*/ AF_UNSPEC;
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
int connect(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
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*/, scalar_t__);
int /*<<< orphan*/ stderr;
int test_connection(char *proxy_port) {
  const char *hostname = "127.0.0.1";
  struct addrinfo hints;
  struct addrinfo *result = 0;
  struct addrinfo *rp = 0;
  int socket_fd = 0;
  int gai_return_value = 0;
  memset(&hints, 0, sizeof(struct addrinfo));
  hints.ai_family = AF_UNSPEC;
  hints.ai_socktype = SOCK_STREAM;
  hints.ai_flags = 0;
  hints.ai_protocol = 0;
  if ((gai_return_value = getaddrinfo(hostname, proxy_port, &hints, &result)) !=
      0) {
    fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(gai_return_value));
    exit(EXIT_FAILURE);
  }
  for (rp = result; rp != NULL; rp = rp->ai_next) {
    socket_fd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
    if (socket_fd == -1)
      continue;
    if (connect(socket_fd, rp->ai_addr, rp->ai_addrlen) != -1) {
      freeaddrinfo(result);
      close(socket_fd);
      return 0;
    }
    close(socket_fd);
  }
  freeaddrinfo(result);
  return 1;
}


x86 assembly (clang, -O0):
<test_connection>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
lea    0xe5d(%rip),%rax
mov    %rax,-0x18(%rbp)
movq   $0x0,-0x48(%rbp)
movq   $0x0,-0x50(%rbp)
movl   $0x0,-0x54(%rbp)
movl   $0x0,-0x58(%rbp)
lea    -0x40(%rbp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1040 <memset@plt>
mov    0x2e0c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2de8(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
lea    -0x40(%rbp),%rdx
lea    -0x48(%rbp),%rcx
call   1090 <getaddrinfo@plt>
mov    %eax,-0x58(%rbp)
cmp    $0x0,%eax
je     1252 <test_connection+0xc2>
mov    0x2db3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x5c(%rbp)
mov    -0x58(%rbp),%edi
call   1030 <gai_strerror@plt>
mov    -0x5c(%rbp),%edi
mov    %rax,%rdx
lea    0xdcb(%rip),%rsi
call   1060 <fprintf@plt>
mov    0x2d7d(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    -0x48(%rbp),%rax
mov    %rax,-0x50(%rbp)
cmpq   $0x0,-0x50(%rbp)
je     12e6 <test_connection+0x156>
mov    -0x50(%rbp),%rax
mov    0x14(%rax),%edi
mov    -0x50(%rbp),%rax
mov    0x10(%rax),%esi
mov    -0x50(%rbp),%rax
mov    0x8(%rax),%rdx
call   10b0 <socket@plt>
mov    %eax,-0x54(%rbp)
cmpl   $0xffffffff,-0x54(%rbp)
jne    1292 <test_connection+0x102>
jmp    12d5 <test_connection+0x145>
mov    -0x54(%rbp),%edi
mov    -0x50(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x50(%rbp),%rax
mov    (%rax),%edx
call   1080 <connect@plt>
cmp    $0xffffffff,%eax
je     12cd <test_connection+0x13d>
mov    -0x48(%rbp),%rdi
call   10a0 <freeaddrinfo@plt>
mov    -0x54(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
jmp    12f6 <test_connection+0x166>
mov    -0x54(%rbp),%edi
call   1050 <close@plt>
mov    -0x50(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x50(%rbp)
jmp    125a <test_connection+0xca>
mov    -0x48(%rbp),%rdi
call   10a0 <freeaddrinfo@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_connection>:
push   %rbp
push   %r14
push   %rbx
sub    $0x30,%rsp
mov    %rdi,%rbx
movq   $0x0,(%rsp)
lea    0x8(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1040 <memset@plt>
mov    0x2e2a(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x1c(%rsp)
mov    0x2e05(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
movq   $0x0,0x28(%rsp)
movq   $0x0,0x10(%rsp)
lea    0xe16(%rip),%rdi
mov    %rsp,%rcx
mov    %rbx,%rsi
mov    %r14,%rdx
call   1090 <getaddrinfo@plt>
test   %eax,%eax
jne    1274 <test_connection+0xe4>
mov    (%rsp),%rbx
test   %rbx,%rbx
jne    1219 <test_connection+0x89>
xor    %edi,%edi
jmp    124d <test_connection+0xbd>
nopl   0x0(%rax)
mov    0x18(%rbx),%rbx
test   %rbx,%rbx
je     1249 <test_connection+0xb9>
mov    0x14(%rbx),%edi
mov    0x10(%rbx),%esi
mov    0x8(%rbx),%rdx
call   10b0 <socket@plt>
cmp    $0xffffffff,%eax
je     1210 <test_connection+0x80>
mov    %eax,%ebp
mov    (%rbx),%edx
mov    0x4(%rbx),%esi
mov    %eax,%edi
call   1080 <connect@plt>
cmp    $0xffffffff,%eax
jne    1259 <test_connection+0xc9>
mov    %ebp,%edi
call   1050 <close@plt>
jmp    1210 <test_connection+0x80>
mov    (%rsp),%rdi
call   10a0 <freeaddrinfo@plt>
mov    $0x1,%eax
jmp    126b <test_connection+0xdb>
mov    (%rsp),%rdi
call   10a0 <freeaddrinfo@plt>
mov    %ebp,%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2d5d(%rip),%rcx
mov    (%rcx),%ebx
mov    %eax,%edi
call   1030 <gai_strerror@plt>
lea    0xd7f(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1060 <fprintf@plt>
mov    0x2d2c(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>

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

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