#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 /*<<< orphan*/ exit(int);
int /*<<< orphan*/ printf(char *);
int read(int, void *, int);
void readit(int f, void *buf, int len) {
  int res;
  while (len > 0) {
    if ((res = read(f, buf, len)) <= 0) {
      printf("Read failed: %m");
      exit(5);
    }
    len -= res;
    buf += res;
  }
}


x86 assembly (clang, -O0):
<readit>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jle    119e <readit+0x6e>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1040 <read@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jg     117d <readit+0x4d>
lea    0xe92(%rip),%rdi
call   1030 <printf@plt>
mov    $0x5,%edi
call   1050 <exit@plt>
mov    -0x18(%rbp),%ecx
mov    -0x14(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x14(%rbp)
mov    -0x18(%rbp),%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
jmp    1142 <readit+0x12>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readit>:
push   %rbp
push   %r14
push   %rbx
test   %edx,%edx
jle    115a <readit+0x2a>
mov    %edx,%ebp
mov    %rsi,%rbx
mov    %edi,%r14d
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1040 <read@plt>
test   %eax,%eax
jle    115f <readit+0x2f>
mov    %eax,%ecx
add    %rcx,%rbx
sub    %eax,%ebp
jg     1140 <readit+0x10>
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe9a(%rip),%rdi
call   1030 <printf@plt>
mov    $0x5,%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@.str = [16 x i8] c"Read failed: %m\00"
define void @readit(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32 %2, i32* %6
br label %8
8: ; preds = %20, %3
%9 = load i32, i32* %6
%10 = icmp sgt i32 %9, 0
br i1 %10, label %11, label %28
11: ; preds = %8
%12 = load i32, i32* %4
%13 = load i8*, i8** %5
%14 = load i32, i32* %6
%15 = call i32 @read(i32 %12, i8* %13, i32 %14)
store i32 %15, i32* %7
%16 = icmp sle i32 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %11
%18 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%19 = call i32 @exit(i32 5)
unreachable
20: ; preds = %11
%21 = load i32, i32* %7
%22 = load i32, i32* %6
%23 = sub nsw i32 %22, %21
store i32 %23, i32* %6
%24 = load i32, i32* %7
%25 = load i8*, i8** %5
%26 = sext i32 %24 to i64
%27 = getelementptr i8, i8* %25, i64 %26
store i8* %27, i8** %5
br label %8
28: ; preds = %8
ret void
}

LLVM IR (-O2):
@.str = [16 x i8] c"Read failed: %m\00"
define void @readit(i32 %0, i8* nocapture %1, i32 %2) {
%4 = icmp sgt i32 %2, 0
br i1 %4, label %5, label %18
5: ; preds = %3, %13
%6 = phi i8* [ %16, %13 ], [ %1, %3 ]
%7 = phi i32 [ %14, %13 ], [ %2, %3 ]
%8 = tail call i32 @read(i32 %0, i8* %6, i32 %7)
%9 = icmp slt i32 %8, 1
br i1 %9, label %10, label %13
10: ; preds = %5
%11 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%12 = tail call i32 @exit(i32 5)
unreachable
13: ; preds = %5
%14 = sub nsw i32 %7, %8
%15 = zext i32 %8 to i64
%16 = getelementptr i8, i8* %6, i64 %15
%17 = icmp sgt i32 %14, 0
br i1 %17, label %5, label %18
18: ; preds = %13, %3
ret void
}