#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 */
scalar_t__ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ scanf(char *, char *);
FILE *safeFileWrite(char *__fn__) {
  FILE *__ft__ = fopen(__fn__, "r");
  if (__ft__ != NULL) {
    printf("%s already exists! Overwrite(y/n)?\n", __fn__);
    char __yn__;
    scanf("%c", &__yn__);
    if (__yn__ != 'y' && __yn__ != 'Y') {
      printf("Not overwriting.\n");
      return NULL;
    }
  }
  if (fclose(__ft__))
    return NULL;
  return fopen(__fn__, "w");
}


x86 assembly (clang, -O0):
<safeFileWrite>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xea9(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11c7 <safeFileWrite+0x87>
mov    -0x10(%rbp),%rsi
lea    0xe8c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xea2(%rip),%rdi
lea    -0x19(%rbp),%rsi
call   1050 <scanf@plt>
movsbl -0x19(%rbp),%eax
cmp    $0x79,%eax
je     11c2 <safeFileWrite+0x82>
movsbl -0x19(%rbp),%eax
cmp    $0x59,%eax
je     11c2 <safeFileWrite+0x82>
lea    0xe7b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    11fb <safeFileWrite+0xbb>
jmp    11c7 <safeFileWrite+0x87>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
cmp    $0x0,%rax
je     11e7 <safeFileWrite+0xa7>
movq   $0x0,-0x8(%rbp)
jmp    11fb <safeFileWrite+0xbb>
mov    -0x10(%rbp),%rdi
lea    0xe49(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<safeFileWrite>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
lea    0xea2(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,%rbx
test   %rax,%rax
je     1197 <safeFileWrite+0x47>
lea    0xe90(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xea3(%rip),%rdi
lea    0x7(%rsp),%rsi
call   1060 <scanf@plt>
mov    0x7(%rsp),%al
and    $0xdf,%al
cmp    $0x59,%al
jne    11a8 <safeFileWrite+0x58>
mov    %rbx,%rdi
call   1040 <fclose@plt>
test   %rax,%rax
je     11b8 <safeFileWrite+0x68>
xor    %eax,%eax
jmp    11c7 <safeFileWrite+0x77>
lea    0xe7c(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
jmp    11c7 <safeFileWrite+0x77>
lea    0xe6a(%rip),%rsi
mov    %r14,%rdi
call   1070 <fopen@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [36 x i8] c"%s already exists! Overwrite(y/n)?\0A\00"
@.str.2 = [3 x i8] c"%c\00"
@.str.3 = [18 x i8] c"Not overwriting.\0A\00"
@.str.4 = [2 x i8] c"w\00"
define i32* @safeFileWrite(i8* %0) {
%2 = alloca i32*
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i8
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%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 = load i32*, i32** %4
%9 = icmp ne i32* %8, null
br i1 %9, label %10, label %24
10: ; preds = %1
%11 = load i8*, i8** %3
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0), i8* %11)
%13 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %5)
%14 = load i8, i8* %5
%15 = sext i8 %14 to i32
%16 = icmp ne i32 %15, 121
br i1 %16, label %17, label %23
17: ; preds = %10
%18 = load i8, i8* %5
%19 = sext i8 %18 to i32
%20 = icmp ne i32 %19, 89
br i1 %20, label %21, label %23
21: ; preds = %17
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0))
store i32* null, i32** %2
br label %32
23: ; preds = %17, %10
br label %24
24: ; preds = %23, %1
%25 = load i32*, i32** %4
%26 = call i64 @fclose(i32* %25)
%27 = icmp ne i64 %26, 0
br i1 %27, label %28, label %29
28: ; preds = %24
store i32* null, i32** %2
br label %32
29: ; preds = %24
%30 = load i8*, i8** %3
%31 = call i32* @fopen(i8* %30, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
store i32* %31, i32** %2
br label %32
32: ; preds = %29, %28, %21
%33 = load i32*, i32** %2
ret i32* %33
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [36 x i8] c"%s already exists! Overwrite(y/n)?\0A\00"
@.str.2 = [3 x i8] c"%c\00"
@.str.4 = [2 x i8] c"w\00"
@str = [17 x i8] c"Not overwriting.\00"
define noalias i32* @safeFileWrite(i8* %0) {
%2 = alloca i8
%3 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %14, label %5
5: ; preds = %1
%6 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%7 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %2)
%8 = load i8, i8* %2
%9 = and i8 %8, -33
%10 = icmp eq i8 %9, 89
br i1 %10, label %13, label %11
11: ; preds = %5
%12 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str, i64 0, i64 0))
br label %19
13: ; preds = %5
br label %14
14: ; preds = %13, %1
%15 = call i64 @fclose(i32* %3)
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %14
%18 = call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
br label %19
19: ; preds = %11, %14, %17
%20 = phi i32* [ %18, %17 ], [ null, %11 ], [ null, %14 ]
ret i32* %20
}