#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 */
struct stat {
  scalar_t__ st_size;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
scalar_t__ ENOENT;
int EOF;
scalar_t__ errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ ferror(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ free(char *);
int getc(int /*<<< orphan*/ *);
char *malloc(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ putc(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ sprintf(char *, char *, char *);
int stat(char *, struct stat *);
int strlen(char *);
int backup(char *orig_file) {
  FILE *in_file, *out_file;
  char *new_name;
  int c, namesize;
  struct stat file_stat;
  if (stat(orig_file, &file_stat) == -1) {
    if (errno == ENOENT)
      return 0;
    else {
      perror("stat");
      return 1;
    }
  }
  if (file_stat.st_size == 0)
    return 0;
  in_file = fopen(orig_file, "r");
  if (in_file == NULL) {
    perror("fopen");
    return 1;
  }
  namesize = strlen(orig_file);
  new_name = malloc(namesize + 5);
  sprintf(new_name, "%s.bak", orig_file);
  out_file = fopen(new_name, "w");
  if (out_file == NULL) {
    perror("fopen");
    fclose(in_file);
    free(new_name);
    return 1;
  }
  while ((c = getc(in_file)) != EOF)
    putc(c, out_file);
  if (ferror(in_file) || ferror(out_file)) {
    perror("putc");
    fclose(in_file);
    fclose(out_file);
    free(new_name);
    return 1;
  }
  fclose(in_file);
  fclose(out_file);
  free(new_name);
  return 0;
}


x86 assembly (clang, -O0):
<backup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x38(%rbp),%rsi
call   1080 <stat@plt>
cmp    $0xffffffff,%eax
jne    1210 <backup+0x60>
mov    0x2dff(%rip),%rax
mov    (%rax),%rax
mov    0x2de5(%rip),%rcx
cmp    (%rcx),%rax
jne    11f8 <backup+0x48>
movl   $0x0,-0x4(%rbp)
jmp    1382 <backup+0x1d2>
lea    0xe01(%rip),%rdi
call   10b0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1382 <backup+0x1d2>
cmpq   $0x0,-0x38(%rbp)
jne    1227 <backup+0x77>
movl   $0x0,-0x4(%rbp)
jmp    1382 <backup+0x1d2>
mov    -0x10(%rbp),%rdi
lea    0xdd3(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    125e <backup+0xae>
lea    0xdba(%rip),%rdi
call   10b0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1382 <backup+0x1d2>
mov    -0x10(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,-0x30(%rbp)
mov    -0x30(%rbp),%edi
add    $0x5,%edi
call   1090 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rdx
lea    0xd85(%rip),%rsi
call   10c0 <sprintf@plt>
mov    -0x28(%rbp),%rdi
lea    0xd7c(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    12d6 <backup+0x126>
lea    0xd54(%rip),%rdi
call   10b0 <perror@plt>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movl   $0x1,-0x4(%rbp)
jmp    1382 <backup+0x1d2>
jmp    12db <backup+0x12b>
mov    -0x18(%rbp),%rdi
call   10d0 <getc@plt>
mov    %eax,-0x2c(%rbp)
mov    0x2cfa(%rip),%rcx
cmp    (%rcx),%eax
je     1307 <backup+0x157>
mov    -0x2c(%rbp),%edi
mov    -0x20(%rbp),%rsi
call   1070 <putc@plt>
jmp    12db <backup+0x12b>
mov    -0x18(%rbp),%rdi
call   1040 <ferror@plt>
cmp    $0x0,%rax
jne    132d <backup+0x17d>
mov    -0x20(%rbp),%rdi
call   1040 <ferror@plt>
cmp    $0x0,%rax
je     1360 <backup+0x1b0>
lea    0xce2(%rip),%rdi
call   10b0 <perror@plt>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movl   $0x1,-0x4(%rbp)
jmp    1382 <backup+0x1d2>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<backup>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbp
lea    0x8(%rsp),%rsi
call   1080 <stat@plt>
cmp    $0xffffffff,%eax
je     129a <backup+0xea>
xor    %r15d,%r15d
cmpq   $0x0,0x8(%rsp)
je     12b3 <backup+0x103>
lea    0xe1d(%rip),%rsi
mov    %rbp,%rdi
call   10a0 <fopen@plt>
test   %rax,%rax
je     12dc <backup+0x12c>
mov    %rax,%r12
mov    %rbp,%rdi
call   1060 <strlen@plt>
lea    0x5(%rax),%edi
call   1090 <malloc@plt>
mov    %rax,%r14
lea    0xdf7(%rip),%rsi
mov    %rax,%rdi
mov    %rbp,%rdx
call   10c0 <sprintf@plt>
lea    0xdec(%rip),%rsi
mov    %r14,%rdi
call   10a0 <fopen@plt>
test   %rax,%rax
je     12e5 <backup+0x135>
mov    %rax,%rbp
mov    %r12,%rdi
call   10d0 <getc@plt>
mov    0x2d9d(%rip),%rbx
cmp    (%rbx),%eax
je     1266 <backup+0xb6>
nop
mov    %eax,%edi
mov    %rbp,%rsi
call   1070 <putc@plt>
mov    %r12,%rdi
call   10d0 <getc@plt>
cmp    (%rbx),%eax
jne    1250 <backup+0xa0>
mov    %r12,%rdi
call   1040 <ferror@plt>
test   %rax,%rax
jne    12c3 <backup+0x113>
mov    %rbp,%rdi
call   1040 <ferror@plt>
test   %rax,%rax
jne    12c3 <backup+0x113>
mov    %r12,%rdi
call   1050 <fclose@plt>
mov    %rbp,%rdi
call   1050 <fclose@plt>
mov    %r14,%rdi
call   1030 <free@plt>
jmp    12b3 <backup+0x103>
mov    0x2d37(%rip),%rax
mov    (%rax),%rax
mov    0x2d1d(%rip),%rcx
xor    %r15d,%r15d
cmp    (%rcx),%rax
jne    1309 <backup+0x159>
mov    %r15d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd4c(%rip),%rdi
call   10b0 <perror@plt>
mov    %r12,%rdi
call   1050 <fclose@plt>
mov    %rbp,%rdi
jmp    12f4 <backup+0x144>
lea    0xd24(%rip),%rdi
jmp    1310 <backup+0x160>
lea    0xd1b(%rip),%rdi
call   10b0 <perror@plt>
mov    %r12,%rdi
call   1050 <fclose@plt>
mov    %r14,%rdi
call   1030 <free@plt>
mov    $0x1,%r15d
jmp    12b3 <backup+0x103>
lea    0xcf0(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%r15d
jmp    12b3 <backup+0x103>

LLVM IR (-O0):
%struct.stat = type { i64 }
@errno = global i64 0
@ENOENT = global i64 0
@.str = [5 x i8] c"stat\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [6 x i8] c"fopen\00"
@.str.3 = [7 x i8] c"%s.bak\00"
@.str.4 = [2 x i8] c"w\00"
@EOF = global i32 0
@.str.5 = [5 x i8] c"putc\00"
define i32 @backup(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i32*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca %struct.stat
store i8* %0, i8** %3
%10 = load i8*, i8** %3
%11 = call i32 @stat(i8* %10, %struct.stat* %9)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %20
13: ; preds = %1
%14 = load i64, i64* @errno
%15 = load i64, i64* @ENOENT
%16 = icmp eq i64 %14, %15
br i1 %16, label %17, label %18
17: ; preds = %13
store i32 0, i32* %2
br label %84
18: ; preds = %13
%19 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %2
br label %84
20: ; preds = %1
%21 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%22 = load i64, i64* %21
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %20
store i32 0, i32* %2
br label %84
25: ; preds = %20
%26 = load i8*, i8** %3
%27 = call i32* @fopen(i8* %26, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %27, i32** %4
%28 = load i32*, i32** %4
%29 = icmp eq i32* %28, null
br i1 %29, label %30, label %32
30: ; preds = %25
%31 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %2
br label %84
32: ; preds = %25
%33 = load i8*, i8** %3
%34 = call i32 @strlen(i8* %33)
store i32 %34, i32* %8
%35 = load i32, i32* %8
%36 = add nsw i32 %35, 5
%37 = call i8* @malloc(i32 %36)
store i8* %37, i8** %6
%38 = load i8*, i8** %6
%39 = load i8*, i8** %3
%40 = call i32 @sprintf(i8* %38, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i8* %39)
%41 = load i8*, i8** %6
%42 = call i32* @fopen(i8* %41, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
store i32* %42, i32** %5
%43 = load i32*, i32** %5
%44 = icmp eq i32* %43, null
br i1 %44, label %45, label %51
45: ; preds = %32
%46 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%47 = load i32*, i32** %4
%48 = call i32 @fclose(i32* %47)
%49 = load i8*, i8** %6
%50 = call i32 @free(i8* %49)
store i32 1, i32* %2
br label %84
51: ; preds = %32
br label %52
52: ; preds = %57, %51
%53 = load i32*, i32** %4
%54 = call i32 @getc(i32* %53)
store i32 %54, i32* %7
%55 = load i32, i32* @EOF
%56 = icmp ne i32 %54, %55
br i1 %56, label %57, label %61
57: ; preds = %52
%58 = load i32, i32* %7
%59 = load i32*, i32** %5
%60 = call i32 @putc(i32 %58, i32* %59)
br label %52
61: ; preds = %52
%62 = load i32*, i32** %4
%63 = call i64 @ferror(i32* %62)
%64 = icmp ne i64 %63, 0
br i1 %64, label %69, label %65
65: ; preds = %61
%66 = load i32*, i32** %5
%67 = call i64 @ferror(i32* %66)
%68 = icmp ne i64 %67, 0
br i1 %68, label %69, label %77
69: ; preds = %65, %61
%70 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
%71 = load i32*, i32** %4
%72 = call i32 @fclose(i32* %71)
%73 = load i32*, i32** %5
%74 = call i32 @fclose(i32* %73)
%75 = load i8*, i8** %6
%76 = call i32 @free(i8* %75)
store i32 1, i32* %2
br label %84
77: ; preds = %65
%78 = load i32*, i32** %4
%79 = call i32 @fclose(i32* %78)
%80 = load i32*, i32** %5
%81 = call i32 @fclose(i32* %80)
%82 = load i8*, i8** %6
%83 = call i32 @free(i8* %82)
store i32 0, i32* %2
br label %84
84: ; preds = %77, %69, %45, %30, %24, %18, %17
%85 = load i32, i32* %2
ret i32 %85
}

LLVM IR (-O2):
%struct.stat = type { i64 }
@errno = global i64 0
@ENOENT = global i64 0
@.str = [5 x i8] c"stat\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [6 x i8] c"fopen\00"
@.str.3 = [7 x i8] c"%s.bak\00"
@.str.4 = [2 x i8] c"w\00"
@EOF = global i32 0
@.str.5 = [5 x i8] c"putc\00"
define i32 @backup(i8* %0) {
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %2 to i8*
%4 = call i32 @stat(i8* %0, %struct.stat* %2)
%5 = icmp eq i32 %4, -1
br i1 %5, label %6, label %12
6: ; preds = %1
%7 = load i64, i64* @errno
%8 = load i64, i64* @ENOENT
%9 = icmp eq i64 %7, %8
br i1 %9, label %57, label %10
10: ; preds = %6
%11 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %57
12: ; preds = %1
%13 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%14 = load i64, i64* %13
%15 = icmp eq i64 %14, 0
br i1 %15, label %57, label %16
16: ; preds = %12
%17 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%18 = icmp eq i32* %17, null
br i1 %18, label %19, label %21
19: ; preds = %16
%20 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
br label %57
21: ; preds = %16
%22 = tail call i32 @strlen(i8* %0)
%23 = add nsw i32 %22, 5
%24 = tail call i8* @malloc(i32 %23)
%25 = tail call i32 @sprintf(i8* dereferenceable(1) %24, i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i8* %0)
%26 = tail call i32* @fopen(i8* %24, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%27 = icmp eq i32* %26, null
br i1 %27, label %32, label %28
28: ; preds = %21
%29 = tail call i32 @getc(i32* %17)
%30 = load i32, i32* @EOF
%31 = icmp eq i32 %29, %30
br i1 %31, label %42, label %36
32: ; preds = %21
%33 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%34 = tail call i32 @fclose(i32* %17)
%35 = tail call i32 @free(i8* %24)
br label %57
36: ; preds = %28, %36
%37 = phi i32 [ %39, %36 ], [ %29, %28 ]
%38 = tail call i32 @putc(i32 %37, i32* %26)
%39 = tail call i32 @getc(i32* %17)
%40 = load i32, i32* @EOF
%41 = icmp eq i32 %39, %40
br i1 %41, label %42, label %36
42: ; preds = %36, %28
%43 = tail call i64 @ferror(i32* %17)
%44 = icmp eq i64 %43, 0
br i1 %44, label %45, label %48
45: ; preds = %42
%46 = tail call i64 @ferror(i32* %26)
%47 = icmp eq i64 %46, 0
br i1 %47, label %53, label %48
48: ; preds = %45, %42
%49 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
%50 = tail call i32 @fclose(i32* %17)
%51 = tail call i32 @fclose(i32* %26)
%52 = tail call i32 @free(i8* %24)
br label %57
53: ; preds = %45
%54 = tail call i32 @fclose(i32* %17)
%55 = tail call i32 @fclose(i32* %26)
%56 = tail call i32 @free(i8* %24)
br label %57
57: ; preds = %12, %6, %53, %48, %32, %19, %10
%58 = phi i32 [ 1, %10 ], [ 1, %19 ], [ 1, %32 ], [ 1, %48 ], [ 0, %53 ], [ 0, %6 ], [ 0, %12 ]
ret i32 %58
}