#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 EOF;
int FILENAME_MAX;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fputc(int, int /*<<< orphan*/ *);
int getchar();
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ scanf(char *, char *);
int main(void) {
  int ch;
  FILE *fp;
  char fname[FILENAME_MAX];
  printf("File name: ");
  scanf("%s\n", fname);
  if ((fp = fopen(fname, "w")) == NULL)
    printf("\a Failed to open the file.\n");
  else {
    while ((ch = getchar()) != EOF)
      fputc(ch, fp);
    fclose(fp);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e6a(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x18(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x28(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x20(%rbp)
lea    0xe5e(%rip),%rdi
call   1040 <printf@plt>
mov    -0x28(%rbp),%rsi
lea    0xe5a(%rip),%rdi
call   1070 <scanf@plt>
mov    -0x28(%rbp),%rdi
lea    0xe4e(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    11e6 <main+0x86>
lea    0xe36(%rip),%rdi
call   1040 <printf@plt>
jmp    121e <main+0xbe>
jmp    11eb <main+0x8b>
mov    $0x0,%al
call   1060 <getchar@plt>
mov    %eax,-0x8(%rbp)
mov    0x2dec(%rip),%rcx
cmp    (%rcx),%eax
je     1215 <main+0xb5>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1050 <fputc@plt>
jmp    11eb <main+0x8b>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
mov    %rsp,%rbp
push   %r14
push   %rbx
mov    0x2e62(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
lea    0xe68(%rip),%rdi
call   1050 <printf@plt>
lea    0xe68(%rip),%rdi
mov    %rbx,%rsi
call   1080 <scanf@plt>
lea    0xe5d(%rip),%rsi
mov    %rbx,%rdi
call   1090 <fopen@plt>
test   %rax,%rax
je     1200 <main+0x90>
mov    %rax,%rbx
xor    %eax,%eax
call   1070 <getchar@plt>
mov    0x2e17(%rip),%r14
cmp    (%r14),%eax
je     11f6 <main+0x86>
cs nopw 0x0(%rax,%rax,1)
mov    %eax,%edi
mov    %rbx,%rsi
call   1060 <fputc@plt>
xor    %eax,%eax
call   1070 <getchar@plt>
cmp    (%r14),%eax
jne    11e0 <main+0x70>
mov    %rbx,%rdi
call   1040 <fclose@plt>
jmp    120c <main+0x9c>
lea    0xe0b(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
lea    -0x10(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@FILENAME_MAX = global i32 0
@.str = [12 x i8] c"File name: \00"
@.str.1 = [4 x i8] c"%s\0A\00"
@.str.2 = [2 x i8] c"w\00"
@.str.3 = [28 x i8] c"\07 Failed to open the file.\0A\00"
@EOF = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32*
%4 = alloca i8*
%5 = alloca i64
store i32 0, i32* %1
%6 = load i32, i32* @FILENAME_MAX
%7 = zext i32 %6 to i64
%8 = call i8* @llvm.stacksave()
store i8* %8, i8** %4
%9 = alloca i8, i64 %7
store i64 %7, i64* %5
%10 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%11 = call i32 @scanf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %9)
%12 = call i32* @fopen(i8* %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %12, i32** %3
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %16
14: ; preds = %0
%15 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0))
br label %28
16: ; preds = %0
br label %17
17: ; preds = %21, %16
%18 = call i32 (...) @getchar()
store i32 %18, i32* %2
%19 = load i32, i32* @EOF
%20 = icmp ne i32 %18, %19
br i1 %20, label %21, label %25
21: ; preds = %17
%22 = load i32, i32* %2
%23 = load i32*, i32** %3
%24 = call i32 @fputc(i32 %22, i32* %23)
br label %17
25: ; preds = %17
%26 = load i32*, i32** %3
%27 = call i32 @fclose(i32* %26)
br label %28
28: ; preds = %25, %14
store i32 0, i32* %1
%29 = load i8*, i8** %4
%30 = load i32, i32* %1
ret i32 %30
}

LLVM IR (-O2):
@FILENAME_MAX = global i32 0
@.str = [12 x i8] c"File name: \00"
@.str.1 = [4 x i8] c"%s\0A\00"
@.str.2 = [2 x i8] c"w\00"
@EOF = global i32 0
@str = [27 x i8] c"\07 Failed to open the file.\00"
define i32 @main() {
%1 = load i32, i32* @FILENAME_MAX
%2 = zext i32 %1 to i64
%3 = alloca i8, i64 %2
%4 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%5 = call i32 @scanf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %3)
%6 = call i32* @fopen(i8* %3, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%7 = icmp eq i32* %6, null
br i1 %7, label %12, label %8
8: ; preds = %0
%9 = call i32 (...) @getchar()
%10 = load i32, i32* @EOF
%11 = icmp eq i32 %9, %10
br i1 %11, label %20, label %14
12: ; preds = %0
%13 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str, i64 0, i64 0))
br label %22
14: ; preds = %8, %14
%15 = phi i32 [ %17, %14 ], [ %9, %8 ]
%16 = call i32 @fputc(i32 %15, i32* %6)
%17 = call i32 (...) @getchar()
%18 = load i32, i32* @EOF
%19 = icmp eq i32 %17, %18
br i1 %19, label %20, label %14
20: ; preds = %14, %8
%21 = call i32 @fclose(i32* %6)
br label %22
22: ; preds = %20, %12
ret i32 0
}