#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*/ O_RDONLY;
int open(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ panic(char *);
scalar_t__ read(int, char *, scalar_t__);
int read_file(const char *in_path, char *buf, int maxsize) {
  ssize_t nread, totread = 0, ssize_inc = 8192;
  int from;
  if ((from = open(in_path, O_RDONLY)) < 0)
    return -1;
  maxsize -= ssize_inc;
  if (maxsize < 0)
    panic("input file exceeds max size");
  while ((nread = read(from, buf, ssize_inc)) > 0) {
    if (nread < 0)
      return -1;
    totread += nread;
    if (nread < ssize_inc)
      return totread;
    buf += ssize_inc;
    if (totread > maxsize)
      panic("file exceeds max size\n");
  }
  return totread;
}


x86 assembly (clang, -O0):
<read_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
movq   $0x0,-0x30(%rbp)
movq   $0x2000,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e8a(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x3c(%rbp)
cmp    $0x0,%eax
jge    117d <read_file+0x4d>
movl   $0xffffffff,-0x4(%rbp)
jmp    1236 <read_file+0x106>
mov    -0x38(%rbp),%rcx
movslq -0x1c(%rbp),%rax
sub    %rcx,%rax
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11a1 <read_file+0x71>
lea    0xe64(%rip),%rdi
call   1030 <panic@plt>
jmp    11a6 <read_file+0x76>
mov    -0x3c(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x38(%rbp),%rdx
call   1040 <read@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jle    122f <read_file+0xff>
cmpq   $0x0,-0x28(%rbp)
jge    11db <read_file+0xab>
movl   $0xffffffff,-0x4(%rbp)
jmp    1236 <read_file+0x106>
mov    -0x28(%rbp),%rax
add    -0x30(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x38(%rbp),%rax
jge    1201 <read_file+0xd1>
mov    -0x30(%rbp),%rax
mov    %eax,-0x4(%rbp)
jmp    1236 <read_file+0x106>
mov    -0x38(%rbp),%rax
add    -0x18(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x30(%rbp),%rax
movslq -0x1c(%rbp),%rcx
cmp    %rcx,%rax
jle    122a <read_file+0xfa>
lea    0xdf7(%rip),%rdi
call   1030 <panic@plt>
jmp    11a6 <read_file+0x76>
mov    -0x30(%rbp),%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_file>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edx,%ebp
mov    %rsi,%rbx
mov    0x2ea4(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
test   %eax,%eax
js     11c6 <read_file+0x96>
mov    %eax,%r14d
add    $0xffffe000,%ebp
jns    1166 <read_file+0x36>
lea    0xe9f(%rip),%rdi
call   1030 <panic@plt>
mov    $0x2000,%edx
mov    %r14d,%edi
mov    %rbx,%rsi
call   1040 <read@plt>
test   %rax,%rax
jle    11cd <read_file+0x9d>
movslq %ebp,%r12
add    $0x2000,%rbx
xor    %ebp,%ebp
lea    0xe8e(%rip),%r15
jmp    11ac <read_file+0x7c>
mov    $0x2000,%edx
mov    %r14d,%edi
mov    %rbx,%rsi
call   1040 <read@plt>
add    $0x2000,%rbx
test   %rax,%rax
jle    11cf <read_file+0x9f>
add    %rax,%rbp
cmp    $0x1fff,%rax
jbe    11cf <read_file+0x9f>
cmp    %r12,%rbp
jle    1190 <read_file+0x60>
mov    %r15,%rdi
call   1030 <panic@plt>
jmp    1190 <read_file+0x60>
mov    $0xffffffff,%ebp
jmp    11cf <read_file+0x9f>
xor    %ebp,%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
@.str = [28 x i8] c"input file exceeds max size\00"
@.str.1 = [23 x i8] c"file exceeds max size\0A\00"
define i32 @read_file(i8* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i64
%9 = alloca i64
%10 = alloca i64
%11 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i64 0, i64* %9
store i64 8192, i64* %10
%12 = load i8*, i8** %5
%13 = load i32, i32* @O_RDONLY
%14 = call i32 @open(i8* %12, i32 %13)
store i32 %14, i32* %11
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %3
store i32 -1, i32* %4
br label %62
17: ; preds = %3
%18 = load i64, i64* %10
%19 = load i32, i32* %7
%20 = sext i32 %19 to i64
%21 = sub nsw i64 %20, %18
%22 = trunc i64 %21 to i32
store i32 %22, i32* %7
%23 = load i32, i32* %7
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %17
%26 = call i32 @panic(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
br label %27
27: ; preds = %25, %17
br label %28
28: ; preds = %58, %27
%29 = load i32, i32* %11
%30 = load i8*, i8** %6
%31 = load i64, i64* %10
%32 = call i64 @read(i32 %29, i8* %30, i64 %31)
store i64 %32, i64* %8
%33 = icmp sgt i64 %32, 0
br i1 %33, label %34, label %59
34: ; preds = %28
%35 = load i64, i64* %8
%36 = icmp slt i64 %35, 0
br i1 %36, label %37, label %38
37: ; preds = %34
store i32 -1, i32* %4
br label %62
38: ; preds = %34
%39 = load i64, i64* %8
%40 = load i64, i64* %9
%41 = add nsw i64 %40, %39
store i64 %41, i64* %9
%42 = load i64, i64* %8
%43 = load i64, i64* %10
%44 = icmp slt i64 %42, %43
br i1 %44, label %45, label %48
45: ; preds = %38
%46 = load i64, i64* %9
%47 = trunc i64 %46 to i32
store i32 %47, i32* %4
br label %62
48: ; preds = %38
%49 = load i64, i64* %10
%50 = load i8*, i8** %6
%51 = getelementptr inbounds i8, i8* %50, i64 %49
store i8* %51, i8** %6
%52 = load i64, i64* %9
%53 = load i32, i32* %7
%54 = sext i32 %53 to i64
%55 = icmp sgt i64 %52, %54
br i1 %55, label %56, label %58
56: ; preds = %48
%57 = call i32 @panic(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
br label %58
58: ; preds = %56, %48
br label %28
59: ; preds = %28
%60 = load i64, i64* %9
%61 = trunc i64 %60 to i32
store i32 %61, i32* %4
br label %62
62: ; preds = %59, %45, %37, %16
%63 = load i32, i32* %4
ret i32 %63
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@.str = [28 x i8] c"input file exceeds max size\00"
@.str.1 = [23 x i8] c"file exceeds max size\0A\00"
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 slt i32 %5, 0
br i1 %6, label %35, label %7
7: ; preds = %3
%8 = add i32 %2, -8192
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %7
%11 = tail call i32 @panic(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
br label %12
12: ; preds = %10, %7
%13 = tail call i64 @read(i32 %5, i8* %1, i64 8192)
%14 = icmp sgt i64 %13, 0
br i1 %14, label %15, label %35
15: ; preds = %12
%16 = sext i32 %8 to i64
br label %17
17: ; preds = %15, %30
%18 = phi i64 [ %13, %15 ], [ %31, %30 ]
%19 = phi i8* [ %1, %15 ], [ %26, %30 ]
%20 = phi i64 [ 0, %15 ], [ %21, %30 ]
%21 = add nsw i64 %18, %20
%22 = icmp ult i64 %18, 8192
br i1 %22, label %23, label %25
23: ; preds = %17
%24 = trunc i64 %21 to i32
br label %35
25: ; preds = %17
%26 = getelementptr inbounds i8, i8* %19, i64 8192
%27 = icmp sgt i64 %21, %16
br i1 %27, label %28, label %30
28: ; preds = %25
%29 = tail call i32 @panic(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
br label %30
30: ; preds = %28, %25
%31 = tail call i64 @read(i32 %5, i8* %26, i64 8192)
%32 = icmp sgt i64 %31, 0
br i1 %32, label %17, label %33
33: ; preds = %30
%34 = trunc i64 %21 to i32
br label %35
35: ; preds = %12, %33, %3, %23
%36 = phi i32 [ %24, %23 ], [ -1, %3 ], [ 0, %12 ], [ %34, %33 ]
ret i32 %36
}