#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*/ O_RDONLY;
int open(char const *, int /*<<< orphan*/);
int read(int, char *, int);
int read_file(const char *file, char *buffer, int max_length) {
  int f = open(file, O_RDONLY);
  if (f == -1)
    return -1;
  int bytes_read = 0;
  while (true) {
    int bytes_to_read = 1024;
    if (bytes_to_read > max_length - bytes_read)
      bytes_to_read = max_length - bytes_read;
    int rv = read(f, &buffer[bytes_read], bytes_to_read);
    if (rv == -1)
      return -1;
    bytes_read += rv;
    if (rv == 0)
      return bytes_read;
  }
}


x86 assembly (clang, -O0):
<read_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2eaa(%rip),%rax
mov    (%rax),%esi
call   1040 <open@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    115e <read_file+0x3e>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d8 <read_file+0xb8>
movl   $0x0,-0x24(%rbp)
movl   $0x400,-0x28(%rbp)
mov    -0x28(%rbp),%eax
mov    -0x1c(%rbp),%ecx
sub    -0x24(%rbp),%ecx
cmp    %ecx,%eax
jle    1186 <read_file+0x66>
mov    -0x1c(%rbp),%eax
sub    -0x24(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x20(%rbp),%edi
mov    -0x18(%rbp),%rsi
movslq -0x24(%rbp),%rax
add    %rax,%rsi
mov    -0x28(%rbp),%edx
call   1030 <read@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
jne    11b5 <read_file+0x95>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d8 <read_file+0xb8>
mov    -0x2c(%rbp),%eax
add    -0x24(%rbp),%eax
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jne    11d3 <read_file+0xb3>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11d8 <read_file+0xb8>
jmp    1165 <read_file+0x45>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_file>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edx,%r14d
mov    %rsi,%r15
mov    0x2eb3(%rip),%rax
mov    (%rax),%esi
call   1040 <open@plt>
cmp    $0xffffffff,%eax
je     117c <read_file+0x5c>
mov    %eax,%ebp
xor    %ecx,%ecx
mov    $0x400,%r12d
nopl   0x0(%rax,%rax,1)
mov    %ecx,%ebx
mov    %r14d,%edx
sub    %ecx,%edx
cmp    $0x400,%edx
cmovge %r12d,%edx
movslq %ecx,%rsi
add    %r15,%rsi
mov    %ebp,%edi
call   1030 <read@plt>
cmp    $0xffffffff,%eax
je     117c <read_file+0x5c>
lea    (%rax,%rbx,1),%ecx
test   %eax,%eax
jne    1150 <read_file+0x30>
jmp    1181 <read_file+0x61>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
define i32 @read_file(i8* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%12 = load i8*, i8** %5
%13 = load i32, i32* @O_RDONLY
%14 = call i32 @open(i8* %12, i32 %13)
store i32 %14, i32* %8
%15 = load i32, i32* %8
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %18
17: ; preds = %3
store i32 -1, i32* %4
br label %49
18: ; preds = %3
store i32 0, i32* %9
br label %19
19: ; preds = %18, %48
store i32 1024, i32* %10
%20 = load i32, i32* %10
%21 = load i32, i32* %7
%22 = load i32, i32* %9
%23 = sub nsw i32 %21, %22
%24 = icmp sgt i32 %20, %23
br i1 %24, label %25, label %29
25: ; preds = %19
%26 = load i32, i32* %7
%27 = load i32, i32* %9
%28 = sub nsw i32 %26, %27
store i32 %28, i32* %10
br label %29
29: ; preds = %25, %19
%30 = load i32, i32* %8
%31 = load i8*, i8** %6
%32 = load i32, i32* %9
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds i8, i8* %31, i64 %33
%35 = load i32, i32* %10
%36 = call i32 @read(i32 %30, i8* %34, i32 %35)
store i32 %36, i32* %11
%37 = load i32, i32* %11
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %40
39: ; preds = %29
store i32 -1, i32* %4
br label %49
40: ; preds = %29
%41 = load i32, i32* %11
%42 = load i32, i32* %9
%43 = add nsw i32 %42, %41
store i32 %43, i32* %9
%44 = load i32, i32* %11
%45 = icmp eq i32 %44, 0
br i1 %45, label %46, label %48
46: ; preds = %40
%47 = load i32, i32* %9
store i32 %47, i32* %4
br label %49
48: ; preds = %40
br label %19
49: ; preds = %46, %39, %17
%50 = load i32, i32* %4
ret i32 %50
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
define i32 @read_file(i8* nocapture %0, i8* nocapture %1, i32 %2) {
%4 = load i32, i32* @O_RDONLY
%5 = tail call i32 @open(i8* %0, i32 %4)
%6 = icmp eq i32 %5, -1
br i1 %6, label %19, label %7
7: ; preds = %3, %16
%8 = phi i32 [ %17, %16 ], [ 0, %3 ]
%9 = sub nsw i32 %2, %8
%10 = icmp slt i32 %9, 1024
%11 = select i1 %10, i32 %9, i32 1024
%12 = sext i32 %8 to i64
%13 = getelementptr inbounds i8, i8* %1, i64 %12
%14 = tail call i32 @read(i32 %5, i8* %13, i32 %11)
%15 = icmp eq i32 %14, -1
br i1 %15, label %19, label %16
16: ; preds = %7
%17 = add nsw i32 %14, %8
%18 = icmp eq i32 %14, 0
br i1 %18, label %19, label %7
19: ; preds = %7, %16, %3
%20 = phi i32 [ -1, %3 ], [ -1, %7 ], [ %8, %16 ]
ret i32 %20
}