#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*/ tv;
typedef int /*<<< orphan*/ time_t;
typedef int /*<<< orphan*/ suseconds_t;
struct timeval {
  int /*<<< orphan*/ tv_usec;
  int /*<<< orphan*/ tv_sec;
};
typedef int /*<<< orphan*/ ssize_t;

/* Variables and functions */
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_RCVTIMEO;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ recv(int, void *, size_t, int);
int setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, struct timeval *,
               int);
ssize_t timeoutRecv(int sockfd, void *buf, size_t len, int flags, time_t sec,
                    suseconds_t usec) {
  struct timeval tv;
  ssize_t ret;
  int tmpi;
  tv.tv_sec = sec;
  tv.tv_usec = usec;
  (tmpi) = setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
  if ((tmpi) == (-1)) {
    perror("setsockopt");
    exit(1);
  };
  ;
  ret = recv(sockfd, buf, len, flags);
  return ret;
}


x86 assembly (clang, -O0):
<timeoutRecv>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    %r8d,-0x20(%rbp)
mov    %r9d,-0x24(%rbp)
mov    -0x20(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2e74(%rip),%rax
mov    (%rax),%esi
mov    0x2e5b(%rip),%rax
mov    (%rax),%edx
lea    -0x30(%rbp),%rcx
mov    $0x8,%r8d
call   1040 <setsockopt@plt>
mov    %eax,-0x38(%rbp)
cmpl   $0xffffffff,-0x38(%rbp)
jne    11b1 <timeoutRecv+0x71>
lea    0xe5e(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x1c(%rbp),%ecx
call   1030 <recv@plt>
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<timeoutRecv>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %ecx,%r14d
mov    %rdx,%r15
mov    %rsi,%rbp
mov    %edi,%ebx
mov    %r8d,0x4(%rsp)
mov    %r9d,(%rsp)
mov    0x2e86(%rip),%rax
mov    (%rax),%esi
mov    0x2e6d(%rip),%rax
mov    (%rax),%edx
mov    %rsp,%rcx
mov    $0x8,%r8d
call   1040 <setsockopt@plt>
cmp    $0xffffffff,%eax
je     119b <timeoutRecv+0x5b>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    %r15,%rdx
mov    %r14d,%ecx
call   1030 <recv@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe5e(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
%struct.timeval = type { i32, i32 }
@SOL_SOCKET = global i32 0
@SO_RCVTIMEO = global i32 0
@.str = [11 x i8] c"setsockopt\00"
define i32 @timeoutRecv(i32 %0, i8* %1, i64 %2, i32 %3, i32 %4, i32 %5) {
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca %struct.timeval
%14 = alloca i32
%15 = alloca i32
store i32 %0, i32* %7
store i8* %1, i8** %8
store i64 %2, i64* %9
store i32 %3, i32* %10
store i32 %4, i32* %11
store i32 %5, i32* %12
%16 = load i32, i32* %11
%17 = getelementptr inbounds %struct.timeval, %struct.timeval* %13, i32 0, i32 1
store i32 %16, i32* %17
%18 = load i32, i32* %12
%19 = getelementptr inbounds %struct.timeval, %struct.timeval* %13, i32 0, i32 0
store i32 %18, i32* %19
%20 = load i32, i32* %7
%21 = load i32, i32* @SOL_SOCKET
%22 = load i32, i32* @SO_RCVTIMEO
%23 = call i32 @setsockopt(i32 %20, i32 %21, i32 %22, %struct.timeval* %13, i32 8)
store i32 %23, i32* %15
%24 = load i32, i32* %15
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %29
26: ; preds = %6
%27 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%28 = call i32 @exit(i32 1)
unreachable
29: ; preds = %6
%30 = load i32, i32* %7
%31 = load i8*, i8** %8
%32 = load i64, i64* %9
%33 = load i32, i32* %10
%34 = call i32 @recv(i32 %30, i8* %31, i64 %32, i32 %33)
store i32 %34, i32* %14
%35 = load i32, i32* %14
ret i32 %35
}

LLVM IR (-O2):
%struct.timeval = type { i32, i32 }
@SOL_SOCKET = global i32 0
@SO_RCVTIMEO = global i32 0
@.str = [11 x i8] c"setsockopt\00"
define i32 @timeoutRecv(i32 %0, i8* %1, i64 %2, i32 %3, i32 %4, i32 %5) {
%7 = alloca %struct.timeval
%8 = bitcast %struct.timeval* %7 to i8*
%9 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i64 0, i32 1
store i32 %4, i32* %9
%10 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i64 0, i32 0
store i32 %5, i32* %10
%11 = load i32, i32* @SOL_SOCKET
%12 = load i32, i32* @SO_RCVTIMEO
%13 = call i32 @setsockopt(i32 %0, i32 %11, i32 %12, %struct.timeval* %7, i32 8)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %18
15: ; preds = %6
%16 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %6
%19 = call i32 @recv(i32 %0, i8* %1, i64 %2, i32 %3)
ret i32 %19
}