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

/* Variables and functions */
scalar_t__ EINTR;
int /*<<< orphan*/ FD_ISSET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
scalar_t__ errno;
int recv(int, char *, int, int /*<<< orphan*/);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int clamd_recv(int sock, char *buffer, int size) {
  int ret;
  fd_set rfds;
  struct timeval tv;
  if (sock < 0)
    return -1;
  do {
    FD_ZERO(&rfds);
    FD_SET(sock, &rfds);
    tv.tv_sec = 60;
    tv.tv_usec = 0;
    ret = select(sock + 1, &rfds, NULL, NULL, &tv);
  } while (ret < 0 && errno == EINTR);
  if (ret <= 0)
    return -1;
  if (!FD_ISSET(sock, &rfds))
    return -1;
  do {
    ret = recv(sock, buffer, size - 1, 0);
  } while (ret < 0 && errno == EINTR);
  if (ret <= 0)
    return -1;
  return 0;
}


x86 assembly (clang, -O0):
<clamd_recv>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    1178 <clamd_recv+0x28>
movl   $0xffffffff,-0x4(%rbp)
jmp    1296 <clamd_recv+0x146>
jmp    117d <clamd_recv+0x2d>
lea    -0x1c(%rbp),%rdi
call   1050 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0x1c(%rbp),%rsi
call   1070 <FD_SET@plt>
movl   $0x3c,-0x30(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x8(%rbp),%edi
add    $0x1,%edi
lea    -0x1c(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x30(%rbp),%r8
mov    %rcx,%rdx
call   1060 <select@plt>
mov    %eax,-0x18(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x18(%rbp)
mov    %al,-0x31(%rbp)
jge    11e7 <clamd_recv+0x97>
mov    0x2e0c(%rip),%rax
mov    (%rax),%rax
mov    0x2dfa(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x31(%rbp)
mov    -0x31(%rbp),%al
test   $0x1,%al
jne    117d <clamd_recv+0x2d>
cmpl   $0x0,-0x18(%rbp)
jg     1208 <clamd_recv+0xb8>
movl   $0xffffffff,-0x4(%rbp)
jmp    1296 <clamd_recv+0x146>
mov    -0x8(%rbp),%edi
lea    -0x1c(%rbp),%rsi
call   1040 <FD_ISSET@plt>
cmp    $0x0,%eax
jne    1229 <clamd_recv+0xd9>
movl   $0xffffffff,-0x4(%rbp)
jmp    1296 <clamd_recv+0x146>
jmp    122e <clamd_recv+0xde>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
sub    $0x1,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x18(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x18(%rbp)
mov    %al,-0x32(%rbp)
jge    126e <clamd_recv+0x11e>
mov    0x2d85(%rip),%rax
mov    (%rax),%rax
mov    0x2d73(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x32(%rbp)
mov    -0x32(%rbp),%al
test   $0x1,%al
jne    122e <clamd_recv+0xde>
cmpl   $0x0,-0x18(%rbp)
jg     128f <clamd_recv+0x13f>
movl   $0xffffffff,-0x4(%rbp)
jmp    1296 <clamd_recv+0x146>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<clamd_recv>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    $0xffffffff,%ecx
test   %edi,%edi
js     123c <clamd_recv+0xec>
mov    %edi,%r13d
mov    %rsi,0x10(%rsp)
mov    %edx,0x8(%rsp)
lea    0x1(%r13),%ebp
lea    0xc(%rsp),%rbx
lea    0x18(%rsp),%r15
mov    0x2e54(%rip),%r14
mov    0x2e45(%rip),%r12
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %rbx,%rdi
call   1050 <FD_ZERO@plt>
mov    %r13d,%edi
mov    %rbx,%rsi
call   1070 <FD_SET@plt>
movl   $0x3c,0x18(%rsp)
movq   $0x0,0x20(%rsp)
mov    %ebp,%edi
mov    %rbx,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
mov    %r15,%r8
call   1060 <select@plt>
test   %eax,%eax
jns    11e2 <clamd_recv+0x92>
mov    (%r12),%rcx
cmp    %rcx,(%r14)
je     11a0 <clamd_recv+0x50>
test   %eax,%eax
jle    1237 <clamd_recv+0xe7>
lea    0xc(%rsp),%rsi
mov    %r13d,%edi
call   1040 <FD_ISSET@plt>
test   %eax,%eax
mov    $0xffffffff,%ecx
je     123c <clamd_recv+0xec>
mov    0x8(%rsp),%ebx
add    $0xffffffff,%ebx
mov    0x10(%rsp),%rbp
nopl   0x0(%rax,%rax,1)
mov    %r13d,%edi
mov    %rbp,%rsi
mov    %ebx,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %eax,%eax
jns    122c <clamd_recv+0xdc>
mov    (%r12),%rcx
cmp    %rcx,(%r14)
je     1210 <clamd_recv+0xc0>
xor    %ecx,%ecx
test   %eax,%eax
setle  %cl
neg    %ecx
jmp    123c <clamd_recv+0xec>
mov    $0xffffffff,%ecx
mov    %ecx,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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