#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 EEXIST;
scalar_t__ __link(char const *, char const *);
int /*<<< orphan*/ __set_errno(int);
scalar_t__ __unlink(char const *);
int errno;
int rename(const char *old, const char *new) {
  int save = errno;
  if (__link(old, new) < 0) {
    if (errno == EEXIST) {
      __set_errno(save);
      if (__unlink(new) < 0 || __link(old, new) < 0)
        return -1;
    } else
      return -1;
  }
  if (__unlink(old) < 0) {
    save = errno;
    if (__unlink(new) == 0)
      __set_errno(save);
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<rename>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2e91(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <__link@plt>
cmp    $0x0,%rax
jge    11cf <rename+0x9f>
mov    0x2e6e(%rip),%rax
mov    (%rax),%eax
mov    0x2e6d(%rip),%rcx
cmp    (%rcx),%eax
jne    11be <rename+0x8e>
mov    -0x1c(%rbp),%edi
call   1030 <__set_errno@plt>
mov    -0x18(%rbp),%rdi
call   1050 <__unlink@plt>
cmp    $0x0,%rax
jl     11ad <rename+0x7d>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <__link@plt>
cmp    $0x0,%rax
jge    11b9 <rename+0x89>
movl   $0xffffffff,-0x4(%rbp)
jmp    121c <rename+0xec>
jmp    11ca <rename+0x9a>
movl   $0xffffffff,-0x4(%rbp)
jmp    121c <rename+0xec>
jmp    11cf <rename+0x9f>
mov    -0x10(%rbp),%rdi
call   1050 <__unlink@plt>
cmp    $0x0,%rax
jge    1215 <rename+0xe5>
mov    0x2def(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <__unlink@plt>
cmp    $0x0,%rax
jne    1209 <rename+0xd9>
mov    -0x1c(%rbp),%edi
call   1030 <__set_errno@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    121c <rename+0xec>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rename>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%rbx
mov    %rdi,%rbp
mov    0x2e93(%rip),%r12
mov    (%r12),%r15d
call   1040 <__link@plt>
test   %rax,%rax
js     1183 <rename+0x53>
mov    %rbp,%rdi
call   1050 <__unlink@plt>
xor    %r14d,%r14d
test   %rax,%rax
jns    11bd <rename+0x8d>
mov    (%r12),%ebp
mov    %rbx,%rdi
call   1050 <__unlink@plt>
mov    $0xffffffff,%r14d
test   %rax,%rax
jne    11bd <rename+0x8d>
mov    %ebp,%edi
call   1030 <__set_errno@plt>
jmp    11bd <rename+0x8d>
mov    (%r12),%eax
mov    0x2e52(%rip),%rcx
mov    $0xffffffff,%r14d
cmp    (%rcx),%eax
jne    11bd <rename+0x8d>
mov    %r15d,%edi
call   1030 <__set_errno@plt>
mov    %rbx,%rdi
call   1050 <__unlink@plt>
test   %rax,%rax
js     11bd <rename+0x8d>
mov    %rbp,%rdi
mov    %rbx,%rsi
call   1040 <__link@plt>
test   %rax,%rax
jns    1153 <rename+0x23>
mov    %r14d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i32 0
@EEXIST = global i32 0
define i32 @rename(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
%7 = load i32, i32* @errno
store i32 %7, i32* %6
%8 = load i8*, i8** %4
%9 = load i8*, i8** %5
%10 = call i64 @__link(i8* %8, i8* %9)
%11 = icmp slt i64 %10, 0
br i1 %11, label %12, label %31
12: ; preds = %2
%13 = load i32, i32* @errno
%14 = load i32, i32* @EEXIST
%15 = icmp eq i32 %13, %14
br i1 %15, label %16, label %29
16: ; preds = %12
%17 = load i32, i32* %6
%18 = call i32 @__set_errno(i32 %17)
%19 = load i8*, i8** %5
%20 = call i64 @__unlink(i8* %19)
%21 = icmp slt i64 %20, 0
br i1 %21, label %27, label %22
22: ; preds = %16
%23 = load i8*, i8** %4
%24 = load i8*, i8** %5
%25 = call i64 @__link(i8* %23, i8* %24)
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %28
27: ; preds = %22, %16
store i32 -1, i32* %3
br label %45
28: ; preds = %22
br label %30
29: ; preds = %12
store i32 -1, i32* %3
br label %45
30: ; preds = %28
br label %31
31: ; preds = %30, %2
%32 = load i8*, i8** %4
%33 = call i64 @__unlink(i8* %32)
%34 = icmp slt i64 %33, 0
br i1 %34, label %35, label %44
35: ; preds = %31
%36 = load i32, i32* @errno
store i32 %36, i32* %6
%37 = load i8*, i8** %5
%38 = call i64 @__unlink(i8* %37)
%39 = icmp eq i64 %38, 0
br i1 %39, label %40, label %43
40: ; preds = %35
%41 = load i32, i32* %6
%42 = call i32 @__set_errno(i32 %41)
br label %43
43: ; preds = %40, %35
store i32 -1, i32* %3
br label %45
44: ; preds = %31
store i32 0, i32* %3
br label %45
45: ; preds = %44, %43, %29, %27
%46 = load i32, i32* %3
ret i32 %46
}

LLVM IR (-O2):
@errno = global i32 0
@EEXIST = global i32 0
define i32 @rename(i8* %0, i8* %1) {
%3 = load i32, i32* @errno
%4 = tail call i64 @__link(i8* %0, i8* %1)
%5 = icmp slt i64 %4, 0
br i1 %5, label %6, label %17
6: ; preds = %2
%7 = load i32, i32* @errno
%8 = load i32, i32* @EEXIST
%9 = icmp eq i32 %7, %8
br i1 %9, label %10, label %26
10: ; preds = %6
%11 = tail call i32 @__set_errno(i32 %3)
%12 = tail call i64 @__unlink(i8* %1)
%13 = icmp slt i64 %12, 0
br i1 %13, label %26, label %14
14: ; preds = %10
%15 = tail call i64 @__link(i8* %0, i8* %1)
%16 = icmp slt i64 %15, 0
br i1 %16, label %26, label %17
17: ; preds = %14, %2
%18 = tail call i64 @__unlink(i8* %0)
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %26
20: ; preds = %17
%21 = load i32, i32* @errno
%22 = tail call i64 @__unlink(i8* %1)
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %20
%25 = tail call i32 @__set_errno(i32 %21)
br label %26
26: ; preds = %17, %20, %24, %6, %10, %14
%27 = phi i32 [ -1, %14 ], [ -1, %10 ], [ -1, %6 ], [ -1, %24 ], [ -1, %20 ], [ 0, %17 ]
ret i32 %27
}