#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 */
typedef scalar_t__ ssize_t;

/* Variables and functions */
int EAGAIN;
int EINTR;
int errno;
int /*<<< orphan*/ msg(char *, size_t, ...);
scalar_t__ read(int, void *, size_t);
int /*<<< orphan*/ strerror(int);
ssize_t read_len(int fd, const void *buf, size_t len) {
  const char *bufptr = buf;
  size_t read_actual = 0;
  if (len == 0)
    return 0;
  while (1) {
    ssize_t cur = read(fd, (void *)bufptr, len - read_actual);
    if (cur < 0) {
      int real_errno;
      if (errno == EINTR || errno == EAGAIN)
        continue;
      real_errno = errno;
      msg("Could not read %u bytes: %s", len - read_actual, strerror(errno));
      errno = real_errno;
      break;
    }
    if (cur == 0) {
      msg("read return 0, read_actual: %u", read_actual);
      return 0;
    }
    bufptr += cur;
    read_actual += cur;
    if (read_actual >= len)
      break;
  }
  return read_actual;
}


x86 assembly (clang, -O0):
<read_len>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    116b <read_len+0x3b>
movq   $0x0,-0x8(%rbp)
jmp    1275 <read_len+0x145>
jmp    1170 <read_len+0x40>
mov    -0xc(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0x20(%rbp),%rdx
sub    -0x30(%rbp),%rdx
call   1030 <read@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jge    1213 <read_len+0xe3>
mov    0x2e46(%rip),%rax
mov    (%rax),%eax
mov    0x2e35(%rip),%rcx
cmp    (%rcx),%eax
je     11c3 <read_len+0x93>
mov    0x2e2e(%rip),%rax
mov    (%rax),%eax
mov    0x2e0d(%rip),%rcx
cmp    (%rcx),%eax
jne    11c8 <read_len+0x98>
jmp    1170 <read_len+0x40>
mov    0x2e11(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x20(%rbp),%rax
sub    -0x30(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    0x2df9(%rip),%rax
mov    (%rax),%edi
call   1040 <strerror@plt>
mov    -0x48(%rbp),%rsi
mov    %eax,%edx
lea    0xe05(%rip),%rdi
mov    $0x0,%al
call   1050 <msg@plt>
mov    -0x3c(%rbp),%ecx
mov    0x2dd4(%rip),%rax
mov    %ecx,(%rax)
jmp    126d <read_len+0x13d>
cmpq   $0x0,-0x38(%rbp)
jne    123d <read_len+0x10d>
mov    -0x30(%rbp),%rsi
lea    0xdf3(%rip),%rdi
mov    $0x0,%al
call   1050 <msg@plt>
movq   $0x0,-0x8(%rbp)
jmp    1275 <read_len+0x145>
mov    -0x38(%rbp),%rax
add    -0x28(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x38(%rbp),%rax
add    -0x30(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
cmp    -0x20(%rbp),%rax
jb     1268 <read_len+0x138>
jmp    126d <read_len+0x13d>
jmp    1170 <read_len+0x40>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_len>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
test   %rdx,%rdx
je     11f3 <read_len+0xc3>
mov    %rdx,%rbp
mov    %rsi,%r13
mov    %edi,%r12d
xor    %eax,%eax
mov    0x2e87(%rip),%r14
mov    0x2e78(%rip),%r15
mov    %rdx,0x10(%rsp)
mov    %rax,0x8(%rsp)
sub    %rax,%rbp
nopl   (%rax)
mov    %r12d,%edi
mov    %r13,%rsi
mov    %rbp,%rdx
call   1030 <read@plt>
test   %rax,%rax
jns    11a0 <read_len+0x70>
mov    (%r14),%ebx
cmp    (%r15),%ebx
je     1170 <read_len+0x40>
mov    0x2e36(%rip),%rax
cmp    (%rax),%ebx
je     1170 <read_len+0x40>
jmp    11bc <read_len+0x8c>
nopl   0x0(%rax,%rax,1)
je     11e0 <read_len+0xb0>
add    %rax,%r13
mov    0x8(%rsp),%rcx
add    %rax,%rcx
mov    %rcx,%rax
mov    0x10(%rsp),%rbp
cmp    %rbp,%rcx
jb     1165 <read_len+0x35>
jmp    11f5 <read_len+0xc5>
mov    %ebx,%edi
call   1040 <strerror@plt>
lea    0xe36(%rip),%rdi
mov    %rbp,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1050 <msg@plt>
mov    %ebx,(%r14)
mov    0x8(%rsp),%rax
jmp    11f5 <read_len+0xc5>
lea    0xe35(%rip),%rdi
mov    0x8(%rsp),%rsi
xor    %eax,%eax
call   1050 <msg@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i32 0
@EINTR = global i32 0
@EAGAIN = global i32 0
@.str = [28 x i8] c"Could not read %u bytes: %s\00"
@.str.1 = [31 x i8] c"read return 0, read_actual: %u\00"
define i64 @read_len(i32 %0, i8* %1, i64 %2) {
%4 = alloca i64
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i64
%11 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%12 = load i8*, i8** %6
store i8* %12, i8** %8
store i64 0, i64* %9
%13 = load i64, i64* %7
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %3
store i64 0, i64* %4
br label %64
16: ; preds = %3
br label %17
17: ; preds = %16, %34, %61
%18 = load i32, i32* %5
%19 = load i8*, i8** %8
%20 = load i64, i64* %7
%21 = load i64, i64* %9
%22 = sub i64 %20, %21
%23 = call i64 @read(i32 %18, i8* %19, i64 %22)
store i64 %23, i64* %10
%24 = load i64, i64* %10
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %44
26: ; preds = %17
%27 = load i32, i32* @errno
%28 = load i32, i32* @EINTR
%29 = icmp eq i32 %27, %28
br i1 %29, label %34, label %30
30: ; preds = %26
%31 = load i32, i32* @errno
%32 = load i32, i32* @EAGAIN
%33 = icmp eq i32 %31, %32
br i1 %33, label %34, label %35
34: ; preds = %30, %26
br label %17
35: ; preds = %30
%36 = load i32, i32* @errno
store i32 %36, i32* %11
%37 = load i64, i64* %7
%38 = load i64, i64* %9
%39 = sub i64 %37, %38
%40 = load i32, i32* @errno
%41 = call i32 @strerror(i32 %40)
%42 = call i32 (i8*, i64, ...) @msg(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i64 %39, i32 %41)
%43 = load i32, i32* %11
store i32 %43, i32* @errno
br label %62
44: ; preds = %17
%45 = load i64, i64* %10
%46 = icmp eq i64 %45, 0
br i1 %46, label %47, label %50
47: ; preds = %44
%48 = load i64, i64* %9
%49 = call i32 (i8*, i64, ...) @msg(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i64 %48)
store i64 0, i64* %4
br label %64
50: ; preds = %44
%51 = load i64, i64* %10
%52 = load i8*, i8** %8
%53 = getelementptr inbounds i8, i8* %52, i64 %51
store i8* %53, i8** %8
%54 = load i64, i64* %10
%55 = load i64, i64* %9
%56 = add i64 %55, %54
store i64 %56, i64* %9
%57 = load i64, i64* %9
%58 = load i64, i64* %7
%59 = icmp uge i64 %57, %58
br i1 %59, label %60, label %61
60: ; preds = %50
br label %62
61: ; preds = %50
br label %17
62: ; preds = %60, %35
%63 = load i64, i64* %9
store i64 %63, i64* %4
br label %64
64: ; preds = %62, %47, %15
%65 = load i64, i64* %4
ret i64 %65
}

LLVM IR (-O2):
@errno = global i32 0
@EINTR = global i32 0
@EAGAIN = global i32 0
@.str = [28 x i8] c"Could not read %u bytes: %s\00"
@.str.1 = [31 x i8] c"read return 0, read_actual: %u\00"
define i64 @read_len(i32 %0, i8* nocapture %1, i64 %2) {
%4 = icmp eq i64 %2, 0
br i1 %4, label %30, label %5
5: ; preds = %3, %26
%6 = phi i64 [ %28, %26 ], [ 0, %3 ]
%7 = phi i8* [ %27, %26 ], [ %1, %3 ]
%8 = sub i64 %2, %6
br label %9
9: ; preds = %5, %12
%10 = tail call i64 @read(i32 %0, i8* %7, i64 %8)
%11 = icmp slt i64 %10, 0
br i1 %11, label %12, label %22
12: ; preds = %9
%13 = load i32, i32* @errno
%14 = load i32, i32* @EINTR
%15 = icmp eq i32 %13, %14
%16 = load i32, i32* @EAGAIN
%17 = icmp eq i32 %13, %16
%18 = select i1 %15, i1 true, i1 %17
br i1 %18, label %9, label %19
19: ; preds = %12
%20 = tail call i32 @strerror(i32 %13)
%21 = tail call i32 (i8*, i64, ...) @msg(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i64 %8, i32 %20)
store i32 %13, i32* @errno
br label %30
22: ; preds = %9
%23 = icmp eq i64 %10, 0
br i1 %23, label %24, label %26
24: ; preds = %22
%25 = tail call i32 (i8*, i64, ...) @msg(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i64 %6)
br label %30
26: ; preds = %22
%27 = getelementptr inbounds i8, i8* %7, i64 %10
%28 = add i64 %10, %6
%29 = icmp ult i64 %28, %2
br i1 %29, label %5, label %30
30: ; preds = %26, %19, %24, %3
%31 = phi i64 [ 0, %3 ], [ 0, %24 ], [ %6, %19 ], [ %28, %26 ]
ret i64 %31
}