#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 /*<<< orphan*/ error2(char *);
int /*<<< orphan*/ exit(int);
scalar_t__ fopen(char *, char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ puts(char *);
int /*<<< orphan*/ scanf(char *, char *);
int /*<<< orphan*/ strchr(char *, char);
FILE *gfopen(char *filename, char *mode) {
  FILE *fp;
  if (filename == NULL || filename[0] == 0)
    error2("file name empty.");
  fp = (FILE *)fopen(filename, mode);
  if (fp == NULL) {
    printf("\nerror when opening file %s\n", filename);
    if (!strchr(mode, 'r'))
      exit(-1);
    printf("tell me the full path-name of the file? ");
    scanf("%s", filename);
    if ((fp = (FILE *)fopen(filename, mode)) != NULL)
      return (fp);
    puts("Can't find the file.  I give up.");
    exit(-1);
  }
  return (fp);
}


x86 assembly (clang, -O0):
<gfopen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     119b <gfopen+0x2b>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    11a7 <gfopen+0x37>
lea    0xe5e(%rip),%rdi
call   1060 <error2@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1252 <gfopen+0xe2>
mov    -0x10(%rbp),%rsi
lea    0xe43(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x18(%rbp),%rdi
mov    $0x72,%esi
call   1040 <strchr@plt>
cmp    $0x0,%eax
jne    11f6 <gfopen+0x86>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
lea    0xe31(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x10(%rbp),%rsi
lea    0xe48(%rip),%rdi
call   1070 <scanf@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     123c <gfopen+0xcc>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    125a <gfopen+0xea>
lea    0xe17(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gfopen>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
test   %rdi,%rdi
je     1184 <gfopen+0x14>
cmpb   $0x0,(%rbx)
jne    1190 <gfopen+0x20>
lea    0xe75(%rip),%rdi
call   1060 <error2@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1080 <fopen@plt>
test   %rax,%rax
jne    11ef <gfopen+0x7f>
lea    0xe6a(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r14,%rdi
mov    $0x72,%esi
call   1040 <strchr@plt>
test   %eax,%eax
je     11f7 <gfopen+0x87>
lea    0xe65(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe80(%rip),%rdi
mov    %rbx,%rsi
call   1070 <scanf@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     1201 <gfopen+0x91>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
mov    $0xffffffff,%edi
call   1090 <exit@plt>
lea    0xe52(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [17 x i8] c"file name empty.\00"
@.str.1 = [29 x i8] c"\0Aerror when opening file %s\0A\00"
@.str.2 = [41 x i8] c"tell me the full path-name of the file? \00"
@.str.3 = [3 x i8] c"%s\00"
@.str.4 = [33 x i8] c"Can't find the file. I give up.\00"
define i32* @gfopen(i8* %0, i8* %1) {
%3 = alloca i32*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32*
store i8* %0, i8** %4
store i8* %1, i8** %5
%7 = load i8*, i8** %4
%8 = icmp eq i8* %7, null
br i1 %8, label %15, label %9
9: ; preds = %2
%10 = load i8*, i8** %4
%11 = getelementptr inbounds i8, i8* %10, i64 0
%12 = load i8, i8* %11
%13 = sext i8 %12 to i32
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %9, %2
%16 = call i32 @error2(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
br label %17
17: ; preds = %15, %9
%18 = load i8*, i8** %4
%19 = load i8*, i8** %5
%20 = call i64 @fopen(i8* %18, i8* %19)
%21 = inttoptr i64 %20 to i32*
store i32* %21, i32** %6
%22 = load i32*, i32** %6
%23 = icmp eq i32* %22, null
br i1 %23, label %24, label %46
24: ; preds = %17
%25 = load i8*, i8** %4
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i8* %25)
%27 = load i8*, i8** %5
%28 = call i32 @strchr(i8* %27, i8 signext 114)
%29 = icmp ne i32 %28, 0
br i1 %29, label %32, label %30
30: ; preds = %24
%31 = call i32 @exit(i32 -1)
unreachable
32: ; preds = %24
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0))
%34 = load i8*, i8** %4
%35 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %34)
%36 = load i8*, i8** %4
%37 = load i8*, i8** %5
%38 = call i64 @fopen(i8* %36, i8* %37)
%39 = inttoptr i64 %38 to i32*
store i32* %39, i32** %6
%40 = icmp ne i32* %39, null
br i1 %40, label %41, label %43
41: ; preds = %32
%42 = load i32*, i32** %6
store i32* %42, i32** %3
br label %48
43: ; preds = %32
%44 = call i32 @puts(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.4, i64 0, i64 0))
%45 = call i32 @exit(i32 -1)
unreachable
46: ; preds = %17
%47 = load i32*, i32** %6
store i32* %47, i32** %3
br label %48
48: ; preds = %46, %41
%49 = load i32*, i32** %3
ret i32* %49
}

LLVM IR (-O2):
@.str = [17 x i8] c"file name empty.\00"
@.str.1 = [29 x i8] c"\0Aerror when opening file %s\0A\00"
@.str.2 = [41 x i8] c"tell me the full path-name of the file? \00"
@.str.3 = [3 x i8] c"%s\00"
@.str.4 = [33 x i8] c"Can't find the file. I give up.\00"
define i32* @gfopen(i8* %0, i8* %1) {
%3 = icmp eq i8* %0, null
br i1 %3, label %7, label %4
4: ; preds = %2
%5 = load i8, i8* %0
%6 = icmp eq i8 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %4, %2
%8 = tail call i32 @error2(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
br label %9
9: ; preds = %7, %4
%10 = tail call i64 @fopen(i8* %0, i8* %1)
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %26
12: ; preds = %9
%13 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%14 = tail call i32 @strchr(i8* %1, i8 signext 114)
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %12
%17 = tail call i32 @exit(i32 -1)
unreachable
18: ; preds = %12
%19 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0))
%20 = tail call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %0)
%21 = tail call i64 @fopen(i8* %0, i8* %1)
%22 = icmp eq i64 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %18
%24 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str.4, i64 0, i64 0))
%25 = tail call i32 @exit(i32 -1)
unreachable
26: ; preds = %9, %18
%27 = phi i64 [ %21, %18 ], [ %10, %9 ]
%28 = inttoptr i64 %27 to i32*
ret i32* %28
}