#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;
scalar_t__ EINPROGRESS;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
int connect(int, int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ errno;
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
int /*<<< orphan*/ gai_strerror(int);
int getaddrinfo(char const *, char *, struct addrinfo *, struct addrinfo **);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int /*<<< orphan*/ net_err(char *, char *, int /*<<< orphan*/);
int reuse(char *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strerror(scalar_t__);
scalar_t__ tcp_nonblock(char *, int);
int tcp_connect(char *err, const char *addr, int port, int nonblock) {
  int fd = -1;
  int rv;
  char port_buf[6];
  struct addrinfo hints, *servinfo, *p;
  snprintf(port_buf, 6, "%d", port);
  memset(&hints, 0, sizeof hints);
  hints.ai_family = AF_UNSPEC;
  hints.ai_socktype = SOCK_STREAM;
  if ((rv = getaddrinfo(addr, port_buf, &hints, &servinfo)) != 0) {
    net_err(err, "%s", gai_strerror(rv));
    return -1;
  }
  for (p = servinfo; p != NULL; p = p->ai_next) {
    if ((fd == socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1)
      continue;
    if (reuse(err, fd) == -1)
      goto e;
    if (nonblock == 1 && tcp_nonblock(err, fd) != 0)
      goto e;
    if (connect(fd, p->ai_addr, p->ai_addrlen) == -1) {
      if (errno == EINPROGRESS && nonblock == 1)
        goto end;
      close(fd);
      continue;
    }
    goto end;
  }
  if (p == NULL) {
    net_err(err, "connecting %s", strerror(errno));
    goto e;
  }
e:
  fd = -1;
end:
  freeaddrinfo(servinfo);
  return fd;
}


x86 assembly (clang, -O0):
<tcp_connect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %ecx,-0x20(%rbp)
movl   $0xffffffff,-0x24(%rbp)
lea    -0x2e(%rbp),%rdi
mov    -0x1c(%rbp),%ecx
mov    $0x6,%esi
lea    0xe0d(%rip),%rdx
call   1030 <snprintf@plt>
lea    -0x50(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1050 <memset@plt>
mov    0x2dd1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
mov    0x2db5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    -0x18(%rbp),%rdi
lea    -0x2e(%rbp),%rsi
lea    -0x50(%rbp),%rdx
lea    -0x58(%rbp),%rcx
call   10b0 <getaddrinfo@plt>
mov    %eax,-0x28(%rbp)
cmp    $0x0,%eax
je     126f <tcp_connect+0xaf>
mov    -0x10(%rbp),%rax
mov    %rax,-0x68(%rbp)
mov    -0x28(%rbp),%edi
call   1040 <gai_strerror@plt>
mov    -0x68(%rbp),%rdi
mov    %eax,%edx
lea    0xdb0(%rip),%rsi
call   1090 <net_err@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13bc <tcp_connect+0x1fc>
mov    -0x58(%rbp),%rax
mov    %rax,-0x60(%rbp)
cmpq   $0x0,-0x60(%rbp)
je     1368 <tcp_connect+0x1a8>
mov    -0x24(%rbp),%eax
mov    %eax,-0x6c(%rbp)
mov    -0x60(%rbp),%rax
mov    0x10(%rax),%edi
mov    -0x60(%rbp),%rax
mov    0xc(%rax),%esi
mov    -0x60(%rbp),%rax
mov    0x8(%rax),%edx
call   10e0 <socket@plt>
mov    %eax,%ecx
mov    -0x6c(%rbp),%eax
cmp    %ecx,%eax
sete   %al
and    $0x1,%al
movzbl %al,%eax
cmp    $0xffffffff,%eax
jne    12bf <tcp_connect+0xff>
jmp    1357 <tcp_connect+0x197>
mov    -0x10(%rbp),%rdi
mov    -0x24(%rbp),%esi
call   1070 <reuse@plt>
cmp    $0xffffffff,%eax
jne    12d9 <tcp_connect+0x119>
jmp    13a6 <tcp_connect+0x1e6>
cmpl   $0x1,-0x20(%rbp)
jne    12fe <tcp_connect+0x13e>
mov    -0x10(%rbp),%rdi
mov    -0x24(%rbp),%esi
call   10a0 <tcp_nonblock@plt>
cmp    $0x0,%rax
je     12fe <tcp_connect+0x13e>
jmp    13a6 <tcp_connect+0x1e6>
mov    -0x24(%rbp),%edi
mov    -0x60(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x60(%rbp),%rax
mov    (%rax),%edx
call   1080 <connect@plt>
cmp    $0xffffffff,%eax
jne    1352 <tcp_connect+0x192>
mov    0x2ca5(%rip),%rax
mov    (%rax),%rax
mov    0x2cbb(%rip),%rcx
cmp    (%rcx),%rax
jne    1345 <tcp_connect+0x185>
cmpl   $0x1,-0x20(%rbp)
jne    1345 <tcp_connect+0x185>
jmp    13ad <tcp_connect+0x1ed>
mov    -0x24(%rbp),%edi
call   1060 <close@plt>
jmp    1357 <tcp_connect+0x197>
jmp    13ad <tcp_connect+0x1ed>
mov    -0x60(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x60(%rbp)
jmp    1277 <tcp_connect+0xb7>
cmpq   $0x0,-0x60(%rbp)
jne    13a1 <tcp_connect+0x1e1>
mov    -0x10(%rbp),%rax
mov    %rax,-0x78(%rbp)
mov    0x2c46(%rip),%rax
mov    (%rax),%rdi
call   10c0 <strerror@plt>
mov    -0x78(%rbp),%rdi
mov    %eax,%edx
lea    0xc6c(%rip),%rsi
call   1090 <net_err@plt>
jmp    13a6 <tcp_connect+0x1e6>
jmp    13a6 <tcp_connect+0x1e6>
movl   $0xffffffff,-0x24(%rbp)
mov    -0x58(%rbp),%rdi
call   10d0 <freeaddrinfo@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tcp_connect>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x30,%rsp
mov    %ecx,%r12d
mov    %edx,%ecx
mov    %rsi,%rbx
mov    %rdi,%r14
lea    0xe22(%rip),%rdx
lea    0xa(%rsp),%r15
mov    %r15,%rdi
mov    $0x6,%esi
call   1030 <snprintf@plt>
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1050 <memset@plt>
mov    0x2dd5(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rsp)
mov    0x2db8(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x1c(%rsp)
mov    %rsp,%rcx
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %rbp,%rdx
call   10b0 <getaddrinfo@plt>
test   %eax,%eax
je     1250 <tcp_connect+0x90>
mov    %eax,%edi
call   1040 <gai_strerror@plt>
lea    0xdcd(%rip),%rsi
mov    %r14,%rdi
mov    %eax,%edx
call   1090 <net_err@plt>
jmp    1309 <tcp_connect+0x149>
mov    (%rsp),%rbx
test   %rbx,%rbx
je     12e0 <tcp_connect+0x120>
mov    0x2d64(%rip),%r15
mov    0x2d7d(%rip),%rbp
jmp    1283 <tcp_connect+0xc3>
nopl   (%rax)
mov    $0xffffffff,%edi
call   1060 <close@plt>
mov    0x18(%rbx),%rbx
test   %rbx,%rbx
je     12e0 <tcp_connect+0x120>
mov    0x10(%rbx),%edi
mov    0x8(%rbx),%edx
mov    0xc(%rbx),%esi
call   10e0 <socket@plt>
mov    %r14,%rdi
mov    $0xffffffff,%esi
call   1070 <reuse@plt>
cmp    $0xffffffff,%eax
je     1300 <tcp_connect+0x140>
cmp    $0x1,%r12d
jne    12bb <tcp_connect+0xfb>
mov    %r14,%rdi
mov    $0xffffffff,%esi
call   10a0 <tcp_nonblock@plt>
test   %rax,%rax
jne    1300 <tcp_connect+0x140>
mov    (%rbx),%edx
mov    0x4(%rbx),%esi
mov    $0xffffffff,%edi
call   1080 <connect@plt>
cmp    $0xffffffff,%eax
jne    1300 <tcp_connect+0x140>
cmp    $0x1,%r12d
jne    1270 <tcp_connect+0xb0>
mov    0x0(%rbp),%rax
cmp    %rax,(%r15)
jne    1270 <tcp_connect+0xb0>
jmp    1300 <tcp_connect+0x140>
mov    0x2ce1(%rip),%rax
mov    (%rax),%rdi
call   10c0 <strerror@plt>
lea    0xd0d(%rip),%rsi
mov    %r14,%rdi
mov    %eax,%edx
call   1090 <net_err@plt>
mov    (%rsp),%rdi
call   10d0 <freeaddrinfo@plt>
mov    $0xffffffff,%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* }
@.str = [3 x i8] c"%d\00"
@AF_UNSPEC = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [3 x i8] c"%s\00"
@errno = global i64 0
@EINPROGRESS = global i64 0
@.str.2 = [14 x i8] c"connecting %s\00"
define i32 @tcp_connect(i8* %0, i8* %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca [6 x i8]
%13 = alloca %struct.addrinfo
%14 = alloca %struct.addrinfo*
%15 = alloca %struct.addrinfo*
store i8* %0, i8** %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store i32 %3, i32* %9
store i32 -1, i32* %10
%16 = getelementptr inbounds [6 x i8], [6 x i8]* %12, i64 0, i64 0
%17 = load i32, i32* %8
%18 = call i32 @snprintf(i8* %16, i32 6, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %17)
%19 = call i32 @memset(%struct.addrinfo* %13, i32 0, i32 32)
%20 = load i32, i32* @AF_UNSPEC
%21 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %13, i32 0, i32 4
store i32 %20, i32* %21
%22 = load i32, i32* @SOCK_STREAM
%23 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %13, i32 0, i32 3
store i32 %22, i32* %23
%24 = load i8*, i8** %7
%25 = getelementptr inbounds [6 x i8], [6 x i8]* %12, i64 0, i64 0
%26 = call i32 @getaddrinfo(i8* %24, i8* %25, %struct.addrinfo* %13, %struct.addrinfo** %14)
store i32 %26, i32* %11
%27 = icmp ne i32 %26, 0
br i1 %27, label %28, label %33
28: ; preds = %4
%29 = load i8*, i8** %6
%30 = load i32, i32* %11
%31 = call i32 @gai_strerror(i32 %30)
%32 = call i32 @net_err(i8* %29, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 %31)
store i32 -1, i32* %5
br label %109
33: ; preds = %4
%34 = load %struct.addrinfo*, %struct.addrinfo** %14
store %struct.addrinfo* %34, %struct.addrinfo** %15
br label %35
35: ; preds = %91, %33
%36 = load %struct.addrinfo*, %struct.addrinfo** %15
%37 = icmp ne %struct.addrinfo* %36, null
br i1 %37, label %38, label %95
38: ; preds = %35
%39 = load i32, i32* %10
%40 = load %struct.addrinfo*, %struct.addrinfo** %15
%41 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %40, i32 0, i32 4
%42 = load i32, i32* %41
%43 = load %struct.addrinfo*, %struct.addrinfo** %15
%44 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %43, i32 0, i32 3
%45 = load i32, i32* %44
%46 = load %struct.addrinfo*, %struct.addrinfo** %15
%47 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %46, i32 0, i32 2
%48 = load i32, i32* %47
%49 = call i32 @socket(i32 %42, i32 %45, i32 %48)
%50 = icmp eq i32 %39, %49
%51 = zext i1 %50 to i32
%52 = icmp eq i32 %51, -1
br i1 %52, label %53, label %54
53: ; preds = %38
br label %91
54: ; preds = %38
%55 = load i8*, i8** %6
%56 = load i32, i32* %10
%57 = call i32 @reuse(i8* %55, i32 %56)
%58 = icmp eq i32 %57, -1
br i1 %58, label %59, label %60
59: ; preds = %54
br label %104
60: ; preds = %54
%61 = load i32, i32* %9
%62 = icmp eq i32 %61, 1
br i1 %62, label %63, label %69
63: ; preds = %60
%64 = load i8*, i8** %6
%65 = load i32, i32* %10
%66 = call i64 @tcp_nonblock(i8* %64, i32 %65)
%67 = icmp ne i64 %66, 0
br i1 %67, label %68, label %69
68: ; preds = %63
br label %104
69: ; preds = %63, %60
%70 = load i32, i32* %10
%71 = load %struct.addrinfo*, %struct.addrinfo** %15
%72 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %71, i32 0, i32 1
%73 = load i32, i32* %72
%74 = load %struct.addrinfo*, %struct.addrinfo** %15
%75 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %74, i32 0, i32 0
%76 = load i32, i32* %75
%77 = call i32 @connect(i32 %70, i32 %73, i32 %76)
%78 = icmp eq i32 %77, -1
br i1 %78, label %79, label %90
79: ; preds = %69
%80 = load i64, i64* @errno
%81 = load i64, i64* @EINPROGRESS
%82 = icmp eq i64 %80, %81
br i1 %82, label %83, label %87
83: ; preds = %79
%84 = load i32, i32* %9
%85 = icmp eq i32 %84, 1
br i1 %85, label %86, label %87
86: ; preds = %83
br label %105
87: ; preds = %83, %79
%88 = load i32, i32* %10
%89 = call i32 @close(i32 %88)
br label %91
90: ; preds = %69
br label %105
91: ; preds = %87, %53
%92 = load %struct.addrinfo*, %struct.addrinfo** %15
%93 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %92, i32 0, i32 5
%94 = load %struct.addrinfo*, %struct.addrinfo** %93
store %struct.addrinfo* %94, %struct.addrinfo** %15
br label %35
95: ; preds = %35
%96 = load %struct.addrinfo*, %struct.addrinfo** %15
%97 = icmp eq %struct.addrinfo* %96, null
br i1 %97, label %98, label %103
98: ; preds = %95
%99 = load i8*, i8** %6
%100 = load i64, i64* @errno
%101 = call i32 @strerror(i64 %100)
%102 = call i32 @net_err(i8* %99, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 %101)
br label %104
103: ; preds = %95
br label %104
104: ; preds = %103, %98, %68, %59
store i32 -1, i32* %10
br label %105
105: ; preds = %104, %90, %86
%106 = load %struct.addrinfo*, %struct.addrinfo** %14
%107 = call i32 @freeaddrinfo(%struct.addrinfo* %106)
%108 = load i32, i32* %10
store i32 %108, i32* %5
br label %109
109: ; preds = %105, %28
%110 = load i32, i32* %5
ret i32 %110
}

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