#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 */
struct stat {
  size_t st_size;
};

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ free(char *);
int fstat(int, struct stat *);
scalar_t__ malloc(size_t);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, char);
int read(int, char *, size_t);
int main(int argc, char *argv[]) {
  char *filename;
  struct stat file_struct;
  int fd;
  if (argc < 2)
    exit(-1);
  filename = argv[1];
  fd = open(filename, O_RDONLY);
  int res = fstat(fd, &file_struct);
  if (res != 0)
    exit(-1);
  size_t filesize = file_struct.st_size;
  char *buff = (char *)malloc(filesize);
  int read_res = read(fd, buff, filesize);
  if (read_res == -1)
    exit(-1);
  for (int i = 0; i < filesize; i++) {
    printf("%c", buff[i]);
  }
  free(buff);
  close(fd);
  return (0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11aa <main+0x2a>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    0x2e27(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   10a0 <fstat@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
je     11ee <main+0x6e>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
call   1070 <malloc@plt>
mov    %rax,-0x38(%rbp)
mov    -0x24(%rbp),%edi
mov    -0x38(%rbp),%rsi
mov    -0x30(%rbp),%rdx
call   1060 <read@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0xffffffff,-0x3c(%rbp)
jne    122a <main+0xaa>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
movl   $0x0,-0x40(%rbp)
movslq -0x40(%rbp),%rax
cmp    -0x30(%rbp),%rax
jae    1265 <main+0xe5>
mov    -0x38(%rbp),%rax
movslq -0x40(%rbp),%rcx
lea    0xdb2(%rip),%rdi
movsbl (%rax,%rcx,1),%esi
call   1040 <printf@plt>
mov    -0x40(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x40(%rbp)
jmp    1231 <main+0xb1>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
cmp    $0x1,%edi
jle    1225 <main+0xa5>
mov    0x8(%rsi),%rdi
mov    0x2e48(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,%r14d
lea    0x8(%rsp),%rsi
mov    %eax,%edi
call   10a0 <fstat@plt>
test   %eax,%eax
jne    1225 <main+0xa5>
mov    0x8(%rsp),%r12
mov    %r12,%rdi
call   1070 <malloc@plt>
mov    %rax,%rbp
mov    %r14d,%edi
mov    %rax,%rsi
mov    %r12,%rdx
call   1060 <read@plt>
cmp    $0xffffffff,%eax
je     1225 <main+0xa5>
test   %r12,%r12
je     1206 <main+0x86>
xor    %ebx,%ebx
lea    0xe15(%rip),%r15
nopl   0x0(%rax,%rax,1)
movsbl 0x0(%rbp,%rbx,1),%esi
mov    %r15,%rdi
call   1040 <printf@plt>
add    $0x1,%rbx
cmp    %rbx,%r12
jne    11f0 <main+0x70>
mov    %rbp,%rdi
call   1030 <free@plt>
mov    %r14d,%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0xffffffff,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i64 }
@O_RDONLY = global i32 0
@.str = [3 x i8] c"%c\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca %struct.stat
%8 = alloca i32
%9 = alloca i32
%10 = alloca i64
%11 = alloca i8*
%12 = alloca i32
%13 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%14 = load i32, i32* %4
%15 = icmp slt i32 %14, 2
br i1 %15, label %16, label %18
16: ; preds = %2
%17 = call i32 @exit(i32 -1)
unreachable
18: ; preds = %2
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 1
%21 = load i8*, i8** %20
store i8* %21, i8** %6
%22 = load i8*, i8** %6
%23 = load i32, i32* @O_RDONLY
%24 = call i32 @open(i8* %22, i32 %23)
store i32 %24, i32* %8
%25 = load i32, i32* %8
%26 = call i32 @fstat(i32 %25, %struct.stat* %7)
store i32 %26, i32* %9
%27 = load i32, i32* %9
%28 = icmp ne i32 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %18
%30 = call i32 @exit(i32 -1)
unreachable
31: ; preds = %18
%32 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%33 = load i64, i64* %32
store i64 %33, i64* %10
%34 = load i64, i64* %10
%35 = call i64 @malloc(i64 %34)
%36 = inttoptr i64 %35 to i8*
store i8* %36, i8** %11
%37 = load i32, i32* %8
%38 = load i8*, i8** %11
%39 = load i64, i64* %10
%40 = call i32 @read(i32 %37, i8* %38, i64 %39)
store i32 %40, i32* %12
%41 = load i32, i32* %12
%42 = icmp eq i32 %41, -1
br i1 %42, label %43, label %45
43: ; preds = %31
%44 = call i32 @exit(i32 -1)
unreachable
45: ; preds = %31
store i32 0, i32* %13
br label %46
46: ; preds = %58, %45
%47 = load i32, i32* %13
%48 = sext i32 %47 to i64
%49 = load i64, i64* %10
%50 = icmp ult i64 %48, %49
br i1 %50, label %51, label %61
51: ; preds = %46
%52 = load i8*, i8** %11
%53 = load i32, i32* %13
%54 = sext i32 %53 to i64
%55 = getelementptr inbounds i8, i8* %52, i64 %54
%56 = load i8, i8* %55
%57 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8 signext %56)
br label %58
58: ; preds = %51
%59 = load i32, i32* %13
%60 = add nsw i32 %59, 1
store i32 %60, i32* %13
br label %46
61: ; preds = %46
%62 = load i8*, i8** %11
%63 = call i32 @free(i8* %62)
%64 = load i32, i32* %8
%65 = call i32 @close(i32 %64)
ret i32 0
}

LLVM IR (-O2):
%struct.stat = type { i64 }
@O_RDONLY = global i32 0
@.str = [3 x i8] c"%c\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = icmp slt i32 %0, 2
br i1 %5, label %6, label %8
6: ; preds = %2
%7 = tail call i32 @exit(i32 -1)
unreachable
8: ; preds = %2
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = load i32, i32* @O_RDONLY
%12 = tail call i32 @open(i8* %10, i32 %11)
%13 = call i32 @fstat(i32 %12, %struct.stat* %3)
%14 = icmp eq i32 %13, 0
br i1 %14, label %17, label %15
15: ; preds = %8
%16 = tail call i32 @exit(i32 -1)
unreachable
17: ; preds = %8
%18 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%19 = load i64, i64* %18
%20 = tail call i64 @malloc(i64 %19)
%21 = inttoptr i64 %20 to i8*
%22 = tail call i32 @read(i32 %12, i8* %21, i64 %19)
%23 = icmp eq i32 %22, -1
br i1 %23, label %26, label %24
24: ; preds = %17
%25 = icmp eq i64 %19, 0
br i1 %25, label %28, label %31
26: ; preds = %17
%27 = tail call i32 @exit(i32 -1)
unreachable
28: ; preds = %31, %24
%29 = tail call i32 @free(i8* %21)
%30 = tail call i32 @close(i32 %12)
ret i32 0
31: ; preds = %24, %31
%32 = phi i64 [ %36, %31 ], [ 0, %24 ]
%33 = getelementptr inbounds i8, i8* %21, i64 %32
%34 = load i8, i8* %33
%35 = tail call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8 signext %34)
%36 = add nuw i64 %32, 1
%37 = icmp eq i64 %36, %19
br i1 %37, label %28, label %31
}