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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ file_exists(char *);
int /*<<< orphan*/ *fopen(char *, char *);
int fread(char *, int, int const, int /*<<< orphan*/ *);
int /*<<< orphan*/ internal_error(char *);
int /*<<< orphan*/ write(int, char *, int);
void dump_file_to_stdout(char *filename) {
  const int buf_size = 1024;
  char buf[buf_size];
  FILE *f;
  int n;
  if (filename == NULL || !file_exists(filename))
    internal_error("file does not exist");
  f = fopen(filename, "r");
  if (f == NULL)
    internal_error("cannot open file for read");
  while ((n = fread(buf, 1, buf_size, f)) > 0) {
    write(1, buf, n);
  }
  fclose(f);
}


x86 assembly (clang, -O0):
<dump_file_to_stdout>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x400,-0xc(%rbp)
mov    %rsp,%rax
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     119a <dump_file_to_stdout+0x3a>
mov    -0x8(%rbp),%rdi
call   1030 <file_exists@plt>
cmp    $0x0,%eax
jne    11a6 <dump_file_to_stdout+0x46>
lea    0xe5f(%rip),%rdi
call   1070 <internal_error@plt>
mov    -0x8(%rbp),%rdi
lea    0xe63(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11d1 <dump_file_to_stdout+0x71>
lea    0xe4a(%rip),%rdi
call   1070 <internal_error@plt>
jmp    11d6 <dump_file_to_stdout+0x76>
mov    -0x20(%rbp),%rcx
lea    -0x430(%rbp),%rdi
mov    $0x1,%esi
mov    $0x400,%edx
call   1040 <fread@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jle    1215 <dump_file_to_stdout+0xb5>
mov    -0x24(%rbp),%edx
mov    $0x1,%edi
lea    -0x430(%rbp),%rsi
call   1050 <write@plt>
jmp    11d6 <dump_file_to_stdout+0x76>
mov    -0x20(%rbp),%rdi
call   1060 <fclose@plt>
mov    -0x18(%rbp),%rax
mov    %rax,%rsp
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dump_file_to_stdout>:
push   %r14
push   %rbx
sub    $0x408,%rsp
mov    %rdi,%rbx
test   %rdi,%rdi
je     117e <dump_file_to_stdout+0x1e>
mov    %rbx,%rdi
call   1030 <file_exists@plt>
test   %eax,%eax
jne    118a <dump_file_to_stdout+0x2a>
lea    0xe7b(%rip),%rdi
call   1070 <internal_error@plt>
lea    0xe83(%rip),%rsi
mov    %rbx,%rdi
call   1080 <fopen@plt>
mov    %rax,%r14
test   %rax,%rax
jne    11ad <dump_file_to_stdout+0x4d>
lea    0xe6e(%rip),%rdi
call   1070 <internal_error@plt>
mov    %rsp,%rdi
mov    $0x1,%esi
mov    $0x400,%edx
mov    %r14,%rcx
call   1040 <fread@plt>
test   %eax,%eax
jle    11f8 <dump_file_to_stdout+0x98>
mov    %rsp,%rbx
nopl   0x0(%rax)
mov    $0x1,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1050 <write@plt>
mov    %rbx,%rdi
mov    $0x1,%esi
mov    $0x400,%edx
mov    %r14,%rcx
call   1040 <fread@plt>
test   %eax,%eax
jg     11d0 <dump_file_to_stdout+0x70>
mov    %r14,%rdi
add    $0x408,%rsp
pop    %rbx
pop    %r14
jmp    1060 <fclose@plt>

LLVM IR (-O0):
@.str = [20 x i8] c"file does not exist\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [26 x i8] c"cannot open file for read\00"
define void @dump_file_to_stdout(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i32
store i8* %0, i8** %2
store i32 1024, i32* %3
%7 = call i8* @llvm.stacksave()
store i8* %7, i8** %4
%8 = alloca i8, i64 1024
%9 = load i8*, i8** %2
%10 = icmp eq i8* %9, null
br i1 %10, label %15, label %11
11: ; preds = %1
%12 = load i8*, i8** %2
%13 = call i32 @file_exists(i8* %12)
%14 = icmp ne i32 %13, 0
br i1 %14, label %17, label %15
15: ; preds = %11, %1
%16 = call i32 @internal_error(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
br label %17
17: ; preds = %15, %11
%18 = load i8*, i8** %2
%19 = call i32* @fopen(i8* %18, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %19, i32** %5
%20 = load i32*, i32** %5
%21 = icmp eq i32* %20, null
br i1 %21, label %22, label %24
22: ; preds = %17
%23 = call i32 @internal_error(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
br label %24
24: ; preds = %22, %17
br label %25
25: ; preds = %29, %24
%26 = load i32*, i32** %5
%27 = call i32 @fread(i8* %8, i32 1, i32 1024, i32* %26)
store i32 %27, i32* %6
%28 = icmp sgt i32 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %25
%30 = load i32, i32* %6
%31 = call i32 @write(i32 1, i8* %8, i32 %30)
br label %25
32: ; preds = %25
%33 = load i32*, i32** %5
%34 = call i32 @fclose(i32* %33)
%35 = load i8*, i8** %4
ret void
}

LLVM IR (-O2):
@.str = [20 x i8] c"file does not exist\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [26 x i8] c"cannot open file for read\00"
define void @dump_file_to_stdout(i8* %0) {
%2 = alloca [1024 x i8]
%3 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%4 = icmp eq i8* %0, null
br i1 %4, label %8, label %5
5: ; preds = %1
%6 = tail call i32 @file_exists(i8* %0)
%7 = icmp eq i32 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %5, %1
%9 = tail call i32 @internal_error(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
br label %10
10: ; preds = %8, %5
%11 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %15
13: ; preds = %10
%14 = tail call i32 @internal_error(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
br label %15
15: ; preds = %13, %10
%16 = call i32 @fread(i8* %3, i32 1, i32 1024, i32* %11)
%17 = icmp sgt i32 %16, 0
br i1 %17, label %18, label %23
18: ; preds = %15, %18
%19 = phi i32 [ %21, %18 ], [ %16, %15 ]
%20 = call i32 @write(i32 1, i8* %3, i32 %19)
%21 = call i32 @fread(i8* %3, i32 1, i32 1024, i32* %11)
%22 = icmp sgt i32 %21, 0
br i1 %22, label %18, label %23
23: ; preds = %18, %15
%24 = tail call i32 @fclose(i32* %11)
ret void
}