#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 timeval {
  scalar_t__ tv_usec;
  scalar_t__ tv_sec;
};
struct addrinfo {
  struct addrinfo *ai_next;
  int /*<<< orphan*/ ai_addrlen;
  int /*<<< orphan*/ ai_addr;
  int /*<<< orphan*/ ai_protocol;
  int /*<<< orphan*/ ai_socktype;
  int /*<<< orphan*/ ai_family;
};
typedef int /*<<< orphan*/ on;

/* Variables and functions */
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
int /*<<< orphan*/ close(int);
int connect(int, int /*<<< orphan*/, int /*<<< orphan*/);
int errno;
int gettimeofday(struct timeval *, int /*<<< orphan*/ *);
scalar_t__ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int const *,
                      int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int connect_to(struct addrinfo *addr, struct timeval *rtt) {
  int fd;
  struct timeval start;
  int connect_result;
  const int on = 1;
  int rv = 0;
  while (addr != NULL) {
    if ((fd = socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol)) ==
        -1)
      goto next_addr0;
    if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0)
      goto next_addr1;
    if (gettimeofday(&start, NULL) == -1)
      goto next_addr1;
    if ((connect_result = connect(fd, addr->ai_addr, addr->ai_addrlen)) == 0) {
      if (gettimeofday(rtt, NULL) == -1)
        goto next_addr1;
      rtt->tv_sec = rtt->tv_sec - start.tv_sec;
      rtt->tv_usec = rtt->tv_usec - start.tv_usec;
      close(fd);
      return 0;
    }
  next_addr1:
    close(fd);
  next_addr0:
    addr = addr->ai_next;
  }
  rv = rv ? rv : -errno;
  return rv;
}


x86 assembly (clang, -O0):
<connect_to>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x1,-0x38(%rbp)
movl   $0x0,-0x3c(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1286 <connect_to+0x136>
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x14(%rax),%esi
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%edx
call   1070 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0xffffffff,%eax
jne    11a4 <connect_to+0x54>
jmp    1276 <connect_to+0x126>
mov    -0x1c(%rbp),%edi
mov    0x2e3a(%rip),%rax
mov    (%rax),%esi
mov    0x2e19(%rip),%rax
mov    (%rax),%edx
lea    -0x38(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0x0,%rax
jge    11d7 <connect_to+0x87>
jmp    126e <connect_to+0x11e>
lea    -0x30(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1040 <gettimeofday@plt>
cmp    $0xffffffff,%eax
jne    11f2 <connect_to+0xa2>
jmp    126e <connect_to+0x11e>
mov    -0x1c(%rbp),%edi
mov    -0x10(%rbp),%rax
mov    0xc(%rax),%esi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%edx
call   1060 <connect@plt>
mov    %eax,-0x34(%rbp)
cmp    $0x0,%eax
jne    1269 <connect_to+0x119>
mov    -0x18(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1040 <gettimeofday@plt>
cmp    $0xffffffff,%eax
jne    122f <connect_to+0xdf>
jmp    126e <connect_to+0x11e>
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%rcx
sub    -0x28(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,0x8(%rax)
mov    -0x18(%rbp),%rax
mov    (%rax),%rcx
sub    -0x30(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
jmp    12b5 <connect_to+0x165>
jmp    126e <connect_to+0x11e>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x10(%rbp)
jmp    116e <connect_to+0x1e>
cmpl   $0x0,-0x3c(%rbp)
je     129b <connect_to+0x14b>
mov    -0x3c(%rbp),%eax
mov    %eax,-0x40(%rbp)
jmp    12a9 <connect_to+0x159>
xor    %eax,%eax
mov    0x2d34(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x40(%rbp)
mov    -0x40(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<connect_to>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rsi,0x8(%rsp)
movl   $0x1,0x4(%rsp)
test   %rdi,%rdi
je     1233 <connect_to+0xe3>
mov    %rdi,%rbx
mov    0x2e6a(%rip),%r13
mov    0x2e4b(%rip),%r14
lea    0x4(%rsp),%r15
lea    0x10(%rsp),%r12
jmp    11b3 <connect_to+0x63>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %ebp,%edi
call   1050 <close@plt>
mov    (%rbx),%rbx
test   %rbx,%rbx
je     1233 <connect_to+0xe3>
mov    0x18(%rbx),%edi
mov    0x10(%rbx),%edx
mov    0x14(%rbx),%esi
call   1070 <socket@plt>
cmp    $0xffffffff,%eax
je     11a7 <connect_to+0x57>
mov    %eax,%ebp
mov    0x0(%r13),%esi
mov    (%r14),%edx
mov    %eax,%edi
mov    %r15,%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
test   %rax,%rax
js     11a0 <connect_to+0x50>
mov    %r12,%rdi
xor    %esi,%esi
call   1040 <gettimeofday@plt>
cmp    $0xffffffff,%eax
je     11a0 <connect_to+0x50>
mov    0x8(%rbx),%edx
mov    0xc(%rbx),%esi
mov    %ebp,%edi
call   1060 <connect@plt>
test   %eax,%eax
jne    11a0 <connect_to+0x50>
mov    0x8(%rsp),%rdi
xor    %esi,%esi
call   1040 <gettimeofday@plt>
cmp    $0xffffffff,%eax
je     11a0 <connect_to+0x50>
mov    0x8(%rsp),%rax
movdqu (%rax),%xmm0
psubq  0x10(%rsp),%xmm0
movdqu %xmm0,(%rax)
mov    %ebp,%edi
call   1050 <close@plt>
xor    %eax,%eax
jmp    123e <connect_to+0xee>
mov    0x2d9e(%rip),%rcx
xor    %eax,%eax
sub    (%rcx),%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.addrinfo = type { %struct.addrinfo*, i32, i32, i32, i32, i32 }
%struct.timeval = type { i64, i64 }
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@errno = global i32 0
define i32 @connect_to(%struct.addrinfo* %0, %struct.timeval* %1) {
%3 = alloca i32
%4 = alloca %struct.addrinfo*
%5 = alloca %struct.timeval*
%6 = alloca i32
%7 = alloca %struct.timeval
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store %struct.addrinfo* %0, %struct.addrinfo** %4
store %struct.timeval* %1, %struct.timeval** %5
store i32 1, i32* %9
store i32 0, i32* %10
br label %11
11: ; preds = %76, %2
%12 = load %struct.addrinfo*, %struct.addrinfo** %4
%13 = icmp ne %struct.addrinfo* %12, null
br i1 %13, label %14, label %80
14: ; preds = %11
%15 = load %struct.addrinfo*, %struct.addrinfo** %4
%16 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %15, i32 0, i32 5
%17 = load i32, i32* %16
%18 = load %struct.addrinfo*, %struct.addrinfo** %4
%19 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %18, i32 0, i32 4
%20 = load i32, i32* %19
%21 = load %struct.addrinfo*, %struct.addrinfo** %4
%22 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %21, i32 0, i32 3
%23 = load i32, i32* %22
%24 = call i32 @socket(i32 %17, i32 %20, i32 %23)
store i32 %24, i32* %6
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %27
26: ; preds = %14
br label %76
27: ; preds = %14
%28 = load i32, i32* %6
%29 = load i32, i32* @SOL_SOCKET
%30 = load i32, i32* @SO_REUSEADDR
%31 = call i64 @setsockopt(i32 %28, i32 %29, i32 %30, i32* %9, i32 4)
%32 = icmp slt i64 %31, 0
br i1 %32, label %33, label %34
33: ; preds = %27
br label %73
34: ; preds = %27
%35 = call i32 @gettimeofday(%struct.timeval* %7, i32* null)
%36 = icmp eq i32 %35, -1
br i1 %36, label %37, label %38
37: ; preds = %34
br label %73
38: ; preds = %34
%39 = load i32, i32* %6
%40 = load %struct.addrinfo*, %struct.addrinfo** %4
%41 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %40, i32 0, i32 2
%42 = load i32, i32* %41
%43 = load %struct.addrinfo*, %struct.addrinfo** %4
%44 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %43, i32 0, i32 1
%45 = load i32, i32* %44
%46 = call i32 @connect(i32 %39, i32 %42, i32 %45)
store i32 %46, i32* %8
%47 = icmp eq i32 %46, 0
br i1 %47, label %48, label %72
48: ; preds = %38
%49 = load %struct.timeval*, %struct.timeval** %5
%50 = call i32 @gettimeofday(%struct.timeval* %49, i32* null)
%51 = icmp eq i32 %50, -1
br i1 %51, label %52, label %53
52: ; preds = %48
br label %73
53: ; preds = %48
%54 = load %struct.timeval*, %struct.timeval** %5
%55 = getelementptr inbounds %struct.timeval, %struct.timeval* %54, i32 0, i32 1
%56 = load i64, i64* %55
%57 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i32 0, i32 1
%58 = load i64, i64* %57
%59 = sub nsw i64 %56, %58
%60 = load %struct.timeval*, %struct.timeval** %5
%61 = getelementptr inbounds %struct.timeval, %struct.timeval* %60, i32 0, i32 1
store i64 %59, i64* %61
%62 = load %struct.timeval*, %struct.timeval** %5
%63 = getelementptr inbounds %struct.timeval, %struct.timeval* %62, i32 0, i32 0
%64 = load i64, i64* %63
%65 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i32 0, i32 0
%66 = load i64, i64* %65
%67 = sub nsw i64 %64, %66
%68 = load %struct.timeval*, %struct.timeval** %5
%69 = getelementptr inbounds %struct.timeval, %struct.timeval* %68, i32 0, i32 0
store i64 %67, i64* %69
%70 = load i32, i32* %6
%71 = call i32 @close(i32 %70)
store i32 0, i32* %3
br label %91
72: ; preds = %38
br label %73
73: ; preds = %72, %52, %37, %33
%74 = load i32, i32* %6
%75 = call i32 @close(i32 %74)
br label %76
76: ; preds = %73, %26
%77 = load %struct.addrinfo*, %struct.addrinfo** %4
%78 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %77, i32 0, i32 0
%79 = load %struct.addrinfo*, %struct.addrinfo** %78
store %struct.addrinfo* %79, %struct.addrinfo** %4
br label %11
80: ; preds = %11
%81 = load i32, i32* %10
%82 = icmp ne i32 %81, 0
br i1 %82, label %83, label %85
83: ; preds = %80
%84 = load i32, i32* %10
br label %88
85: ; preds = %80
%86 = load i32, i32* @errno
%87 = sub nsw i32 0, %86
br label %88
88: ; preds = %85, %83
%89 = phi i32 [ %84, %83 ], [ %87, %85 ]
store i32 %89, i32* %10
%90 = load i32, i32* %10
store i32 %90, i32* %3
br label %91
91: ; preds = %88, %53
%92 = load i32, i32* %3
ret i32 %92
}

LLVM IR (-O2):
%struct.addrinfo = type { %struct.addrinfo*, i32, i32, i32, i32, i32 }
%struct.timeval = type { i64, i64 }
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@errno = global i32 0
define i32 @connect_to(%struct.addrinfo* %0, %struct.timeval* nocapture %1) {
%3 = alloca <2 x i64>
%4 = bitcast <2 x i64>* %3 to %struct.timeval*
%5 = alloca i32
%6 = bitcast <2 x i64>* %3 to i8*
%7 = bitcast i32* %5 to i8*
store i32 1, i32* %5
%8 = icmp eq %struct.addrinfo* %0, null
br i1 %8, label %50, label %9
9: ; preds = %2, %46
%10 = phi %struct.addrinfo* [ %48, %46 ], [ %0, %2 ]
%11 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %10, i64 0, i32 5
%12 = load i32, i32* %11
%13 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %10, i64 0, i32 4
%14 = load i32, i32* %13
%15 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %10, i64 0, i32 3
%16 = load i32, i32* %15
%17 = call i32 @socket(i32 %12, i32 %14, i32 %16)
%18 = icmp eq i32 %17, -1
br i1 %18, label %46, label %19
19: ; preds = %9
%20 = load i32, i32* @SOL_SOCKET
%21 = load i32, i32* @SO_REUSEADDR
%22 = call i64 @setsockopt(i32 %17, i32 %20, i32 %21, i32* %5, i32 4)
%23 = icmp slt i64 %22, 0
br i1 %23, label %44, label %24
24: ; preds = %19
%25 = call i32 @gettimeofday(%struct.timeval* %4, i32* null)
%26 = icmp eq i32 %25, -1
br i1 %26, label %44, label %27
27: ; preds = %24
%28 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %10, i64 0, i32 2
%29 = load i32, i32* %28
%30 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %10, i64 0, i32 1
%31 = load i32, i32* %30
%32 = call i32 @connect(i32 %17, i32 %29, i32 %31)
%33 = icmp eq i32 %32, 0
br i1 %33, label %34, label %44
34: ; preds = %27
%35 = call i32 @gettimeofday(%struct.timeval* %1, i32* null)
%36 = icmp eq i32 %35, -1
br i1 %36, label %44, label %37
37: ; preds = %34
%38 = bitcast %struct.timeval* %1 to <2 x i64>*
%39 = load <2 x i64>, <2 x i64>* %38
%40 = load <2 x i64>, <2 x i64>* %3
%41 = sub nsw <2 x i64> %39, %40
%42 = bitcast %struct.timeval* %1 to <2 x i64>*
store <2 x i64> %41, <2 x i64>* %42
%43 = call i32 @close(i32 %17)
br label %53
44: ; preds = %27, %34, %24, %19
%45 = call i32 @close(i32 %17)
br label %46
46: ; preds = %9, %44
%47 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %10, i64 0, i32 0
%48 = load %struct.addrinfo*, %struct.addrinfo** %47
%49 = icmp eq %struct.addrinfo* %48, null
br i1 %49, label %50, label %9
50: ; preds = %46, %2
%51 = load i32, i32* @errno
%52 = sub nsw i32 0, %51
br label %53
53: ; preds = %50, %37
%54 = phi i32 [ 0, %37 ], [ %52, %50 ]
ret i32 %54
}