#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 {
  int tv_sec;
  scalar_t__ tv_usec;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_family;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_addr;
};
struct sockaddr {
  int dummy;
};
struct in_addr {
  int /*<<< orphan*/ s_addr;
};
typedef int /*<<< orphan*/ sin;
typedef int /*<<< orphan*/ fd_set;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
scalar_t__ EINPROGRESS;
scalar_t__ EWOULDBLOCK;
scalar_t__ FD_ISSET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr *, int);
scalar_t__ errno;
int /*<<< orphan*/ htons(short);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ *, int /*<<< orphan*/ *, int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
scalar_t__ select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                  int /*<<< orphan*/ *, struct timeval *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int sockndelay(int, int);
int tconnect(struct in_addr const *paddr, int port, int timeout) {
  int sfd;
  struct sockaddr_in sin;
  fd_set wfds;
  struct timeval tv;
  if ((sfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
    perror("socket");
    return (-1);
  }
  memset(&sin, 0, sizeof(sin));
  memcpy(&sin.sin_addr, &paddr->s_addr, 4);
  sin.sin_port = htons((short int)port);
  sin.sin_family = AF_INET;
  if (sockndelay(sfd, 1) == -1) {
    perror("sockndelay");
    close(sfd);
    return (-1);
  }
  if (connect(sfd, (struct sockaddr *)&sin, sizeof(sin)) == 0) {
    sockndelay(sfd, 0);
    return (sfd);
  }
  if ((errno != EINPROGRESS) && (errno != EWOULDBLOCK)) {
    perror("connect");
    close(sfd);
    return (-1);
  }
  FD_ZERO(&wfds);
  FD_SET(sfd, &wfds);
  tv.tv_sec = timeout;
  tv.tv_usec = 0;
  if (select(sfd + 1, (fd_set *)0, &wfds, (fd_set *)0, &tv) <= 0) {
    perror("select");
    close(sfd);
    return (-1);
  }
  if (FD_ISSET(sfd, &wfds)) {
    sockndelay(sfd, 0);
    return (sfd);
  }
  close(sfd);
  return (-1);
}


x86 assembly (clang, -O0):
<tconnect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    0x2e0f(%rip),%rax
mov    (%rax),%edi
mov    0x2dee(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0xffffffff,%eax
jne    120f <tconnect+0x4f>
lea    0xe02(%rip),%rdi
call   10b0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13aa <tconnect+0x1ea>
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
lea    -0x28(%rbp),%rdi
add    $0x8,%rdi
mov    -0x10(%rbp),%rsi
mov    $0x4,%edx
call   1070 <memcpy@plt>
mov    -0x14(%rbp),%eax
movswl %ax,%edi
call   1040 <htons@plt>
mov    %eax,-0x24(%rbp)
mov    0x2d9e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x1c(%rbp),%edi
mov    $0x1,%esi
call   10a0 <sockndelay@plt>
cmp    $0xffffffff,%eax
jne    1285 <tconnect+0xc5>
lea    0xd9b(%rip),%rdi
call   10b0 <perror@plt>
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13aa <tconnect+0x1ea>
mov    -0x1c(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0xc,%edx
call   10c0 <connect@plt>
cmp    $0x0,%rax
jne    12b5 <tconnect+0xf5>
mov    -0x1c(%rbp),%edi
xor    %esi,%esi
call   10a0 <sockndelay@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    13aa <tconnect+0x1ea>
mov    0x2d0c(%rip),%rax
mov    (%rax),%rax
mov    0x2d1a(%rip),%rcx
cmp    (%rcx),%rax
je     1309 <tconnect+0x149>
mov    0x2cf2(%rip),%rax
mov    (%rax),%rax
mov    0x2ce0(%rip),%rcx
cmp    (%rcx),%rax
je     1309 <tconnect+0x149>
lea    0xd22(%rip),%rdi
call   10b0 <perror@plt>
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13aa <tconnect+0x1ea>
lea    -0x2c(%rbp),%rdi
call   1080 <FD_ZERO@plt>
mov    -0x1c(%rbp),%edi
lea    -0x2c(%rbp),%rsi
call   10d0 <FD_SET@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x40(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x1c(%rbp),%edi
add    $0x1,%edi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x2c(%rbp),%rdx
lea    -0x40(%rbp),%r8
mov    %rcx,%rsi
call   1090 <select@plt>
cmp    $0x0,%rax
jg     1370 <tconnect+0x1b0>
lea    0xcc3(%rip),%rdi
call   10b0 <perror@plt>
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13aa <tconnect+0x1ea>
mov    -0x1c(%rbp),%edi
lea    -0x2c(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%rax
je     139b <tconnect+0x1db>
mov    -0x1c(%rbp),%edi
xor    %esi,%esi
call   10a0 <sockndelay@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    13aa <tconnect+0x1ea>
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tconnect>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x30,%rsp
mov    %edx,%r14d
mov    %esi,%r15d
mov    %rdi,%rbp
mov    0x2e0c(%rip),%r12
mov    (%r12),%edi
mov    0x2de9(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
cmp    $0xffffffff,%eax
je     12dc <tconnect+0x11c>
mov    %eax,%ebx
lea    0x10(%rsp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
lea    0x18(%rsp),%rdi
mov    %rbp,%rsi
mov    $0x4,%edx
call   1070 <memcpy@plt>
movswl %r15w,%edi
call   1040 <htons@plt>
mov    %eax,0x14(%rsp)
mov    (%r12),%eax
mov    %eax,0x10(%rsp)
mov    %ebx,%edi
mov    $0x1,%esi
call   10a0 <sockndelay@plt>
cmp    $0xffffffff,%eax
je     12ea <tconnect+0x12a>
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   10c0 <connect@plt>
test   %rax,%rax
je     12d1 <tconnect+0x111>
mov    0x2d63(%rip),%rax
mov    (%rax),%rax
mov    0x2d71(%rip),%rcx
cmp    (%rcx),%rax
je     1280 <tconnect+0xc0>
mov    0x2d45(%rip),%rcx
cmp    (%rcx),%rax
jne    12f3 <tconnect+0x133>
lea    0xc(%rsp),%rbp
mov    %rbp,%rdi
call   1080 <FD_ZERO@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
call   10d0 <FD_SET@plt>
mov    %r14d,0x20(%rsp)
movq   $0x0,0x28(%rsp)
mov    %ebx,%edi
add    $0x1,%edi
lea    0x20(%rsp),%r8
xor    %esi,%esi
mov    %rbp,%rdx
xor    %ecx,%ecx
call   1090 <select@plt>
test   %rax,%rax
jle    12fc <tconnect+0x13c>
lea    0xc(%rsp),%rsi
mov    %ebx,%edi
call   1030 <FD_ISSET@plt>
test   %rax,%rax
je     1308 <tconnect+0x148>
mov    %ebx,%edi
xor    %esi,%esi
call   10a0 <sockndelay@plt>
jmp    1314 <tconnect+0x154>
lea    0xd1d(%rip),%rdi
call   10b0 <perror@plt>
jmp    130f <tconnect+0x14f>
lea    0xd16(%rip),%rdi
jmp    1303 <tconnect+0x143>
lea    0xd18(%rip),%rdi
jmp    1303 <tconnect+0x143>
lea    0xd17(%rip),%rdi
call   10b0 <perror@plt>
mov    %ebx,%edi
call   1060 <close@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.in_addr = type { i32 }
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.timeval = type { i32, i64 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [7 x i8] c"socket\00"
@.str.1 = [11 x i8] c"sockndelay\00"
@errno = global i64 0
@EINPROGRESS = global i64 0
@EWOULDBLOCK = global i64 0
@.str.2 = [8 x i8] c"connect\00"
@.str.3 = [7 x i8] c"select\00"
define i32 @tconnect(%struct.in_addr* %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca %struct.in_addr*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca %struct.sockaddr_in
%10 = alloca i32
%11 = alloca %struct.timeval
store %struct.in_addr* %0, %struct.in_addr** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%12 = load i32, i32* @AF_INET
%13 = load i32, i32* @SOCK_STREAM
%14 = call i32 @socket(i32 %12, i32 %13, i32 0)
store i32 %14, i32* %8
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %18
16: ; preds = %3
%17 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %4
br label %84
18: ; preds = %3
%19 = call i32 @memset(%struct.sockaddr_in* %9, i32 0, i32 12)
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 2
%21 = load %struct.in_addr*, %struct.in_addr** %5
%22 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %21, i32 0, i32 0
%23 = call i32 @memcpy(i32* %20, i32* %22, i32 4)
%24 = load i32, i32* %6
%25 = trunc i32 %24 to i16
%26 = call i32 @htons(i16 signext %25)
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 1
store i32 %26, i32* %27
%28 = load i32, i32* @AF_INET
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 0
store i32 %28, i32* %29
%30 = load i32, i32* %8
%31 = call i32 @sockndelay(i32 %30, i32 1)
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %37
33: ; preds = %18
%34 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%35 = load i32, i32* %8
%36 = call i32 @close(i32 %35)
store i32 -1, i32* %4
br label %84
37: ; preds = %18
%38 = load i32, i32* %8
%39 = bitcast %struct.sockaddr_in* %9 to %struct.sockaddr*
%40 = call i64 @connect(i32 %38, %struct.sockaddr* %39, i32 12)
%41 = icmp eq i64 %40, 0
br i1 %41, label %42, label %46
42: ; preds = %37
%43 = load i32, i32* %8
%44 = call i32 @sockndelay(i32 %43, i32 0)
%45 = load i32, i32* %8
store i32 %45, i32* %4
br label %84
46: ; preds = %37
%47 = load i64, i64* @errno
%48 = load i64, i64* @EINPROGRESS
%49 = icmp ne i64 %47, %48
br i1 %49, label %50, label %58
50: ; preds = %46
%51 = load i64, i64* @errno
%52 = load i64, i64* @EWOULDBLOCK
%53 = icmp ne i64 %51, %52
br i1 %53, label %54, label %58
54: ; preds = %50
%55 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
%56 = load i32, i32* %8
%57 = call i32 @close(i32 %56)
store i32 -1, i32* %4
br label %84
58: ; preds = %50, %46
%59 = call i32 @FD_ZERO(i32* %10)
%60 = load i32, i32* %8
%61 = call i32 @FD_SET(i32 %60, i32* %10)
%62 = load i32, i32* %7
%63 = getelementptr inbounds %struct.timeval, %struct.timeval* %11, i32 0, i32 0
store i32 %62, i32* %63
%64 = getelementptr inbounds %struct.timeval, %struct.timeval* %11, i32 0, i32 1
store i64 0, i64* %64
%65 = load i32, i32* %8
%66 = add nsw i32 %65, 1
%67 = call i64 @select(i32 %66, i32* null, i32* %10, i32* null, %struct.timeval* %11)
%68 = icmp sle i64 %67, 0
br i1 %68, label %69, label %73
69: ; preds = %58
%70 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%71 = load i32, i32* %8
%72 = call i32 @close(i32 %71)
store i32 -1, i32* %4
br label %84
73: ; preds = %58
%74 = load i32, i32* %8
%75 = call i64 @FD_ISSET(i32 %74, i32* %10)
%76 = icmp ne i64 %75, 0
br i1 %76, label %77, label %81
77: ; preds = %73
%78 = load i32, i32* %8
%79 = call i32 @sockndelay(i32 %78, i32 0)
%80 = load i32, i32* %8
store i32 %80, i32* %4
br label %84
81: ; preds = %73
%82 = load i32, i32* %8
%83 = call i32 @close(i32 %82)
store i32 -1, i32* %4
br label %84
84: ; preds = %81, %77, %69, %54, %42, %33, %16
%85 = load i32, i32* %4
ret i32 %85
}

LLVM IR (-O2):
%struct.in_addr = type { i32 }
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.timeval = type { i32, i64 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [7 x i8] c"socket\00"
@.str.1 = [11 x i8] c"sockndelay\00"
@errno = global i64 0
@EINPROGRESS = global i64 0
@EWOULDBLOCK = global i64 0
@.str.2 = [8 x i8] c"connect\00"
@.str.3 = [7 x i8] c"select\00"
define i32 @tconnect(%struct.in_addr* %0, i32 %1, i32 %2) {
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
%6 = alloca %struct.timeval
%7 = bitcast %struct.sockaddr_in* %4 to i8*
%8 = bitcast i32* %5 to i8*
%9 = bitcast %struct.timeval* %6 to i8*
%10 = load i32, i32* @AF_INET
%11 = load i32, i32* @SOCK_STREAM
%12 = tail call i32 @socket(i32 %10, i32 %11, i32 0)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %16
14: ; preds = %3
%15 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %65
16: ; preds = %3
%17 = call i32 @memset(%struct.sockaddr_in* %4, i32 0, i32 12)
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2
%19 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %0, i64 0, i32 0
%20 = call i32 @memcpy(i32* %18, i32* %19, i32 4)
%21 = trunc i32 %1 to i16
%22 = call i32 @htons(i16 signext %21)
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1
store i32 %22, i32* %23
%24 = load i32, i32* @AF_INET
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0
store i32 %24, i32* %25
%26 = call i32 @sockndelay(i32 %12, i32 1)
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %31
28: ; preds = %16
%29 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%30 = call i32 @close(i32 %12)
br label %65
31: ; preds = %16
%32 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%33 = call i64 @connect(i32 %12, %struct.sockaddr* %32, i32 12)
%34 = icmp eq i64 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %31
%36 = call i32 @sockndelay(i32 %12, i32 0)
br label %65
37: ; preds = %31
%38 = load i64, i64* @errno
%39 = load i64, i64* @EINPROGRESS
%40 = icmp eq i64 %38, %39
%41 = load i64, i64* @EWOULDBLOCK
%42 = icmp eq i64 %38, %41
%43 = select i1 %40, i1 true, i1 %42
br i1 %43, label %47, label %44
44: ; preds = %37
%45 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
%46 = call i32 @close(i32 %12)
br label %65
47: ; preds = %37
%48 = call i32 @FD_ZERO(i32* %5)
%49 = call i32 @FD_SET(i32 %12, i32* %5)
%50 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i64 0, i32 0
store i32 %2, i32* %50
%51 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i64 0, i32 1
store i64 0, i64* %51
%52 = add nuw nsw i32 %12, 1
%53 = call i64 @select(i32 %52, i32* null, i32* %5, i32* null, %struct.timeval* %6)
%54 = icmp slt i64 %53, 1
br i1 %54, label %55, label %58
55: ; preds = %47
%56 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%57 = call i32 @close(i32 %12)
br label %65
58: ; preds = %47
%59 = call i64 @FD_ISSET(i32 %12, i32* %5)
%60 = icmp eq i64 %59, 0
br i1 %60, label %63, label %61
61: ; preds = %58
%62 = call i32 @sockndelay(i32 %12, i32 0)
br label %65
63: ; preds = %58
%64 = call i32 @close(i32 %12)
br label %65
65: ; preds = %63, %61, %55, %44, %35, %28, %14
%66 = phi i32 [ -1, %14 ], [ -1, %28 ], [ %12, %35 ], [ -1, %44 ], [ -1, %55 ], [ %12, %61 ], [ -1, %63 ]
ret i32 %66
}