#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*/ assert(int);
int safe_read(int, void *, int);
int read_exact(int fd, void *buf, int len) {
  int i, got = 0;
  do {
    i = safe_read(fd, buf + got, len - got);
    assert(i > 0);
    got += i;
  } while (got < len);
  assert(got == len);
  return len;
}


x86 assembly (clang, -O0):
<read_exact>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
movslq -0x1c(%rbp),%rax
add    %rax,%rsi
mov    -0x14(%rbp),%edx
sub    -0x1c(%rbp),%edx
call   1040 <safe_read@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
setg   %al
and    $0x1,%al
movzbl %al,%edi
call   1030 <assert@plt>
mov    -0x18(%rbp),%eax
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jl     1139 <read_exact+0x19>
mov    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1030 <assert@plt>
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_exact>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edx,%r12d
mov    %rsi,%r14
mov    %edi,%r15d
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
movslq %ebp,%rbx
lea    (%r14,%rbx,1),%rsi
mov    %r12d,%edx
sub    %ebx,%edx
mov    %r15d,%edi
call   1040 <safe_read@plt>
mov    %eax,%ebp
xor    %edi,%edi
test   %eax,%eax
setg   %dil
call   1030 <assert@plt>
add    %ebx,%ebp
cmp    %r12d,%ebp
jl     1140 <read_exact+0x20>
sete   %al
movzbl %al,%edi
call   1030 <assert@plt>
mov    %r12d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @read_exact(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32 %2, i32* %6
store i32 0, i32* %8
br label %9
9: ; preds = %26, %3
%10 = load i32, i32* %4
%11 = load i8*, i8** %5
%12 = load i32, i32* %8
%13 = sext i32 %12 to i64
%14 = getelementptr i8, i8* %11, i64 %13
%15 = load i32, i32* %6
%16 = load i32, i32* %8
%17 = sub nsw i32 %15, %16
%18 = call i32 @safe_read(i32 %10, i8* %14, i32 %17)
store i32 %18, i32* %7
%19 = load i32, i32* %7
%20 = icmp sgt i32 %19, 0
%21 = zext i1 %20 to i32
%22 = call i32 @assert(i32 %21)
%23 = load i32, i32* %7
%24 = load i32, i32* %8
%25 = add nsw i32 %24, %23
store i32 %25, i32* %8
br label %26
26: ; preds = %9
%27 = load i32, i32* %8
%28 = load i32, i32* %6
%29 = icmp slt i32 %27, %28
br i1 %29, label %9, label %30
30: ; preds = %26
%31 = load i32, i32* %8
%32 = load i32, i32* %6
%33 = icmp eq i32 %31, %32
%34 = zext i1 %33 to i32
%35 = call i32 @assert(i32 %34)
%36 = load i32, i32* %6
ret i32 %36
}

LLVM IR (-O2):
define i32 @read_exact(i32 %0, i8* %1, i32 returned %2) {
br label %4
4: ; preds = %4, %3
%5 = phi i32 [ 0, %3 ], [ %13, %4 ]
%6 = sext i32 %5 to i64
%7 = getelementptr i8, i8* %1, i64 %6
%8 = sub nsw i32 %2, %5
%9 = tail call i32 @safe_read(i32 %0, i8* %7, i32 %8)
%10 = icmp sgt i32 %9, 0
%11 = zext i1 %10 to i32
%12 = tail call i32 @assert(i32 %11)
%13 = add nsw i32 %9, %5
%14 = icmp slt i32 %13, %2
br i1 %14, label %4, label %15
15: ; preds = %4
%16 = icmp eq i32 %13, %2
%17 = zext i1 %16 to i32
%18 = tail call i32 @assert(i32 %17)
ret i32 %2
}