#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 {
  unsigned int tv_sec;
  unsigned int tv_usec;
};
typedef int /*<<< orphan*/ fd_set;

/* Variables and functions */
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int recv(int, char *, int, int /*<<< orphan*/);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int cr_receivedata(int fd, unsigned int msecs, char *buf, int size) {
  fd_set fds;
  struct timeval tv;
  int rc;
  tv.tv_sec = msecs / 1000;
  tv.tv_usec = (msecs % 1000) * 1000;
  FD_ZERO(&fds);
  FD_SET(fd, &fds);
  rc = select(fd + 1, &fds, NULL, NULL, &tv);
  if (rc > 0)
    return recv(fd, buf, size, 0);
  else if (rc == 0)
    return -2;
  else
    return -1;
}


x86 assembly (clang, -O0):
<cr_receivedata>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    -0xc(%rbp),%eax
mov    $0x3e8,%ecx
xor    %edx,%edx
div    %ecx
mov    %eax,-0x28(%rbp)
mov    -0xc(%rbp),%eax
mov    $0x3e8,%ecx
xor    %edx,%edx
div    %ecx
imul   $0x3e8,%edx,%eax
mov    %eax,-0x24(%rbp)
lea    -0x20(%rbp),%rdi
call   1040 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   1060 <FD_SET@plt>
mov    -0x8(%rbp),%edi
add    $0x1,%edi
lea    -0x20(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x28(%rbp),%r8
mov    %rcx,%rdx
call   1050 <select@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jle    11ce <cr_receivedata+0x8e>
mov    -0x8(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x4(%rbp)
jmp    11eb <cr_receivedata+0xab>
cmpl   $0x0,-0x2c(%rbp)
jne    11e4 <cr_receivedata+0xa4>
movl   $0xfffffffe,-0x4(%rbp)
jmp    11eb <cr_receivedata+0xab>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cr_receivedata>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %ecx,%r14d
mov    %rdx,%r15
mov    %edi,%ebp
mov    %esi,%eax
imul   $0x10624dd3,%rax,%rax
shr    $0x26,%rax
mov    %eax,0x8(%rsp)
imul   $0x3e8,%eax,%eax
sub    %eax,%esi
imul   $0x3e8,%esi,%eax
mov    %eax,0xc(%rsp)
lea    0x14(%rsp),%rbx
mov    %rbx,%rdi
call   1040 <FD_ZERO@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
call   1060 <FD_SET@plt>
lea    0x1(%rbp),%edi
lea    0x8(%rsp),%r8
mov    %rbx,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
call   1050 <select@plt>
test   %eax,%eax
jle    11b5 <cr_receivedata+0x75>
mov    %ebp,%edi
mov    %r15,%rsi
mov    %r14d,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
jmp    11bd <cr_receivedata+0x7d>
sete   %al
movzbl %al,%eax
not    %eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i32 }
define i32 @cr_receivedata(i32 %0, i32 %1, i8* %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
%11 = alloca %struct.timeval
%12 = alloca i32
store i32 %0, i32* %6
store i32 %1, i32* %7
store i8* %2, i8** %8
store i32 %3, i32* %9
%13 = load i32, i32* %7
%14 = udiv i32 %13, 1000
%15 = getelementptr inbounds %struct.timeval, %struct.timeval* %11, i32 0, i32 0
store i32 %14, i32* %15
%16 = load i32, i32* %7
%17 = urem i32 %16, 1000
%18 = mul i32 %17, 1000
%19 = getelementptr inbounds %struct.timeval, %struct.timeval* %11, i32 0, i32 1
store i32 %18, i32* %19
%20 = call i32 @FD_ZERO(i32* %10)
%21 = load i32, i32* %6
%22 = call i32 @FD_SET(i32 %21, i32* %10)
%23 = load i32, i32* %6
%24 = add nsw i32 %23, 1
%25 = call i32 @select(i32 %24, i32* %10, i32* null, i32* null, %struct.timeval* %11)
store i32 %25, i32* %12
%26 = load i32, i32* %12
%27 = icmp sgt i32 %26, 0
br i1 %27, label %28, label %33
28: ; preds = %4
%29 = load i32, i32* %6
%30 = load i8*, i8** %8
%31 = load i32, i32* %9
%32 = call i32 @recv(i32 %29, i8* %30, i32 %31, i32 0)
store i32 %32, i32* %5
br label %38
33: ; preds = %4
%34 = load i32, i32* %12
%35 = icmp eq i32 %34, 0
br i1 %35, label %36, label %37
36: ; preds = %33
store i32 -2, i32* %5
br label %38
37: ; preds = %33
store i32 -1, i32* %5
br label %38
38: ; preds = %37, %36, %28
%39 = load i32, i32* %5
ret i32 %39
}

LLVM IR (-O2):
%struct.timeval = type { i32, i32 }
define i32 @cr_receivedata(i32 %0, i32 %1, i8* %2, i32 %3) {
%5 = alloca i32
%6 = alloca %struct.timeval
%7 = bitcast i32* %5 to i8*
%8 = bitcast %struct.timeval* %6 to i8*
%9 = udiv i32 %1, 1000
%10 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i64 0, i32 0
store i32 %9, i32* %10
%11 = urem i32 %1, 1000
%12 = mul nuw nsw i32 %11, 1000
%13 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i64 0, i32 1
store i32 %12, i32* %13
%14 = call i32 @FD_ZERO(i32* %5)
%15 = call i32 @FD_SET(i32 %0, i32* %5)
%16 = add nsw i32 %0, 1
%17 = call i32 @select(i32 %16, i32* %5, i32* null, i32* null, %struct.timeval* %6)
%18 = icmp sgt i32 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %4
%20 = call i32 @recv(i32 %0, i8* %2, i32 %3, i32 0)
br label %24
21: ; preds = %4
%22 = icmp eq i32 %17, 0
%23 = select i1 %22, i32 -2, i32 -1
br label %24
24: ; preds = %21, %19
%25 = phi i32 [ %20, %19 ], [ %23, %21 ]
ret i32 %25
}