#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 */

/* Variables and functions */
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ free(char *);
char *get_next_filename(char *);
scalar_t__ nmalloc(int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char const *);
int strlen(char const *);
int write_file(char *, int, int /*<<< orphan*/, int /*<<< orphan*/);
void die_save_file(const char *die_filename) {
  char *ret;
  int i = -1;
  if (die_filename[0] == '\0')
    ret = get_next_filename("nano.save");
  else {
    char *buf = (char *)nmalloc((strlen(die_filename) + 6) * sizeof(char));
    strcpy(buf, die_filename);
    strcat(buf, ".save");
    ret = get_next_filename(buf);
    free(buf);
  }
  if (ret[0] != '\0')
    i = write_file(ret, 1, 0, 0);
  if (i != -1)
    fprintf(stderr, ("\nBuffer written to %s\n"), ret);
  else
    fprintf(stderr, ("\nNo %s written (too many backup files?)\n"), ret);
  free(ret);
}


x86 assembly (clang, -O0):
<die_save_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0xffffffff,-0x14(%rbp)
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    11b8 <die_save_file+0x38>
lea    0xe56(%rip),%rdi
call   1050 <get_next_filename@plt>
mov    %rax,-0x10(%rbp)
jmp    1208 <die_save_file+0x88>
mov    -0x8(%rbp),%rdi
call   1060 <strlen@plt>
add    $0x6,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1080 <nmalloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x20(%rbp),%rdi
lea    0xe17(%rip),%rsi
call   10a0 <strcat@plt>
mov    -0x20(%rbp),%rdi
call   1050 <get_next_filename@plt>
mov    %rax,-0x10(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     122d <die_save_file+0xad>
mov    -0x10(%rbp),%rdi
mov    $0x1,%esi
xor    %ecx,%ecx
mov    %ecx,%edx
call   1070 <write_file@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
je     1255 <die_save_file+0xd5>
mov    0x2da2(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xdbf(%rip),%rsi
call   1090 <fprintf@plt>
jmp    126e <die_save_file+0xee>
mov    0x2d84(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xdb8(%rip),%rsi
call   1090 <fprintf@plt>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<die_save_file>:
push   %r14
push   %rbx
push   %rax
cmpb   $0x0,(%rdi)
je     11d0 <die_save_file+0x50>
mov    %rdi,%rbx
call   1060 <strlen@plt>
lea    0x6(%rax),%edi
call   1080 <nmalloc@plt>
mov    %rax,%r14
mov    %rax,%rdi
mov    %rbx,%rsi
call   1040 <strcpy@plt>
lea    0xe56(%rip),%rsi
mov    %r14,%rdi
call   10a0 <strcat@plt>
mov    %r14,%rdi
call   1050 <get_next_filename@plt>
mov    %rax,%rbx
mov    %r14,%rdi
call   1030 <free@plt>
cmpb   $0x0,(%rbx)
jne    11e4 <die_save_file+0x64>
jmp    1203 <die_save_file+0x83>
lea    0xe29(%rip),%rdi
call   1050 <get_next_filename@plt>
mov    %rax,%rbx
cmpb   $0x0,(%rbx)
je     1203 <die_save_file+0x83>
mov    %rbx,%rdi
mov    $0x1,%esi
xor    %edx,%edx
xor    %ecx,%ecx
call   1070 <write_file@plt>
cmp    $0xffffffff,%eax
je     1203 <die_save_file+0x83>
lea    0xe09(%rip),%rsi
jmp    120a <die_save_file+0x8a>
lea    0xe17(%rip),%rsi
mov    0x2dcf(%rip),%rax
mov    (%rax),%edi
mov    %rbx,%rdx
call   1090 <fprintf@plt>
mov    %rbx,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1030 <free@plt>

LLVM IR (-O0):
@.str = [10 x i8] c"nano.save\00"
@.str.1 = [6 x i8] c".save\00"
@stderr = global i32 0
@.str.2 = [23 x i8] c"\0ABuffer written to %s\0A\00"
@.str.3 = [41 x i8] c"\0ANo %s written (too many backup files?)\0A\00"
define void @die_save_file(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8*
store i8* %0, i8** %2
store i32 -1, i32* %4
%6 = load i8*, i8** %2
%7 = getelementptr inbounds i8, i8* %6, i64 0
%8 = load i8, i8* %7
%9 = sext i8 %8 to i32
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %1
%12 = call i8* @get_next_filename(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
store i8* %12, i8** %3
br label %31
13: ; preds = %1
%14 = load i8*, i8** %2
%15 = call i32 @strlen(i8* %14)
%16 = add nsw i32 %15, 6
%17 = sext i32 %16 to i64
%18 = mul i64 %17, 1
%19 = trunc i64 %18 to i32
%20 = call i64 @nmalloc(i32 %19)
%21 = inttoptr i64 %20 to i8*
store i8* %21, i8** %5
%22 = load i8*, i8** %5
%23 = load i8*, i8** %2
%24 = call i32 @strcpy(i8* %22, i8* %23)
%25 = load i8*, i8** %5
%26 = call i32 @strcat(i8* %25, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%27 = load i8*, i8** %5
%28 = call i8* @get_next_filename(i8* %27)
store i8* %28, i8** %3
%29 = load i8*, i8** %5
%30 = call i32 @free(i8* %29)
br label %31
31: ; preds = %13, %11
%32 = load i8*, i8** %3
%33 = getelementptr inbounds i8, i8* %32, i64 0
%34 = load i8, i8* %33
%35 = sext i8 %34 to i32
%36 = icmp ne i32 %35, 0
br i1 %36, label %37, label %40
37: ; preds = %31
%38 = load i8*, i8** %3
%39 = call i32 @write_file(i8* %38, i32 1, i32 0, i32 0)
store i32 %39, i32* %4
br label %40
40: ; preds = %37, %31
%41 = load i32, i32* %4
%42 = icmp ne i32 %41, -1
br i1 %42, label %43, label %47
43: ; preds = %40
%44 = load i32, i32* @stderr
%45 = load i8*, i8** %3
%46 = call i32 @fprintf(i32 %44, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i8* %45)
br label %51
47: ; preds = %40
%48 = load i32, i32* @stderr
%49 = load i8*, i8** %3
%50 = call i32 @fprintf(i32 %48, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.3, i64 0, i64 0), i8* %49)
br label %51
51: ; preds = %47, %43
%52 = load i8*, i8** %3
%53 = call i32 @free(i8* %52)
ret void
}

LLVM IR (-O2):
@.str = [10 x i8] c"nano.save\00"
@.str.1 = [6 x i8] c".save\00"
@stderr = global i32 0
@.str.2 = [23 x i8] c"\0ABuffer written to %s\0A\00"
@.str.3 = [41 x i8] c"\0ANo %s written (too many backup files?)\0A\00"
define void @die_save_file(i8* %0) {
%2 = load i8, i8* %0
%3 = icmp eq i8 %2, 0
br i1 %3, label %4, label %6
4: ; preds = %1
%5 = tail call i8* @get_next_filename(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
br label %15
6: ; preds = %1
%7 = tail call i32 @strlen(i8* %0)
%8 = add nsw i32 %7, 6
%9 = tail call i64 @nmalloc(i32 %8)
%10 = inttoptr i64 %9 to i8*
%11 = tail call i32 @strcpy(i8* %10, i8* %0)
%12 = tail call i32 @strcat(i8* %10, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%13 = tail call i8* @get_next_filename(i8* %10)
%14 = tail call i32 @free(i8* %10)
br label %15
15: ; preds = %6, %4
%16 = phi i8* [ %5, %4 ], [ %13, %6 ]
%17 = load i8, i8* %16
%18 = icmp eq i8 %17, 0
br i1 %18, label %22, label %19
19: ; preds = %15
%20 = tail call i32 @write_file(i8* %16, i32 1, i32 0, i32 0)
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %23
22: ; preds = %15, %19
br label %23
23: ; preds = %19, %22
%24 = phi i8* [ getelementptr inbounds ([41 x i8], [41 x i8]* @.str.3, i64 0, i64 0), %22 ], [ getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), %19 ]
%25 = load i32, i32* @stderr
%26 = tail call i32 @fprintf(i32 %25, i8* %24, i8* %16)
%27 = tail call i32 @free(i8* %16)
ret void
}