#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct timeval {
  int tv_sec;
  scalar_t__ tv_usec;
};
struct TYPE_2__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
struct in_addr {
  int /*<<< orphan*/ s_addr;
};
struct hostent {
  scalar_t__ h_addr;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ fd_set;
typedef int /*<<< orphan*/ address;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
scalar_t__ EINPROGRESS;
scalar_t__ EINTR;
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ F_GETFL;
int /*<<< orphan*/ F_SETFL;
long O_NONBLOCK;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_ERROR;
int /*<<< orphan*/ close(int);
int connect(int, struct sockaddr *, int);
scalar_t__ errno;
scalar_t__ fcntl(int, int /*<<< orphan*/, long);
struct hostent *gethostbyname(char *);
scalar_t__ getsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, void *,
                      int *);
int /*<<< orphan*/ htons(int);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int tcp_connect(char *hostname, int port) {
  struct hostent *host;
  int sockfd;
  struct sockaddr_in address;
  long arg = 0;
  int connection;
  if (hostname == NULL) {
    return -1;
  }
  host = gethostbyname(hostname);
  if (host == NULL) {
    return -1;
  }
  sockfd = socket(AF_INET, SOCK_STREAM, 0);
  if (sockfd < 0) {
    return -1;
  }
  address.sin_family = AF_INET;
  address.sin_addr.s_addr = ((struct in_addr *)host->h_addr)->s_addr;
  address.sin_port = htons(port);
  arg = 0;
  if ((arg = fcntl(sockfd, F_GETFL, NULL)) < 0) {
    close(sockfd);
    return -1;
  }
  arg |= O_NONBLOCK;
  if (fcntl(sockfd, F_SETFL, arg) < 0) {
    close(sockfd);
    return -1;
  }
  connection = connect(sockfd, (struct sockaddr *)&address, sizeof(address));
  if (connection < 0) {
    if (errno == EINPROGRESS) {
      fd_set myset;
      struct timeval tv;
      int valopt;
      tv.tv_sec = 5;
      tv.tv_usec = 0;
      FD_ZERO(&myset);
      FD_SET(sockfd, &myset);
      connection = select(sockfd + 1, NULL, &myset, NULL, &tv);
      while (connection < 0 && errno != EINTR) {
        connection = select(sockfd + 1, NULL, &myset, NULL, &tv);
      }
      if (connection > 0) {
        socklen_t sock_len = sizeof(int);
        if (getsockopt(sockfd, SOL_SOCKET, SO_ERROR, (void *)(&valopt),
                       &sock_len) < 0) {
          close(sockfd);
          return -1;
        }
        if (valopt) {
          close(sockfd);
          return -1;
        }
      } else {
        close(sockfd);
        return -1;
      }
    } else {
      close(sockfd);
      return -1;
    }
  }
  if ((arg = fcntl(sockfd, F_GETFL, NULL)) < 0) {
    close(sockfd);
    return -1;
  }
  arg &= (~O_NONBLOCK);
  if (fcntl(sockfd, F_SETFL, arg) < 0) {
    close(sockfd);
    return -1;
  }
  return sockfd;
}


x86 assembly (clang, -O0):
<tcp_connect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movq   $0x0,-0x38(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11ce <tcp_connect+0x2e>
movl   $0xffffffff,-0x4(%rbp)
jmp    14cb <tcp_connect+0x32b>
mov    -0x10(%rbp),%rdi
call   1070 <gethostbyname@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11f2 <tcp_connect+0x52>
movl   $0xffffffff,-0x4(%rbp)
jmp    14cb <tcp_connect+0x32b>
mov    0x2de7(%rip),%rax
mov    (%rax),%edi
mov    0x2db6(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    1224 <tcp_connect+0x84>
movl   $0xffffffff,-0x4(%rbp)
jmp    14cb <tcp_connect+0x32b>
mov    0x2db5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x14(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x24(%rbp),%edi
mov    0x2d47(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
mov    %eax,%edx
call   1030 <fcntl@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
jge    1286 <tcp_connect+0xe6>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    14cb <tcp_connect+0x32b>
mov    0x2d4b(%rip),%rax
mov    (%rax),%rax
or     -0x38(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x24(%rbp),%edi
mov    0x2d1e(%rip),%rax
mov    (%rax),%esi
mov    -0x38(%rbp),%rdx
call   1030 <fcntl@plt>
cmp    $0x0,%rax
jge    12cb <tcp_connect+0x12b>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    14cb <tcp_connect+0x32b>
mov    -0x24(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0xc,%edx
call   10a0 <connect@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jge    1445 <tcp_connect+0x2a5>
mov    0x2cc0(%rip),%rax
mov    (%rax),%rax
mov    0x2cd6(%rip),%rcx
cmp    (%rcx),%rax
jne    142c <tcp_connect+0x28c>
movl   $0x5,-0x50(%rbp)
movq   $0x0,-0x48(%rbp)
lea    -0x40(%rbp),%rdi
call   1080 <FD_ZERO@plt>
mov    -0x24(%rbp),%edi
lea    -0x40(%rbp),%rsi
call   10b0 <FD_SET@plt>
mov    -0x24(%rbp),%edi
add    $0x1,%edi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x40(%rbp),%rdx
lea    -0x50(%rbp),%r8
mov    %rcx,%rsi
call   1090 <select@plt>
mov    %eax,-0x3c(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x3c(%rbp)
mov    %al,-0x59(%rbp)
jge    136d <tcp_connect+0x1cd>
mov    0x2c56(%rip),%rax
mov    (%rax),%rax
mov    0x2c34(%rip),%rcx
cmp    (%rcx),%rax
setne  %al
mov    %al,-0x59(%rbp)
mov    -0x59(%rbp),%al
test   $0x1,%al
jne    137d <tcp_connect+0x1dd>
jmp    139f <tcp_connect+0x1ff>
mov    -0x24(%rbp),%edi
add    $0x1,%edi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x40(%rbp),%rdx
lea    -0x50(%rbp),%r8
mov    %rcx,%rsi
call   1090 <select@plt>
mov    %eax,-0x3c(%rbp)
jmp    1344 <tcp_connect+0x1a4>
cmpl   $0x0,-0x3c(%rbp)
jle    1413 <tcp_connect+0x273>
movl   $0x4,-0x58(%rbp)
mov    -0x24(%rbp),%edi
mov    0x2c2e(%rip),%rax
mov    (%rax),%esi
mov    0x2be5(%rip),%rax
mov    (%rax),%edx
lea    -0x54(%rbp),%rcx
lea    -0x58(%rbp),%r8
call   1060 <getsockopt@plt>
cmp    $0x0,%rax
jge    13f0 <tcp_connect+0x250>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    14cb <tcp_connect+0x32b>
cmpl   $0x0,-0x54(%rbp)
je     140e <tcp_connect+0x26e>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    14cb <tcp_connect+0x32b>
jmp    1427 <tcp_connect+0x287>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    14cb <tcp_connect+0x32b>
jmp    1440 <tcp_connect+0x2a0>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    14cb <tcp_connect+0x32b>
jmp    1445 <tcp_connect+0x2a5>
mov    -0x24(%rbp),%edi
mov    0x2b51(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
mov    %eax,%edx
call   1030 <fcntl@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
jge    147c <tcp_connect+0x2dc>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    14cb <tcp_connect+0x32b>
mov    0x2b55(%rip),%rax
mov    (%rax),%rax
xor    $0xffffffffffffffff,%rax
and    -0x38(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x24(%rbp),%edi
mov    0x2b24(%rip),%rax
mov    (%rax),%esi
mov    -0x38(%rbp),%rdx
call   1030 <fcntl@plt>
cmp    $0x0,%rax
jge    14c5 <tcp_connect+0x325>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    14cb <tcp_connect+0x32b>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tcp_connect>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    $0xffffffff,%r15d
test   %rdi,%rdi
je     13b4 <tcp_connect+0x214>
mov    %esi,%ebx
call   1070 <gethostbyname@plt>
test   %rax,%rax
je     13b4 <tcp_connect+0x214>
mov    %rax,%rbp
mov    0x2e09(%rip),%r14
mov    (%r14),%edi
mov    0x2dd7(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
test   %eax,%eax
js     13b4 <tcp_connect+0x214>
mov    %eax,%r12d
mov    (%r14),%eax
mov    %eax,0x20(%rsp)
mov    0x0(%rbp),%rax
mov    (%rax),%eax
mov    %eax,0x1c(%rsp)
mov    %ebx,%edi
call   1040 <htons@plt>
mov    %eax,0x18(%rsp)
mov    0x2d88(%rip),%rbx
mov    (%rbx),%esi
mov    %r12d,%edi
xor    %edx,%edx
call   1030 <fcntl@plt>
test   %rax,%rax
js     13ac <tcp_connect+0x20c>
mov    0x2da4(%rip),%rbp
or     0x0(%rbp),%rax
mov    0x2d81(%rip),%r14
mov    (%r14),%esi
mov    %r12d,%edi
mov    %rax,%rdx
call   1030 <fcntl@plt>
test   %rax,%rax
js     13ac <tcp_connect+0x20c>
lea    0x18(%rsp),%rsi
mov    %r12d,%edi
mov    $0xc,%edx
call   10a0 <connect@plt>
test   %eax,%eax
js     12aa <tcp_connect+0x10a>
mov    (%rbx),%esi
mov    %r12d,%edi
xor    %edx,%edx
call   1030 <fcntl@plt>
test   %rax,%rax
js     13ac <tcp_connect+0x20c>
mov    0x0(%rbp),%rcx
not    %rcx
and    %rcx,%rax
mov    (%r14),%esi
mov    %r12d,%edi
mov    %rax,%rdx
call   1030 <fcntl@plt>
test   %rax,%rax
js     13ac <tcp_connect+0x20c>
mov    %r12d,%r15d
jmp    13b4 <tcp_connect+0x214>
mov    0x2cff(%rip),%rbp
mov    0x0(%rbp),%rax
mov    0x2d14(%rip),%rcx
cmp    (%rcx),%rax
jne    13ac <tcp_connect+0x20c>
movl   $0x5,0x28(%rsp)
movq   $0x0,0x30(%rsp)
lea    0x14(%rsp),%rbx
mov    %rbx,%rdi
call   1080 <FD_ZERO@plt>
mov    %r12d,%edi
mov    %rbx,%rsi
call   10b0 <FD_SET@plt>
mov    %r12d,%r14d
add    $0x1,%r14d
lea    0x28(%rsp),%r8
mov    %r14d,%edi
xor    %esi,%esi
mov    %rbx,%rdx
xor    %ecx,%ecx
call   1090 <select@plt>
test   %eax,%eax
jns    1356 <tcp_connect+0x1b6>
mov    0x2c84(%rip),%r13
mov    0x0(%r13),%rcx
cmp    %rcx,0x0(%rbp)
je     1356 <tcp_connect+0x1b6>
lea    0x14(%rsp),%rbx
lea    0x28(%rsp),%rbp
nopl   0x0(%rax,%rax,1)
mov    %r14d,%edi
xor    %esi,%esi
mov    %rbx,%rdx
xor    %ecx,%ecx
mov    %rbp,%r8
call   1090 <select@plt>
test   %eax,%eax
jns    1356 <tcp_connect+0x1b6>
mov    0x0(%r13),%rcx
mov    0x2c5f(%rip),%rdx
cmp    %rcx,(%rdx)
jne    1330 <tcp_connect+0x190>
test   %eax,%eax
jle    13ac <tcp_connect+0x20c>
movl   $0x4,0xc(%rsp)
mov    0x2c7f(%rip),%rax
mov    (%rax),%esi
mov    0x2c36(%rip),%rax
mov    (%rax),%edx
lea    0x10(%rsp),%rcx
lea    0xc(%rsp),%r8
mov    %r12d,%edi
call   1060 <getsockopt@plt>
test   %rax,%rax
js     13ac <tcp_connect+0x20c>
cmpl   $0x0,0x10(%rsp)
jne    13ac <tcp_connect+0x20c>
mov    0x2c07(%rip),%rbx
mov    0x2c20(%rip),%r14
mov    0x2c31(%rip),%rbp
jmp    126c <tcp_connect+0xcc>
mov    %r12d,%edi
call   1050 <close@plt>
mov    %r15d,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.hostent = type { i64 }
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.timeval = type { i32, i64 }
%struct.in_addr = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@F_GETFL = global i32 0
@O_NONBLOCK = global i64 0
@F_SETFL = global i32 0
@errno = global i64 0
@EINPROGRESS = global i64 0
@EINTR = global i64 0
@SOL_SOCKET = global i32 0
@SO_ERROR = global i32 0
define i32 @tcp_connect(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca %struct.hostent*
%7 = alloca i32
%8 = alloca %struct.sockaddr_in
%9 = alloca i64
%10 = alloca i32
%11 = alloca i32
%12 = alloca %struct.timeval
%13 = alloca i32
%14 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i64 0, i64* %9
%15 = load i8*, i8** %4
%16 = icmp eq i8* %15, null
br i1 %16, label %17, label %18
17: ; preds = %2
store i32 -1, i32* %3
br label %147
18: ; preds = %2
%19 = load i8*, i8** %4
%20 = call %struct.hostent* @gethostbyname(i8* %19)
store %struct.hostent* %20, %struct.hostent** %6
%21 = load %struct.hostent*, %struct.hostent** %6
%22 = icmp eq %struct.hostent* %21, null
br i1 %22, label %23, label %24
23: ; preds = %18
store i32 -1, i32* %3
br label %147
24: ; preds = %18
%25 = load i32, i32* @AF_INET
%26 = load i32, i32* @SOCK_STREAM
%27 = call i32 @socket(i32 %25, i32 %26, i32 0)
store i32 %27, i32* %7
%28 = load i32, i32* %7
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %31
30: ; preds = %24
store i32 -1, i32* %3
br label %147
31: ; preds = %24
%32 = load i32, i32* @AF_INET
%33 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 2
store i32 %32, i32* %33
%34 = load %struct.hostent*, %struct.hostent** %6
%35 = getelementptr inbounds %struct.hostent, %struct.hostent* %34, i32 0, i32 0
%36 = load i64, i64* %35
%37 = inttoptr i64 %36 to %struct.in_addr*
%38 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %37, i32 0, i32 0
%39 = load i32, i32* %38
%40 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 1
%41 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %40, i32 0, i32 0
store i32 %39, i32* %41
%42 = load i32, i32* %5
%43 = call i32 @htons(i32 %42)
%44 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 0
store i32 %43, i32* %44
store i64 0, i64* %9
%45 = load i32, i32* %7
%46 = load i32, i32* @F_GETFL
%47 = call i64 @fcntl(i32 %45, i32 %46, i64 0)
store i64 %47, i64* %9
%48 = icmp slt i64 %47, 0
br i1 %48, label %49, label %52
49: ; preds = %31
%50 = load i32, i32* %7
%51 = call i32 @close(i32 %50)
store i32 -1, i32* %3
br label %147
52: ; preds = %31
%53 = load i64, i64* @O_NONBLOCK
%54 = load i64, i64* %9
%55 = or i64 %54, %53
store i64 %55, i64* %9
%56 = load i32, i32* %7
%57 = load i32, i32* @F_SETFL
%58 = load i64, i64* %9
%59 = call i64 @fcntl(i32 %56, i32 %57, i64 %58)
%60 = icmp slt i64 %59, 0
br i1 %60, label %61, label %64
61: ; preds = %52
%62 = load i32, i32* %7
%63 = call i32 @close(i32 %62)
store i32 -1, i32* %3
br label %147
64: ; preds = %52
%65 = load i32, i32* %7
%66 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%67 = call i32 @connect(i32 %65, %struct.sockaddr* %66, i32 12)
store i32 %67, i32* %10
%68 = load i32, i32* %10
%69 = icmp slt i32 %68, 0
br i1 %69, label %70, label %124
70: ; preds = %64
%71 = load i64, i64* @errno
%72 = load i64, i64* @EINPROGRESS
%73 = icmp eq i64 %71, %72
br i1 %73, label %74, label %120
74: ; preds = %70
%75 = getelementptr inbounds %struct.timeval, %struct.timeval* %12, i32 0, i32 0
store i32 5, i32* %75
%76 = getelementptr inbounds %struct.timeval, %struct.timeval* %12, i32 0, i32 1
store i64 0, i64* %76
%77 = call i32 @FD_ZERO(i32* %11)
%78 = load i32, i32* %7
%79 = call i32 @FD_SET(i32 %78, i32* %11)
%80 = load i32, i32* %7
%81 = add nsw i32 %80, 1
%82 = call i32 @select(i32 %81, i32* null, i32* %11, i32* null, %struct.timeval* %12)
store i32 %82, i32* %10
br label %83
83: ; preds = %92, %74
%84 = load i32, i32* %10
%85 = icmp slt i32 %84, 0
br i1 %85, label %86, label %90
86: ; preds = %83
%87 = load i64, i64* @errno
%88 = load i64, i64* @EINTR
%89 = icmp ne i64 %87, %88
br label %90
90: ; preds = %86, %83
%91 = phi i1 [ false, %83 ], [ %89, %86 ]
br i1 %91, label %92, label %96
92: ; preds = %90
%93 = load i32, i32* %7
%94 = add nsw i32 %93, 1
%95 = call i32 @select(i32 %94, i32* null, i32* %11, i32* null, %struct.timeval* %12)
store i32 %95, i32* %10
br label %83
96: ; preds = %90
%97 = load i32, i32* %10
%98 = icmp sgt i32 %97, 0
br i1 %98, label %99, label %116
99: ; preds = %96
store i32 4, i32* %14
%100 = load i32, i32* %7
%101 = load i32, i32* @SOL_SOCKET
%102 = load i32, i32* @SO_ERROR
%103 = bitcast i32* %13 to i8*
%104 = call i64 @getsockopt(i32 %100, i32 %101, i32 %102, i8* %103, i32* %14)
%105 = icmp slt i64 %104, 0
br i1 %105, label %106, label %109
106: ; preds = %99
%107 = load i32, i32* %7
%108 = call i32 @close(i32 %107)
store i32 -1, i32* %3
br label %147
109: ; preds = %99
%110 = load i32, i32* %13
%111 = icmp ne i32 %110, 0
br i1 %111, label %112, label %115
112: ; preds = %109
%113 = load i32, i32* %7
%114 = call i32 @close(i32 %113)
store i32 -1, i32* %3
br label %147
115: ; preds = %109
br label %119
116: ; preds = %96
%117 = load i32, i32* %7
%118 = call i32 @close(i32 %117)
store i32 -1, i32* %3
br label %147
119: ; preds = %115
br label %123
120: ; preds = %70
%121 = load i32, i32* %7
%122 = call i32 @close(i32 %121)
store i32 -1, i32* %3
br label %147
123: ; preds = %119
br label %124
124: ; preds = %123, %64
%125 = load i32, i32* %7
%126 = load i32, i32* @F_GETFL
%127 = call i64 @fcntl(i32 %125, i32 %126, i64 0)
store i64 %127, i64* %9
%128 = icmp slt i64 %127, 0
br i1 %128, label %129, label %132
129: ; preds = %124
%130 = load i32, i32* %7
%131 = call i32 @close(i32 %130)
store i32 -1, i32* %3
br label %147
132: ; preds = %124
%133 = load i64, i64* @O_NONBLOCK
%134 = xor i64 %133, -1
%135 = load i64, i64* %9
%136 = and i64 %135, %134
store i64 %136, i64* %9
%137 = load i32, i32* %7
%138 = load i32, i32* @F_SETFL
%139 = load i64, i64* %9
%140 = call i64 @fcntl(i32 %137, i32 %138, i64 %139)
%141 = icmp slt i64 %140, 0
br i1 %141, label %142, label %145
142: ; preds = %132
%143 = load i32, i32* %7
%144 = call i32 @close(i32 %143)
store i32 -1, i32* %3
br label %147
145: ; preds = %132
%146 = load i32, i32* %7
store i32 %146, i32* %3
br label %147
147: ; preds = %145, %142, %129, %120, %116, %112, %106, %61, %49, %30, %23, %17
%148 = load i32, i32* %3
ret i32 %148
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.timeval = type { i32, i64 }
%struct.hostent = type { i64 }
%struct.in_addr = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@F_GETFL = global i32 0
@O_NONBLOCK = global i64 0
@F_SETFL = global i32 0
@errno = global i64 0
@EINPROGRESS = global i64 0
@EINTR = global i64 0
@SOL_SOCKET = global i32 0
@SO_ERROR = global i32 0
define i32 @tcp_connect(i8* %0, i32 %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca i32
%5 = alloca %struct.timeval
%6 = alloca i32
%7 = alloca i32
%8 = bitcast %struct.sockaddr_in* %3 to i8*
%9 = icmp eq i8* %0, null
br i1 %9, label %106, label %10
10: ; preds = %2
%11 = tail call %struct.hostent* @gethostbyname(i8* %0)
%12 = icmp eq %struct.hostent* %11, null
br i1 %12, label %106, label %13
13: ; preds = %10
%14 = load i32, i32* @AF_INET
%15 = load i32, i32* @SOCK_STREAM
%16 = tail call i32 @socket(i32 %14, i32 %15, i32 0)
%17 = icmp slt i32 %16, 0
br i1 %17, label %106, label %18
18: ; preds = %13
%19 = load i32, i32* @AF_INET
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %19, i32* %20
%21 = getelementptr inbounds %struct.hostent, %struct.hostent* %11, i64 0, i32 0
%22 = load i64, i64* %21
%23 = inttoptr i64 %22 to %struct.in_addr*
%24 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %23, i64 0, i32 0
%25 = load i32, i32* %24
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1, i32 0
store i32 %25, i32* %26
%27 = tail call i32 @htons(i32 %1)
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %27, i32* %28
%29 = load i32, i32* @F_GETFL
%30 = tail call i64 @fcntl(i32 %16, i32 %29, i64 0)
%31 = icmp slt i64 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %18
%33 = tail call i32 @close(i32 %16)
br label %106
34: ; preds = %18
%35 = load i64, i64* @O_NONBLOCK
%36 = or i64 %35, %30
%37 = load i32, i32* @F_SETFL
%38 = tail call i64 @fcntl(i32 %16, i32 %37, i64 %36)
%39 = icmp slt i64 %38, 0
br i1 %39, label %40, label %42
40: ; preds = %34
%41 = tail call i32 @close(i32 %16)
br label %106
42: ; preds = %34
%43 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%44 = call i32 @connect(i32 %16, %struct.sockaddr* %43, i32 12)
%45 = icmp slt i32 %44, 0
br i1 %45, label %46, label %91
46: ; preds = %42
%47 = load i64, i64* @errno
%48 = load i64, i64* @EINPROGRESS
%49 = icmp eq i64 %47, %48
br i1 %49, label %50, label %89
50: ; preds = %46
%51 = bitcast i32* %4 to i8*
%52 = bitcast %struct.timeval* %5 to i8*
%53 = bitcast i32* %6 to i8*
%54 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i64 0, i32 0
store i32 5, i32* %54
%55 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i64 0, i32 1
store i64 0, i64* %55
%56 = call i32 @FD_ZERO(i32* %4)
%57 = call i32 @FD_SET(i32 %16, i32* %4)
%58 = add nuw nsw i32 %16, 1
%59 = call i32 @select(i32 %58, i32* null, i32* %4, i32* null, %struct.timeval* %5)
%60 = icmp slt i32 %59, 0
%61 = load i64, i64* @errno
%62 = load i64, i64* @EINTR
%63 = icmp ne i64 %61, %62
%64 = select i1 %60, i1 %63, i1 false
br i1 %64, label %65, label %72
65: ; preds = %50, %65
%66 = call i32 @select(i32 %58, i32* null, i32* %4, i32* null, %struct.timeval* %5)
%67 = icmp slt i32 %66, 0
%68 = load i64, i64* @errno
%69 = load i64, i64* @EINTR
%70 = icmp ne i64 %68, %69
%71 = select i1 %67, i1 %70, i1 false
br i1 %71, label %65, label %72
72: ; preds = %65, %50
%73 = phi i32 [ %59, %50 ], [ %66, %65 ]
%74 = icmp sgt i32 %73, 0
br i1 %74, label %75, label %84
75: ; preds = %72
%76 = bitcast i32* %7 to i8*
store i32 4, i32* %7
%77 = load i32, i32* @SOL_SOCKET
%78 = load i32, i32* @SO_ERROR
%79 = call i64 @getsockopt(i32 %16, i32 %77, i32 %78, i8* %53, i32* %7)
%80 = icmp sgt i64 %79, -1
%81 = load i32, i32* %6
%82 = icmp eq i32 %81, 0
%83 = select i1 %80, i1 %82, i1 false
br i1 %83, label %88, label %86
84: ; preds = %72
%85 = call i32 @close(i32 %16)
br label %106
86: ; preds = %75
%87 = call i32 @close(i32 %16)
br label %106
88: ; preds = %75
br label %91
89: ; preds = %46
%90 = call i32 @close(i32 %16)
br label %106
91: ; preds = %88, %42
%92 = load i32, i32* @F_GETFL
%93 = call i64 @fcntl(i32 %16, i32 %92, i64 0)
%94 = icmp slt i64 %93, 0
br i1 %94, label %95, label %97
95: ; preds = %91
%96 = call i32 @close(i32 %16)
br label %106
97: ; preds = %91
%98 = load i64, i64* @O_NONBLOCK
%99 = xor i64 %98, -1
%100 = and i64 %93, %99
%101 = load i32, i32* @F_SETFL
%102 = call i64 @fcntl(i32 %16, i32 %101, i64 %100)
%103 = icmp slt i64 %102, 0
br i1 %103, label %104, label %106
104: ; preds = %97
%105 = call i32 @close(i32 %16)
br label %106
106: ; preds = %86, %84, %97, %13, %10, %2, %104, %95, %89, %40, %32
%107 = phi i32 [ -1, %32 ], [ -1, %40 ], [ -1, %95 ], [ -1, %104 ], [ -1, %89 ], [ -1, %2 ], [ -1, %10 ], [ -1, %13 ], [ %16, %97 ], [ -1, %84 ], [ -1, %86 ]
ret i32 %107
}