#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*/ Log(char *);
int /*<<< orphan*/ LogErrno(char *);
scalar_t__ read(int, char *, size_t);
size_t ReadChars(int fd, char *buf, size_t n, int eof_permitted) {
  size_t total = 0;
  while (total < n) {
    ssize_t got = read(fd, buf + total, n - total);
    if (got < 0) {
      LogErrno("read");
      return 0;
    }
    if (got == 0) {
      if (!eof_permitted) {
        Log("read: unexpected end of file");
        return 0;
      }
      break;
    }
    if ((size_t)got > n - total) {
      Log("read: overlong read (should never happen)");
    }
    total += got;
  }
  return total;
}


x86 assembly (clang, -O0):
<ReadChars>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%rax
cmp    -0x20(%rbp),%rax
jae    1201 <ReadChars+0xd1>
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
add    -0x30(%rbp),%rsi
mov    -0x20(%rbp),%rdx
sub    -0x30(%rbp),%rdx
call   1040 <read@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jge    119c <ReadChars+0x6c>
lea    0xe76(%rip),%rdi
call   1030 <LogErrno@plt>
movq   $0x0,-0x8(%rbp)
jmp    1209 <ReadChars+0xd9>
cmpq   $0x0,-0x38(%rbp)
jne    11cf <ReadChars+0x9f>
cmpl   $0x0,-0x24(%rbp)
jne    11ca <ReadChars+0x9a>
lea    0xe4d(%rip),%rdi
call   1050 <Log@plt>
movq   $0x0,-0x8(%rbp)
jmp    1209 <ReadChars+0xd9>
jmp    1201 <ReadChars+0xd1>
mov    -0x38(%rbp),%rax
mov    -0x20(%rbp),%rcx
sub    -0x30(%rbp),%rcx
cmp    %rcx,%rax
jbe    11f0 <ReadChars+0xc0>
lea    0xe37(%rip),%rdi
call   1050 <Log@plt>
mov    -0x38(%rbp),%rax
add    -0x30(%rbp),%rax
mov    %rax,-0x30(%rbp)
jmp    114e <ReadChars+0x1e>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ReadChars>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %rdx,%rdx
je     11bb <ReadChars+0x8b>
mov    %rdx,%r13
mov    %rsi,%r15
mov    %edi,%r12d
mov    %ecx,0x4(%rsp)
xor    %ebx,%ebx
jmp    1168 <ReadChars+0x38>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
add    %r14,%rbx
cmp    %r13,%rbx
jae    11bd <ReadChars+0x8d>
lea    (%r15,%rbx,1),%rsi
mov    %r13,%rbp
sub    %rbx,%rbp
mov    %r12d,%edi
mov    %rbp,%rdx
call   1040 <read@plt>
test   %rax,%rax
js     119a <ReadChars+0x6a>
je     11a8 <ReadChars+0x78>
mov    %rax,%r14
cmp    %rbp,%rax
jbe    1160 <ReadChars+0x30>
lea    0xe8f(%rip),%rdi
call   1050 <Log@plt>
jmp    1160 <ReadChars+0x30>
lea    0xe5f(%rip),%rdi
call   1030 <LogErrno@plt>
jmp    11bb <ReadChars+0x8b>
cmpl   $0x0,0x4(%rsp)
jne    11bd <ReadChars+0x8d>
lea    0xe4f(%rip),%rdi
call   1050 <Log@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [5 x i8] c"read\00"
@.str.1 = [29 x i8] c"read: unexpected end of file\00"
@.str.2 = [42 x i8] c"read: overlong read (should never happen)\00"
define i64 @ReadChars(i32 %0, i8* %1, i64 %2, i32 %3) {
%5 = alloca i64
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i32
%10 = alloca i64
%11 = alloca i64
store i32 %0, i32* %6
store i8* %1, i8** %7
store i64 %2, i64* %8
store i32 %3, i32* %9
store i64 0, i64* %10
br label %12
12: ; preds = %46, %4
%13 = load i64, i64* %10
%14 = load i64, i64* %8
%15 = icmp ult i64 %13, %14
br i1 %15, label %16, label %50
16: ; preds = %12
%17 = load i32, i32* %6
%18 = load i8*, i8** %7
%19 = load i64, i64* %10
%20 = getelementptr inbounds i8, i8* %18, i64 %19
%21 = load i64, i64* %8
%22 = load i64, i64* %10
%23 = sub i64 %21, %22
%24 = call i64 @read(i32 %17, i8* %20, i64 %23)
store i64 %24, i64* %11
%25 = load i64, i64* %11
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %16
%28 = call i32 @LogErrno(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i64 0, i64* %5
br label %52
29: ; preds = %16
%30 = load i64, i64* %11
%31 = icmp eq i64 %30, 0
br i1 %31, label %32, label %38
32: ; preds = %29
%33 = load i32, i32* %9
%34 = icmp ne i32 %33, 0
br i1 %34, label %37, label %35
35: ; preds = %32
%36 = call i32 @Log(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
store i64 0, i64* %5
br label %52
37: ; preds = %32
br label %50
38: ; preds = %29
%39 = load i64, i64* %11
%40 = load i64, i64* %8
%41 = load i64, i64* %10
%42 = sub i64 %40, %41
%43 = icmp ugt i64 %39, %42
br i1 %43, label %44, label %46
44: ; preds = %38
%45 = call i32 @Log(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.2, i64 0, i64 0))
br label %46
46: ; preds = %44, %38
%47 = load i64, i64* %11
%48 = load i64, i64* %10
%49 = add i64 %48, %47
store i64 %49, i64* %10
br label %12
50: ; preds = %37, %12
%51 = load i64, i64* %10
store i64 %51, i64* %5
br label %52
52: ; preds = %50, %35, %27
%53 = load i64, i64* %5
ret i64 %53
}

LLVM IR (-O2):
@.str = [5 x i8] c"read\00"
@.str.1 = [29 x i8] c"read: unexpected end of file\00"
@.str.2 = [42 x i8] c"read: overlong read (should never happen)\00"
define i64 @ReadChars(i32 %0, i8* nocapture %1, i64 %2, i32 %3) {
%5 = icmp eq i64 %2, 0
br i1 %5, label %27, label %6
6: ; preds = %4, %24
%7 = phi i64 [ %25, %24 ], [ 0, %4 ]
%8 = getelementptr inbounds i8, i8* %1, i64 %7
%9 = sub i64 %2, %7
%10 = tail call i64 @read(i32 %0, i8* %8, i64 %9)
%11 = icmp slt i64 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %6
%13 = tail call i32 @LogErrno(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %27
14: ; preds = %6
%15 = icmp eq i64 %10, 0
br i1 %15, label %16, label %20
16: ; preds = %14
%17 = icmp eq i32 %3, 0
br i1 %17, label %18, label %27
18: ; preds = %16
%19 = tail call i32 @Log(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
br label %27
20: ; preds = %14
%21 = icmp ugt i64 %10, %9
br i1 %21, label %22, label %24
22: ; preds = %20
%23 = tail call i32 @Log(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.2, i64 0, i64 0))
br label %24
24: ; preds = %20, %22
%25 = add i64 %10, %7
%26 = icmp ult i64 %25, %2
br i1 %26, label %6, label %27
27: ; preds = %24, %4, %18, %12, %16
%28 = phi i64 [ %7, %16 ], [ 0, %12 ], [ 0, %18 ], [ 0, %4 ], [ %25, %24 ]
ret i64 %28
}