#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 */
int EAGAIN;
int EINPROGRESS;
int EINVAL;
int errno;
int /*<<< orphan*/ perror(char *);
int recv(int, char *, int, int /*<<< orphan*/);
int ReadNBytes(int fd, char *buf, int len) {
  int toRead = len;
  int readed = 0;
  int ret = 0;
  int err = 0;
  while (toRead > 0) {
    ret = recv(fd, buf + readed, toRead, 0);
    if (ret == 0) {
      err = EINVAL;
      break;
    } else if (ret < 0) {
      err = errno;
      if (err == EAGAIN || err == EINPROGRESS) {
        err = 0;
        continue;
      }
      perror("recv failed!");
      break;
    } else {
      readed += ret;
      toRead -= ret;
    }
  }
  return err;
}


x86 assembly (clang, -O0):
<ReadNBytes>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
cmpl   $0x0,-0x18(%rbp)
jle    1202 <ReadNBytes+0xe2>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
movslq -0x1c(%rbp),%rax
add    %rax,%rsi
mov    -0x18(%rbp),%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jne    118d <ReadNBytes+0x6d>
mov    0x2e4d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
jmp    1202 <ReadNBytes+0xe2>
cmpl   $0x0,-0x20(%rbp)
jge    11e4 <ReadNBytes+0xc4>
mov    0x2e3a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    0x2e13(%rip),%rcx
cmp    (%rcx),%eax
je     11c7 <ReadNBytes+0xa7>
mov    -0x24(%rbp),%eax
mov    0x2e29(%rip),%rcx
cmp    (%rcx),%eax
jne    11d3 <ReadNBytes+0xb3>
movl   $0x0,-0x24(%rbp)
jmp    114d <ReadNBytes+0x2d>
lea    0xe26(%rip),%rdi
call   1040 <perror@plt>
jmp    1202 <ReadNBytes+0xe2>
mov    -0x20(%rbp),%eax
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x20(%rbp),%ecx
mov    -0x18(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x18(%rbp)
jmp    11fd <ReadNBytes+0xdd>
jmp    114d <ReadNBytes+0x2d>
mov    -0x24(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ReadNBytes>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
test   %edx,%edx
jle    11a6 <ReadNBytes+0x86>
mov    %edx,%r12d
mov    %edi,%r15d
xor    %ecx,%ecx
mov    0x2e97(%rip),%r14
mov    0x2e78(%rip),%r13
mov    %rsi,%rbp
mov    %rsi,0x10(%rsp)
mov    %ecx,0xc(%rsp)
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r15d,%edi
mov    %rbp,%rsi
mov    %r12d,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %eax,%eax
je     11aa <ReadNBytes+0x8a>
jns    1190 <ReadNBytes+0x70>
mov    (%r14),%ebx
cmp    0x0(%r13),%ebx
je     1160 <ReadNBytes+0x40>
mov    0x2e62(%rip),%rax
cmp    (%rax),%ebx
je     1160 <ReadNBytes+0x40>
jmp    11c4 <ReadNBytes+0xa4>
nopl   0x0(%rax)
mov    0xc(%rsp),%ecx
add    %eax,%ecx
movslq %ecx,%rbp
mov    0x10(%rsp),%rsi
add    %rsi,%rbp
sub    %eax,%r12d
jg     1150 <ReadNBytes+0x30>
xor    %ebx,%ebx
jmp    11b3 <ReadNBytes+0x93>
mov    0x2e1f(%rip),%rax
mov    (%rax),%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe35(%rip),%rdi
call   1040 <perror@plt>
jmp    11b3 <ReadNBytes+0x93>

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

LLVM IR (-O2):
@EINVAL = global i32 0
@errno = global i32 0
@EAGAIN = global i32 0
@EINPROGRESS = global i32 0
@.str = [13 x i8] c"recv failed!\00"
define i32 @ReadNBytes(i32 %0, i8* %1, i32 %2) {
%4 = icmp sgt i32 %2, 0
br i1 %4, label %5, label %31
5: ; preds = %3, %25
%6 = phi i8* [ %30, %25 ], [ %1, %3 ]
%7 = phi i32 [ %26, %25 ], [ 0, %3 ]
%8 = phi i32 [ %27, %25 ], [ %2, %3 ]
br label %9
9: ; preds = %5, %16
%10 = tail call i32 @recv(i32 %0, i8* %6, i32 %8, i32 0)
%11 = icmp eq i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %9
%13 = load i32, i32* @EINVAL
br label %31
14: ; preds = %9
%15 = icmp slt i32 %10, 0
br i1 %15, label %16, label %25
16: ; preds = %14
%17 = load i32, i32* @errno
%18 = load i32, i32* @EAGAIN
%19 = icmp eq i32 %17, %18
%20 = load i32, i32* @EINPROGRESS
%21 = icmp eq i32 %17, %20
%22 = select i1 %19, i1 true, i1 %21
br i1 %22, label %9, label %23
23: ; preds = %16
%24 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
br label %31
25: ; preds = %14
%26 = add nsw i32 %10, %7
%27 = sub nsw i32 %8, %10
%28 = icmp sgt i32 %27, 0
%29 = sext i32 %26 to i64
%30 = getelementptr inbounds i8, i8* %1, i64 %29
br i1 %28, label %5, label %31
31: ; preds = %25, %3, %23, %12
%32 = phi i32 [ %13, %12 ], [ %17, %23 ], [ 0, %3 ], [ 0, %25 ]
ret i32 %32
}