#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_usec;
  scalar_t__ tv_sec;
};
typedef int /*<<< orphan*/ fd_set;

/* Variables and functions */
scalar_t__ FD_ISSET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ F_GETFL;
int /*<<< orphan*/ F_SETFL;
int O_NONBLOCK;
int fcntl(int, int /*<<< orphan*/, int);
int recv(int, char *, int, int);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int recv_to(int fd, char *buffer, int len, int flags, int to) {
  fd_set readset, tempset;
  int result, iof = -1;
  struct timeval tv;
  FD_ZERO(&readset);
  FD_SET(fd, &readset);
  tv.tv_sec = 0;
  tv.tv_usec = to * 1000;
  result = select(fd + 1, &tempset, NULL, NULL, &tv);
  if (result < 0)
    return -1;
  else if (result > 0 && FD_ISSET(fd, &tempset)) {
    if ((iof = fcntl(fd, F_GETFL, 0)) != -1)
      fcntl(fd, F_SETFL, iof | O_NONBLOCK);
    result = recv(fd, buffer, len, flags);
    if (iof != -1)
      fcntl(fd, F_SETFL, iof);
    return result;
  }
  return -2;
}


x86 assembly (clang, -O0):
<recv_to>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
mov    %r8d,-0x1c(%rbp)
movl   $0xffffffff,-0x2c(%rbp)
lea    -0x20(%rbp),%rdi
call   1060 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   1080 <FD_SET@plt>
movq   $0x0,-0x38(%rbp)
imul   $0x3e8,-0x1c(%rbp),%eax
mov    %eax,-0x40(%rbp)
mov    -0x8(%rbp),%edi
add    $0x1,%edi
lea    -0x24(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x40(%rbp),%r8
mov    %rcx,%rdx
call   1070 <select@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jge    11da <recv_to+0x7a>
movl   $0xffffffff,-0x4(%rbp)
jmp    1280 <recv_to+0x120>
cmpl   $0x0,-0x28(%rbp)
jle    1274 <recv_to+0x114>
mov    -0x8(%rbp),%edi
lea    -0x24(%rbp),%rsi
call   1050 <FD_ISSET@plt>
cmp    $0x0,%rax
je     1274 <recv_to+0x114>
mov    -0x8(%rbp),%edi
mov    0x2dcc(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1040 <fcntl@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0xffffffff,%eax
je     1236 <recv_to+0xd6>
mov    -0x8(%rbp),%edi
mov    0x2db5(%rip),%rax
mov    (%rax),%esi
mov    -0x2c(%rbp),%edx
mov    0x2db9(%rip),%rax
or     (%rax),%edx
call   1040 <fcntl@plt>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
mov    -0x18(%rbp),%ecx
call   1030 <recv@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
je     1269 <recv_to+0x109>
mov    -0x8(%rbp),%edi
mov    0x2d79(%rip),%rax
mov    (%rax),%esi
mov    -0x2c(%rbp),%edx
call   1040 <fcntl@plt>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1280 <recv_to+0x120>
jmp    1279 <recv_to+0x119>
movl   $0xfffffffe,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<recv_to>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %r8d,%ebp
mov    %ecx,%r13d
mov    %edx,%r15d
mov    %rsi,%r12
mov    %edi,%r14d
lea    0x4(%rsp),%rbx
mov    %rbx,%rdi
call   1060 <FD_ZERO@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
call   1080 <FD_SET@plt>
movq   $0x0,0x10(%rsp)
imul   $0x3e8,%ebp,%eax
mov    %eax,0x8(%rsp)
lea    0x1(%r14),%edi
mov    %rsp,%rsi
lea    0x8(%rsp),%r8
xor    %edx,%edx
xor    %ecx,%ecx
call   1070 <select@plt>
test   %eax,%eax
js     1238 <recv_to+0xd8>
mov    $0xfffffffe,%ebp
je     1251 <recv_to+0xf1>
mov    %rsp,%rsi
mov    %r14d,%edi
call   1050 <FD_ISSET@plt>
test   %rax,%rax
je     1251 <recv_to+0xf1>
mov    %r13d,%ebp
mov    0x2dea(%rip),%rax
mov    (%rax),%esi
mov    %r14d,%edi
xor    %edx,%edx
call   1040 <fcntl@plt>
cmp    $0xffffffff,%eax
je     123f <recv_to+0xdf>
mov    %eax,%r13d
mov    0x2dd7(%rip),%rbx
mov    (%rbx),%esi
mov    0x2dde(%rip),%rax
mov    (%rax),%edx
or     %r13d,%edx
mov    %r14d,%edi
call   1040 <fcntl@plt>
mov    %r14d,%edi
mov    %r12,%rsi
mov    %r15d,%edx
mov    %ebp,%ecx
call   1030 <recv@plt>
mov    %eax,%ebp
mov    (%rbx),%esi
mov    %r14d,%edi
mov    %r13d,%edx
call   1040 <fcntl@plt>
jmp    1251 <recv_to+0xf1>
mov    $0xffffffff,%ebp
jmp    1251 <recv_to+0xf1>
mov    %r14d,%edi
mov    %r12,%rsi
mov    %r15d,%edx
mov    %ebp,%ecx
call   1030 <recv@plt>
mov    %eax,%ebp
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i64 }
@F_GETFL = global i32 0
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
define i32 @recv_to(i32 %0, i8* %1, i32 %2, i32 %3, i32 %4) {
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
%16 = alloca %struct.timeval
store i32 %0, i32* %7
store i8* %1, i8** %8
store i32 %2, i32* %9
store i32 %3, i32* %10
store i32 %4, i32* %11
store i32 -1, i32* %15
%17 = call i32 @FD_ZERO(i32* %12)
%18 = load i32, i32* %7
%19 = call i32 @FD_SET(i32 %18, i32* %12)
%20 = getelementptr inbounds %struct.timeval, %struct.timeval* %16, i32 0, i32 1
store i64 0, i64* %20
%21 = load i32, i32* %11
%22 = mul nsw i32 %21, 1000
%23 = getelementptr inbounds %struct.timeval, %struct.timeval* %16, i32 0, i32 0
store i32 %22, i32* %23
%24 = load i32, i32* %7
%25 = add nsw i32 %24, 1
%26 = call i32 @select(i32 %25, i32* %13, i32* null, i32* null, %struct.timeval* %16)
store i32 %26, i32* %14
%27 = load i32, i32* %14
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %30
29: ; preds = %5
store i32 -1, i32* %6
br label %66
30: ; preds = %5
%31 = load i32, i32* %14
%32 = icmp sgt i32 %31, 0
br i1 %32, label %33, label %64
33: ; preds = %30
%34 = load i32, i32* %7
%35 = call i64 @FD_ISSET(i32 %34, i32* %13)
%36 = icmp ne i64 %35, 0
br i1 %36, label %37, label %64
37: ; preds = %33
%38 = load i32, i32* %7
%39 = load i32, i32* @F_GETFL
%40 = call i32 @fcntl(i32 %38, i32 %39, i32 0)
store i32 %40, i32* %15
%41 = icmp ne i32 %40, -1
br i1 %41, label %42, label %49
42: ; preds = %37
%43 = load i32, i32* %7
%44 = load i32, i32* @F_SETFL
%45 = load i32, i32* %15
%46 = load i32, i32* @O_NONBLOCK
%47 = or i32 %45, %46
%48 = call i32 @fcntl(i32 %43, i32 %44, i32 %47)
br label %49
49: ; preds = %42, %37
%50 = load i32, i32* %7
%51 = load i8*, i8** %8
%52 = load i32, i32* %9
%53 = load i32, i32* %10
%54 = call i32 @recv(i32 %50, i8* %51, i32 %52, i32 %53)
store i32 %54, i32* %14
%55 = load i32, i32* %15
%56 = icmp ne i32 %55, -1
br i1 %56, label %57, label %62
57: ; preds = %49
%58 = load i32, i32* %7
%59 = load i32, i32* @F_SETFL
%60 = load i32, i32* %15
%61 = call i32 @fcntl(i32 %58, i32 %59, i32 %60)
br label %62
62: ; preds = %57, %49
%63 = load i32, i32* %14
store i32 %63, i32* %6
br label %66
64: ; preds = %33, %30
br label %65
65: ; preds = %64
store i32 -2, i32* %6
br label %66
66: ; preds = %65, %62, %29
%67 = load i32, i32* %6
ret i32 %67
}

LLVM IR (-O2):
%struct.timeval = type { i32, i64 }
@F_GETFL = global i32 0
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
define i32 @recv_to(i32 %0, i8* %1, i32 %2, i32 %3, i32 %4) {
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.timeval
%9 = bitcast i32* %6 to i8*
%10 = bitcast i32* %7 to i8*
%11 = bitcast %struct.timeval* %8 to i8*
%12 = call i32 @FD_ZERO(i32* %6)
%13 = call i32 @FD_SET(i32 %0, i32* %6)
%14 = getelementptr inbounds %struct.timeval, %struct.timeval* %8, i64 0, i32 1
store i64 0, i64* %14
%15 = mul nsw i32 %4, 1000
%16 = getelementptr inbounds %struct.timeval, %struct.timeval* %8, i64 0, i32 0
store i32 %15, i32* %16
%17 = add nsw i32 %0, 1
%18 = call i32 @select(i32 %17, i32* %7, i32* null, i32* null, %struct.timeval* %8)
%19 = icmp slt i32 %18, 0
br i1 %19, label %39, label %20
20: ; preds = %5
%21 = icmp eq i32 %18, 0
br i1 %21, label %39, label %22
22: ; preds = %20
%23 = call i64 @FD_ISSET(i32 %0, i32* %7)
%24 = icmp eq i64 %23, 0
br i1 %24, label %39, label %25
25: ; preds = %22
%26 = load i32, i32* @F_GETFL
%27 = call i32 @fcntl(i32 %0, i32 %26, i32 0)
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %31
29: ; preds = %25
%30 = call i32 @recv(i32 %0, i8* %1, i32 %2, i32 %3)
br label %39
31: ; preds = %25
%32 = load i32, i32* @F_SETFL
%33 = load i32, i32* @O_NONBLOCK
%34 = or i32 %33, %27
%35 = call i32 @fcntl(i32 %0, i32 %32, i32 %34)
%36 = call i32 @recv(i32 %0, i8* %1, i32 %2, i32 %3)
%37 = load i32, i32* @F_SETFL
%38 = call i32 @fcntl(i32 %0, i32 %37, i32 %27)
br label %39
39: ; preds = %29, %22, %20, %31, %5
%40 = phi i32 [ -1, %5 ], [ %36, %31 ], [ -2, %20 ], [ -2, %22 ], [ %30, %29 ]
ret i32 %40
}