#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 dummy;
};
struct sockaddr {
  int dummy;
};
typedef int socklen_t;

/* Variables and functions */
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_RCVTIMEO;
int connect(int, struct sockaddr *, int);
scalar_t__ getsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, char *,
                      int *);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              char *, int);
int connectWithTimeout(int sfd, struct sockaddr *addr, int addrlen,
                       struct timeval *timeout) {
  struct timeval sv;
  socklen_t svlen = sizeof sv;
  int ret;
  if (!timeout)
    return connect(sfd, addr, addrlen);
  if (getsockopt(sfd, SOL_SOCKET, SO_RCVTIMEO, (char *)&sv, &svlen) < 0)
    return -1;
  if (setsockopt(sfd, SOL_SOCKET, SO_RCVTIMEO, (char *)timeout,
                 sizeof *timeout) < 0)
    return -1;
  ret = connect(sfd, addr, addrlen);
  setsockopt(sfd, SOL_SOCKET, SO_RCVTIMEO, (char *)&sv, sizeof sv);
  return ret;
}


x86 assembly (clang, -O0):
<connectWithTimeout>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
movl   $0x4,-0x2c(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    116f <connectWithTimeout+0x3f>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1050 <connect@plt>
mov    %eax,-0x4(%rbp)
jmp    121c <connectWithTimeout+0xec>
mov    -0x8(%rbp),%edi
mov    0x2e6f(%rip),%rax
mov    (%rax),%esi
mov    0x2e56(%rip),%rax
mov    (%rax),%edx
lea    -0x28(%rbp),%rcx
lea    -0x2c(%rbp),%r8
call   1040 <getsockopt@plt>
cmp    $0x0,%rax
jge    11a7 <connectWithTimeout+0x77>
movl   $0xffffffff,-0x4(%rbp)
jmp    121c <connectWithTimeout+0xec>
mov    -0x8(%rbp),%edi
mov    0x2e37(%rip),%rax
mov    (%rax),%esi
mov    0x2e1e(%rip),%rax
mov    (%rax),%edx
mov    -0x20(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0x0,%eax
jge    11e0 <connectWithTimeout+0xb0>
movl   $0xffffffff,-0x4(%rbp)
jmp    121c <connectWithTimeout+0xec>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1050 <connect@plt>
mov    %eax,-0x30(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2dec(%rip),%rax
mov    (%rax),%esi
mov    0x2dd3(%rip),%rax
mov    (%rax),%edx
lea    -0x28(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    -0x30(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<connectWithTimeout>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%r14d
mov    %rsi,%r15
mov    %edi,%ebx
movl   $0x4,0x4(%rsp)
test   %rcx,%rcx
je     11e5 <connectWithTimeout+0xb5>
mov    %rcx,%rbp
mov    0x2e8a(%rip),%rax
mov    (%rax),%esi
mov    0x2e71(%rip),%r13
mov    0x0(%r13),%edx
mov    %rsp,%rcx
lea    0x4(%rsp),%r8
mov    %ebx,%edi
call   1040 <getsockopt@plt>
mov    $0xffffffff,%r12d
test   %rax,%rax
js     11d3 <connectWithTimeout+0xa3>
mov    0x2e5c(%rip),%rax
mov    (%rax),%esi
mov    0x0(%r13),%edx
mov    %ebx,%edi
mov    %rbp,%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
test   %eax,%eax
js     11d3 <connectWithTimeout+0xa3>
mov    %ebx,%edi
mov    %r15,%rsi
mov    %r14d,%edx
call   1050 <connect@plt>
mov    %eax,%r12d
mov    0x2e2b(%rip),%rax
mov    (%rax),%esi
mov    0x0(%r13),%edx
mov    %rsp,%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    %r12d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %ebx,%edi
mov    %r15,%rsi
mov    %r14d,%edx
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <connect@plt>

LLVM IR (-O0):
%struct.sockaddr = type { i32 }
%struct.timeval = type { i32 }
@SOL_SOCKET = global i32 0
@SO_RCVTIMEO = global i32 0
define i32 @connectWithTimeout(i32 %0, %struct.sockaddr* %1, i32 %2, %struct.timeval* %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.sockaddr*
%8 = alloca i32
%9 = alloca %struct.timeval*
%10 = alloca %struct.timeval
%11 = alloca i32
%12 = alloca i32
store i32 %0, i32* %6
store %struct.sockaddr* %1, %struct.sockaddr** %7
store i32 %2, i32* %8
store %struct.timeval* %3, %struct.timeval** %9
store i32 4, i32* %11
%13 = load %struct.timeval*, %struct.timeval** %9
%14 = icmp ne %struct.timeval* %13, null
br i1 %14, label %20, label %15
15: ; preds = %4
%16 = load i32, i32* %6
%17 = load %struct.sockaddr*, %struct.sockaddr** %7
%18 = load i32, i32* %8
%19 = call i32 @connect(i32 %16, %struct.sockaddr* %17, i32 %18)
store i32 %19, i32* %5
br label %48
20: ; preds = %4
%21 = load i32, i32* %6
%22 = load i32, i32* @SOL_SOCKET
%23 = load i32, i32* @SO_RCVTIMEO
%24 = bitcast %struct.timeval* %10 to i8*
%25 = call i64 @getsockopt(i32 %21, i32 %22, i32 %23, i8* %24, i32* %11)
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %28
27: ; preds = %20
store i32 -1, i32* %5
br label %48
28: ; preds = %20
%29 = load i32, i32* %6
%30 = load i32, i32* @SOL_SOCKET
%31 = load i32, i32* @SO_RCVTIMEO
%32 = load %struct.timeval*, %struct.timeval** %9
%33 = bitcast %struct.timeval* %32 to i8*
%34 = call i32 @setsockopt(i32 %29, i32 %30, i32 %31, i8* %33, i32 4)
%35 = icmp slt i32 %34, 0
br i1 %35, label %36, label %37
36: ; preds = %28
store i32 -1, i32* %5
br label %48
37: ; preds = %28
%38 = load i32, i32* %6
%39 = load %struct.sockaddr*, %struct.sockaddr** %7
%40 = load i32, i32* %8
%41 = call i32 @connect(i32 %38, %struct.sockaddr* %39, i32 %40)
store i32 %41, i32* %12
%42 = load i32, i32* %6
%43 = load i32, i32* @SOL_SOCKET
%44 = load i32, i32* @SO_RCVTIMEO
%45 = bitcast %struct.timeval* %10 to i8*
%46 = call i32 @setsockopt(i32 %42, i32 %43, i32 %44, i8* %45, i32 4)
%47 = load i32, i32* %12
store i32 %47, i32* %5
br label %48
48: ; preds = %37, %36, %27, %15
%49 = load i32, i32* %5
ret i32 %49
}

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
%struct.timeval = type { i32 }
@SOL_SOCKET = global i32 0
@SO_RCVTIMEO = global i32 0
define i32 @connectWithTimeout(i32 %0, %struct.sockaddr* %1, i32 %2, %struct.timeval* %3) {
%5 = alloca %struct.timeval
%6 = alloca i32
%7 = bitcast %struct.timeval* %5 to i8*
%8 = bitcast i32* %6 to i8*
store i32 4, i32* %6
%9 = icmp eq %struct.timeval* %3, null
br i1 %9, label %10, label %12
10: ; preds = %4
%11 = tail call i32 @connect(i32 %0, %struct.sockaddr* %1, i32 %2)
br label %28
12: ; preds = %4
%13 = load i32, i32* @SOL_SOCKET
%14 = load i32, i32* @SO_RCVTIMEO
%15 = call i64 @getsockopt(i32 %0, i32 %13, i32 %14, i8* %7, i32* %6)
%16 = icmp slt i64 %15, 0
br i1 %16, label %28, label %17
17: ; preds = %12
%18 = load i32, i32* @SOL_SOCKET
%19 = load i32, i32* @SO_RCVTIMEO
%20 = bitcast %struct.timeval* %3 to i8*
%21 = call i32 @setsockopt(i32 %0, i32 %18, i32 %19, i8* %20, i32 4)
%22 = icmp slt i32 %21, 0
br i1 %22, label %28, label %23
23: ; preds = %17
%24 = call i32 @connect(i32 %0, %struct.sockaddr* %1, i32 %2)
%25 = load i32, i32* @SOL_SOCKET
%26 = load i32, i32* @SO_RCVTIMEO
%27 = call i32 @setsockopt(i32 %0, i32 %25, i32 %26, i8* %7, i32 4)
br label %28
28: ; preds = %17, %12, %23, %10
%29 = phi i32 [ %24, %23 ], [ %11, %10 ], [ -1, %12 ], [ -1, %17 ]
ret i32 %29
}