#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 fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ stderr;
int main(int argc, char **argv) {
  char buff[100];
  char *default_file_name = "fclose-wrapper.c";
  char *file_name = default_file_name;
  FILE *fd;
  int ret;
  if (argc > 1) {
    file_name = argv[1];
  }
  fd = fopen(file_name, "r");
  if (fd == NULL) {
    fprintf(stderr, "Failed opening file: '%s'\n", file_name);
    return 1;
  }
  while (fgets(buff, 100, fd) != NULL) {
    printf("%s", buff);
  }
  ret = fclose(fd);
  if (ret != 0) {
    fprintf(stderr, "Failed closing file: '%s'\n", file_name);
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe90(%rip),%rax
mov    %rax,-0x88(%rbp)
mov    -0x88(%rbp),%rax
mov    %rax,-0x90(%rbp)
cmpl   $0x1,-0x8(%rbp)
jle    119e <main+0x4e>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x90(%rbp)
mov    -0x90(%rbp),%rdi
lea    0xe65(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x98(%rbp)
cmpq   $0x0,-0x98(%rbp)
jne    11ee <main+0x9e>
mov    0x2e13(%rip),%rax
mov    (%rax),%edi
mov    -0x90(%rbp),%rdx
lea    0xe36(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1275 <main+0x125>
jmp    11f3 <main+0xa3>
lea    -0x80(%rbp),%rdi
mov    -0x98(%rbp),%rdx
mov    $0x64,%esi
call   1050 <fgets@plt>
cmp    $0x0,%rax
je     1227 <main+0xd7>
lea    -0x80(%rbp),%rsi
lea    0xe11(%rip),%rdi
call   1040 <printf@plt>
jmp    11f3 <main+0xa3>
mov    -0x98(%rbp),%rdi
call   1030 <fclose@plt>
mov    %eax,-0x9c(%rbp)
cmpl   $0x0,-0x9c(%rbp)
je     126e <main+0x11e>
mov    0x2d93(%rip),%rax
mov    (%rax),%edi
mov    -0x90(%rbp),%rdx
lea    0xdd4(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1275 <main+0x125>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xa0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x68,%rsp
cmp    $0x2,%edi
jl     1166 <main+0x16>
mov    0x8(%rsi),%r14
jmp    116d <main+0x1d>
lea    0xe93(%rip),%r14
lea    0xe9d(%rip),%rsi
mov    %r14,%rdi
call   1070 <fopen@plt>
test   %rax,%rax
je     11ee <main+0x9e>
mov    %rax,%r12
mov    %rsp,%rdi
mov    $0x64,%esi
mov    %rax,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
je     11d0 <main+0x80>
lea    0xe8e(%rip),%r15
mov    %rsp,%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r15,%rdi
mov    %rbx,%rsi
call   1040 <printf@plt>
mov    %rbx,%rdi
mov    $0x64,%esi
mov    %r12,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
jne    11b0 <main+0x60>
mov    %r12,%rdi
call   1030 <fclose@plt>
test   %eax,%eax
je     120d <main+0xbd>
mov    0x2dfd(%rip),%rax
mov    (%rax),%edi
lea    0xe45(%rip),%rsi
jmp    11fe <main+0xae>
mov    0x2deb(%rip),%rax
mov    (%rax),%edi
lea    0xe15(%rip),%rsi
mov    %r14,%rdx
call   1060 <fprintf@plt>
mov    $0x1,%eax
jmp    120f <main+0xbf>
xor    %eax,%eax
add    $0x68,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [17 x i8] c"fclose-wrapper.c\00"
@.str.1 = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.2 = [27 x i8] c"Failed opening file: '%s'\0A\00"
@.str.3 = [3 x i8] c"%s\00"
@.str.4 = [27 x i8] c"Failed closing file: '%s'\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [100 x i8]
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32*
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8** %7
%11 = load i8*, i8** %7
store i8* %11, i8** %8
%12 = load i32, i32* %4
%13 = icmp sgt i32 %12, 1
br i1 %13, label %14, label %18
14: ; preds = %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 1
%17 = load i8*, i8** %16
store i8* %17, i8** %8
br label %18
18: ; preds = %14, %2
%19 = load i8*, i8** %8
%20 = call i32* @fopen(i8* %19, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %20, i32** %9
%21 = load i32*, i32** %9
%22 = icmp eq i32* %21, null
br i1 %22, label %23, label %27
23: ; preds = %18
%24 = load i32, i32* @stderr
%25 = load i8*, i8** %8
%26 = call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i8* %25)
store i32 1, i32* %3
br label %46
27: ; preds = %18
br label %28
28: ; preds = %33, %27
%29 = getelementptr inbounds [100 x i8], [100 x i8]* %6, i64 0, i64 0
%30 = load i32*, i32** %9
%31 = call i32* @fgets(i8* %29, i32 100, i32* %30)
%32 = icmp ne i32* %31, null
br i1 %32, label %33, label %36
33: ; preds = %28
%34 = getelementptr inbounds [100 x i8], [100 x i8]* %6, i64 0, i64 0
%35 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %34)
br label %28
36: ; preds = %28
%37 = load i32*, i32** %9
%38 = call i32 @fclose(i32* %37)
store i32 %38, i32* %10
%39 = load i32, i32* %10
%40 = icmp ne i32 %39, 0
br i1 %40, label %41, label %45
41: ; preds = %36
%42 = load i32, i32* @stderr
%43 = load i8*, i8** %8
%44 = call i32 @fprintf(i32 %42, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.4, i64 0, i64 0), i8* %43)
store i32 1, i32* %3
br label %46
45: ; preds = %36
store i32 0, i32* %3
br label %46
46: ; preds = %45, %41, %23
%47 = load i32, i32* %3
ret i32 %47
}

LLVM IR (-O2):
@.str = [17 x i8] c"fclose-wrapper.c\00"
@.str.1 = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.2 = [27 x i8] c"Failed opening file: '%s'\0A\00"
@.str.3 = [3 x i8] c"%s\00"
@.str.4 = [27 x i8] c"Failed closing file: '%s'\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [100 x i8]
%4 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%5 = icmp sgt i32 %0, 1
br i1 %5, label %6, label %9
6: ; preds = %2
%7 = getelementptr inbounds i8*, i8** %1, i64 1
%8 = load i8*, i8** %7
br label %9
9: ; preds = %6, %2
%10 = phi i8* [ %8, %6 ], [ getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), %2 ]
%11 = tail call i32* @fopen(i8* %10, 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 %16, label %13
13: ; preds = %9
%14 = call i32* @fgets(i8* %4, i32 100, i32* %11)
%15 = icmp eq i32* %14, null
br i1 %15, label %23, label %19
16: ; preds = %9
%17 = load i32, i32* @stderr
%18 = tail call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i8* %10)
br label %29
19: ; preds = %13, %19
%20 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %4)
%21 = call i32* @fgets(i8* %4, i32 100, i32* %11)
%22 = icmp eq i32* %21, null
br i1 %22, label %23, label %19
23: ; preds = %19, %13
%24 = call i32 @fclose(i32* %11)
%25 = icmp eq i32 %24, 0
br i1 %25, label %29, label %26
26: ; preds = %23
%27 = load i32, i32* @stderr
%28 = call i32 @fprintf(i32 %27, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.4, i64 0, i64 0), i8* %10)
br label %29
29: ; preds = %23, %26, %16
%30 = phi i32 [ 1, %16 ], [ 1, %26 ], [ 0, %23 ]
ret i32 %30
}