#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 */
typedef int /*<<< orphan*/ timeout;
struct timeval {
  int tv_sec;
  scalar_t__ tv_usec;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_addr;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ address;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
scalar_t__ EINPROGRESS;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_SNDTIMEO;
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ bzero(struct sockaddr_in *, int);
int connect(int, struct sockaddr *, int);
scalar_t__ errno;
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_pton(int /*<<< orphan*/, char const *,
                             int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *);
int setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, struct timeval *,
               int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int timeout_connect(const char *ip, int port, int time) {
  int ret = 0;
  struct sockaddr_in address;
  bzero(&address, sizeof(address));
  address.sin_family = AF_INET;
  inet_pton(AF_INET, ip, &address.sin_addr);
  address.sin_port = htons(port);
  int sockfd = socket(PF_INET, SOCK_STREAM, 0);
  assert(sockfd >= 0);
  struct timeval timeout;
  timeout.tv_sec = time;
  timeout.tv_usec = 0;
  socklen_t len = sizeof(timeout);
  ret = setsockopt(sockfd, SOL_SOCKET, SO_SNDTIMEO, &timeout, len);
  assert(ret != -1);
  ret = connect(sockfd, (struct sockaddr *)&address, sizeof(address));
  if (ret == -1) {
    if (errno == EINPROGRESS) {
      printf("connecting timeout\n");
      return -1;
    }
    printf("error occur when connecting to server\n");
    return -1;
  }
  return sockfd;
}


x86 assembly (clang, -O0):
<timeout_connect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
lea    -0x28(%rbp),%rdi
mov    $0xc,%esi
call   1080 <bzero@plt>
mov    0x2e2a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2e1e(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rsi
lea    -0x28(%rbp),%rdx
add    $0x4,%rdx
call   1070 <inet_pton@plt>
mov    -0x14(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x28(%rbp)
mov    0x2dd1(%rip),%rax
mov    (%rax),%edi
mov    0x2dd8(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
setge  %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x40(%rbp)
movq   $0x0,-0x38(%rbp)
movl   $0x10,-0x44(%rbp)
mov    -0x2c(%rbp),%edi
mov    0x2dbc(%rip),%rax
mov    (%rax),%esi
mov    0x2dc3(%rip),%rax
mov    (%rax),%edx
mov    -0x44(%rbp),%r8d
lea    -0x40(%rbp),%rcx
call   1030 <setsockopt@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    -0x2c(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0xc,%edx
call   1090 <connect@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    12b8 <timeout_connect+0x138>
mov    0x2d43(%rip),%rax
mov    (%rax),%rax
mov    0x2d51(%rip),%rcx
cmp    (%rcx),%rax
jne    12a0 <timeout_connect+0x120>
lea    0xd71(%rip),%rdi
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12be <timeout_connect+0x13e>
lea    0xd6d(%rip),%rdi
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12be <timeout_connect+0x13e>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<timeout_connect>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %edx,%r15d
mov    %esi,%ebp
mov    %rdi,%rbx
lea    0x8(%rsp),%r14
mov    %r14,%rdi
mov    $0xc,%esi
call   1080 <bzero@plt>
mov    0x2e2d(%rip),%rax
mov    (%rax),%edi
mov    %edi,0x10(%rsp)
lea    0xc(%rsp),%rdx
mov    %rbx,%rsi
call   1070 <inet_pton@plt>
mov    %ebp,%edi
call   1050 <htons@plt>
mov    %eax,0x8(%rsp)
mov    0x2de0(%rip),%rax
mov    (%rax),%edi
mov    0x2de7(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,%ebp
mov    %eax,%edi
not    %edi
shr    $0x1f,%edi
call   1060 <assert@plt>
mov    %r15d,0x18(%rsp)
movq   $0x0,0x20(%rsp)
mov    0x2ddb(%rip),%rax
mov    (%rax),%esi
mov    0x2de2(%rip),%rax
mov    (%rax),%edx
lea    0x18(%rsp),%rcx
mov    %ebp,%edi
mov    $0x10,%r8d
call   1040 <setsockopt@plt>
xor    %edi,%edi
cmp    $0xffffffff,%eax
setne  %dil
call   1060 <assert@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0xc,%edx
call   1090 <connect@plt>
cmp    $0xffffffff,%eax
jne    1274 <timeout_connect+0xf4>
mov    0x2d6d(%rip),%rax
mov    (%rax),%rax
mov    0x2d7b(%rip),%rcx
cmp    (%rcx),%rax
lea    0xdc7(%rip),%rax
lea    0xd9a(%rip),%rdi
cmove  %rax,%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.timeval = type { i32, i64 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@SOL_SOCKET = global i32 0
@SO_SNDTIMEO = global i32 0
@errno = global i64 0
@EINPROGRESS = global i64 0
@.str = [20 x i8] c"connecting timeout\0A\00"
@.str.1 = [39 x i8] c"error occur when connecting to server\0A\00"
define i32 @timeout_connect(i8* %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca %struct.sockaddr_in
%10 = alloca i32
%11 = alloca %struct.timeval
%12 = alloca i32
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i32 0, i32* %8
%13 = call i32 @bzero(%struct.sockaddr_in* %9, i32 12)
%14 = load i32, i32* @AF_INET
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 2
store i32 %14, i32* %15
%16 = load i32, i32* @AF_INET
%17 = load i8*, i8** %5
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 1
%19 = call i32 @inet_pton(i32 %16, i8* %17, i32* %18)
%20 = load i32, i32* %6
%21 = call i32 @htons(i32 %20)
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 0
store i32 %21, i32* %22
%23 = load i32, i32* @PF_INET
%24 = load i32, i32* @SOCK_STREAM
%25 = call i32 @socket(i32 %23, i32 %24, i32 0)
store i32 %25, i32* %10
%26 = load i32, i32* %10
%27 = icmp sge i32 %26, 0
%28 = zext i1 %27 to i32
%29 = call i32 @assert(i32 %28)
%30 = load i32, i32* %7
%31 = getelementptr inbounds %struct.timeval, %struct.timeval* %11, i32 0, i32 0
store i32 %30, i32* %31
%32 = getelementptr inbounds %struct.timeval, %struct.timeval* %11, i32 0, i32 1
store i64 0, i64* %32
store i32 16, i32* %12
%33 = load i32, i32* %10
%34 = load i32, i32* @SOL_SOCKET
%35 = load i32, i32* @SO_SNDTIMEO
%36 = load i32, i32* %12
%37 = call i32 @setsockopt(i32 %33, i32 %34, i32 %35, %struct.timeval* %11, i32 %36)
store i32 %37, i32* %8
%38 = load i32, i32* %8
%39 = icmp ne i32 %38, -1
%40 = zext i1 %39 to i32
%41 = call i32 @assert(i32 %40)
%42 = load i32, i32* %10
%43 = bitcast %struct.sockaddr_in* %9 to %struct.sockaddr*
%44 = call i32 @connect(i32 %42, %struct.sockaddr* %43, i32 12)
store i32 %44, i32* %8
%45 = load i32, i32* %8
%46 = icmp eq i32 %45, -1
br i1 %46, label %47, label %55
47: ; preds = %3
%48 = load i64, i64* @errno
%49 = load i64, i64* @EINPROGRESS
%50 = icmp eq i64 %48, %49
br i1 %50, label %51, label %53
51: ; preds = %47
%52 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %4
br label %57
53: ; preds = %47
%54 = call i32 @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %4
br label %57
55: ; preds = %3
%56 = load i32, i32* %10
store i32 %56, i32* %4
br label %57
57: ; preds = %55, %53, %51
%58 = load i32, i32* %4
ret i32 %58
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.timeval = type { i32, i64 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@SOL_SOCKET = global i32 0
@SO_SNDTIMEO = global i32 0
@errno = global i64 0
@EINPROGRESS = global i64 0
@str = [38 x i8] c"error occur when connecting to server\00"
@str.2 = [19 x i8] c"connecting timeout\00"
define i32 @timeout_connect(i8* %0, i32 %1, i32 %2) {
%4 = alloca %struct.sockaddr_in
%5 = alloca %struct.timeval
%6 = bitcast %struct.sockaddr_in* %4 to i8*
%7 = call i32 @bzero(%struct.sockaddr_in* %4, i32 12)
%8 = load i32, i32* @AF_INET
%9 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2
store i32 %8, i32* %9
%10 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1
%11 = call i32 @inet_pton(i32 %8, i8* %0, i32* %10)
%12 = call i32 @htons(i32 %1)
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0
store i32 %12, i32* %13
%14 = load i32, i32* @PF_INET
%15 = load i32, i32* @SOCK_STREAM
%16 = call i32 @socket(i32 %14, i32 %15, i32 0)
%17 = xor i32 %16, -1
%18 = lshr i32 %17, 31
%19 = call i32 @assert(i32 %18)
%20 = bitcast %struct.timeval* %5 to i8*
%21 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i64 0, i32 0
store i32 %2, i32* %21
%22 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i64 0, i32 1
store i64 0, i64* %22
%23 = load i32, i32* @SOL_SOCKET
%24 = load i32, i32* @SO_SNDTIMEO
%25 = call i32 @setsockopt(i32 %16, i32 %23, i32 %24, %struct.timeval* %5, i32 16)
%26 = icmp ne i32 %25, -1
%27 = zext i1 %26 to i32
%28 = call i32 @assert(i32 %27)
%29 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%30 = call i32 @connect(i32 %16, %struct.sockaddr* %29, i32 12)
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %38
32: ; preds = %3
%33 = load i64, i64* @errno
%34 = load i64, i64* @EINPROGRESS
%35 = icmp eq i64 %33, %34
%36 = select i1 %35, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @str.2, i64 0, i64 0), i8* getelementptr inbounds ([38 x i8], [38 x i8]* @str, i64 0, i64 0)
%37 = call i32 @puts(i8* dereferenceable(1) %36)
br label %38
38: ; preds = %32, %3
%39 = phi i32 [ %16, %3 ], [ -1, %32 ]
ret i32 %39
}