#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__ {
  scalar_t__ s_addr;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
struct in_addr {
  scalar_t__ s_addr;
};
struct hostent {
  scalar_t__ h_addr;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ n;
typedef int /*<<< orphan*/ fd_set;
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
scalar_t__ EINPROGRESS;
scalar_t__ FD_ISSET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
scalar_t__ INADDR_NONE;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_ERROR;
int connect(int, struct sockaddr *, int);
scalar_t__ errno;
struct hostent *gethostbyname(char const *);
int /*<<< orphan*/ getsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              int *, int *);
int /*<<< orphan*/ htons(unsigned short);
scalar_t__ inet_addr(char const *);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int select(int, int /*<<< orphan*/, int /*<<< orphan*/ *, int /*<<< orphan*/,
           struct timeval *);
scalar_t__ sock_set_blocking(int *, int /*<<< orphan*/);
int sock_connect(int *fd, const char *host, unsigned short port,
                 int timeout_secs) {
  int n = 0;
  socklen_t len = sizeof(n);
  struct sockaddr_in addr;
  struct hostent *lphost = NULL;
  fd_set writefds;
  struct timeval timeout;
  memset(&addr, 0, sizeof(addr));
  addr.sin_family = AF_INET;
  addr.sin_addr.s_addr = inet_addr(host);
  if (addr.sin_addr.s_addr == INADDR_NONE) {
    lphost = gethostbyname(host);
    if (lphost) {
      addr.sin_addr.s_addr = ((struct in_addr *)lphost->h_addr)->s_addr;
      lphost = NULL;
    } else
      return -1;
  }
  addr.sin_port = htons(port);
  if (sock_set_blocking(fd, 0))
    return -1;
  n = connect((*fd), (struct sockaddr *)&addr, sizeof(struct sockaddr_in));
  if (n == -1) {
    if (errno == EINPROGRESS) {
      FD_ZERO(&writefds);
      FD_SET((*fd), &writefds);
      timeout.tv_sec = timeout_secs;
      timeout.tv_usec = 0;
      n = select(((*fd) + 1), 0, &writefds, 0, &timeout);
      if ((n > 0) && FD_ISSET((*fd), &writefds)) {
        getsockopt((*fd), SOL_SOCKET, SO_ERROR, &n, &len);
        return (n == 0) ? 0 : -1;
      }
    } else
      return -1;
  } else
    return 0;
  return -1;
}


x86 assembly (clang, -O0):
<sock_connect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %dx,%ax
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %ax,-0x1a(%rbp)
mov    %ecx,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
movl   $0x4,-0x28(%rbp)
movq   $0x0,-0x48(%rbp)
lea    -0x40(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1050 <memset@plt>
mov    0x2de9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
call   1070 <inet_addr@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
mov    0x2dbc(%rip),%rcx
cmp    (%rcx),%rax
jne    1261 <sock_connect+0xb1>
mov    -0x18(%rbp),%rdi
call   1080 <gethostbyname@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
je     1250 <sock_connect+0xa0>
mov    -0x48(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%rax
mov    %rax,-0x38(%rbp)
movq   $0x0,-0x48(%rbp)
jmp    125c <sock_connect+0xac>
movl   $0xffffffff,-0x4(%rbp)
jmp    1395 <sock_connect+0x1e5>
jmp    1261 <sock_connect+0xb1>
movzwl -0x1a(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x40(%rbp)
mov    -0x10(%rbp),%rdi
xor    %esi,%esi
call   10b0 <sock_set_blocking@plt>
cmp    $0x0,%rax
je     128e <sock_connect+0xde>
movl   $0xffffffff,-0x4(%rbp)
jmp    1395 <sock_connect+0x1e5>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
lea    -0x40(%rbp),%rsi
mov    $0x18,%edx
call   10c0 <connect@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    1382 <sock_connect+0x1d2>
mov    0x2d0a(%rip),%rax
mov    (%rax),%rax
mov    0x2d18(%rip),%rcx
cmp    (%rcx),%rax
jne    1371 <sock_connect+0x1c1>
lea    -0x4c(%rbp),%rdi
call   1090 <FD_ZERO@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
lea    -0x4c(%rbp),%rsi
call   10d0 <FD_SET@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x60(%rbp)
movq   $0x0,-0x58(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
add    $0x1,%edi
xor    %ecx,%ecx
lea    -0x4c(%rbp),%rdx
lea    -0x60(%rbp),%r8
mov    %ecx,%esi
call   10a0 <select@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jle    136c <sock_connect+0x1bc>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
lea    -0x4c(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%rax
je     136c <sock_connect+0x1bc>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
mov    0x2cac(%rip),%rax
mov    (%rax),%esi
mov    0x2c73(%rip),%rax
mov    (%rax),%edx
lea    -0x24(%rbp),%rcx
lea    -0x28(%rbp),%r8
call   1060 <getsockopt@plt>
mov    -0x24(%rbp),%edx
mov    $0xffffffff,%eax
xor    %ecx,%ecx
cmp    $0x0,%edx
cmove  %ecx,%eax
mov    %eax,-0x4(%rbp)
jmp    1395 <sock_connect+0x1e5>
jmp    137d <sock_connect+0x1cd>
movl   $0xffffffff,-0x4(%rbp)
jmp    1395 <sock_connect+0x1e5>
jmp    138e <sock_connect+0x1de>
movl   $0x0,-0x4(%rbp)
jmp    1395 <sock_connect+0x1e5>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sock_connect>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x38,%rsp
mov    %ecx,%r14d
mov    %edx,%ebp
mov    %rsi,%rbx
mov    %rdi,%r15
movl   $0x4,0xc(%rsp)
lea    0x10(%rsp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1050 <memset@plt>
mov    0x2dfb(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rsp)
mov    %rbx,%rdi
call   1070 <inet_addr@plt>
mov    %rax,0x18(%rsp)
mov    0x2dd1(%rip),%rcx
cmp    (%rcx),%rax
jne    121c <sock_connect+0x6c>
mov    %rbx,%rdi
call   1080 <gethostbyname@plt>
test   %rax,%rax
je     1239 <sock_connect+0x89>
mov    (%rax),%rax
mov    (%rax),%rax
mov    %rax,0x18(%rsp)
movzwl %bp,%edi
call   1040 <htons@plt>
mov    %eax,0x10(%rsp)
xor    %ebp,%ebp
mov    %r15,%rdi
xor    %esi,%esi
call   10b0 <sock_set_blocking@plt>
test   %rax,%rax
je     124b <sock_connect+0x9b>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    (%r15),%edi
lea    0x10(%rsp),%rsi
mov    $0x18,%edx
call   10c0 <connect@plt>
cmp    $0xffffffff,%eax
jne    123e <sock_connect+0x8e>
mov    0x2d57(%rip),%rax
mov    (%rax),%rax
mov    0x2d65(%rip),%rcx
cmp    (%rcx),%rax
jne    1239 <sock_connect+0x89>
lea    0x8(%rsp),%rbx
mov    %rbx,%rdi
call   1090 <FD_ZERO@plt>
mov    (%r15),%edi
mov    %rbx,%rsi
call   10d0 <FD_SET@plt>
mov    %r14d,0x28(%rsp)
movq   $0x0,0x30(%rsp)
mov    (%r15),%edi
add    $0x1,%edi
lea    0x28(%rsp),%r8
xor    %esi,%esi
mov    %rbx,%rdx
xor    %ecx,%ecx
call   10a0 <select@plt>
mov    %eax,0x4(%rsp)
test   %eax,%eax
jle    1239 <sock_connect+0x89>
mov    (%r15),%edi
lea    0x8(%rsp),%rsi
call   1030 <FD_ISSET@plt>
mov    $0xffffffff,%ebp
test   %rax,%rax
je     123e <sock_connect+0x8e>
mov    (%r15),%edi
mov    0x2d02(%rip),%rax
mov    (%rax),%esi
mov    0x2cc9(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%rcx
lea    0xc(%rsp),%r8
call   1060 <getsockopt@plt>
xor    %eax,%eax
cmp    0x4(%rsp),%eax
sbb    %ebp,%ebp
jmp    123e <sock_connect+0x8e>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i64 }
%struct.hostent = type { i64 }
%struct.timeval = type { i32, i64 }
%struct.in_addr = type { i64 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@INADDR_NONE = global i64 0
@errno = global i64 0
@EINPROGRESS = global i64 0
@SOL_SOCKET = global i32 0
@SO_ERROR = global i32 0
define i32 @sock_connect(i32* %0, i8* %1, i16 zeroext %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i8*
%8 = alloca i16
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca %struct.sockaddr_in
%13 = alloca %struct.hostent*
%14 = alloca i32
%15 = alloca %struct.timeval
store i32* %0, i32** %6
store i8* %1, i8** %7
store i16 %2, i16* %8
store i32 %3, i32* %9
store i32 0, i32* %10
store i32 4, i32* %11
store %struct.hostent* null, %struct.hostent** %13
%16 = call i32 @memset(%struct.sockaddr_in* %12, i32 0, i32 24)
%17 = load i32, i32* @AF_INET
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %12, i32 0, i32 2
store i32 %17, i32* %18
%19 = load i8*, i8** %7
%20 = call i64 @inet_addr(i8* %19)
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %12, i32 0, i32 1
%22 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %21, i32 0, i32 0
store i64 %20, i64* %22
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %12, i32 0, i32 1
%24 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %23, i32 0, i32 0
%25 = load i64, i64* %24
%26 = load i64, i64* @INADDR_NONE
%27 = icmp eq i64 %25, %26
br i1 %27, label %28, label %44
28: ; preds = %4
%29 = load i8*, i8** %7
%30 = call %struct.hostent* @gethostbyname(i8* %29)
store %struct.hostent* %30, %struct.hostent** %13
%31 = load %struct.hostent*, %struct.hostent** %13
%32 = icmp ne %struct.hostent* %31, null
br i1 %32, label %33, label %42
33: ; preds = %28
%34 = load %struct.hostent*, %struct.hostent** %13
%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 i64, i64* %38
%40 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %12, i32 0, i32 1
%41 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %40, i32 0, i32 0
store i64 %39, i64* %41
store %struct.hostent* null, %struct.hostent** %13
br label %43
42: ; preds = %28
store i32 -1, i32* %5
br label %97
43: ; preds = %33
br label %44
44: ; preds = %43, %4
%45 = load i16, i16* %8
%46 = call i32 @htons(i16 zeroext %45)
%47 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %12, i32 0, i32 0
store i32 %46, i32* %47
%48 = load i32*, i32** %6
%49 = call i64 @sock_set_blocking(i32* %48, i32 0)
%50 = icmp ne i64 %49, 0
br i1 %50, label %51, label %52
51: ; preds = %44
store i32 -1, i32* %5
br label %97
52: ; preds = %44
%53 = load i32*, i32** %6
%54 = load i32, i32* %53
%55 = bitcast %struct.sockaddr_in* %12 to %struct.sockaddr*
%56 = call i32 @connect(i32 %54, %struct.sockaddr* %55, i32 24)
store i32 %56, i32* %10
%57 = load i32, i32* %10
%58 = icmp eq i32 %57, -1
br i1 %58, label %59, label %95
59: ; preds = %52
%60 = load i64, i64* @errno
%61 = load i64, i64* @EINPROGRESS
%62 = icmp eq i64 %60, %61
br i1 %62, label %63, label %93
63: ; preds = %59
%64 = call i32 @FD_ZERO(i32* %14)
%65 = load i32*, i32** %6
%66 = load i32, i32* %65
%67 = call i32 @FD_SET(i32 %66, i32* %14)
%68 = load i32, i32* %9
%69 = getelementptr inbounds %struct.timeval, %struct.timeval* %15, i32 0, i32 0
store i32 %68, i32* %69
%70 = getelementptr inbounds %struct.timeval, %struct.timeval* %15, i32 0, i32 1
store i64 0, i64* %70
%71 = load i32*, i32** %6
%72 = load i32, i32* %71
%73 = add nsw i32 %72, 1
%74 = call i32 @select(i32 %73, i32 0, i32* %14, i32 0, %struct.timeval* %15)
store i32 %74, i32* %10
%75 = load i32, i32* %10
%76 = icmp sgt i32 %75, 0
br i1 %76, label %77, label %92
77: ; preds = %63
%78 = load i32*, i32** %6
%79 = load i32, i32* %78
%80 = call i64 @FD_ISSET(i32 %79, i32* %14)
%81 = icmp ne i64 %80, 0
br i1 %81, label %82, label %92
82: ; preds = %77
%83 = load i32*, i32** %6
%84 = load i32, i32* %83
%85 = load i32, i32* @SOL_SOCKET
%86 = load i32, i32* @SO_ERROR
%87 = call i32 @getsockopt(i32 %84, i32 %85, i32 %86, i32* %10, i32* %11)
%88 = load i32, i32* %10
%89 = icmp eq i32 %88, 0
%90 = zext i1 %89 to i64
%91 = select i1 %89, i32 0, i32 -1
store i32 %91, i32* %5
br label %97
92: ; preds = %77, %63
br label %94
93: ; preds = %59
store i32 -1, i32* %5
br label %97
94: ; preds = %92
br label %96
95: ; preds = %52
store i32 0, i32* %5
br label %97
96: ; preds = %94
store i32 -1, i32* %5
br label %97
97: ; preds = %96, %95, %93, %82, %51, %42
%98 = load i32, i32* %5
ret i32 %98
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i64 }
%struct.timeval = type { i32, i64 }
%struct.hostent = type { i64 }
%struct.in_addr = type { i64 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@INADDR_NONE = global i64 0
@errno = global i64 0
@EINPROGRESS = global i64 0
@SOL_SOCKET = global i32 0
@SO_ERROR = global i32 0
define i32 @sock_connect(i32* %0, i8* %1, i16 zeroext %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.sockaddr_in
%8 = alloca i32
%9 = alloca %struct.timeval
%10 = bitcast i32* %5 to i8*
%11 = bitcast i32* %6 to i8*
store i32 4, i32* %6
%12 = bitcast %struct.sockaddr_in* %7 to i8*
%13 = bitcast i32* %8 to i8*
%14 = bitcast %struct.timeval* %9 to i8*
%15 = call i32 @memset(%struct.sockaddr_in* %7, i32 0, i32 24)
%16 = load i32, i32* @AF_INET
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i64 0, i32 2
store i32 %16, i32* %17
%18 = call i64 @inet_addr(i8* %1)
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i64 0, i32 1, i32 0
store i64 %18, i64* %19
%20 = load i64, i64* @INADDR_NONE
%21 = icmp eq i64 %18, %20
br i1 %21, label %22, label %31
22: ; preds = %4
%23 = call %struct.hostent* @gethostbyname(i8* %1)
%24 = icmp eq %struct.hostent* %23, null
br i1 %24, label %67, label %25
25: ; preds = %22
%26 = getelementptr inbounds %struct.hostent, %struct.hostent* %23, i64 0, i32 0
%27 = load i64, i64* %26
%28 = inttoptr i64 %27 to %struct.in_addr*
%29 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %28, i64 0, i32 0
%30 = load i64, i64* %29
store i64 %30, i64* %19
br label %31
31: ; preds = %25, %4
%32 = call i32 @htons(i16 zeroext %2)
%33 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i64 0, i32 0
store i32 %32, i32* %33
%34 = call i64 @sock_set_blocking(i32* %0, i32 0)
%35 = icmp eq i64 %34, 0
br i1 %35, label %36, label %67
36: ; preds = %31
%37 = load i32, i32* %0
%38 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%39 = call i32 @connect(i32 %37, %struct.sockaddr* %38, i32 24)
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %67
41: ; preds = %36
%42 = load i64, i64* @errno
%43 = load i64, i64* @EINPROGRESS
%44 = icmp eq i64 %42, %43
br i1 %44, label %45, label %67
45: ; preds = %41
%46 = call i32 @FD_ZERO(i32* %8)
%47 = load i32, i32* %0
%48 = call i32 @FD_SET(i32 %47, i32* %8)
%49 = getelementptr inbounds %struct.timeval, %struct.timeval* %9, i64 0, i32 0
store i32 %3, i32* %49
%50 = getelementptr inbounds %struct.timeval, %struct.timeval* %9, i64 0, i32 1
store i64 0, i64* %50
%51 = load i32, i32* %0
%52 = add nsw i32 %51, 1
%53 = call i32 @select(i32 %52, i32 0, i32* %8, i32 0, %struct.timeval* %9)
store i32 %53, i32* %5
%54 = icmp sgt i32 %53, 0
br i1 %54, label %55, label %67
55: ; preds = %45
%56 = load i32, i32* %0
%57 = call i64 @FD_ISSET(i32 %56, i32* %8)
%58 = icmp eq i64 %57, 0
br i1 %58, label %67, label %59
59: ; preds = %55
%60 = load i32, i32* %0
%61 = load i32, i32* @SOL_SOCKET
%62 = load i32, i32* @SO_ERROR
%63 = call i32 @getsockopt(i32 %60, i32 %61, i32 %62, i32* %5, i32* %6)
%64 = load i32, i32* %5
%65 = icmp ne i32 %64, 0
%66 = sext i1 %65 to i32
br label %67
67: ; preds = %45, %55, %36, %41, %31, %22, %59
%68 = phi i32 [ %66, %59 ], [ -1, %22 ], [ -1, %31 ], [ -1, %41 ], [ 0, %36 ], [ -1, %55 ], [ -1, %45 ]
ret i32 %68
}