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


x86 assembly (clang, -O0):
<open_appendfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x410,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movq   $0x0,-0x408(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe88(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x408(%rbp)
cmp    $0x0,%rax
jne    1276 <open_appendfile+0x126>
mov    0x2e43(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe60(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    0x2e28(%rip),%rax
mov    (%rax),%edi
lea    0xe73(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
lea    -0x400(%rbp),%rdi
mov    0x2e1a(%rip),%rax
mov    (%rax),%edx
mov    $0x3e8,%esi
call   1040 <fgets@plt>
lea    -0x400(%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,-0x409(%rbp)
jne    1228 <open_appendfile+0xd8>
mov    -0x8(%rbp),%rdi
lea    0xe38(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x408(%rbp)
cmp    $0x0,%rax
sete   %al
mov    %al,-0x409(%rbp)
mov    -0x409(%rbp),%al
test   $0x1,%al
jne    123b <open_appendfile+0xeb>
jmp    1271 <open_appendfile+0x121>
mov    0x2d96(%rip),%rax
mov    (%rax),%edi
lea    0xdfe(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
lea    -0x400(%rbp),%rdi
mov    0x2d88(%rip),%rax
mov    (%rax),%edx
mov    $0x3e8,%esi
call   1040 <fgets@plt>
jmp    11da <open_appendfile+0x8a>
jmp    116d <open_appendfile+0x1d>
cmpl   $0x0,-0xc(%rbp)
je     1290 <open_appendfile+0x140>
mov    -0x8(%rbp),%rsi
lea    0xde8(%rip),%rdi
call   1030 <printf@plt>
mov    -0x408(%rbp),%rax
add    $0x410,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_appendfile>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x3f8,%rsp
mov    %esi,%ebp
mov    %rdi,%rbx
lea    0xe93(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     11a2 <open_appendfile+0x52>
test   %ebp,%ebp
je     1190 <open_appendfile+0x40>
lea    0xef1(%rip),%rdi
mov    %rbx,%rsi
mov    %rax,%rbx
call   1030 <printf@plt>
mov    %rbx,%rax
add    $0x3f8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %ebp,0xc(%rsp)
mov    0x2e2b(%rip),%r14
mov    0x2e34(%rip),%r15
lea    0x10(%rsp),%rbp
lea    0xe84(%rip),%r12
lea    0xe82(%rip),%r13
nopw   0x0(%rax,%rax,1)
mov    (%r14),%edi
lea    0xe28(%rip),%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
lea    0xe41(%rip),%rsi
nopl   0x0(%rax,%rax,1)
mov    (%r14),%edi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    (%r15),%edx
mov    %rbp,%rdi
mov    $0x3e8,%esi
call   1040 <fgets@plt>
mov    %rbp,%rdi
mov    %r12,%rsi
mov    %rbx,%rdx
call   1060 <sscanf@plt>
mov    %r13,%rsi
cmp    $0x1,%eax
jne    11f0 <open_appendfile+0xa0>
mov    %rbx,%rdi
lea    0xe1d(%rip),%rsi
call   1070 <fopen@plt>
mov    %r13,%rsi
test   %rax,%rax
je     11f0 <open_appendfile+0xa0>
mov    %rbx,%rdi
lea    0xdbf(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     11d0 <open_appendfile+0x80>
mov    0xc(%rsp),%ebp
test   %ebp,%ebp
jne    117b <open_appendfile+0x2b>
jmp    1190 <open_appendfile+0x40>

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

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