#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 */
scalar_t__ EOF;
scalar_t__ fclose(int /*<<< orphan*/ *);
scalar_t__ ferror(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/);
int /*<<< orphan*/ free(char *);
char *malloc(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ stdout;
int main(int argc, char *argv[]) {
  char *usage = "printer2 <FILEPATH>";
  char *buf = malloc(sizeof(char) * 256);
  if (buf == NULL) {
    printf("malloc(): ERROR\n");
    return -1;
  }
  if (argc != 2) {
    printf("%s\n", usage);
    return -1;
  }
  FILE *infile;
  infile = fopen(argv[1], "r");
  if (infile == NULL) {
    perror("fopen():");
    return -1;
  }
  while (fgets(buf, 256, infile) != NULL) {
    fputs(buf, stdout);
  }
  if (ferror(infile)) {
    perror("");
  }
  if (fclose(infile) == EOF) {
    perror("fclose():");
    return -1;
  }
  free(buf);
  buf = NULL;
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe53(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    $0x100,%edi
call   1090 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11e4 <main+0x54>
lea    0xe43(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12e7 <main+0x157>
cmpl   $0x2,-0x8(%rbp)
je     120c <main+0x7c>
mov    -0x18(%rbp),%rsi
lea    0xe2c(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12e7 <main+0x157>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe0e(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1247 <main+0xb7>
lea    0xdf5(%rip),%rdi
call   10b0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12e7 <main+0x157>
jmp    124c <main+0xbc>
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rdx
mov    $0x100,%esi
call   1080 <fgets@plt>
cmp    $0x0,%rax
je     127f <main+0xef>
mov    -0x20(%rbp),%rdi
mov    0x2d65(%rip),%rax
mov    (%rax),%esi
call   1070 <fputs@plt>
jmp    124c <main+0xbc>
mov    -0x28(%rbp),%rdi
call   1040 <ferror@plt>
cmp    $0x0,%rax
je     129e <main+0x10e>
lea    0xd8b(%rip),%rdi
call   10b0 <perror@plt>
mov    -0x28(%rbp),%rdi
call   1050 <fclose@plt>
mov    0x2d3a(%rip),%rcx
cmp    (%rcx),%rax
jne    12cf <main+0x13f>
lea    0xd76(%rip),%rdi
call   10b0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12e7 <main+0x157>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x20(%rbp)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %edi,%ebp
mov    $0x100,%edi
call   1090 <malloc@plt>
test   %rax,%rax
je     123d <main+0xad>
cmp    $0x2,%ebp
jne    1246 <main+0xb6>
mov    %rax,%rbx
mov    0x8(%r14),%rdi
lea    0xe51(%rip),%rsi
call   10a0 <fopen@plt>
test   %rax,%rax
je     125c <main+0xcc>
mov    %rax,%r14
mov    %rbx,%rdi
mov    $0x100,%esi
mov    %rax,%rdx
call   1080 <fgets@plt>
test   %rax,%rax
je     1210 <main+0x80>
mov    0x2de8(%rip),%rbp
mov    0x0(%rbp),%esi
mov    %rbx,%rdi
call   1070 <fputs@plt>
mov    %rbx,%rdi
mov    $0x100,%esi
mov    %r14,%rdx
call   1080 <fgets@plt>
test   %rax,%rax
jne    11f0 <main+0x60>
mov    %r14,%rdi
call   1050 <ferror@plt>
test   %rax,%rax
jne    1265 <main+0xd5>
mov    %r14,%rdi
call   1060 <fclose@plt>
mov    0x2dbc(%rip),%rcx
cmp    (%rcx),%rax
je     1273 <main+0xe3>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %eax,%eax
jmp    1257 <main+0xc7>
lea    0xde5(%rip),%rdi
jmp    124d <main+0xbd>
lea    0xdb3(%rip),%rdi
call   1040 <puts@plt>
mov    $0xffffffff,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xdb3(%rip),%rdi
jmp    127a <main+0xea>
lea    0xdbc(%rip),%rdi
call   10b0 <perror@plt>
jmp    121d <main+0x8d>
lea    0xda5(%rip),%rdi
call   10b0 <perror@plt>
jmp    1252 <main+0xc2>

LLVM IR (-O0):
@.str = [20 x i8] c"printer2 <FILEPATH>\00"
@.str.1 = [17 x i8] c"malloc(): ERROR\0A\00"
@.str.2 = [4 x i8] c"%s\0A\00"
@.str.3 = [2 x i8] c"r\00"
@.str.4 = [9 x i8] c"fopen():\00"
@stdout = global i32 0
@.str.5 = [1 x i8] zeroinitializer
@EOF = global i64 0
@.str.6 = [10 x i8] c"fclose():\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8** %6
%9 = call i8* @malloc(i32 256)
store i8* %9, i8** %7
%10 = load i8*, i8** %7
%11 = icmp eq i8* %10, null
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %55
14: ; preds = %2
%15 = load i32, i32* %4
%16 = icmp ne i32 %15, 2
br i1 %16, label %17, label %20
17: ; preds = %14
%18 = load i8*, i8** %6
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %18)
store i32 -1, i32* %3
br label %55
20: ; preds = %14
%21 = load i8**, i8*** %5
%22 = getelementptr inbounds i8*, i8** %21, i64 1
%23 = load i8*, i8** %22
%24 = call i32* @fopen(i8* %23, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %24, i32** %8
%25 = load i32*, i32** %8
%26 = icmp eq i32* %25, null
br i1 %26, label %27, label %29
27: ; preds = %20
%28 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0))
store i32 -1, i32* %3
br label %55
29: ; preds = %20
br label %30
30: ; preds = %35, %29
%31 = load i8*, i8** %7
%32 = load i32*, i32** %8
%33 = call i32* @fgets(i8* %31, i32 256, i32* %32)
%34 = icmp ne i32* %33, null
br i1 %34, label %35, label %39
35: ; preds = %30
%36 = load i8*, i8** %7
%37 = load i32, i32* @stdout
%38 = call i32 @fputs(i8* %36, i32 %37)
br label %30
39: ; preds = %30
%40 = load i32*, i32** %8
%41 = call i64 @ferror(i32* %40)
%42 = icmp ne i64 %41, 0
br i1 %42, label %43, label %45
43: ; preds = %39
%44 = call i32 @perror(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.5, i64 0, i64 0))
br label %45
45: ; preds = %43, %39
%46 = load i32*, i32** %8
%47 = call i64 @fclose(i32* %46)
%48 = load i64, i64* @EOF
%49 = icmp eq i64 %47, %48
br i1 %49, label %50, label %52
50: ; preds = %45
%51 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.6, i64 0, i64 0))
store i32 -1, i32* %3
br label %55
52: ; preds = %45
%53 = load i8*, i8** %7
%54 = call i32 @free(i8* %53)
store i8* null, i8** %7
store i32 0, i32* %3
br label %55
55: ; preds = %52, %50, %27, %17, %12
%56 = load i32, i32* %3
ret i32 %56
}

LLVM IR (-O2):
@.str = [20 x i8] c"printer2 <FILEPATH>\00"
@.str.3 = [2 x i8] c"r\00"
@.str.4 = [9 x i8] c"fopen():\00"
@stdout = global i32 0
@.str.5 = [1 x i8] zeroinitializer
@EOF = global i64 0
@.str.6 = [10 x i8] c"fclose():\00"
@str = [16 x i8] c"malloc(): ERROR\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = tail call dereferenceable_or_null(256) i8* @malloc(i32 256)
%4 = icmp eq i8* %3, null
br i1 %4, label %5, label %7
5: ; preds = %2
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str, i64 0, i64 0))
br label %39
7: ; preds = %2
%8 = icmp eq i32 %0, 2
br i1 %8, label %11, label %9
9: ; preds = %7
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
br label %39
11: ; preds = %7
%12 = getelementptr inbounds i8*, i8** %1, i64 1
%13 = load i8*, i8** %12
%14 = tail call i32* @fopen(i8* %13, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%15 = icmp eq i32* %14, null
br i1 %15, label %19, label %16
16: ; preds = %11
%17 = tail call i32* @fgets(i8* %3, i32 256, i32* %14)
%18 = icmp eq i32* %17, null
br i1 %18, label %26, label %21
19: ; preds = %11
%20 = tail call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0))
br label %39
21: ; preds = %16, %21
%22 = load i32, i32* @stdout
%23 = tail call i32 @fputs(i8* %3, i32 %22)
%24 = tail call i32* @fgets(i8* %3, i32 256, i32* %14)
%25 = icmp eq i32* %24, null
br i1 %25, label %26, label %21
26: ; preds = %21, %16
%27 = tail call i64 @ferror(i32* %14)
%28 = icmp eq i64 %27, 0
br i1 %28, label %31, label %29
29: ; preds = %26
%30 = tail call i32 @perror(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.5, i64 0, i64 0))
br label %31
31: ; preds = %29, %26
%32 = tail call i64 @fclose(i32* %14)
%33 = load i64, i64* @EOF
%34 = icmp eq i64 %32, %33
br i1 %34, label %35, label %37
35: ; preds = %31
%36 = tail call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.6, i64 0, i64 0))
br label %39
37: ; preds = %31
%38 = tail call i32 @free(i8* %3)
br label %39
39: ; preds = %19, %35, %37, %9, %5
%40 = phi i32 [ -1, %5 ], [ -1, %9 ], [ -1, %19 ], [ -1, %35 ], [ 0, %37 ]
ret i32 %40
}