#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 {
  int dummy;
};

/* Variables and functions */
scalar_t__ EEXIST;
scalar_t__ ENOSYS;
scalar_t__ EPERM;
scalar_t__ EXDEV;
int compare_stat(struct stat *, struct stat *);
scalar_t__ errno;
scalar_t__ link(char const *, char const *);
int lstat(char const *, struct stat *);
int rename(char const *, char const *);
int unlink(char const *);
int safe_rename(const char *src, const char *target) {
  struct stat ssb, tsb;
  if (!src || !target)
    return -1;
  if (link(src, target) != 0) {
    do {
    } while (0);
    if (errno == EXDEV || errno == ENOSYS || errno == EPERM) {
      do {
      } while (0);
      if (rename(src, target) == -1) {
        do {
        } while (0);
        return -1;
      }
      do {
      } while (0);
      return 0;
    }
    return -1;
  }
  if (lstat(src, &ssb) == -1) {
    do {
    } while (0);
    return -1;
  }
  if (lstat(target, &tsb) == -1) {
    do {
    } while (0);
    return -1;
  }
  if (compare_stat(&ssb, &tsb) == -1) {
    do {
    } while (0);
    errno = EEXIST;
    return -1;
  }
  if (unlink(src) == -1) {
    do {
    } while (0);
  }
  return 0;
}


x86 assembly (clang, -O0):
<safe_rename>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1176 <safe_rename+0x26>
cmpq   $0x0,-0x18(%rbp)
jne    1182 <safe_rename+0x32>
movl   $0xffffffff,-0x4(%rbp)
jmp    1309 <safe_rename+0x1b9>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1070 <link@plt>
cmp    $0x0,%rax
je     1249 <safe_rename+0xf9>
jmp    119e <safe_rename+0x4e>
jmp    11a3 <safe_rename+0x53>
mov    0x2e1e(%rip),%rax
mov    (%rax),%rax
mov    0x2e1c(%rip),%rcx
cmp    (%rcx),%rax
je     11f1 <safe_rename+0xa1>
mov    0x2e04(%rip),%rax
mov    (%rax),%rax
mov    0x2e1a(%rip),%rcx
cmp    (%rcx),%rax
je     11f1 <safe_rename+0xa1>
mov    0x2dea(%rip),%rax
mov    (%rax),%rax
mov    0x2dd0(%rip),%rcx
cmp    (%rcx),%rax
jne    123d <safe_rename+0xed>
jmp    11f6 <safe_rename+0xa6>
jmp    11fb <safe_rename+0xab>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1060 <rename@plt>
cmp    $0xffffffff,%eax
jne    1227 <safe_rename+0xd7>
jmp    1216 <safe_rename+0xc6>
jmp    121b <safe_rename+0xcb>
movl   $0xffffffff,-0x4(%rbp)
jmp    1309 <safe_rename+0x1b9>
jmp    122c <safe_rename+0xdc>
jmp    1231 <safe_rename+0xe1>
movl   $0x0,-0x4(%rbp)
jmp    1309 <safe_rename+0x1b9>
movl   $0xffffffff,-0x4(%rbp)
jmp    1309 <safe_rename+0x1b9>
mov    -0x10(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1040 <lstat@plt>
cmp    $0xffffffff,%eax
jne    1275 <safe_rename+0x125>
jmp    1264 <safe_rename+0x114>
jmp    1269 <safe_rename+0x119>
movl   $0xffffffff,-0x4(%rbp)
jmp    1309 <safe_rename+0x1b9>
mov    -0x18(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1040 <lstat@plt>
cmp    $0xffffffff,%eax
jne    12a1 <safe_rename+0x151>
jmp    1290 <safe_rename+0x140>
jmp    1295 <safe_rename+0x145>
movl   $0xffffffff,-0x4(%rbp)
jmp    1309 <safe_rename+0x1b9>
lea    -0x20(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1050 <compare_stat@plt>
cmp    $0xffffffff,%eax
jne    12e1 <safe_rename+0x191>
jmp    12bc <safe_rename+0x16c>
jmp    12c1 <safe_rename+0x171>
mov    0x2d10(%rip),%rax
mov    (%rax),%rcx
mov    0x2cf6(%rip),%rax
mov    %rcx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1309 <safe_rename+0x1b9>
mov    -0x10(%rbp),%rdi
call   1030 <unlink@plt>
cmp    $0xffffffff,%eax
jne    1302 <safe_rename+0x1b2>
jmp    12f8 <safe_rename+0x1a8>
jmp    12fd <safe_rename+0x1ad>
jmp    1302 <safe_rename+0x1b2>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<safe_rename>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    $0xffffffff,%r14d
test   %rdi,%rdi
je     1220 <safe_rename+0xd0>
mov    %rsi,%rbp
test   %rsi,%rsi
je     1220 <safe_rename+0xd0>
mov    %rdi,%rbx
mov    %rbp,%rsi
call   1070 <link@plt>
test   %rax,%rax
je     11cb <safe_rename+0x7b>
mov    0x2e3e(%rip),%rax
mov    (%rax),%rax
mov    0x2e3c(%rip),%rcx
cmp    (%rcx),%rax
je     11b1 <safe_rename+0x61>
mov    0x2e48(%rip),%rcx
cmp    (%rcx),%rax
je     11b1 <safe_rename+0x61>
mov    0x2e0c(%rip),%rcx
cmp    (%rcx),%rax
jne    1220 <safe_rename+0xd0>
mov    %rbx,%rdi
mov    %rbp,%rsi
call   1060 <rename@plt>
xor    %r14d,%r14d
cmp    $0xffffffff,%eax
sete   %r14b
neg    %r14d
jmp    1220 <safe_rename+0xd0>
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
call   1040 <lstat@plt>
cmp    $0xffffffff,%eax
je     1220 <safe_rename+0xd0>
mov    %rsp,%rsi
mov    %rbp,%rdi
call   1040 <lstat@plt>
cmp    $0xffffffff,%eax
je     1220 <safe_rename+0xd0>
lea    0x8(%rsp),%rdi
mov    %rsp,%rsi
call   1050 <compare_stat@plt>
cmp    $0xffffffff,%eax
je     120c <safe_rename+0xbc>
mov    %rbx,%rdi
call   1030 <unlink@plt>
xor    %r14d,%r14d
jmp    1220 <safe_rename+0xd0>
mov    0x2dc5(%rip),%rax
mov    (%rax),%rax
mov    0x2dab(%rip),%rcx
mov    %rax,(%rcx)
mov    %r14d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@errno = global i64 0
@EXDEV = global i64 0
@ENOSYS = global i64 0
@EPERM = global i64 0
@EEXIST = global i64 0
define i32 @safe_rename(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca %struct.stat
%7 = alloca %struct.stat
store i8* %0, i8** %4
store i8* %1, i8** %5
%8 = load i8*, i8** %4
%9 = icmp ne i8* %8, null
br i1 %9, label %10, label %13
10: ; preds = %2
%11 = load i8*, i8** %5
%12 = icmp ne i8* %11, null
br i1 %12, label %14, label %13
13: ; preds = %10, %2
store i32 -1, i32* %3
br label %76
14: ; preds = %10
%15 = load i8*, i8** %4
%16 = load i8*, i8** %5
%17 = call i64 @link(i8* %15, i8* %16)
%18 = icmp ne i64 %17, 0
br i1 %18, label %19, label %47
19: ; preds = %14
br label %20
20: ; preds = %19
br label %21
21: ; preds = %20
%22 = load i64, i64* @errno
%23 = load i64, i64* @EXDEV
%24 = icmp eq i64 %22, %23
br i1 %24, label %33, label %25
25: ; preds = %21
%26 = load i64, i64* @errno
%27 = load i64, i64* @ENOSYS
%28 = icmp eq i64 %26, %27
br i1 %28, label %33, label %29
29: ; preds = %25
%30 = load i64, i64* @errno
%31 = load i64, i64* @EPERM
%32 = icmp eq i64 %30, %31
br i1 %32, label %33, label %46
33: ; preds = %29, %25, %21
br label %34
34: ; preds = %33
br label %35
35: ; preds = %34
%36 = load i8*, i8** %4
%37 = load i8*, i8** %5
%38 = call i32 @rename(i8* %36, i8* %37)
%39 = icmp eq i32 %38, -1
br i1 %39, label %40, label %43
40: ; preds = %35
br label %41
41: ; preds = %40
br label %42
42: ; preds = %41
store i32 -1, i32* %3
br label %76
43: ; preds = %35
br label %44
44: ; preds = %43
br label %45
45: ; preds = %44
store i32 0, i32* %3
br label %76
46: ; preds = %29
store i32 -1, i32* %3
br label %76
47: ; preds = %14
%48 = load i8*, i8** %4
%49 = call i32 @lstat(i8* %48, %struct.stat* %6)
%50 = icmp eq i32 %49, -1
br i1 %50, label %51, label %54
51: ; preds = %47
br label %52
52: ; preds = %51
br label %53
53: ; preds = %52
store i32 -1, i32* %3
br label %76
54: ; preds = %47
%55 = load i8*, i8** %5
%56 = call i32 @lstat(i8* %55, %struct.stat* %7)
%57 = icmp eq i32 %56, -1
br i1 %57, label %58, label %61
58: ; preds = %54
br label %59
59: ; preds = %58
br label %60
60: ; preds = %59
store i32 -1, i32* %3
br label %76
61: ; preds = %54
%62 = call i32 @compare_stat(%struct.stat* %6, %struct.stat* %7)
%63 = icmp eq i32 %62, -1
br i1 %63, label %64, label %68
64: ; preds = %61
br label %65
65: ; preds = %64
br label %66
66: ; preds = %65
%67 = load i64, i64* @EEXIST
store i64 %67, i64* @errno
store i32 -1, i32* %3
br label %76
68: ; preds = %61
%69 = load i8*, i8** %4
%70 = call i32 @unlink(i8* %69)
%71 = icmp eq i32 %70, -1
br i1 %71, label %72, label %75
72: ; preds = %68
br label %73
73: ; preds = %72
br label %74
74: ; preds = %73
br label %75
75: ; preds = %74, %68
store i32 0, i32* %3
br label %76
76: ; preds = %75, %66, %60, %53, %46, %45, %42, %13
%77 = load i32, i32* %3
ret i32 %77
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@errno = global i64 0
@EXDEV = global i64 0
@ENOSYS = global i64 0
@EPERM = global i64 0
@EEXIST = global i64 0
define i32 @safe_rename(i8* %0, i8* %1) {
%3 = alloca %struct.stat
%4 = alloca %struct.stat
%5 = bitcast %struct.stat* %3 to i8*
%6 = bitcast %struct.stat* %4 to i8*
%7 = icmp ne i8* %0, null
%8 = icmp ne i8* %1, null
%9 = and i1 %7, %8
br i1 %9, label %10, label %40
10: ; preds = %2
%11 = tail call i64 @link(i8* %0, i8* %1)
%12 = icmp eq i64 %11, 0
br i1 %12, label %27, label %13
13: ; preds = %10
%14 = load i64, i64* @errno
%15 = load i64, i64* @EXDEV
%16 = icmp eq i64 %14, %15
%17 = load i64, i64* @ENOSYS
%18 = icmp eq i64 %14, %17
%19 = select i1 %16, i1 true, i1 %18
%20 = load i64, i64* @EPERM
%21 = icmp eq i64 %14, %20
%22 = select i1 %19, i1 true, i1 %21
br i1 %22, label %23, label %40
23: ; preds = %13
%24 = tail call i32 @rename(i8* %0, i8* %1)
%25 = icmp eq i32 %24, -1
%26 = sext i1 %25 to i32
br label %40
27: ; preds = %10
%28 = call i32 @lstat(i8* %0, %struct.stat* %3)
%29 = icmp eq i32 %28, -1
br i1 %29, label %40, label %30
30: ; preds = %27
%31 = call i32 @lstat(i8* %1, %struct.stat* %4)
%32 = icmp eq i32 %31, -1
br i1 %32, label %40, label %33
33: ; preds = %30
%34 = call i32 @compare_stat(%struct.stat* %3, %struct.stat* %4)
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %38
36: ; preds = %33
%37 = load i64, i64* @EEXIST
store i64 %37, i64* @errno
br label %40
38: ; preds = %33
%39 = call i32 @unlink(i8* %0)
br label %40
40: ; preds = %30, %27, %13, %23, %2, %38, %36
%41 = phi i32 [ -1, %36 ], [ 0, %38 ], [ -1, %2 ], [ %26, %23 ], [ -1, %13 ], [ -1, %27 ], [ -1, %30 ]
ret i32 %41
}