#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 access(char const *, int /*<<< orphan*/);
int link(char const *, char const *);
int unlink(char const *);
int rename(const char *a, const char *b) {
  if (access(a, 0) == -1)
    return (-1);
  unlink(b);
  if (link(a, b) == -1)
    return (-1);
  if (unlink(a) == -1) {
    unlink(b);
    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    -0x10(%rbp),%rdi
xor    %esi,%esi
call   1040 <access@plt>
cmp    $0xffffffff,%eax
jne    1160 <rename+0x30>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b9 <rename+0x89>
mov    -0x18(%rbp),%rdi
call   1030 <unlink@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1050 <link@plt>
cmp    $0xffffffff,%eax
jne    118b <rename+0x5b>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b9 <rename+0x89>
mov    -0x10(%rbp),%rdi
call   1030 <unlink@plt>
cmp    $0xffffffff,%eax
jne    11b2 <rename+0x82>
mov    -0x18(%rbp),%rdi
call   1030 <unlink@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b9 <rename+0x89>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rename>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%rbx
xor    %esi,%esi
call   1040 <access@plt>
mov    $0xffffffff,%ebp
cmp    $0xffffffff,%eax
je     117f <rename+0x4f>
mov    %r14,%rdi
call   1030 <unlink@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1050 <link@plt>
cmp    $0xffffffff,%eax
je     117f <rename+0x4f>
mov    %rbx,%rdi
call   1030 <unlink@plt>
xor    %ebp,%ebp
cmp    $0xffffffff,%eax
jne    117f <rename+0x4f>
mov    %r14,%rdi
call   1030 <unlink@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i32 @rename(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
%6 = load i8*, i8** %4
%7 = call i32 @access(i8* %6, i32 0)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %10
9: ; preds = %2
store i32 -1, i32* %3
br label %26
10: ; preds = %2
%11 = load i8*, i8** %5
%12 = call i32 @unlink(i8* %11)
%13 = load i8*, i8** %4
%14 = load i8*, i8** %5
%15 = call i32 @link(i8* %13, i8* %14)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %18
17: ; preds = %10
store i32 -1, i32* %3
br label %26
18: ; preds = %10
%19 = load i8*, i8** %4
%20 = call i32 @unlink(i8* %19)
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %25
22: ; preds = %18
%23 = load i8*, i8** %5
%24 = call i32 @unlink(i8* %23)
store i32 -1, i32* %3
br label %26
25: ; preds = %18
store i32 0, i32* %3
br label %26
26: ; preds = %25, %22, %17, %9
%27 = load i32, i32* %3
ret i32 %27
}

LLVM IR (-O2):
define i32 @rename(i8* %0, i8* %1) {
%3 = tail call i32 @access(i8* %0, i32 0)
%4 = icmp eq i32 %3, -1
br i1 %4, label %14, label %5
5: ; preds = %2
%6 = tail call i32 @unlink(i8* %1)
%7 = tail call i32 @link(i8* %0, i8* %1)
%8 = icmp eq i32 %7, -1
br i1 %8, label %14, label %9
9: ; preds = %5
%10 = tail call i32 @unlink(i8* %0)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %14
12: ; preds = %9
%13 = tail call i32 @unlink(i8* %1)
br label %14
14: ; preds = %9, %5, %2, %12
%15 = phi i32 [ -1, %12 ], [ -1, %2 ], [ -1, %5 ], [ 0, %9 ]
ret i32 %15
}