#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 */
char EOF;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ ferror(int /*<<< orphan*/);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, ...);
char getc(int /*<<< orphan*/ *);
int /*<<< orphan*/ putchar(char);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
int main(int argc, char *argv[]) {
  char *prog = argv[0];
  int i;
  char *fn;
  if (!(argc > 1 && (fn = argv[1]))) {
    fprintf(stderr, "%s: usage: readf FILENAME\n", prog);
    exit(0);
  }
  {
    FILE *fp;
    char c;
    if (NULL == (fp = fopen(fn, "r"))) {
      fprintf(stderr, "%s: error open file %s", prog, fn);
      exit(1);
    }
    while (EOF != (c = getc(fp))) {
      putchar(c);
    }
    fclose(fp);
  }
  if (ferror(stdout)) {
    fprintf(stderr, "%s: error writing stdout\n", prog);
    exit(2);
  }
  exit(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)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x18(%rbp)
cmpl   $0x1,-0x8(%rbp)
jle    11b1 <main+0x41>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    11d3 <main+0x63>
mov    0x2e20(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
lea    0xe3b(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
xor    %edi,%edi
call   1080 <exit@plt>
mov    -0x28(%rbp),%rdi
lea    0xe3d(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,%rcx
mov    %rcx,-0x30(%rbp)
xor    %eax,%eax
cmp    %rcx,%rax
jne    121e <main+0xae>
mov    0x2ddc(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
mov    -0x28(%rbp),%rcx
lea    0xe10(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
jmp    1223 <main+0xb3>
mov    0x2dbe(%rip),%rax
movsbl (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    -0x30(%rbp),%rdi
call   1090 <getc@plt>
mov    %al,%cl
mov    -0x38(%rbp),%eax
mov    %cl,-0x31(%rbp)
movsbl %cl,%ecx
cmp    %ecx,%eax
je     125a <main+0xea>
movsbl -0x31(%rbp),%edi
call   1030 <putchar@plt>
jmp    1223 <main+0xb3>
mov    -0x30(%rbp),%rdi
call   1050 <fclose@plt>
mov    0x2d66(%rip),%rax
mov    (%rax),%edi
call   1040 <ferror@plt>
cmp    $0x0,%rax
je     12a0 <main+0x130>
mov    0x2d56(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
lea    0xda5(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    $0x2,%edi
call   1080 <exit@plt>
xor    %edi,%edi
call   1080 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    (%rsi),%r14
cmp    $0x2,%edi
jl     1188 <main+0x18>
mov    0x8(%rsi),%r15
test   %r15,%r15
jne    11a9 <main+0x39>
mov    0x2e49(%rip),%rax
mov    (%rax),%edi
lea    0xe68(%rip),%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
xor    %edi,%edi
call   1080 <exit@plt>
lea    0xe6b(%rip),%rsi
mov    %r15,%rdi
call   1070 <fopen@plt>
test   %rax,%rax
jne    11e4 <main+0x74>
mov    0x2e14(%rip),%rax
mov    (%rax),%edi
lea    0xe50(%rip),%rsi
mov    %r14,%rdx
mov    %r15,%rcx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    %rax,%rbp
mov    0x2dfa(%rip),%r15
xchg   %ax,%ax
movzbl (%r15),%ebx
mov    %rbp,%rdi
call   1090 <getc@plt>
cmp    %al,%bl
je     120a <main+0x9a>
movsbl %al,%edi
call   1030 <putchar@plt>
jmp    11f0 <main+0x80>
mov    %rbp,%rdi
call   1050 <fclose@plt>
mov    0x2db7(%rip),%rax
mov    (%rax),%edi
call   1040 <ferror@plt>
test   %rax,%rax
jne    122c <main+0xbc>
xor    %edi,%edi
call   1080 <exit@plt>
mov    0x2da5(%rip),%rax
mov    (%rax),%edi
lea    0xdf8(%rip),%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0x2,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [27 x i8] c"%s: usage: readf FILENAME\0A\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [23 x i8] c"%s: error open file %s\00"
@EOF = global i8 0
@stdout = global i32 0
@.str.3 = [26 x i8] c"%s: error writing stdout\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32*
%10 = alloca i8
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i8**, i8*** %5
%12 = getelementptr inbounds i8*, i8** %11, i64 0
%13 = load i8*, i8** %12
store i8* %13, i8** %6
%14 = load i32, i32* %4
%15 = icmp sgt i32 %14, 1
br i1 %15, label %16, label %21
16: ; preds = %2
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 1
%19 = load i8*, i8** %18
store i8* %19, i8** %8
%20 = icmp ne i8* %19, null
br i1 %20, label %26, label %21
21: ; preds = %16, %2
%22 = load i32, i32* @stderr
%23 = load i8*, i8** %6
%24 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %22, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %23)
%25 = call i32 @exit(i32 0)
unreachable
26: ; preds = %16
%27 = load i8*, i8** %8
%28 = call i32* @fopen(i8* %27, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %28, i32** %9
%29 = icmp eq i32* null, %28
br i1 %29, label %30, label %36
30: ; preds = %26
%31 = load i32, i32* @stderr
%32 = load i8*, i8** %6
%33 = load i8*, i8** %8
%34 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %31, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i8* %32, i8* %33)
%35 = call i32 @exit(i32 1)
unreachable
36: ; preds = %26
br label %37
37: ; preds = %44, %36
%38 = load i8, i8* @EOF
%39 = sext i8 %38 to i32
%40 = load i32*, i32** %9
%41 = call signext i8 @getc(i32* %40)
store i8 %41, i8* %10
%42 = sext i8 %41 to i32
%43 = icmp ne i32 %39, %42
br i1 %43, label %44, label %47
44: ; preds = %37
%45 = load i8, i8* %10
%46 = call i32 @putchar(i8 signext %45)
br label %37
47: ; preds = %37
%48 = load i32*, i32** %9
%49 = call i32 @fclose(i32* %48)
%50 = load i32, i32* @stdout
%51 = call i64 @ferror(i32 %50)
%52 = icmp ne i64 %51, 0
br i1 %52, label %53, label %58
53: ; preds = %47
%54 = load i32, i32* @stderr
%55 = load i8*, i8** %6
%56 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %54, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), i8* %55)
%57 = call i32 @exit(i32 2)
unreachable
58: ; preds = %47
%59 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [27 x i8] c"%s: usage: readf FILENAME\0A\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [23 x i8] c"%s: error open file %s\00"
@EOF = global i8 0
@stdout = global i32 0
@.str.3 = [26 x i8] c"%s: error writing stdout\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = load i8*, i8** %1
%4 = icmp sgt i32 %0, 1
br i1 %4, label %5, label %9
5: ; preds = %2
%6 = getelementptr inbounds i8*, i8** %1, i64 1
%7 = load i8*, i8** %6
%8 = icmp eq i8* %7, null
br i1 %8, label %9, label %13
9: ; preds = %5, %2
%10 = load i32, i32* @stderr
%11 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %10, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %3)
%12 = tail call i32 @exit(i32 0)
unreachable
13: ; preds = %5
%14 = tail call i32* @fopen(i8* %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%15 = icmp eq i32* %14, null
br i1 %15, label %20, label %16
16: ; preds = %13
%17 = load i8, i8* @EOF
%18 = tail call signext i8 @getc(i32* %14)
%19 = icmp eq i8 %17, %18
br i1 %19, label %30, label %24
20: ; preds = %13
%21 = load i32, i32* @stderr
%22 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %21, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i8* %3, i8* %7)
%23 = tail call i32 @exit(i32 1)
unreachable
24: ; preds = %16, %24
%25 = phi i8 [ %28, %24 ], [ %18, %16 ]
%26 = tail call i32 @putchar(i8 signext %25)
%27 = load i8, i8* @EOF
%28 = tail call signext i8 @getc(i32* %14)
%29 = icmp eq i8 %27, %28
br i1 %29, label %30, label %24
30: ; preds = %24, %16
%31 = tail call i32 @fclose(i32* %14)
%32 = load i32, i32* @stdout
%33 = tail call i64 @ferror(i32 %32)
%34 = icmp eq i64 %33, 0
br i1 %34, label %39, label %35
35: ; preds = %30
%36 = load i32, i32* @stderr
%37 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %36, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), i8* %3)
%38 = tail call i32 @exit(i32 2)
unreachable
39: ; preds = %30
%40 = tail call i32 @exit(i32 0)
unreachable
}