#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__ uint32_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ SEEK_END;
scalar_t__ calloc(scalar_t__, int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fread(char *, int, scalar_t__, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
scalar_t__ ftell(int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
void print_file(char *filename) {
  char *pbuf;
  uint32_t file_size;
  FILE *fd;
  fd = fopen(filename, "r");
  if (fd == NULL) {
    printf("fail to open file\n");
  }
  fseek(fd, 0, SEEK_END);
  file_size = ftell(fd);
  rewind(fd);
  pbuf = (char *)calloc(file_size, sizeof(char));
  if (pbuf == NULL) {
    printf("fail to allocate memory\n");
  }
  fread(pbuf, sizeof(char), file_size, fd);
  fclose(fd);
  while (file_size > 0) {
    printf("%c", *pbuf);
    pbuf += 1;
    file_size -= 1;
  }
}


x86 assembly (clang, -O0):
<print_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe69(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11b9 <print_file+0x39>
lea    0xe50(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x20(%rbp),%rdi
mov    0x2e24(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1090 <fseek@plt>
mov    -0x20(%rbp),%rdi
call   1080 <ftell@plt>
mov    %rax,-0x18(%rbp)
mov    -0x20(%rbp),%rdi
call   1060 <rewind@plt>
mov    -0x18(%rbp),%rdi
mov    $0x1,%esi
call   1070 <calloc@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    120e <print_file+0x8e>
lea    0xe0e(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
cmpq   $0x0,-0x18(%rbp)
jle    126a <print_file+0xea>
mov    -0x10(%rbp),%rax
movsbl (%rax),%esi
lea    0xde8(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
mov    -0x18(%rbp),%rax
sub    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    122d <print_file+0xad>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<print_file>:
push   %r15
push   %r14
push   %rbx
lea    0xe64(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,%r14
test   %rax,%rax
jne    11b5 <print_file+0x25>
lea    0xe6a(%rip),%rdi
call   1040 <puts@plt>
mov    0x2e2c(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rdi
xor    %esi,%esi
call   10a0 <fseek@plt>
mov    %r14,%rdi
call   1090 <ftell@plt>
mov    %rax,%r15
mov    %r14,%rdi
call   1070 <rewind@plt>
mov    %r15,%rdi
mov    $0x1,%esi
call   1080 <calloc@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11fc <print_file+0x6c>
lea    0xe0b(%rip),%rdi
call   1040 <puts@plt>
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %r15,%rdx
mov    %r14,%rcx
call   1050 <fread@plt>
mov    %r14,%rdi
call   1060 <fclose@plt>
test   %r15,%r15
jle    1236 <print_file+0xa6>
add    $0x1,%r15
movsbl (%rbx),%edi
call   1030 <putchar@plt>
add    $0x1,%rbx
add    $0xffffffffffffffff,%r15
cmp    $0x1,%r15
ja     1220 <print_file+0x90>
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [19 x i8] c"fail to open file\0A\00"
@SEEK_END = global i32 0
@.str.2 = [25 x i8] c"fail to allocate memory\0A\00"
@.str.3 = [3 x i8] c"%c\00"
define void @print_file(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i32*
store i8* %0, i8** %2
%6 = load i8*, i8** %2
%7 = call i32* @fopen(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %7, i32** %5
%8 = load i32*, i32** %5
%9 = icmp eq i32* %8, null
br i1 %9, label %10, label %12
10: ; preds = %1
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
br label %12
12: ; preds = %10, %1
%13 = load i32*, i32** %5
%14 = load i32, i32* @SEEK_END
%15 = call i32 @fseek(i32* %13, i32 0, i32 %14)
%16 = load i32*, i32** %5
%17 = call i64 @ftell(i32* %16)
store i64 %17, i64* %4
%18 = load i32*, i32** %5
%19 = call i32 @rewind(i32* %18)
%20 = load i64, i64* %4
%21 = call i64 @calloc(i64 %20, i32 1)
%22 = inttoptr i64 %21 to i8*
store i8* %22, i8** %3
%23 = load i8*, i8** %3
%24 = icmp eq i8* %23, null
br i1 %24, label %25, label %27
25: ; preds = %12
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
br label %27
27: ; preds = %25, %12
%28 = load i8*, i8** %3
%29 = load i64, i64* %4
%30 = load i32*, i32** %5
%31 = call i32 @fread(i8* %28, i32 1, i64 %29, i32* %30)
%32 = load i32*, i32** %5
%33 = call i32 @fclose(i32* %32)
br label %34
34: ; preds = %37, %27
%35 = load i64, i64* %4
%36 = icmp sgt i64 %35, 0
br i1 %36, label %37, label %46
37: ; preds = %34
%38 = load i8*, i8** %3
%39 = load i8, i8* %38
%40 = sext i8 %39 to i32
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32 %40)
%42 = load i8*, i8** %3
%43 = getelementptr inbounds i8, i8* %42, i64 1
store i8* %43, i8** %3
%44 = load i64, i64* %4
%45 = sub nsw i64 %44, 1
store i64 %45, i64* %4
br label %34
46: ; preds = %34
ret void
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@SEEK_END = global i32 0
@str = [24 x i8] c"fail to allocate memory\00"
@str.4 = [18 x i8] c"fail to open file\00"
define void @print_file(i8* nocapture %0) {
%2 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%3 = icmp eq i32* %2, null
br i1 %3, label %4, label %6
4: ; preds = %1
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str.4, i64 0, i64 0))
br label %6
6: ; preds = %4, %1
%7 = load i32, i32* @SEEK_END
%8 = tail call i32 @fseek(i32* %2, i32 0, i32 %7)
%9 = tail call i64 @ftell(i32* %2)
%10 = tail call i32 @rewind(i32* %2)
%11 = tail call i64 @calloc(i64 %9, i32 1)
%12 = inttoptr i64 %11 to i8*
%13 = icmp eq i64 %11, 0
br i1 %13, label %14, label %16
14: ; preds = %6
%15 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str, i64 0, i64 0))
br label %16
16: ; preds = %14, %6
%17 = tail call i32 @fread(i8* %12, i32 1, i64 %9, i32* %2)
%18 = tail call i32 @fclose(i32* %2)
%19 = icmp sgt i64 %9, 0
br i1 %19, label %20, label %29
20: ; preds = %16, %20
%21 = phi i8* [ %26, %20 ], [ %12, %16 ]
%22 = phi i64 [ %27, %20 ], [ %9, %16 ]
%23 = load i8, i8* %21
%24 = sext i8 %23 to i32
%25 = tail call i32 @putchar(i32 %24)
%26 = getelementptr inbounds i8, i8* %21, i64 1
%27 = add nsw i64 %22, -1
%28 = icmp ugt i64 %22, 1
br i1 %28, label %20, label %29
29: ; preds = %20, %16
ret void
}