#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_sec;
  scalar_t__ tv_usec;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ socklen_t;
typedef int /*<<< orphan*/ fd_set;

/* Variables and functions */
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
scalar_t__ recvfrom(int, void *, int, int /*<<< orphan*/, struct sockaddr *,
                    int /*<<< orphan*/ *);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int cp_recv(int fd, void *buf, struct sockaddr *sinfo, socklen_t *slen) {
  fd_set reads;
  struct timeval tv;
  tv.tv_sec = 3;
  tv.tv_usec = 0;
  FD_ZERO(&reads);
  FD_SET(fd, &reads);
  int ready_value = select(10, &reads, NULL, NULL, &tv);
  if (ready_value == 0) {
    return 1;
  } else if (ready_value == -1) {
    return -1;
  }
  if (recvfrom(fd, buf, 256, 0, sinfo, slen) < 0) {
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<cp_recv>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
movl   $0x3,-0x38(%rbp)
movq   $0x0,-0x30(%rbp)
lea    -0x24(%rbp),%rdi
call   1030 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0x24(%rbp),%rsi
call   1060 <FD_SET@plt>
mov    $0xa,%edi
lea    -0x24(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x38(%rbp),%r8
mov    %rcx,%rdx
call   1040 <select@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jne    11ad <cp_recv+0x6d>
movl   $0x1,-0x4(%rbp)
jmp    1200 <cp_recv+0xc0>
cmpl   $0xffffffff,-0x3c(%rbp)
jne    11c3 <cp_recv+0x83>
movl   $0xffffffff,-0x4(%rbp)
jmp    1200 <cp_recv+0xc0>
jmp    11c8 <cp_recv+0x88>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%r8
mov    -0x20(%rbp),%r9
mov    $0x100,%edx
xor    %ecx,%ecx
call   1050 <recvfrom@plt>
cmp    $0x0,%rax
jge    11f9 <cp_recv+0xb9>
movl   $0xffffffff,-0x4(%rbp)
jmp    1200 <cp_recv+0xc0>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cp_recv>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %rcx,%r14
mov    %rdx,%r15
mov    %rsi,%r12
mov    %edi,%ebp
movl   $0x3,0x10(%rsp)
movq   $0x0,0x18(%rsp)
lea    0xc(%rsp),%rbx
mov    %rbx,%rdi
call   1030 <FD_ZERO@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
call   1060 <FD_SET@plt>
lea    0x10(%rsp),%r8
mov    $0xa,%edi
mov    %rbx,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
call   1040 <select@plt>
test   %eax,%eax
je     11a5 <cp_recv+0x65>
cmp    $0xffffffff,%eax
jne    11ac <cp_recv+0x6c>
mov    $0xffffffff,%eax
jmp    11c7 <cp_recv+0x87>
mov    $0x1,%eax
jmp    11c7 <cp_recv+0x87>
mov    %ebp,%edi
mov    %r12,%rsi
mov    $0x100,%edx
xor    %ecx,%ecx
mov    %r15,%r8
mov    %r14,%r9
call   1050 <recvfrom@plt>
sar    $0x3f,%rax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
%struct.timeval = type { i32, i64 }
define i32 @cp_recv(i32 %0, i8* %1, %struct.sockaddr* %2, i32* %3) {
%5 = alloca i32
%6 = alloca %struct.timeval
%7 = bitcast i32* %5 to i8*
%8 = bitcast %struct.timeval* %6 to i8*
%9 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i64 0, i32 0
store i32 3, i32* %9
%10 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i64 0, i32 1
store i64 0, i64* %10
%11 = call i32 @FD_ZERO(i32* %5)
%12 = call i32 @FD_SET(i32 %0, i32* %5)
%13 = call i32 @select(i32 10, i32* %5, i32* null, i32* null, %struct.timeval* %6)
switch i32 %13, label %15 [
i32 0, label %19
i32 -1, label %14
]
14: ; preds = %4
br label %19
15: ; preds = %4
%16 = call i64 @recvfrom(i32 %0, i8* %1, i32 256, i32 0, %struct.sockaddr* %2, i32* %3)
%17 = ashr i64 %16, 63
%18 = trunc i64 %17 to i32
br label %19
19: ; preds = %15, %4, %14
%20 = phi i32 [ -1, %14 ], [ 1, %4 ], [ %18, %15 ]
ret i32 %20
}