#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;
char *append_string(char *, char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ free(char *);
int getc(int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ putc(int, int /*<<< orphan*/ *);
int backup_file(char *fname) {
  int c;
  FILE *fp, *fp_bak;
  char *fname_bak;
  fp = fopen(fname, "r");
  if (fp == NULL)
    return 1;
  fname_bak = append_string(fname, "~");
  fp_bak = fopen(fname_bak, "w");
  if (fp_bak == NULL) {
    fclose(fp);
    return 1;
  }
  while ((c = getc(fp)) != EOF) {
    putc(c, fp_bak);
  }
  printf("Options file [%s] has been overwritten\n", fname_bak);
  fclose(fp_bak);
  fclose(fp);
  free((char *)fname_bak);
  return 0;
}


x86 assembly (clang, -O0):
<backup_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe79(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11a7 <backup_file+0x37>
movl   $0x1,-0x4(%rbp)
jmp    1252 <backup_file+0xe2>
mov    -0x10(%rbp),%rdi
lea    0xe50(%rip),%rsi
call   1060 <append_string@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
lea    0xe3e(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11ef <backup_file+0x7f>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x1,-0x4(%rbp)
jmp    1252 <backup_file+0xe2>
jmp    11f4 <backup_file+0x84>
mov    -0x20(%rbp),%rdi
call   1090 <getc@plt>
mov    %eax,-0x14(%rbp)
mov    0x2de1(%rip),%rcx
cmp    (%rcx),%eax
je     1220 <backup_file+0xb0>
mov    -0x14(%rbp),%edi
mov    -0x28(%rbp),%rsi
call   1070 <putc@plt>
jmp    11f4 <backup_file+0x84>
mov    -0x30(%rbp),%rsi
lea    0xddb(%rip),%rdi
call   1050 <printf@plt>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<backup_file>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
lea    0xe7f(%rip),%rsi
call   1080 <fopen@plt>
mov    $0x1,%ebp
test   %rax,%rax
je     122a <backup_file+0xba>
mov    %rax,%r15
lea    0xe64(%rip),%rsi
mov    %rbx,%rdi
call   1060 <append_string@plt>
mov    %rax,%r14
lea    0xe54(%rip),%rsi
mov    %rax,%rdi
call   1080 <fopen@plt>
test   %rax,%rax
je     1222 <backup_file+0xb2>
mov    %rax,%rbx
mov    %r15,%rdi
call   1090 <getc@plt>
mov    0x2e19(%rip),%rbp
cmp    0x0(%rbp),%eax
je     11f7 <backup_file+0x87>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %eax,%edi
mov    %rbx,%rsi
call   1070 <putc@plt>
mov    %r15,%rdi
call   1090 <getc@plt>
cmp    0x0(%rbp),%eax
jne    11e0 <backup_file+0x70>
lea    0xe08(%rip),%rdi
mov    %r14,%rsi
call   1050 <printf@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    %r15,%rdi
call   1040 <fclose@plt>
mov    %r14,%rdi
call   1030 <free@plt>
xor    %ebp,%ebp
jmp    122a <backup_file+0xba>
mov    %r15,%rdi
call   1040 <fclose@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [2 x i8] c"~\00"
@.str.2 = [2 x i8] c"w\00"
@EOF = global i32 0
@.str.3 = [40 x i8] c"Options file [%s] has been overwritten\0A\00"
define i32 @backup_file(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca i8*
store i8* %0, i8** %3
%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** %5
%10 = load i32*, i32** %5
%11 = icmp eq i32* %10, null
br i1 %11, label %12, label %13
12: ; preds = %1
store i32 1, i32* %2
br label %42
13: ; preds = %1
%14 = load i8*, i8** %3
%15 = call i8* @append_string(i8* %14, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i8* %15, i8** %7
%16 = load i8*, i8** %7
%17 = call i32* @fopen(i8* %16, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %17, i32** %6
%18 = load i32*, i32** %6
%19 = icmp eq i32* %18, null
br i1 %19, label %20, label %23
20: ; preds = %13
%21 = load i32*, i32** %5
%22 = call i32 @fclose(i32* %21)
store i32 1, i32* %2
br label %42
23: ; preds = %13
br label %24
24: ; preds = %29, %23
%25 = load i32*, i32** %5
%26 = call i32 @getc(i32* %25)
store i32 %26, i32* %4
%27 = load i32, i32* @EOF
%28 = icmp ne i32 %26, %27
br i1 %28, label %29, label %33
29: ; preds = %24
%30 = load i32, i32* %4
%31 = load i32*, i32** %6
%32 = call i32 @putc(i32 %30, i32* %31)
br label %24
33: ; preds = %24
%34 = load i8*, i8** %7
%35 = call i32 @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.3, i64 0, i64 0), i8* %34)
%36 = load i32*, i32** %6
%37 = call i32 @fclose(i32* %36)
%38 = load i32*, i32** %5
%39 = call i32 @fclose(i32* %38)
%40 = load i8*, i8** %7
%41 = call i32 @free(i8* %40)
store i32 0, i32* %2
br label %42
42: ; preds = %33, %20, %12
%43 = load i32, i32* %2
ret i32 %43
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [2 x i8] c"~\00"
@.str.2 = [2 x i8] c"w\00"
@EOF = global i32 0
@.str.3 = [40 x i8] c"Options file [%s] has been overwritten\0A\00"
define i32 @backup_file(i8* %0) {
%2 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%3 = icmp eq i32* %2, null
br i1 %3, label %25, label %4
4: ; preds = %1
%5 = tail call i8* @append_string(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%6 = tail call i32* @fopen(i8* %5, 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 = %4
%9 = tail call i32 @getc(i32* %2)
%10 = load i32, i32* @EOF
%11 = icmp eq i32 %9, %10
br i1 %11, label %20, label %14
12: ; preds = %4
%13 = tail call i32 @fclose(i32* %2)
br label %25
14: ; preds = %8, %14
%15 = phi i32 [ %17, %14 ], [ %9, %8 ]
%16 = tail call i32 @putc(i32 %15, i32* %6)
%17 = tail call i32 @getc(i32* %2)
%18 = load i32, i32* @EOF
%19 = icmp eq i32 %17, %18
br i1 %19, label %20, label %14
20: ; preds = %14, %8
%21 = tail call i32 @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.3, i64 0, i64 0), i8* %5)
%22 = tail call i32 @fclose(i32* %6)
%23 = tail call i32 @fclose(i32* %2)
%24 = tail call i32 @free(i8* %5)
br label %25
25: ; preds = %1, %20, %12
%26 = phi i32 [ 1, %12 ], [ 0, %20 ], [ 1, %1 ]
ret i32 %26
}