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

/* Variables and functions */
int /*<<< orphan*/ feof(int /*<<< orphan*/ *);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ printf(char *, ...);
int cat(char *filename) {
  FILE *fp;
  char buf[4096];
  if ((fp = fopen(filename, "r")) != NULL) {
    while (!feof(fp)) {
      fgets(buf, sizeof(buf), fp);
      printf("%s", buf);
    }
    printf("\n");
  } else {
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<cat>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1020,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xea6(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     11d2 <cat+0x92>
jmp    1172 <cat+0x32>
mov    -0x18(%rbp),%rdi
call   1050 <feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    1190 <cat+0x50>
jmp    11bf <cat+0x7f>
lea    -0x1020(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    $0x1000,%esi
call   1040 <fgets@plt>
lea    -0x1020(%rbp),%rsi
lea    0xe4f(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1172 <cat+0x32>
lea    0xe3f(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    11de <cat+0x9e>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e5 <cat+0xa5>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1020,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cat>:
push   %r15
push   %r14
push   %rbx
sub    $0x1000,%rsp
lea    0xe9d(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     11c7 <cat+0x77>
mov    %rax,%rbx
mov    %rax,%rdi
call   1060 <feof@plt>
test   %eax,%eax
jne    11b9 <cat+0x69>
mov    %rsp,%r14
lea    0xe7c(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
mov    %r14,%rdi
mov    $0x1000,%esi
mov    %rbx,%rdx
call   1050 <fgets@plt>
mov    %r15,%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %rbx,%rdi
call   1060 <feof@plt>
test   %eax,%eax
je     1190 <cat+0x40>
mov    $0xa,%edi
call   1030 <putchar@plt>
xor    %eax,%eax
jmp    11cc <cat+0x7c>
mov    $0xffffffff,%eax
add    $0x1000,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [3 x i8] c"%s\00"
@.str.2 = [2 x i8] c"\0A\00"
define i32 @cat(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca [4096 x i8]
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = call i32* @fopen(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %7, i32** %4
%8 = icmp ne i32* %7, null
br i1 %8, label %9, label %23
9: ; preds = %1
br label %10
10: ; preds = %15, %9
%11 = load i32*, i32** %4
%12 = call i32 @feof(i32* %11)
%13 = icmp ne i32 %12, 0
%14 = xor i1 %13, true
br i1 %14, label %15, label %21
15: ; preds = %10
%16 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%17 = load i32*, i32** %4
%18 = call i32 @fgets(i8* %16, i32 4096, i32* %17)
%19 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %19)
br label %10
21: ; preds = %10
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
br label %24
23: ; preds = %1
store i32 -1, i32* %2
br label %25
24: ; preds = %21
store i32 0, i32* %2
br label %25
25: ; preds = %24, %23
%26 = load i32, i32* %2
ret i32 %26
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [3 x i8] c"%s\00"
define i32 @cat(i8* nocapture %0) {
%2 = alloca [4096 x i8]
%3 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 0
%4 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %16, label %6
6: ; preds = %1
%7 = call i32 @feof(i32* %4)
%8 = icmp eq i32 %7, 0
br i1 %8, label %9, label %14
9: ; preds = %6, %9
%10 = call i32 @fgets(i8* %3, i32 4096, i32* %4)
%11 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %3)
%12 = call i32 @feof(i32* %4)
%13 = icmp eq i32 %12, 0
br i1 %13, label %9, label %14
14: ; preds = %9, %6
%15 = call i32 @putchar(i32 10)
br label %16
16: ; preds = %1, %14
%17 = phi i32 [ 0, %14 ], [ -1, %1 ]
ret i32 %17
}