#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*/ fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ printf(char *, char *);
int sscanf(char *, char *, char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdin;
FILE *open_readfile(char *filename) {
  char line[1000];
  FILE *ifp = NULL;
  while ((ifp = fopen(filename, "r")) == NULL) {
    fprintf(stderr, "ERROR: open_readfile.  Cannot open %s\n", filename);
    fprintf(stderr, " Enter new file name:  ");
    fgets(line, 1000, stdin);
    while (sscanf(line, "%s", filename) != 1 ||
           (ifp = fopen(filename, "r")) == NULL) {
      fprintf(stderr, "ERROR: bad input.  Enter filename again: ");
      fgets(line, 1000, stdin);
    }
  }
  printf("open_readfile: %s now open\n", filename);
  return ifp;
}


x86 assembly (clang, -O0):
<open_readfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x400,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x3f8(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe8b(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x3f8(%rbp)
cmp    $0x0,%rax
jne    1273 <open_readfile+0x123>
mov    0x2e46(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe63(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    0x2e2b(%rip),%rax
mov    (%rax),%edi
lea    0xe73(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
lea    -0x3f0(%rbp),%rdi
mov    0x2e1d(%rip),%rax
mov    (%rax),%edx
mov    $0x3e8,%esi
call   1040 <fgets@plt>
lea    -0x3f0(%rbp),%rdi
mov    -0x8(%rbp),%rdx
lea    0xe58(%rip),%rsi
call   1060 <sscanf@plt>
mov    %eax,%ecx
mov    $0x1,%al
cmp    $0x1,%ecx
mov    %al,-0x3f9(%rbp)
jne    1225 <open_readfile+0xd5>
mov    -0x8(%rbp),%rdi
lea    0xdf4(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x3f8(%rbp)
cmp    $0x0,%rax
sete   %al
mov    %al,-0x3f9(%rbp)
mov    -0x3f9(%rbp),%al
test   $0x1,%al
jne    1238 <open_readfile+0xe8>
jmp    126e <open_readfile+0x11e>
mov    0x2d99(%rip),%rax
mov    (%rax),%edi
lea    0xdfc(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
lea    -0x3f0(%rbp),%rdi
mov    0x2d8b(%rip),%rax
mov    (%rax),%edx
mov    $0x3e8,%esi
call   1040 <fgets@plt>
jmp    11d7 <open_readfile+0x87>
jmp    116a <open_readfile+0x1a>
mov    -0x8(%rbp),%rsi
lea    0xdf0(%rip),%rdi
call   1030 <printf@plt>
mov    -0x3f8(%rbp),%rax
add    $0x400,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_readfile>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x3e8,%rsp
mov    %rdi,%rbx
lea    0xe95(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
jne    121b <open_readfile+0xcb>
mov    0x2e58(%rip),%r14
mov    0x2e61(%rip),%rbp
mov    %rsp,%r15
lea    0xeb0(%rip),%r12
lea    0xeac(%rip),%r13
nopl   0x0(%rax,%rax,1)
mov    (%r14),%edi
lea    0xe58(%rip),%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
lea    0xe6e(%rip),%rsi
nopl   0x0(%rax,%rax,1)
mov    (%r14),%edi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    0x0(%rbp),%edx
mov    %r15,%rdi
mov    $0x3e8,%esi
call   1040 <fgets@plt>
mov    %r15,%rdi
mov    %r12,%rsi
mov    %rbx,%rdx
call   1060 <sscanf@plt>
mov    %r13,%rsi
cmp    $0x1,%eax
jne    11c0 <open_readfile+0x70>
mov    %rbx,%rdi
lea    0xe06(%rip),%rsi
call   1070 <fopen@plt>
mov    %r13,%rsi
test   %rax,%rax
je     11c0 <open_readfile+0x70>
mov    %rbx,%rdi
lea    0xdef(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     11a0 <open_readfile+0x50>
lea    0xe4c(%rip),%rdi
mov    %rbx,%rsi
mov    %rax,%rbx
call   1030 <printf@plt>
mov    %rbx,%rax
add    $0x3e8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.1 = [39 x i8] c"ERROR: open_readfile. Cannot open %s\0A\00"
@.str.2 = [24 x i8] c" Enter new file name: \00"
@stdin = global i32 0
@.str.3 = [3 x i8] c"%s\00"
@.str.4 = [42 x i8] c"ERROR: bad input. Enter filename again: \00"
@.str.5 = [28 x i8] c"open_readfile: %s now open\0A\00"
define i32* @open_readfile(i8* %0) {
%2 = alloca i8*
%3 = alloca [1000 x i8]
%4 = alloca i32*
store i8* %0, i8** %2
store i32* null, i32** %4
br label %5
5: ; preds = %35, %1
%6 = load i8*, i8** %2
%7 = call i32* @fopen(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %7, i32** %4
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %36
9: ; preds = %5
%10 = load i32, i32* @stderr
%11 = load i8*, i8** %2
%12 = call i32 (i32, i8*, ...) @fprintf(i32 %10, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i8* %11)
%13 = load i32, i32* @stderr
%14 = call i32 (i32, i8*, ...) @fprintf(i32 %13, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
%15 = getelementptr inbounds [1000 x i8], [1000 x i8]* %3, i64 0, i64 0
%16 = load i32, i32* @stdin
%17 = call i32 @fgets(i8* %15, i32 1000, i32 %16)
br label %18
18: ; preds = %29, %9
%19 = getelementptr inbounds [1000 x i8], [1000 x i8]* %3, i64 0, i64 0
%20 = load i8*, i8** %2
%21 = call i32 @sscanf(i8* %19, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %20)
%22 = icmp ne i32 %21, 1
br i1 %22, label %27, label %23
23: ; preds = %18
%24 = load i8*, i8** %2
%25 = call i32* @fopen(i8* %24, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %25, i32** %4
%26 = icmp eq i32* %25, null
br label %27
27: ; preds = %23, %18
%28 = phi i1 [ true, %18 ], [ %26, %23 ]
br i1 %28, label %29, label %35
29: ; preds = %27
%30 = load i32, i32* @stderr
%31 = call i32 (i32, i8*, ...) @fprintf(i32 %30, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.4, i64 0, i64 0))
%32 = getelementptr inbounds [1000 x i8], [1000 x i8]* %3, i64 0, i64 0
%33 = load i32, i32* @stdin
%34 = call i32 @fgets(i8* %32, i32 1000, i32 %33)
br label %18
35: ; preds = %27
br label %5
36: ; preds = %5
%37 = load i8*, i8** %2
%38 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.5, i64 0, i64 0), i8* %37)
%39 = load i32*, i32** %4
ret i32* %39
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.1 = [39 x i8] c"ERROR: open_readfile. Cannot open %s\0A\00"
@.str.2 = [24 x i8] c" Enter new file name: \00"
@stdin = global i32 0
@.str.3 = [3 x i8] c"%s\00"
@.str.4 = [42 x i8] c"ERROR: bad input. Enter filename again: \00"
@.str.5 = [28 x i8] c"open_readfile: %s now open\0A\00"
define noalias i32* @open_readfile(i8* %0) {
%2 = alloca [1000 x i8]
%3 = getelementptr inbounds [1000 x i8], [1000 x i8]* %2, i64 0, i64 0
%4 = call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %9, label %24
6: ; preds = %21
%7 = call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %24
9: ; preds = %1, %6
%10 = load i32, i32* @stderr
%11 = call i32 (i32, i8*, ...) @fprintf(i32 %10, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %12
12: ; preds = %20, %9
%13 = phi i8* [ getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), %9 ], [ getelementptr inbounds ([42 x i8], [42 x i8]* @.str.4, i64 0, i64 0), %20 ]
%14 = load i32, i32* @stderr
%15 = call i32 (i32, i8*, ...) @fprintf(i32 %14, i8* %13)
%16 = load i32, i32* @stdin
%17 = call i32 @fgets(i8* %3, i32 1000, i32 %16)
%18 = call i32 @sscanf(i8* %3, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %0)
%19 = icmp eq i32 %18, 1
br i1 %19, label %21, label %20
20: ; preds = %12, %21
br label %12
21: ; preds = %12
%22 = call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%23 = icmp eq i32* %22, null
br i1 %23, label %20, label %6
24: ; preds = %6, %1
%25 = phi i32* [ %4, %1 ], [ %7, %6 ]
%26 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.5, i64 0, i64 0), i8* %0)
ret i32* %25
}