#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 */

/* Variables and functions */
scalar_t__ EAGAIN;
scalar_t__ EWOULDBLOCK;
scalar_t__ errno;
int /*<<< orphan*/ perror(char *);
int recv(int, char *, size_t, int);
scalar_t__ tp_suspend(int /*<<< orphan*/, int);
int tp_recv(int sockfd, char *buf, size_t len, int flags, int recv_timeout) {
  int bytes_recv = 0;
  int bytes;
  while (bytes_recv < 1) {
    bytes = recv(sockfd, buf + bytes_recv, len, flags);
    if (bytes < 0) {
      if (errno != EAGAIN && errno != EWOULDBLOCK) {
        perror("tp_recv");
        return -1;
      }
      if (tp_suspend(0, recv_timeout) < 0)
        return -1;
    } else if (bytes == 0)
      return -1;
    else
      bytes_recv += bytes;
  }
  return bytes_recv;
}


x86 assembly (clang, -O0):
<tp_recv>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    %r8d,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
cmpl   $0x1,-0x24(%rbp)
jge    121c <tp_recv+0xec>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
movslq -0x24(%rbp),%rax
add    %rax,%rsi
mov    -0x18(%rbp),%rdx
mov    -0x1c(%rbp),%ecx
call   1030 <recv@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jge    11f3 <tp_recv+0xc3>
mov    0x2e57(%rip),%rax
mov    (%rax),%rax
mov    0x2e35(%rip),%rcx
cmp    (%rcx),%rax
je     11ce <tp_recv+0x9e>
mov    0x2e3d(%rip),%rax
mov    (%rax),%rax
mov    0x2e2b(%rip),%rcx
cmp    (%rcx),%rax
je     11ce <tp_recv+0x9e>
lea    0xe43(%rip),%rdi
call   1050 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1222 <tp_recv+0xf2>
mov    -0x20(%rbp),%esi
xor    %edi,%edi
call   1040 <tp_suspend@plt>
cmp    $0x0,%rax
jge    11ee <tp_recv+0xbe>
movl   $0xffffffff,-0x4(%rbp)
jmp    1222 <tp_recv+0xf2>
jmp    1217 <tp_recv+0xe7>
cmpl   $0x0,-0x28(%rbp)
jne    1209 <tp_recv+0xd9>
movl   $0xffffffff,-0x4(%rbp)
jmp    1222 <tp_recv+0xf2>
mov    -0x28(%rbp),%eax
add    -0x24(%rbp),%eax
mov    %eax,-0x24(%rbp)
jmp    1217 <tp_recv+0xe7>
jmp    1151 <tp_recv+0x21>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tp_recv>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r8d,%r14d
mov    %ecx,%r15d
mov    %rdx,%r12
mov    %rsi,%rbp
mov    %edi,%ebx
mov    0x2e78(%rip),%r13
jmp    116f <tp_recv+0x3f>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
xor    %edi,%edi
mov    %r14d,%esi
call   1040 <tp_suspend@plt>
test   %rax,%rax
js     11ab <tp_recv+0x7b>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    %r12,%rdx
mov    %r15d,%ecx
call   1030 <recv@plt>
test   %eax,%eax
jns    11b2 <tp_recv+0x82>
mov    0x2e56(%rip),%rax
mov    (%rax),%rax
cmp    0x0(%r13),%rax
je     1160 <tp_recv+0x30>
mov    0x2e3e(%rip),%rcx
cmp    (%rcx),%rax
je     1160 <tp_recv+0x30>
lea    0xe5a(%rip),%rdi
call   1050 <perror@plt>
mov    $0xffffffff,%eax
jmp    11bb <tp_recv+0x8b>
mov    %eax,%ecx
cmp    $0x1,%eax
sbb    %eax,%eax
or     %ecx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i64 0
@EAGAIN = global i64 0
@EWOULDBLOCK = global i64 0
@.str = [8 x i8] c"tp_recv\00"
define i32 @tp_recv(i32 %0, i8* %1, i64 %2, i32 %3, i32 %4) {
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
store i32 %0, i32* %7
store i8* %1, i8** %8
store i64 %2, i64* %9
store i32 %3, i32* %10
store i32 %4, i32* %11
store i32 0, i32* %12
br label %14
14: ; preds = %53, %5
%15 = load i32, i32* %12
%16 = icmp slt i32 %15, 1
br i1 %16, label %17, label %54
17: ; preds = %14
%18 = load i32, i32* %7
%19 = load i8*, i8** %8
%20 = load i32, i32* %12
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds i8, i8* %19, i64 %21
%23 = load i64, i64* %9
%24 = load i32, i32* %10
%25 = call i32 @recv(i32 %18, i8* %22, i64 %23, i32 %24)
store i32 %25, i32* %13
%26 = load i32, i32* %13
%27 = icmp slt i32 %26, 0
br i1 %27, label %28, label %44
28: ; preds = %17
%29 = load i64, i64* @errno
%30 = load i64, i64* @EAGAIN
%31 = icmp ne i64 %29, %30
br i1 %31, label %32, label %38
32: ; preds = %28
%33 = load i64, i64* @errno
%34 = load i64, i64* @EWOULDBLOCK
%35 = icmp ne i64 %33, %34
br i1 %35, label %36, label %38
36: ; preds = %32
%37 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %6
br label %56
38: ; preds = %32, %28
%39 = load i32, i32* %11
%40 = call i64 @tp_suspend(i32 0, i32 %39)
%41 = icmp slt i64 %40, 0
br i1 %41, label %42, label %43
42: ; preds = %38
store i32 -1, i32* %6
br label %56
43: ; preds = %38
br label %53
44: ; preds = %17
%45 = load i32, i32* %13
%46 = icmp eq i32 %45, 0
br i1 %46, label %47, label %48
47: ; preds = %44
store i32 -1, i32* %6
br label %56
48: ; preds = %44
%49 = load i32, i32* %13
%50 = load i32, i32* %12
%51 = add nsw i32 %50, %49
store i32 %51, i32* %12
br label %52
52: ; preds = %48
br label %53
53: ; preds = %52, %43
br label %14
54: ; preds = %14
%55 = load i32, i32* %12
store i32 %55, i32* %6
br label %56
56: ; preds = %54, %47, %42, %36
%57 = load i32, i32* %6
ret i32 %57
}

LLVM IR (-O2):
@errno = global i64 0
@EAGAIN = global i64 0
@EWOULDBLOCK = global i64 0
@.str = [8 x i8] c"tp_recv\00"
define i32 @tp_recv(i32 %0, i8* %1, i64 %2, i32 %3, i32 %4) {
br label %6
6: ; preds = %18, %5
%7 = tail call i32 @recv(i32 %0, i8* %1, i64 %2, i32 %3)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %21
9: ; preds = %6
%10 = load i64, i64* @errno
%11 = load i64, i64* @EAGAIN
%12 = icmp eq i64 %10, %11
%13 = load i64, i64* @EWOULDBLOCK
%14 = icmp eq i64 %10, %13
%15 = select i1 %12, i1 true, i1 %14
br i1 %15, label %18, label %16
16: ; preds = %9
%17 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
br label %24
18: ; preds = %9
%19 = tail call i64 @tp_suspend(i32 0, i32 %4)
%20 = icmp slt i64 %19, 0
br i1 %20, label %24, label %6
21: ; preds = %6
%22 = icmp eq i32 %7, 0
%23 = select i1 %22, i32 -1, i32 %7
br label %24
24: ; preds = %18, %21, %16
%25 = phi i32 [ -1, %16 ], [ %23, %21 ], [ -1, %18 ]
ret i32 %25
}