#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 /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
scalar_t__ malloc(int);
int open(char *, int /*<<< orphan*/);
long read(int, char *, long);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
char *read_file_into_buffer(char *filepath) {
  ;
  char *buffer = (char *)malloc((1 << 20));
  ;
  int fd = open(filepath, O_RDONLY);
  if (fd == -1) {
    fprintf(stderr, "Can't open input file %s!\n", filepath);
    exit(1);
  };
  long left_size = (1 << 20) - 1;
  long curr_read_bytes = -1;
  long n_read_bytes = 0;
  char *insert_here = buffer;
  while (!(left_size == 0 || curr_read_bytes == 0)) {
    curr_read_bytes = read(fd, insert_here, left_size);
    if (curr_read_bytes < 0) {
      fprintf(stderr, "Error reading from file: %s\n", strerror(errno));
      exit(1);
    }
    n_read_bytes += curr_read_bytes;
    left_size -= curr_read_bytes;
    insert_here = &insert_here[curr_read_bytes];
  };
  if (n_read_bytes < 1) {
    fprintf(stderr, "Error nothing read from file\n");
    exit(1);
  };
  buffer[n_read_bytes] = '\0';
  close(fd);
  ;
  return buffer;
}


x86 assembly (clang, -O0):
<read_file_into_buffer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    $0x100000,%edi
call   1060 <malloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e53(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11ce <read_file_into_buffer+0x5e>
mov    0x2e28(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe43(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
movq   $0xfffff,-0x20(%rbp)
movq   $0xffffffffffffffff,-0x28(%rbp)
ff
movq   $0x0,-0x30(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    $0x1,%al
cmpq   $0x0,-0x20(%rbp)
mov    %al,-0x39(%rbp)
je     1209 <read_file_into_buffer+0x99>
cmpq   $0x0,-0x28(%rbp)
sete   %al
mov    %al,-0x39(%rbp)
mov    -0x39(%rbp),%al
xor    $0xff,%al
test   $0x1,%al
jne    121b <read_file_into_buffer+0xab>
jmp    129e <read_file_into_buffer+0x12e>
mov    -0x14(%rbp),%edi
mov    -0x38(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1040 <read@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jge    1272 <read_file_into_buffer+0x102>
mov    0x2d97(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
mov    0x2d83(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x40(%rbp),%edi
mov    %rax,%rdx
lea    0xdba(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x28(%rbp),%rax
add    -0x30(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rcx
mov    -0x20(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x38(%rbp),%rax
add    -0x28(%rbp),%rax
mov    %rax,-0x38(%rbp)
jmp    11ee <read_file_into_buffer+0x7e>
cmpq   $0x1,-0x30(%rbp)
jge    12ca <read_file_into_buffer+0x15a>
mov    0x2d28(%rip),%rax
mov    (%rax),%edi
lea    0xd7f(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x10(%rbp),%rax
mov    -0x30(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x14(%rbp),%edi
call   1030 <close@plt>
mov    -0x10(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_file_into_buffer>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdi,%rbx
mov    $0x100000,%edi
call   1060 <malloc@plt>
mov    %rax,%r14
mov    0x2e59(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     1225 <read_file_into_buffer+0xb5>
mov    %eax,%r15d
mov    $0xfffff,%ebx
xor    %r12d,%r12d
mov    %r14,%rbp
mov    %r15d,%edi
mov    %rbp,%rsi
mov    %rbx,%rdx
call   1040 <read@plt>
test   %rax,%rax
js     11f1 <read_file_into_buffer+0x81>
add    %rax,%r12
sub    %rax,%rbx
je     11d3 <read_file_into_buffer+0x63>
add    %rax,%rbp
test   %rax,%rax
jne    11b0 <read_file_into_buffer+0x40>
test   %r12,%r12
jle    123a <read_file_into_buffer+0xca>
movb   $0x0,(%r14,%r12,1)
mov    %r15d,%edi
call   1030 <close@plt>
mov    %r14,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2de0(%rip),%rax
mov    (%rax),%ebx
mov    0x2dcf(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xe0c(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    0x2dac(%rip),%rax
mov    (%rax),%edi
lea    0xdcb(%rip),%rsi
mov    %rbx,%rdx
jmp    1214 <read_file_into_buffer+0xa4>
mov    0x2d97(%rip),%rax
mov    (%rax),%edi
lea    0xdee(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str = [27 x i8] c"Can't open input file %s!\0A\00"
@.str.1 = [29 x i8] c"Error reading from file: %s\0A\00"
@errno = global i32 0
@.str.2 = [30 x i8] c"Error nothing read from file\0A\00"
define i8* @read_file_into_buffer(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i64
%6 = alloca i64
%7 = alloca i64
%8 = alloca i8*
store i8* %0, i8** %2
%9 = call i64 @malloc(i32 1048576)
%10 = inttoptr i64 %9 to i8*
store i8* %10, i8** %3
%11 = load i8*, i8** %2
%12 = load i32, i32* @O_RDONLY
%13 = call i32 @open(i8* %11, i32 %12)
store i32 %13, i32* %4
%14 = load i32, i32* %4
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %21
16: ; preds = %1
%17 = load i32, i32* @stderr
%18 = load i8*, i8** %2
%19 = call i32 (i32, i8*, ...) @fprintf(i32 %17, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %18)
%20 = call i32 @exit(i32 1)
unreachable
21: ; preds = %1
store i64 1048575, i64* %5
store i64 -1, i64* %6
store i64 0, i64* %7
%22 = load i8*, i8** %3
store i8* %22, i8** %8
br label %23
23: ; preds = %45, %21
%24 = load i64, i64* %5
%25 = icmp eq i64 %24, 0
br i1 %25, label %29, label %26
26: ; preds = %23
%27 = load i64, i64* %6
%28 = icmp eq i64 %27, 0
br label %29
29: ; preds = %26, %23
%30 = phi i1 [ true, %23 ], [ %28, %26 ]
%31 = xor i1 %30, true
br i1 %31, label %32, label %55
32: ; preds = %29
%33 = load i32, i32* %4
%34 = load i8*, i8** %8
%35 = load i64, i64* %5
%36 = call i64 @read(i32 %33, i8* %34, i64 %35)
store i64 %36, i64* %6
%37 = load i64, i64* %6
%38 = icmp slt i64 %37, 0
br i1 %38, label %39, label %45
39: ; preds = %32
%40 = load i32, i32* @stderr
%41 = load i32, i32* @errno
%42 = call i8* @strerror(i32 %41)
%43 = call i32 (i32, i8*, ...) @fprintf(i32 %40, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i8* %42)
%44 = call i32 @exit(i32 1)
unreachable
45: ; preds = %32
%46 = load i64, i64* %6
%47 = load i64, i64* %7
%48 = add nsw i64 %47, %46
store i64 %48, i64* %7
%49 = load i64, i64* %6
%50 = load i64, i64* %5
%51 = sub nsw i64 %50, %49
store i64 %51, i64* %5
%52 = load i8*, i8** %8
%53 = load i64, i64* %6
%54 = getelementptr inbounds i8, i8* %52, i64 %53
store i8* %54, i8** %8
br label %23
55: ; preds = %29
%56 = load i64, i64* %7
%57 = icmp slt i64 %56, 1
br i1 %57, label %58, label %62
58: ; preds = %55
%59 = load i32, i32* @stderr
%60 = call i32 (i32, i8*, ...) @fprintf(i32 %59, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0))
%61 = call i32 @exit(i32 1)
unreachable
62: ; preds = %55
%63 = load i8*, i8** %3
%64 = load i64, i64* %7
%65 = getelementptr inbounds i8, i8* %63, i64 %64
store i8 0, i8* %65
%66 = load i32, i32* %4
%67 = call i32 @close(i32 %66)
%68 = load i8*, i8** %3
ret i8* %68
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str = [27 x i8] c"Can't open input file %s!\0A\00"
@.str.1 = [29 x i8] c"Error reading from file: %s\0A\00"
@errno = global i32 0
@.str.2 = [30 x i8] c"Error nothing read from file\0A\00"
define i8* @read_file_into_buffer(i8* %0) {
%2 = tail call i64 @malloc(i32 1048576)
%3 = inttoptr i64 %2 to i8*
%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 %7, label %11
7: ; preds = %1
%8 = load i32, i32* @stderr
%9 = tail call i32 (i32, i8*, ...) @fprintf(i32 %8, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %0)
%10 = tail call i32 @exit(i32 1)
unreachable
11: ; preds = %1, %23
%12 = phi i8* [ %26, %23 ], [ %3, %1 ]
%13 = phi i64 [ %24, %23 ], [ 0, %1 ]
%14 = phi i64 [ %25, %23 ], [ 1048575, %1 ]
%15 = tail call i64 @read(i32 %5, i8* %12, i64 %14)
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %23
17: ; preds = %11
%18 = load i32, i32* @stderr
%19 = load i32, i32* @errno
%20 = tail call i8* @strerror(i32 %19)
%21 = tail call i32 (i32, i8*, ...) @fprintf(i32 %18, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i8* %20)
%22 = tail call i32 @exit(i32 1)
unreachable
23: ; preds = %11
%24 = add nsw i64 %15, %13
%25 = sub nsw i64 %14, %15
%26 = getelementptr inbounds i8, i8* %12, i64 %15
%27 = icmp ne i64 %25, 0
%28 = icmp ne i64 %15, 0
%29 = and i1 %27, %28
br i1 %29, label %11, label %30
30: ; preds = %23
%31 = icmp slt i64 %24, 1
br i1 %31, label %32, label %36
32: ; preds = %30
%33 = load i32, i32* @stderr
%34 = tail call i32 (i32, i8*, ...) @fprintf(i32 %33, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0))
%35 = tail call i32 @exit(i32 1)
unreachable
36: ; preds = %30
%37 = getelementptr inbounds i8, i8* %3, i64 %24
store i8 0, i8* %37
%38 = tail call i32 @close(i32 %5)
ret i8* %3
}