#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 /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
scalar_t__ read(int, void *, size_t);
int /*<<< orphan*/ stderr;
size_t read_or_die(int fd, void *buff, size_t nbytes) {
  assert(buff);
  void *ptr = buff;
  while (nbytes > 0) {
    ssize_t n = read(fd, ptr, nbytes);
    if (n < 0) {
      do {
        fprintf(stderr, "read");
        exit(EXIT_FAILURE);
      } while (0);
    }
    if (n == 0) {
      break;
    }
    nbytes -= n;
    ptr += n;
  }
  assert(ptr >= buff);
  return (size_t)(ptr - buff);
}


x86 assembly (clang, -O0):
<read_or_die>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    %eax,%edi
call   1040 <assert@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x18(%rbp)
jbe    11e8 <read_or_die+0xa8>
mov    -0x4(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <read@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jge    11b8 <read_or_die+0x78>
jmp    1195 <read_or_die+0x55>
mov    0x2e44(%rip),%rax
mov    (%rax),%edi
lea    0xe5b(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x2e27(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
cmpq   $0x0,-0x28(%rbp)
jne    11c8 <read_or_die+0x88>
jmp    11e8 <read_or_die+0xa8>
mov    -0x28(%rbp),%rcx
mov    -0x18(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x18(%rbp)
mov    -0x28(%rbp),%rax
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
jmp    1166 <read_or_die+0x26>
mov    -0x20(%rbp),%rax
cmp    -0x10(%rbp),%rax
setae  %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    -0x20(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_or_die>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %edi,%ebp
mov    %r14d,%edi
call   1040 <assert@plt>
mov    %r14,%r15
nopw   0x0(%rax,%rax,1)
test   %rbx,%rbx
je     1182 <read_or_die+0x42>
mov    %ebp,%edi
mov    %r15,%rsi
mov    %rbx,%rdx
call   1030 <read@plt>
test   %rax,%rax
js     119e <read_or_die+0x5e>
sub    %rax,%rbx
add    %rax,%r15
test   %rax,%rax
jne    1160 <read_or_die+0x20>
xor    %edi,%edi
sub    %r14,%r15
setae  %dil
call   1040 <assert@plt>
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2e3b(%rip),%rax
mov    (%rax),%edi
lea    0xe52(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x2e1e(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [5 x i8] c"read\00"
@EXIT_FAILURE = global i32 0
define i64 @read_or_die(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i64
store i32 %0, i32* %4
store i8* %1, i8** %5
store i64 %2, i64* %6
%9 = load i8*, i8** %5
%10 = ptrtoint i8* %9 to i32
%11 = call i32 @assert(i32 %10)
%12 = load i8*, i8** %5
store i8* %12, i8** %7
br label %13
13: ; preds = %34, %3
%14 = load i64, i64* %6
%15 = icmp ugt i64 %14, 0
br i1 %15, label %16, label %41
16: ; preds = %13
%17 = load i32, i32* %4
%18 = load i8*, i8** %7
%19 = load i64, i64* %6
%20 = call i64 @read(i32 %17, i8* %18, i64 %19)
store i64 %20, i64* %8
%21 = load i64, i64* %8
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %30
23: ; preds = %16
br label %24
24: ; preds = %23
%25 = load i32, i32* @stderr
%26 = call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%27 = load i32, i32* @EXIT_FAILURE
%28 = call i32 @exit(i32 %27)
unreachable
29: ; No predecessors!
br label %30
30: ; preds = %29, %16
%31 = load i64, i64* %8
%32 = icmp eq i64 %31, 0
br i1 %32, label %33, label %34
33: ; preds = %30
br label %41
34: ; preds = %30
%35 = load i64, i64* %8
%36 = load i64, i64* %6
%37 = sub i64 %36, %35
store i64 %37, i64* %6
%38 = load i64, i64* %8
%39 = load i8*, i8** %7
%40 = getelementptr i8, i8* %39, i64 %38
store i8* %40, i8** %7
br label %13
41: ; preds = %33, %13
%42 = load i8*, i8** %7
%43 = load i8*, i8** %5
%44 = icmp uge i8* %42, %43
%45 = zext i1 %44 to i32
%46 = call i32 @assert(i32 %45)
%47 = load i8*, i8** %7
%48 = load i8*, i8** %5
%49 = ptrtoint i8* %47 to i64
%50 = ptrtoint i8* %48 to i64
%51 = sub i64 %49, %50
ret i64 %51
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [5 x i8] c"read\00"
@EXIT_FAILURE = global i32 0
define i64 @read_or_die(i32 %0, i8* %1, i64 %2) {
%4 = ptrtoint i8* %1 to i64
%5 = trunc i64 %4 to i32
%6 = tail call i32 @assert(i32 %5)
br label %7
7: ; preds = %19, %3
%8 = phi i8* [ %1, %3 ], [ %22, %19 ]
%9 = phi i64 [ %2, %3 ], [ %21, %19 ]
%10 = icmp eq i64 %9, 0
br i1 %10, label %23, label %11
11: ; preds = %7
%12 = tail call i64 @read(i32 %0, i8* %8, i64 %9)
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %19
14: ; preds = %11
%15 = load i32, i32* @stderr
%16 = tail call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%17 = load i32, i32* @EXIT_FAILURE
%18 = tail call i32 @exit(i32 %17)
unreachable
19: ; preds = %11
%20 = icmp eq i64 %12, 0
%21 = sub i64 %9, %12
%22 = getelementptr i8, i8* %8, i64 %12
br i1 %20, label %23, label %7
23: ; preds = %19, %7
%24 = phi i8* [ %22, %19 ], [ %8, %7 ]
%25 = icmp uge i8* %24, %1
%26 = zext i1 %25 to i32
%27 = tail call i32 @assert(i32 %26)
%28 = ptrtoint i8* %24 to i64
%29 = sub i64 %28, %4
ret i64 %29
}