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

/* Variables and functions */
int NI_MAXHOST;
int NI_MAXSERV;
int SOCK_STREAM;
int /*<<< orphan*/ close(int);
int connect(int, int /*<<< orphan*/, int /*<<< orphan*/);
int errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ lookup_addrinfo(struct addrinfo const *, char *, int, char *,
                                   int);
double now();
int socket(int, int, int);
int /*<<< orphan*/ stderr;
char *strerror(int);
const struct addrinfo *select_address(const struct addrinfo *ai) {
  for (; ai; ai = ai->ai_next) {
    int fd = socket(ai->ai_family, SOCK_STREAM, ai->ai_protocol);
    if (fd == -1) {
      fprintf(stderr, "test_connection: socket(%d, %d, %d): %s\n",
              ai->ai_family, ai->ai_protocol, SOCK_STREAM, strerror(errno));
      continue;
    }
    char host[NI_MAXHOST];
    char port[NI_MAXSERV];
    lookup_addrinfo(ai, host, sizeof host, port, sizeof port);
    fprintf(stderr, "Testing connection to %s:%s...\n", host, port);
    double t1 = now();
    int connect_status = connect(fd, ai->ai_addr, ai->ai_addrlen);
    int saved_errno = errno;
    close(fd);
    if (connect_status == 0) {
      double t2 = now();
      fprintf(stderr, "Connection OK in %.3fms.\n", 1e3 * (t2 - t1));
      return ai;
    }
    fprintf(stderr, "Failed to connect: %s\n", strerror(saved_errno));
  }
  return NULL;
}


x86 assembly (clang, -O0):
<select_address>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     137d <select_address+0x20d>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
mov    0x2e39(%rip),%rax
mov    (%rax),%esi
mov    -0x10(%rbp),%rax
mov    0x4(%rax),%edx
call   1090 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    120e <select_address+0x9e>
mov    0x2e1f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x5c(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x58(%rbp)
mov    -0x10(%rbp),%rax
mov    0x4(%rax),%eax
mov    %eax,-0x54(%rbp)
mov    0x2df8(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x50(%rbp)
mov    0x2de4(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x5c(%rbp),%edi
mov    -0x58(%rbp),%edx
mov    -0x54(%rbp),%ecx
mov    -0x50(%rbp),%r8d
mov    %rax,%r9
lea    0xe06(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
jmp    136c <select_address+0x1fc>
mov    0x2dab(%rip),%rax
mov    (%rax),%eax
mov    %eax,%edx
mov    %rsp,%rax
mov    %rax,-0x20(%rbp)
mov    %rdx,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
mov    %rsp,%rsi
sub    %rax,%rsi
mov    %rsi,-0x70(%rbp)
mov    %rsi,%rsp
mov    %rdx,-0x28(%rbp)
mov    0x2da5(%rip),%rax
mov    (%rax),%eax
mov    %rax,%rdi
add    $0xf,%rdi
and    $0xfffffffffffffff0,%rdi
mov    %rsp,%rcx
sub    %rdi,%rcx
mov    %rcx,-0x68(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
mov    %eax,%r8d
call   1050 <lookup_addrinfo@plt>
mov    -0x70(%rbp),%rdx
mov    -0x68(%rbp),%rcx
mov    0x2d5c(%rip),%rax
mov    (%rax),%edi
lea    0xdac(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    $0x0,%al
call   1060 <now@plt>
movsd  %xmm0,-0x38(%rbp)
mov    -0x14(%rbp),%edi
mov    -0x10(%rbp),%rax
mov    0xc(%rax),%esi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%edx
call   1070 <connect@plt>
mov    %eax,-0x3c(%rbp)
mov    0x2d10(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
mov    -0x14(%rbp),%edi
call   1030 <close@plt>
cmpl   $0x0,-0x3c(%rbp)
jne    131c <select_address+0x1ac>
mov    $0x0,%al
call   1060 <now@plt>
movsd  %xmm0,-0x48(%rbp)
mov    0x2cf6(%rip),%rax
mov    (%rax),%edi
movsd  -0x48(%rbp),%xmm1
subsd  -0x38(%rbp),%xmm1
movsd  0xd0a(%rip),%xmm0
mulsd  %xmm1,%xmm0
lea    0xd50(%rip),%rsi
mov    $0x1,%al
call   1040 <fprintf@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
movl   $0x1,-0x4c(%rbp)
jmp    134b <select_address+0x1db>
mov    0x2cb5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x74(%rbp)
mov    -0x40(%rbp),%edi
call   1080 <strerror@plt>
mov    -0x74(%rbp),%edi
mov    %rax,%rdx
lea    0xd2e(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0x0,-0x4c(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    -0x4c(%rbp),%eax
test   %eax,%eax
je     1367 <select_address+0x1f7>
jmp    1362 <select_address+0x1f2>
jmp    1385 <select_address+0x215>
jmp    136c <select_address+0x1fc>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x10(%rbp)
jmp    117f <select_address+0xf>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<select_address>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
test   %rdi,%rdi
je     12da <select_address+0x16a>
mov    %rdi,%r12
mov    0x2e3c(%rip),%r14
mov    0x2e3d(%rip),%r15
jmp    11f8 <select_address+0x88>
nopl   (%rax)
mov    (%r15),%ebx
mov    (%r12),%r14d
mov    0x4(%r12),%r13d
mov    0x2e1d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    0x2e09(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    %ebx,%edi
lea    0xe39(%rip),%rsi
mov    %r14d,%edx
mov    0x2df7(%rip),%r14
mov    %r13d,%ecx
mov    -0x30(%rbp),%r8d
mov    %rax,%r9
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    0x10(%r12),%r12
test   %r12,%r12
je     12da <select_address+0x16a>
mov    (%r14),%esi
mov    (%r12),%edi
mov    0x4(%r12),%edx
call   1090 <socket@plt>
cmp    $0xffffffff,%eax
je     11a0 <select_address+0x30>
mov    %eax,%r13d
mov    %rsp,-0x30(%rbp)
mov    0x2da4(%rip),%rax
mov    (%rax),%edx
mov    %rsp,%r14
mov    %rdx,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r14
mov    %r14,%rsp
mov    0x2daf(%rip),%rax
mov    (%rax),%r8d
mov    %rsp,%rbx
mov    %r8,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    %r12,%rdi
mov    %r14,%rsi
mov    %rbx,%rcx
call   1050 <lookup_addrinfo@plt>
mov    (%r15),%edi
lea    0xdc9(%rip),%rsi
mov    %r14,%rdx
mov    %rbx,%rcx
xor    %eax,%eax
call   1040 <fprintf@plt>
xor    %eax,%eax
call   1060 <now@plt>
movsd  %xmm0,-0x38(%rbp)
mov    0x8(%r12),%edx
mov    0xc(%r12),%esi
mov    %r13d,%edi
call   1070 <connect@plt>
mov    %eax,%r14d
mov    0x2d2b(%rip),%rax
mov    (%rax),%ebx
mov    %r13d,%edi
call   1030 <close@plt>
test   %r14d,%r14d
je     12df <select_address+0x16f>
mov    (%r15),%r14d
mov    %ebx,%edi
call   1080 <strerror@plt>
mov    %r14d,%edi
lea    0xdab(%rip),%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    -0x30(%rbp),%rsp
mov    0x2cfb(%rip),%r14
jmp    11ea <select_address+0x7a>
xor    %r12d,%r12d
jmp    1308 <select_address+0x198>
xor    %eax,%eax
call   1060 <now@plt>
mov    (%r15),%edi
subsd  -0x38(%rbp),%xmm0
mulsd  0xd0a(%rip),%xmm0
lea    0xd54(%rip),%rsi
mov    $0x1,%al
call   1040 <fprintf@plt>
mov    -0x30(%rbp),%rsp
mov    %r12,%rax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.addrinfo = type { i32, i32, i32, i32, %struct.addrinfo* }
@SOCK_STREAM = global i32 0
@stderr = global i32 0
@.str = [41 x i8] c"test_connection: socket(%d, %d, %d): %s\0A\00"
@errno = global i32 0
@NI_MAXHOST = global i32 0
@NI_MAXSERV = global i32 0
@.str.1 = [32 x i8] c"Testing connection to %s:%s...\0A\00"
@.str.2 = [26 x i8] c"Connection OK in %.3fms.\0A\00"
@.str.3 = [23 x i8] c"Failed to connect: %s\0A\00"
define %struct.addrinfo* @select_address(%struct.addrinfo* %0) {
%2 = alloca %struct.addrinfo*
%3 = alloca %struct.addrinfo*
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
%8 = alloca double
%9 = alloca i32
%10 = alloca i32
%11 = alloca double
%12 = alloca i32
store %struct.addrinfo* %0, %struct.addrinfo** %3
br label %13
13: ; preds = %85, %1
%14 = load %struct.addrinfo*, %struct.addrinfo** %3
%15 = icmp ne %struct.addrinfo* %14, null
br i1 %15, label %16, label %89
16: ; preds = %13
%17 = load %struct.addrinfo*, %struct.addrinfo** %3
%18 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %17, i32 0, i32 0
%19 = load i32, i32* %18
%20 = load i32, i32* @SOCK_STREAM
%21 = load %struct.addrinfo*, %struct.addrinfo** %3
%22 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %21, i32 0, i32 1
%23 = load i32, i32* %22
%24 = call i32 @socket(i32 %19, i32 %20, i32 %23)
store i32 %24, i32* %4
%25 = load i32, i32* %4
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %39
27: ; preds = %16
%28 = load i32, i32* @stderr
%29 = load %struct.addrinfo*, %struct.addrinfo** %3
%30 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %29, i32 0, i32 0
%31 = load i32, i32* %30
%32 = load %struct.addrinfo*, %struct.addrinfo** %3
%33 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %32, i32 0, i32 1
%34 = load i32, i32* %33
%35 = load i32, i32* @SOCK_STREAM
%36 = load i32, i32* @errno
%37 = call i8* @strerror(i32 %36)
%38 = call i32 (i32, i8*, ...) @fprintf(i32 %28, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i32 %31, i32 %34, i32 %35, i8* %37)
br label %85
39: ; preds = %16
%40 = load i32, i32* @NI_MAXHOST
%41 = zext i32 %40 to i64
%42 = call i8* @llvm.stacksave()
store i8* %42, i8** %5
%43 = alloca i8, i64 %41
store i64 %41, i64* %6
%44 = load i32, i32* @NI_MAXSERV
%45 = zext i32 %44 to i64
%46 = alloca i8, i64 %45
store i64 %45, i64* %7
%47 = load %struct.addrinfo*, %struct.addrinfo** %3
%48 = trunc i64 %41 to i32
%49 = trunc i64 %45 to i32
%50 = call i32 @lookup_addrinfo(%struct.addrinfo* %47, i8* %43, i32 %48, i8* %46, i32 %49)
%51 = load i32, i32* @stderr
%52 = call i32 (i32, i8*, ...) @fprintf(i32 %51, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i8* %43, i8* %46)
%53 = call double (...) @now()
store double %53, double* %8
%54 = load i32, i32* %4
%55 = load %struct.addrinfo*, %struct.addrinfo** %3
%56 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %55, i32 0, i32 3
%57 = load i32, i32* %56
%58 = load %struct.addrinfo*, %struct.addrinfo** %3
%59 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %58, i32 0, i32 2
%60 = load i32, i32* %59
%61 = call i32 @connect(i32 %54, i32 %57, i32 %60)
store i32 %61, i32* %9
%62 = load i32, i32* @errno
store i32 %62, i32* %10
%63 = load i32, i32* %4
%64 = call i32 @close(i32 %63)
%65 = load i32, i32* %9
%66 = icmp eq i32 %65, 0
br i1 %66, label %67, label %76
67: ; preds = %39
%68 = call double (...) @now()
store double %68, double* %11
%69 = load i32, i32* @stderr
%70 = load double, double* %11
%71 = load double, double* %8
%72 = fsub double %70, %71
%73 = fmul double 1.000000e+03, %72
%74 = call i32 (i32, i8*, ...) @fprintf(i32 %69, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), double %73)
%75 = load %struct.addrinfo*, %struct.addrinfo** %3
store %struct.addrinfo* %75, %struct.addrinfo** %2
store i32 1, i32* %12
br label %81
76: ; preds = %39
%77 = load i32, i32* @stderr
%78 = load i32, i32* %10
%79 = call i8* @strerror(i32 %78)
%80 = call i32 (i32, i8*, ...) @fprintf(i32 %77, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i8* %79)
store i32 0, i32* %12
br label %81
81: ; preds = %76, %67
%82 = load i8*, i8** %5
%83 = load i32, i32* %12
switch i32 %83, label %92 [
i32 0, label %84
i32 1, label %90
]
84: ; preds = %81
br label %85
85: ; preds = %84, %27
%86 = load %struct.addrinfo*, %struct.addrinfo** %3
%87 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %86, i32 0, i32 4
%88 = load %struct.addrinfo*, %struct.addrinfo** %87
store %struct.addrinfo* %88, %struct.addrinfo** %3
br label %13
89: ; preds = %13
store %struct.addrinfo* null, %struct.addrinfo** %2
br label %90
90: ; preds = %89, %81
%91 = load %struct.addrinfo*, %struct.addrinfo** %2
ret %struct.addrinfo* %91
92: ; preds = %81
unreachable
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i32, i32, %struct.addrinfo* }
@SOCK_STREAM = global i32 0
@stderr = global i32 0
@.str = [41 x i8] c"test_connection: socket(%d, %d, %d): %s\0A\00"
@errno = global i32 0
@NI_MAXHOST = global i32 0
@NI_MAXSERV = global i32 0
@.str.1 = [32 x i8] c"Testing connection to %s:%s...\0A\00"
@.str.2 = [26 x i8] c"Connection OK in %.3fms.\0A\00"
@.str.3 = [23 x i8] c"Failed to connect: %s\0A\00"
define %struct.addrinfo* @select_address(%struct.addrinfo* %0) {
%2 = icmp eq %struct.addrinfo* %0, null
br i1 %2, label %54, label %3
3: ; preds = %1, %50
%4 = phi %struct.addrinfo* [ %52, %50 ], [ %0, %1 ]
%5 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 0
%6 = load i32, i32* %5
%7 = load i32, i32* @SOCK_STREAM
%8 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 1
%9 = load i32, i32* %8
%10 = call i32 @socket(i32 %6, i32 %7, i32 %9)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %20
12: ; preds = %3
%13 = load i32, i32* @stderr
%14 = load i32, i32* %5
%15 = load i32, i32* %8
%16 = load i32, i32* @SOCK_STREAM
%17 = load i32, i32* @errno
%18 = call i8* @strerror(i32 %17)
%19 = call i32 (i32, i8*, ...) @fprintf(i32 %13, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i32 %14, i32 %15, i32 %16, i8* %18)
br label %50
20: ; preds = %3
%21 = load i32, i32* @NI_MAXHOST
%22 = zext i32 %21 to i64
%23 = call i8* @llvm.stacksave()
%24 = alloca i8, i64 %22
%25 = load i32, i32* @NI_MAXSERV
%26 = zext i32 %25 to i64
%27 = alloca i8, i64 %26
%28 = call i32 @lookup_addrinfo(%struct.addrinfo* %4, i8* %24, i32 %21, i8* %27, i32 %25)
%29 = load i32, i32* @stderr
%30 = call i32 (i32, i8*, ...) @fprintf(i32 %29, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i8* %24, i8* %27)
%31 = call double (...) @now()
%32 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 3
%33 = load i32, i32* %32
%34 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 2
%35 = load i32, i32* %34
%36 = call i32 @connect(i32 %10, i32 %33, i32 %35)
%37 = load i32, i32* @errno
%38 = call i32 @close(i32 %10)
%39 = icmp eq i32 %36, 0
br i1 %39, label %40, label %46
40: ; preds = %20
%41 = call double (...) @now()
%42 = load i32, i32* @stderr
%43 = fsub double %41, %31
%44 = fmul double %43, 1.000000e+03
%45 = call i32 (i32, i8*, ...) @fprintf(i32 %42, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), double %44)
br label %54
46: ; preds = %20
%47 = load i32, i32* @stderr
%48 = call i8* @strerror(i32 %37)
%49 = call i32 (i32, i8*, ...) @fprintf(i32 %47, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i8* %48)
br label %50
50: ; preds = %46, %12
%51 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 4
%52 = load %struct.addrinfo*, %struct.addrinfo** %51
%53 = icmp eq %struct.addrinfo* %52, null
br i1 %53, label %54, label %3
54: ; preds = %50, %1, %40
%55 = phi %struct.addrinfo* [ %4, %40 ], [ null, %1 ], [ null, %50 ]
ret %struct.addrinfo* %55
}