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


x86 assembly (clang, -O0):
<open_writefile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x7f0,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x7e8(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe8b(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x7e8(%rbp)
cmp    $0x0,%rax
jne    1279 <open_writefile+0x129>
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    0xe75(%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
lea    -0x7e0(%rbp),%rdx
lea    0xe57(%rip),%rsi
call   1060 <sscanf@plt>
mov    %eax,%ecx
mov    $0x1,%al
cmp    $0x1,%ecx
mov    %al,-0x7e9(%rbp)
jne    122b <open_writefile+0xdb>
lea    -0x7e0(%rbp),%rdi
lea    0xdee(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x7e8(%rbp)
cmp    $0x0,%rax
sete   %al
mov    %al,-0x7e9(%rbp)
mov    -0x7e9(%rbp),%al
test   $0x1,%al
jne    123e <open_writefile+0xee>
jmp    1274 <open_writefile+0x124>
mov    0x2d93(%rip),%rax
mov    (%rax),%edi
lea    0xdf8(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
lea    -0x3f0(%rbp),%rdi
mov    0x2d85(%rip),%rax
mov    (%rax),%edx
mov    $0x3e8,%esi
call   1040 <fgets@plt>
jmp    11d7 <open_writefile+0x87>
jmp    1279 <open_writefile+0x129>
mov    -0x8(%rbp),%rsi
lea    0xdec(%rip),%rdi
call   1030 <printf@plt>
mov    -0x7e8(%rbp),%rax
add    $0x7f0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_writefile>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x7e8,%rsp
mov    %rdi,%rbx
lea    0xe95(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,%r14
test   %rax,%rax
jne    1233 <open_writefile+0xe3>
mov    0x2e55(%rip),%rbp
mov    0x0(%rbp),%edi
lea    0xe75(%rip),%rsi
mov    %rbx,0x8(%rsp)
mov    %rbx,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    0x0(%rbp),%edi
lea    0xe85(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    0x2e34(%rip),%r13
lea    0x400(%rsp),%rbx
lea    0xe80(%rip),%r15
lea    0x10(%rsp),%r12
lea    0xe77(%rip),%rbp
jmp    11f3 <open_writefile+0xa3>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    0x2df1(%rip),%rax
mov    (%rax),%edi
mov    %rbp,%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    0x0(%r13),%edx
mov    %rbx,%rdi
mov    $0x3e8,%esi
call   1040 <fgets@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
call   1060 <sscanf@plt>
cmp    $0x1,%eax
jne    11e0 <open_writefile+0x90>
mov    %r12,%rdi
lea    0xddf(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     11e0 <open_writefile+0x90>
mov    %rax,%r14
mov    0x8(%rsp),%rbx
lea    0xe36(%rip),%rdi
mov    %rbx,%rsi
call   1030 <printf@plt>
mov    %r14,%rax
add    $0x7e8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"w\00"
@stderr = global i32 0
@.str.1 = [41 x i8] c"ERROR: open_writefile. 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 = [29 x i8] c"open_writefile: %s now open\0A\00"
define i32* @open_writefile(i8* %0) {
%2 = alloca i8*
%3 = alloca [1000 x i8]
%4 = alloca [1000 x i8]
%5 = alloca i32*
store i8* %0, i8** %2
store i32* null, i32** %5
%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** %5
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %36
9: ; preds = %1
%10 = load i32, i32* @stderr
%11 = load i8*, i8** %2
%12 = call i32 (i32, i8*, ...) @fprintf(i32 %10, i8* getelementptr inbounds ([41 x i8], [41 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 = getelementptr inbounds [1000 x i8], [1000 x i8]* %4, i64 0, i64 0
%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 = getelementptr inbounds [1000 x i8], [1000 x i8]* %4, i64 0, i64 0
%25 = call i32* @fopen(i8* %24, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %25, i32** %5
%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 %36
36: ; preds = %35, %1
%37 = load i8*, i8** %2
%38 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.5, i64 0, i64 0), i8* %37)
%39 = load i32*, i32** %5
ret i32* %39
}

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