#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 close(int);
int /*<<< orphan*/ exit(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int read(int, char *, int);
int write(int, char *, int);
int cat_file(char *filename) {
  char buffer[1024];
  int chars_read;
  int chars_written;
  int fd;
  int status;
  fd = open(filename, O_RDONLY);
  if (fd < 0) {
    perror("open");
    exit(1);
  }
  do {
    chars_read = read(fd, buffer, 1024);
    if (chars_read < 0) {
      perror("read");
      exit(1);
    }
    chars_written = write(1, buffer, chars_read);
    if (chars_written < 0) {
      perror("write");
      exit(1);
    }
  } while (chars_read);
  status = close(fd);
  if (status < 0) {
    perror("close");
    exit(1);
  }
  return 0;
}


x86 assembly (clang, -O0):
<cat_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e6e(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x41c(%rbp)
cmpl   $0x0,-0x41c(%rbp)
jge    11aa <cat_file+0x4a>
lea    0xe65(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
jmp    11af <cat_file+0x4f>
mov    -0x41c(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    $0x400,%edx
call   1050 <read@plt>
mov    %eax,-0x414(%rbp)
cmpl   $0x0,-0x414(%rbp)
jge    11ef <cat_file+0x8f>
lea    0xe25(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
lea    -0x410(%rbp),%rsi
mov    -0x414(%rbp),%edx
mov    $0x1,%edi
call   1030 <write@plt>
mov    %eax,-0x418(%rbp)
cmpl   $0x0,-0x418(%rbp)
jge    122f <cat_file+0xcf>
lea    0xdea(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
jmp    1234 <cat_file+0xd4>
cmpl   $0x0,-0x414(%rbp)
jne    11af <cat_file+0x4f>
mov    -0x41c(%rbp),%edi
call   1040 <close@plt>
mov    %eax,-0x420(%rbp)
cmpl   $0x0,-0x420(%rbp)
jge    1275 <cat_file+0x115>
lea    0xdaa(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
xor    %eax,%eax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cat_file>:
push   %rbp
push   %r14
push   %rbx
sub    $0x400,%rsp
mov    0x2e76(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
test   %eax,%eax
js     11f6 <cat_file+0x96>
mov    %eax,%r14d
mov    %rsp,%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0x400,%edx
call   1050 <read@plt>
test   %eax,%eax
js     11d7 <cat_file+0x77>
mov    %eax,%ebp
mov    $0x1,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
test   %eax,%eax
js     11e0 <cat_file+0x80>
test   %ebp,%ebp
jne    1190 <cat_file+0x30>
mov    %r14d,%edi
call   1040 <close@plt>
test   %eax,%eax
js     11ff <cat_file+0x9f>
xor    %eax,%eax
add    $0x400,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe27(%rip),%rdi
jmp    11e7 <cat_file+0x87>
lea    0xe23(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
lea    0xe03(%rip),%rdi
jmp    11e7 <cat_file+0x87>
lea    0xe0a(%rip),%rdi
jmp    11e7 <cat_file+0x87>

LLVM IR (-O0):
@O_RDONLY = global i32 0
@.str = [5 x i8] c"open\00"
@.str.1 = [5 x i8] c"read\00"
@.str.2 = [6 x i8] c"write\00"
@.str.3 = [6 x i8] c"close\00"
define i32 @cat_file(i8* %0) {
%2 = alloca i8*
%3 = alloca [1024 x i8]
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %2
%8 = load i8*, i8** %2
%9 = load i32, i32* @O_RDONLY
%10 = call i32 @open(i8* %8, i32 %9)
store i32 %10, i32* %6
%11 = load i32, i32* %6
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %1
%14 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%15 = call i32 @exit(i32 1)
unreachable
16: ; preds = %1
br label %17
17: ; preds = %36, %16
%18 = load i32, i32* %6
%19 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%20 = call i32 @read(i32 %18, i8* %19, i32 1024)
store i32 %20, i32* %4
%21 = load i32, i32* %4
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %17
%24 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%25 = call i32 @exit(i32 1)
unreachable
26: ; preds = %17
%27 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%28 = load i32, i32* %4
%29 = call i32 @write(i32 1, i8* %27, i32 %28)
store i32 %29, i32* %5
%30 = load i32, i32* %5
%31 = icmp slt i32 %30, 0
br i1 %31, label %32, label %35
32: ; preds = %26
%33 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%34 = call i32 @exit(i32 1)
unreachable
35: ; preds = %26
br label %36
36: ; preds = %35
%37 = load i32, i32* %4
%38 = icmp ne i32 %37, 0
br i1 %38, label %17, label %39
39: ; preds = %36
%40 = load i32, i32* %6
%41 = call i32 @close(i32 %40)
store i32 %41, i32* %7
%42 = load i32, i32* %7
%43 = icmp slt i32 %42, 0
br i1 %43, label %44, label %47
44: ; preds = %39
%45 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%46 = call i32 @exit(i32 1)
unreachable
47: ; preds = %39
ret i32 0
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@.str = [5 x i8] c"open\00"
@.str.1 = [5 x i8] c"read\00"
@.str.2 = [6 x i8] c"write\00"
@.str.3 = [6 x i8] c"close\00"
define i32 @cat_file(i8* nocapture %0) {
%2 = alloca [1024 x i8]
%3 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%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 %7, label %10
7: ; preds = %1
%8 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%9 = tail call i32 @exit(i32 1)
unreachable
10: ; preds = %1, %22
%11 = call i32 @read(i32 %5, i8* %3, i32 1024)
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %10
%14 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%15 = tail call i32 @exit(i32 1)
unreachable
16: ; preds = %10
%17 = call i32 @write(i32 1, i8* %3, i32 %11)
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %16
%20 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%21 = tail call i32 @exit(i32 1)
unreachable
22: ; preds = %16
%23 = icmp eq i32 %11, 0
br i1 %23, label %24, label %10
24: ; preds = %22
%25 = tail call i32 @close(i32 %5)
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %24
%28 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%29 = tail call i32 @exit(i32 1)
unreachable
30: ; preds = %24
ret i32 0
}