#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__ EINTR;
scalar_t__ EPIPE;
int /*<<< orphan*/ F_GETFL;
int /*<<< orphan*/ F_SETFL;
int O_NONBLOCK;
scalar_t__ errno;
int fcntl(int, int /*<<< orphan*/, int);
int recv(int, char *, int, int /*<<< orphan*/);
int brecv(int s, void *buf, int len) {
  int oldfl, ret, comp = len;
  oldfl = fcntl(s, F_GETFL, 0);
  if (oldfl & O_NONBLOCK)
    fcntl(s, F_SETFL, oldfl & (~O_NONBLOCK));
  while (comp) {
  brecv_restart:
    if ((ret = recv(s, (char *)buf, comp, 0)) < 0) {
      if (errno == EINTR)
        goto brecv_restart;
      return (-1);
    }
    if (!ret) {
      errno = EPIPE;
      return (-1);
    }
    comp -= ret;
    buf = (char *)buf + ret;
  }
  return (len - comp);
}


x86 assembly (clang, -O0):
<brecv>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e7e(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1040 <fcntl@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
mov    0x2e90(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     1185 <brecv+0x65>
mov    -0x8(%rbp),%edi
mov    0x2e63(%rip),%rax
mov    (%rax),%esi
mov    -0x18(%rbp),%edx
mov    0x2e6f(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    %eax,%edx
call   1040 <fcntl@plt>
jmp    118a <brecv+0x6a>
cmpl   $0x0,-0x20(%rbp)
je     122a <brecv+0x10a>
jmp    1199 <brecv+0x79>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    11e1 <brecv+0xc1>
mov    0x2e0b(%rip),%rax
mov    (%rax),%rax
mov    0x2df1(%rip),%rcx
cmp    (%rcx),%rax
jne    11d5 <brecv+0xb5>
jmp    1199 <brecv+0x79>
movl   $0xffffffff,-0x4(%rbp)
jmp    1233 <brecv+0x113>
cmpl   $0x0,-0x1c(%rbp)
jne    120b <brecv+0xeb>
mov    0x2de6(%rip),%rax
mov    (%rax),%rcx
mov    0x2dcc(%rip),%rax
mov    %rcx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1233 <brecv+0x113>
mov    -0x1c(%rbp),%ecx
mov    -0x20(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
jmp    118a <brecv+0x6a>
mov    -0x14(%rbp),%eax
sub    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<brecv>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%r14d
mov    %rsi,%rbx
mov    %edi,%r12d
mov    0x2e85(%rip),%rax
mov    (%rax),%esi
xor    %r15d,%r15d
xor    %edx,%edx
call   1040 <fcntl@plt>
mov    0x2e9a(%rip),%rcx
mov    (%rcx),%ecx
test   %eax,%ecx
je     116b <brecv+0x4b>
mov    0x2e75(%rip),%rdx
mov    (%rdx),%esi
not    %ecx
and    %ecx,%eax
mov    %r12d,%edi
mov    %eax,%edx
call   1040 <fcntl@plt>
test   %r14d,%r14d
je     11d1 <brecv+0xb1>
mov    0x2e51(%rip),%r15
mov    0x2e3a(%rip),%r13
mov    %r14d,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r12d,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %eax,%eax
jns    11ae <brecv+0x8e>
mov    (%r15),%rax
cmp    0x0(%r13),%rax
je     1190 <brecv+0x70>
jmp    11cb <brecv+0xab>
je     11be <brecv+0x9e>
mov    %eax,%ecx
add    %rcx,%rbx
sub    %eax,%ebp
jne    1190 <brecv+0x70>
mov    %r14d,%r15d
jmp    11d1 <brecv+0xb1>
mov    0x2e13(%rip),%rax
mov    (%rax),%rax
mov    %rax,(%r15)
mov    $0xffffffff,%r15d
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@F_GETFL = global i32 0
@O_NONBLOCK = global i32 0
@F_SETFL = global i32 0
@errno = global i64 0
@EINTR = global i64 0
@EPIPE = global i64 0
define i32 @brecv(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 i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%11 = load i32, i32* %7
store i32 %11, i32* %10
%12 = load i32, i32* %5
%13 = load i32, i32* @F_GETFL
%14 = call i32 @fcntl(i32 %12, i32 %13, i32 0)
store i32 %14, i32* %8
%15 = load i32, i32* %8
%16 = load i32, i32* @O_NONBLOCK
%17 = and i32 %15, %16
%18 = icmp ne i32 %17, 0
br i1 %18, label %19, label %27
19: ; preds = %3
%20 = load i32, i32* %5
%21 = load i32, i32* @F_SETFL
%22 = load i32, i32* %8
%23 = load i32, i32* @O_NONBLOCK
%24 = xor i32 %23, -1
%25 = and i32 %22, %24
%26 = call i32 @fcntl(i32 %20, i32 %21, i32 %25)
br label %27
27: ; preds = %19, %3
br label %28
28: ; preds = %49, %27
%29 = load i32, i32* %10
%30 = icmp ne i32 %29, 0
br i1 %30, label %31, label %57
31: ; preds = %28
br label %32
32: ; preds = %42, %31
%33 = load i32, i32* %5
%34 = load i8*, i8** %6
%35 = load i32, i32* %10
%36 = call i32 @recv(i32 %33, i8* %34, i32 %35, i32 0)
store i32 %36, i32* %9
%37 = icmp slt i32 %36, 0
br i1 %37, label %38, label %44
38: ; preds = %32
%39 = load i64, i64* @errno
%40 = load i64, i64* @EINTR
%41 = icmp eq i64 %39, %40
br i1 %41, label %42, label %43
42: ; preds = %38
br label %32
43: ; preds = %38
store i32 -1, i32* %4
br label %61
44: ; preds = %32
%45 = load i32, i32* %9
%46 = icmp ne i32 %45, 0
br i1 %46, label %49, label %47
47: ; preds = %44
%48 = load i64, i64* @EPIPE
store i64 %48, i64* @errno
store i32 -1, i32* %4
br label %61
49: ; preds = %44
%50 = load i32, i32* %9
%51 = load i32, i32* %10
%52 = sub nsw i32 %51, %50
store i32 %52, i32* %10
%53 = load i8*, i8** %6
%54 = load i32, i32* %9
%55 = sext i32 %54 to i64
%56 = getelementptr inbounds i8, i8* %53, i64 %55
store i8* %56, i8** %6
br label %28
57: ; preds = %28
%58 = load i32, i32* %7
%59 = load i32, i32* %10
%60 = sub nsw i32 %58, %59
store i32 %60, i32* %4
br label %61
61: ; preds = %57, %47, %43
%62 = load i32, i32* %4
ret i32 %62
}

LLVM IR (-O2):
@F_GETFL = global i32 0
@O_NONBLOCK = global i32 0
@F_SETFL = global i32 0
@errno = global i64 0
@EINTR = global i64 0
@EPIPE = global i64 0
define i32 @brecv(i32 %0, i8* %1, i32 %2) {
%4 = load i32, i32* @F_GETFL
%5 = tail call i32 @fcntl(i32 %0, i32 %4, i32 0)
%6 = load i32, i32* @O_NONBLOCK
%7 = and i32 %6, %5
%8 = icmp eq i32 %7, 0
br i1 %8, label %14, label %9
9: ; preds = %3
%10 = load i32, i32* @F_SETFL
%11 = xor i32 %6, -1
%12 = and i32 %5, %11
%13 = tail call i32 @fcntl(i32 %0, i32 %10, i32 %12)
br label %14
14: ; preds = %9, %3
%15 = icmp eq i32 %2, 0
br i1 %15, label %35, label %16
16: ; preds = %14, %30
%17 = phi i32 [ %31, %30 ], [ %2, %14 ]
%18 = phi i8* [ %33, %30 ], [ %1, %14 ]
br label %19
19: ; preds = %16, %22
%20 = tail call i32 @recv(i32 %0, i8* %18, i32 %17, i32 0)
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %26
22: ; preds = %19
%23 = load i64, i64* @errno
%24 = load i64, i64* @EINTR
%25 = icmp eq i64 %23, %24
br i1 %25, label %19, label %35
26: ; preds = %19
%27 = icmp eq i32 %20, 0
br i1 %27, label %28, label %30
28: ; preds = %26
%29 = load i64, i64* @EPIPE
store i64 %29, i64* @errno
br label %35
30: ; preds = %26
%31 = sub nsw i32 %17, %20
%32 = zext i32 %20 to i64
%33 = getelementptr inbounds i8, i8* %18, i64 %32
%34 = icmp eq i32 %31, 0
br i1 %34, label %35, label %16
35: ; preds = %30, %22, %14, %28
%36 = phi i32 [ -1, %28 ], [ 0, %14 ], [ -1, %22 ], [ %2, %30 ]
ret i32 %36
}