#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 errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ gets(char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ puts(char *);
int main(int argc, char *argv[]) {
  FILE *fp;
  char filename[80];
  printf("Enter filename: ");
  gets(filename);
  if ((fp = fopen(filename, "r")) == NULL) {
    perror("You goofed!");
    printf("errno = %d.\n", errno);
    exit(1);
  } else {
    puts("File opened for reading.");
    fclose(fp);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe73(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0x70(%rbp),%rdi
call   1060 <gets@plt>
lea    -0x70(%rbp),%rdi
lea    0xe69(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    11e8 <main+0x78>
lea    0xe51(%rip),%rdi
call   1080 <perror@plt>
mov    0x2e12(%rip),%rax
mov    (%rax),%esi
lea    0xe48(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
lea    0xe3d(%rip),%rdi
call   1030 <puts@plt>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x50,%rsp
lea    0xe84(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rsp,%rbx
mov    %rbx,%rdi
call   1060 <gets@plt>
lea    0xe7c(%rip),%rsi
mov    %rbx,%rdi
call   1070 <fopen@plt>
test   %rax,%rax
je     11c1 <main+0x51>
mov    %rax,%rbx
lea    0xe80(%rip),%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbx
ret
lea    0xe4b(%rip),%rdi
call   1080 <perror@plt>
mov    0x2e0c(%rip),%rax
mov    (%rax),%esi
lea    0xe42(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [17 x i8] c"Enter filename: \00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [12 x i8] c"You goofed!\00"
@.str.3 = [13 x i8] c"errno = %d.\0A\00"
@errno = global i32 0
@.str.4 = [25 x i8] c"File opened for reading.\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca [80 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
%9 = getelementptr inbounds [80 x i8], [80 x i8]* %7, i64 0, i64 0
%10 = call i32 @gets(i8* %9)
%11 = getelementptr inbounds [80 x i8], [80 x i8]* %7, i64 0, i64 0
%12 = call i32* @fopen(i8* %11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %12, i32** %6
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %19
14: ; preds = %2
%15 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
%16 = load i32, i32* @errno
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i32 %16)
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %2
%20 = call i32 @puts(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0))
%21 = load i32*, i32** %6
%22 = call i32 @fclose(i32* %21)
br label %23
23: ; preds = %19
ret i32 0
}

LLVM IR (-O2):
@.str = [17 x i8] c"Enter filename: \00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [12 x i8] c"You goofed!\00"
@.str.3 = [13 x i8] c"errno = %d.\0A\00"
@errno = global i32 0
@.str.4 = [25 x i8] c"File opened for reading.\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [80 x i8]
%4 = getelementptr inbounds [80 x i8], [80 x i8]* %3, i64 0, i64 0
%5 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
%6 = call i32 @gets(i8* %4)
%7 = call i32* @fopen(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %14
9: ; preds = %2
%10 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
%11 = load i32, i32* @errno
%12 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i32 %11)
%13 = call i32 @exit(i32 1)
unreachable
14: ; preds = %2
%15 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0))
%16 = call i32 @fclose(i32* %7)
ret i32 0
}