#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 EACCES;
int EPERM;
int O_CREAT;
int O_EXCL;
int /*<<< orphan*/ close(int);
int errno;
scalar_t__ link(char const *, char const *);
int open(char const *, int, int);
int rename(char const *, char const *);
int /*<<< orphan*/ unlink(char const *);
int o_rename(const char *from, const char *to) {
  int h, saved_errno;
  if (link(from, to) == 0) {
    unlink(from);
    return 0;
  }
  if (errno != EPERM && errno != EACCES)
    return -1;
  if ((h = open(to, O_CREAT | O_EXCL, 0666)) == -1)
    return -1;
  close(h);
  if (rename(from, to) == -1) {
    saved_errno = errno;
    unlink(to);
    errno = saved_errno;
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<o_rename>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1070 <link@plt>
cmp    $0x0,%rax
jne    118c <o_rename+0x3c>
mov    -0x10(%rbp),%rdi
call   1030 <unlink@plt>
movl   $0x0,-0x4(%rbp)
jmp    1252 <o_rename+0x102>
mov    0x2e35(%rip),%rax
mov    (%rax),%eax
mov    0x2e1c(%rip),%rcx
cmp    (%rcx),%eax
je     11c8 <o_rename+0x78>
mov    0x2e1d(%rip),%rax
mov    (%rax),%eax
mov    0x2e2c(%rip),%rcx
cmp    (%rcx),%eax
je     11c8 <o_rename+0x78>
movl   $0xffffffff,-0x4(%rbp)
jmp    1252 <o_rename+0x102>
mov    -0x18(%rbp),%rdi
mov    0x2e25(%rip),%rax
mov    (%rax),%esi
mov    0x2df4(%rip),%rax
or     (%rax),%esi
mov    $0x1b6,%edx
call   1050 <open@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0xffffffff,%eax
jne    1200 <o_rename+0xb0>
movl   $0xffffffff,-0x4(%rbp)
jmp    1252 <o_rename+0x102>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1060 <rename@plt>
cmp    $0xffffffff,%eax
jne    124b <o_rename+0xfb>
mov    0x2da3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <unlink@plt>
mov    -0x20(%rbp),%ecx
mov    0x2d8b(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1252 <o_rename+0x102>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<o_rename>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
call   1070 <link@plt>
test   %rax,%rax
je     11de <o_rename+0x8e>
mov    0x2e5a(%rip),%r15
mov    (%r15),%eax
mov    0x2e40(%rip),%rcx
mov    $0xffffffff,%ebp
cmp    (%rcx),%eax
je     118c <o_rename+0x3c>
mov    0x2e58(%rip),%rcx
cmp    (%rcx),%eax
jne    11e8 <o_rename+0x98>
mov    0x2e65(%rip),%rax
mov    0x2e36(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %r14,%rdi
mov    $0x1b6,%edx
call   1050 <open@plt>
cmp    $0xffffffff,%eax
je     11e8 <o_rename+0x98>
mov    %eax,%edi
call   1040 <close@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1060 <rename@plt>
xor    %ebp,%ebp
cmp    $0xffffffff,%eax
jne    11e8 <o_rename+0x98>
mov    (%r15),%ebx
mov    %r14,%rdi
call   1030 <unlink@plt>
mov    %ebx,(%r15)
mov    $0xffffffff,%ebp
jmp    11e8 <o_rename+0x98>
mov    %rbx,%rdi
call   1030 <unlink@plt>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@errno = global i32 0
@EPERM = global i32 0
@EACCES = global i32 0
@O_CREAT = global i32 0
@O_EXCL = global i32 0
define i32 @o_rename(i8* %0, i8* %1) {
%3 = tail call i64 @link(i8* %0, i8* %1)
%4 = icmp eq i64 %3, 0
br i1 %4, label %5, label %7
5: ; preds = %2
%6 = tail call i32 @unlink(i8* %0)
br label %27
7: ; preds = %2
%8 = load i32, i32* @errno
%9 = load i32, i32* @EPERM
%10 = icmp eq i32 %8, %9
%11 = load i32, i32* @EACCES
%12 = icmp eq i32 %8, %11
%13 = select i1 %10, i1 true, i1 %12
br i1 %13, label %14, label %27
14: ; preds = %7
%15 = load i32, i32* @O_CREAT
%16 = load i32, i32* @O_EXCL
%17 = or i32 %16, %15
%18 = tail call i32 @open(i8* %1, i32 %17, i32 438)
%19 = icmp eq i32 %18, -1
br i1 %19, label %27, label %20
20: ; preds = %14
%21 = tail call i32 @close(i32 %18)
%22 = tail call i32 @rename(i8* %0, i8* %1)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %27
24: ; preds = %20
%25 = load i32, i32* @errno
%26 = tail call i32 @unlink(i8* %1)
store i32 %25, i32* @errno
br label %27
27: ; preds = %20, %14, %7, %24, %5
%28 = phi i32 [ 0, %5 ], [ -1, %24 ], [ -1, %7 ], [ -1, %14 ], [ 0, %20 ]
ret i32 %28
}