#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_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ Log(char *, int /*<<< orphan*/);
scalar_t__ errno;
int /*<<< orphan*/ exit(int);
int recv(int, char *, int, int /*<<< orphan*/);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int /*<<< orphan*/ strerror(scalar_t__);
int recv_data(int sd, char *rbuf, int n) {
  int nrecv;
  fd_set f;
  struct timeval tv;
  int ret, max = 3;
retry:
  if ((nrecv = recv(sd, rbuf, n, 0)) == -1 && errno != EINTR) {
    Log("recv failed: %s", strerror(errno));
    exit(1);
  }
  if (nrecv == 0) {
    FD_ZERO(&f);
    FD_SET(sd, &f);
    tv.tv_sec = 1;
    tv.tv_usec = 0;
    if ((ret = select(sd + 1, &f, NULL, NULL, &tv)) == -1) {
      Log("Error on select: %s", strerror(errno));
      exit(1);
    }
    if (ret > 0) {
      if (max--)
        goto retry;
    }
  }
  return nrecv;
}


x86 assembly (clang, -O0):
<recv_data>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x3,-0x38(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x18(%rbp)
cmp    $0xffffffff,%eax
jne    11e7 <recv_data+0x77>
mov    0x2e33(%rip),%rax
mov    (%rax),%rax
mov    0x2e21(%rip),%rcx
cmp    (%rcx),%rax
je     11e7 <recv_data+0x77>
mov    0x2e19(%rip),%rax
mov    (%rax),%rdi
call   1080 <strerror@plt>
mov    %eax,%esi
lea    0xe28(%rip),%rdi
call   1060 <Log@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
cmpl   $0x0,-0x18(%rbp)
jne    128f <recv_data+0x11f>
lea    -0x1c(%rbp),%rdi
call   1040 <FD_ZERO@plt>
mov    -0x4(%rbp),%edi
lea    -0x1c(%rbp),%rsi
call   1090 <FD_SET@plt>
movl   $0x1,-0x30(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x4(%rbp),%edi
add    $0x1,%edi
lea    -0x1c(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x30(%rbp),%r8
mov    %rcx,%rdx
call   1050 <select@plt>
mov    %eax,-0x34(%rbp)
cmp    $0xffffffff,%eax
jne    1262 <recv_data+0xf2>
mov    0x2d9e(%rip),%rax
mov    (%rax),%rdi
call   1080 <strerror@plt>
mov    %eax,%esi
lea    0xdbd(%rip),%rdi
call   1060 <Log@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
cmpl   $0x0,-0x34(%rbp)
jle    128a <recv_data+0x11a>
mov    -0x38(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x38(%rbp)
cmp    $0x0,%eax
je     1285 <recv_data+0x115>
jmp    1189 <recv_data+0x19>
jmp    128a <recv_data+0x11a>
jmp    128f <recv_data+0x11f>
mov    -0x18(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<recv_data>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edx,%r14d
mov    %rsi,%r12
mov    %edi,%ebp
lea    0x1(%rbp),%eax
mov    %eax,(%rsp)
mov    $0xfffffffd,%r15d
lea    0x4(%rsp),%r13
nopw   0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %r12,%rsi
mov    %r14d,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,%ebx
test   %eax,%eax
jne    1203 <recv_data+0x93>
mov    %r13,%rdi
call   1040 <FD_ZERO@plt>
mov    %ebp,%edi
mov    %r13,%rsi
call   1090 <FD_SET@plt>
movl   $0x1,0x8(%rsp)
movq   $0x0,0x10(%rsp)
mov    (%rsp),%edi
mov    %r13,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
lea    0x8(%rsp),%r8
call   1050 <select@plt>
cmp    $0xffffffff,%eax
je     122f <recv_data+0xbf>
test   %eax,%eax
jle    121e <recv_data+0xae>
lea    0x1(%r15),%eax
test   %r15d,%r15d
mov    %eax,%r15d
jne    11a0 <recv_data+0x30>
jmp    121e <recv_data+0xae>
cmp    $0xffffffff,%ebx
jne    121e <recv_data+0xae>
mov    0x2dd1(%rip),%rax
mov    (%rax),%rdi
mov    0x2dbf(%rip),%rax
cmp    (%rax),%rdi
jne    1256 <recv_data+0xe6>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2daa(%rip),%rax
mov    (%rax),%rdi
call   1080 <strerror@plt>
lea    0xdcb(%rip),%rdi
mov    %eax,%esi
call   1060 <Log@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
call   1080 <strerror@plt>
lea    0xd9e(%rip),%rdi
jmp    1245 <recv_data+0xd5>

LLVM IR (-O0):
%struct.timeval = type { i32, i64 }
@errno = global i64 0
@EINTR = global i64 0
@.str = [16 x i8] c"recv failed: %s\00"
@.str.1 = [20 x i8] c"Error on select: %s\00"
define i32 @recv_data(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca %struct.timeval
%10 = alloca i32
%11 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32 %2, i32* %6
store i32 3, i32* %11
br label %12
12: ; preds = %52, %3
%13 = load i32, i32* %4
%14 = load i8*, i8** %5
%15 = load i32, i32* %6
%16 = call i32 @recv(i32 %13, i8* %14, i32 %15, i32 0)
store i32 %16, i32* %7
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %27
18: ; preds = %12
%19 = load i64, i64* @errno
%20 = load i64, i64* @EINTR
%21 = icmp ne i64 %19, %20
br i1 %21, label %22, label %27
22: ; preds = %18
%23 = load i64, i64* @errno
%24 = call i32 @strerror(i64 %23)
%25 = call i32 @Log(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %24)
%26 = call i32 @exit(i32 1)
unreachable
27: ; preds = %18, %12
%28 = load i32, i32* %7
%29 = icmp eq i32 %28, 0
br i1 %29, label %30, label %55
30: ; preds = %27
%31 = call i32 @FD_ZERO(i32* %8)
%32 = load i32, i32* %4
%33 = call i32 @FD_SET(i32 %32, i32* %8)
%34 = getelementptr inbounds %struct.timeval, %struct.timeval* %9, i32 0, i32 0
store i32 1, i32* %34
%35 = getelementptr inbounds %struct.timeval, %struct.timeval* %9, i32 0, i32 1
store i64 0, i64* %35
%36 = load i32, i32* %4
%37 = add nsw i32 %36, 1
%38 = call i32 @select(i32 %37, i32* %8, i32* null, i32* null, %struct.timeval* %9)
store i32 %38, i32* %10
%39 = icmp eq i32 %38, -1
br i1 %39, label %40, label %45
40: ; preds = %30
%41 = load i64, i64* @errno
%42 = call i32 @strerror(i64 %41)
%43 = call i32 @Log(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i32 %42)
%44 = call i32 @exit(i32 1)
unreachable
45: ; preds = %30
%46 = load i32, i32* %10
%47 = icmp sgt i32 %46, 0
br i1 %47, label %48, label %54
48: ; preds = %45
%49 = load i32, i32* %11
%50 = add nsw i32 %49, -1
store i32 %50, i32* %11
%51 = icmp ne i32 %49, 0
br i1 %51, label %52, label %53
52: ; preds = %48
br label %12
53: ; preds = %48
br label %54
54: ; preds = %53, %45
br label %55
55: ; preds = %54, %27
%56 = load i32, i32* %7
ret i32 %56
}

LLVM IR (-O2):
%struct.timeval = type { i32, i64 }
@errno = global i64 0
@EINTR = global i64 0
@.str = [16 x i8] c"recv failed: %s\00"
@.str.1 = [20 x i8] c"Error on select: %s\00"
define i32 @recv_data(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 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i64 0, i32 0
%9 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i64 0, i32 1
%10 = add nsw i32 %0, 1
br label %11
11: ; preds = %32, %3
%12 = phi i32 [ 3, %3 ], [ %34, %32 ]
%13 = call i32 @recv(i32 %0, i8* %1, i32 %2, i32 0)
switch i32 %13, label %37 [
i32 -1, label %14
i32 0, label %22
]
14: ; preds = %11
%15 = load i64, i64* @errno
%16 = load i64, i64* @EINTR
%17 = icmp eq i64 %15, %16
br i1 %17, label %37, label %18
18: ; preds = %14
%19 = call i32 @strerror(i64 %15)
%20 = call i32 @Log(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %19)
%21 = call i32 @exit(i32 1)
unreachable
22: ; preds = %11
%23 = call i32 @FD_ZERO(i32* %4)
%24 = call i32 @FD_SET(i32 %0, i32* %4)
store i32 1, i32* %8
store i64 0, i64* %9
%25 = call i32 @select(i32 %10, i32* %4, i32* null, i32* null, %struct.timeval* %5)
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %32
27: ; preds = %22
%28 = load i64, i64* @errno
%29 = call i32 @strerror(i64 %28)
%30 = call i32 @Log(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i32 %29)
%31 = call i32 @exit(i32 1)
unreachable
32: ; preds = %22
%33 = icmp slt i32 %25, 1
%34 = add nsw i32 %12, -1
%35 = icmp eq i32 %12, 0
%36 = select i1 %33, i1 true, i1 %35
br i1 %36, label %37, label %11
37: ; preds = %11, %32, %14
ret i32 %13
}