#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 */
scalar_t__ EEXIST;
scalar_t__ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *,
                           char const *, ...);
int link(char const *, char const *);
int rename(char const *, char const *);
int /*<<< orphan*/ stderr;
char *strerror(scalar_t__);
int unlink(char const *);
int replace_file(const char *org, const char *new, const char *backup) {
  if (backup) {
    int res;
    if ((res = link(org, backup)) == -1) {
      if (errno == EEXIST) {
        if (unlink(backup) == -1) {
          fprintf(stderr, "Error unlinking old backupfile %s: %s\n", backup,
                  strerror(errno));
          return 0;
        }
        res = link(org, backup);
      }
      if (res == -1) {
        fprintf(stderr, "Error making backupfile %s: %s\n", backup,
                strerror(errno));
        return 0;
      }
    }
  }
  if (rename(new, org) == -1) {
    fprintf(stderr, "Error: failed to replace %s with %s: %s\n", org, new,
            strerror(errno));
    return 0;
  }
  return 1;
}


x86 assembly (clang, -O0):
<replace_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1266 <replace_file+0x116>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1060 <link@plt>
mov    %eax,-0x24(%rbp)
cmp    $0xffffffff,%eax
jne    1261 <replace_file+0x111>
mov    0x2e41(%rip),%rax
mov    (%rax),%rax
mov    0x2e47(%rip),%rcx
cmp    (%rcx),%rax
jne    120b <replace_file+0xbb>
mov    -0x20(%rbp),%rdi
call   1030 <unlink@plt>
cmp    $0xffffffff,%eax
jne    11fb <replace_file+0xab>
mov    0x2e1d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    0x2e01(%rip),%rax
mov    (%rax),%rdi
call   1070 <strerror@plt>
mov    -0x34(%rbp),%edi
mov    -0x30(%rbp),%rdx
mov    %rax,%rcx
lea    0xe18(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12d6 <replace_file+0x186>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1060 <link@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    125c <replace_file+0x10c>
mov    0x2dbc(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    0x2da0(%rip),%rax
mov    (%rax),%rdi
call   1070 <strerror@plt>
mov    -0x44(%rbp),%edi
mov    -0x40(%rbp),%rdx
mov    %rax,%rcx
lea    0xdde(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12d6 <replace_file+0x186>
jmp    1261 <replace_file+0x111>
jmp    1266 <replace_file+0x116>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1050 <rename@plt>
cmp    $0xffffffff,%eax
jne    12cf <replace_file+0x17f>
mov    0x2d55(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x5c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    0x2d31(%rip),%rax
mov    (%rax),%rdi
call   1070 <strerror@plt>
mov    -0x5c(%rbp),%edi
mov    -0x58(%rbp),%rdx
mov    -0x50(%rbp),%rcx
mov    %rax,%r8
lea    0xd8b(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12d6 <replace_file+0x186>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<replace_file>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
test   %rdx,%rdx
je     1175 <replace_file+0x25>
mov    %rdx,%r15
mov    %rbx,%rdi
mov    %rdx,%rsi
call   1060 <link@plt>
cmp    $0xffffffff,%eax
je     11c5 <replace_file+0x75>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1050 <rename@plt>
mov    $0x1,%ebp
cmp    $0xffffffff,%eax
jne    1242 <replace_file+0xf2>
mov    0x2e43(%rip),%rax
mov    (%rax),%r15d
mov    0x2e31(%rip),%rax
mov    (%rax),%rdi
call   1070 <strerror@plt>
lea    0xe99(%rip),%rsi
xor    %ebp,%ebp
mov    %r15d,%edi
mov    %rbx,%rdx
mov    %r14,%rcx
mov    %rax,%r8
xor    %eax,%eax
call   1040 <fprintf@plt>
jmp    1242 <replace_file+0xf2>
mov    0x2e04(%rip),%rbp
mov    0x0(%rbp),%rdi
mov    0x2e09(%rip),%rax
cmp    (%rax),%rdi
jne    1201 <replace_file+0xb1>
mov    %r15,%rdi
call   1030 <unlink@plt>
cmp    $0xffffffff,%eax
je     1218 <replace_file+0xc8>
mov    %rbx,%rdi
mov    %r15,%rsi
call   1060 <link@plt>
cmp    $0xffffffff,%eax
jne    1175 <replace_file+0x25>
mov    0x0(%rbp),%rdi
mov    0x2dd0(%rip),%rax
mov    (%rax),%ebx
call   1070 <strerror@plt>
lea    0xe11(%rip),%rsi
jmp    1231 <replace_file+0xe1>
mov    0x2db9(%rip),%rax
mov    (%rax),%ebx
mov    0x0(%rbp),%rdi
call   1070 <strerror@plt>
lea    0xdcf(%rip),%rsi
xor    %ebp,%ebp
mov    %ebx,%edi
mov    %r15,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i64 0
@EEXIST = global i64 0
@stderr = global i32 0
@.str = [39 x i8] c"Error unlinking old backupfile %s: %s\0A\00"
@.str.1 = [32 x i8] c"Error making backupfile %s: %s\0A\00"
@.str.2 = [41 x i8] c"Error: failed to replace %s with %s: %s\0A\00"
define i32 @replace_file(i8* %0, i8* %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i8* %2, i8** %7
%9 = load i8*, i8** %7
%10 = icmp ne i8* %9, null
br i1 %10, label %11, label %45
11: ; preds = %3
%12 = load i8*, i8** %5
%13 = load i8*, i8** %7
%14 = call i32 @link(i8* %12, i8* %13)
store i32 %14, i32* %8
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %44
16: ; preds = %11
%17 = load i64, i64* @errno
%18 = load i64, i64* @EEXIST
%19 = icmp eq i64 %17, %18
br i1 %19, label %20, label %34
20: ; preds = %16
%21 = load i8*, i8** %7
%22 = call i32 @unlink(i8* %21)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %30
24: ; preds = %20
%25 = load i32, i32* @stderr
%26 = load i8*, i8** %7
%27 = load i64, i64* @errno
%28 = call i8* @strerror(i64 %27)
%29 = call i32 (i32, i8*, i8*, i8*, ...) @fprintf(i32 %25, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0), i8* %26, i8* %28)
store i32 0, i32* %4
br label %58
30: ; preds = %20
%31 = load i8*, i8** %5
%32 = load i8*, i8** %7
%33 = call i32 @link(i8* %31, i8* %32)
store i32 %33, i32* %8
br label %34
34: ; preds = %30, %16
%35 = load i32, i32* %8
%36 = icmp eq i32 %35, -1
br i1 %36, label %37, label %43
37: ; preds = %34
%38 = load i32, i32* @stderr
%39 = load i8*, i8** %7
%40 = load i64, i64* @errno
%41 = call i8* @strerror(i64 %40)
%42 = call i32 (i32, i8*, i8*, i8*, ...) @fprintf(i32 %38, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i8* %39, i8* %41)
store i32 0, i32* %4
br label %58
43: ; preds = %34
br label %44
44: ; preds = %43, %11
br label %45
45: ; preds = %44, %3
%46 = load i8*, i8** %6
%47 = load i8*, i8** %5
%48 = call i32 @rename(i8* %46, i8* %47)
%49 = icmp eq i32 %48, -1
br i1 %49, label %50, label %57
50: ; preds = %45
%51 = load i32, i32* @stderr
%52 = load i8*, i8** %5
%53 = load i8*, i8** %6
%54 = load i64, i64* @errno
%55 = call i8* @strerror(i64 %54)
%56 = call i32 (i32, i8*, i8*, i8*, ...) @fprintf(i32 %51, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0), i8* %52, i8* %53, i8* %55)
store i32 0, i32* %4
br label %58
57: ; preds = %45
store i32 1, i32* %4
br label %58
58: ; preds = %57, %50, %37, %24
%59 = load i32, i32* %4
ret i32 %59
}

LLVM IR (-O2):
@errno = global i64 0
@EEXIST = global i64 0
@stderr = global i32 0
@.str = [39 x i8] c"Error unlinking old backupfile %s: %s\0A\00"
@.str.1 = [32 x i8] c"Error making backupfile %s: %s\0A\00"
@.str.2 = [41 x i8] c"Error: failed to replace %s with %s: %s\0A\00"
define i32 @replace_file(i8* %0, i8* %1, i8* %2) {
%4 = icmp eq i8* %2, null
br i1 %4, label %30, label %5
5: ; preds = %3
%6 = tail call i32 @link(i8* %0, i8* %2)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %30
8: ; preds = %5
%9 = load i64, i64* @errno
%10 = load i64, i64* @EEXIST
%11 = icmp eq i64 %9, %10
br i1 %11, label %12, label %25
12: ; preds = %8
%13 = tail call i32 @unlink(i8* %2)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %20
15: ; preds = %12
%16 = load i32, i32* @stderr
%17 = load i64, i64* @errno
%18 = tail call i8* @strerror(i64 %17)
%19 = tail call i32 (i32, i8*, i8*, i8*, ...) @fprintf(i32 %16, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0), i8* %2, i8* %18)
br label %38
20: ; preds = %12
%21 = tail call i32 @link(i8* %0, i8* %2)
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %30
23: ; preds = %20
%24 = load i64, i64* @errno
br label %25
25: ; preds = %23, %8
%26 = phi i64 [ %24, %23 ], [ %9, %8 ]
%27 = load i32, i32* @stderr
%28 = tail call i8* @strerror(i64 %26)
%29 = tail call i32 (i32, i8*, i8*, i8*, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i8* %2, i8* %28)
br label %38
30: ; preds = %5, %20, %3
%31 = tail call i32 @rename(i8* %1, i8* %0)
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %38
33: ; preds = %30
%34 = load i32, i32* @stderr
%35 = load i64, i64* @errno
%36 = tail call i8* @strerror(i64 %35)
%37 = tail call i32 (i32, i8*, i8*, i8*, ...) @fprintf(i32 %34, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0), i8* %0, i8* %1, i8* %36)
br label %38
38: ; preds = %15, %25, %30, %33
%39 = phi i32 [ 0, %33 ], [ 1, %30 ], [ 0, %25 ], [ 0, %15 ]
ret i32 %39
}