#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;
scalar_t__ link(char const *, char const *);
int unlink(char const *);
int rename_file(const char *from, const char *to) {
  if (link(from, to) < 0) {
    if (errno != EEXIST) {
      return -1;
    }
    if ((unlink(to) < 0) || (link(from, to) < 0)) {
      return -1;
    }
  }
  return (unlink(from));
}


x86 assembly (clang, -O0):
<rename_file>:
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   1040 <link@plt>
cmp    $0x0,%rax
jge    11a7 <rename_file+0x87>
mov    0x2e8a(%rip),%rax
mov    (%rax),%rax
mov    0x2e88(%rip),%rcx
cmp    (%rcx),%rax
je     116d <rename_file+0x4d>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b3 <rename_file+0x93>
mov    -0x18(%rbp),%rdi
call   1030 <unlink@plt>
cmp    $0x0,%eax
jl     1196 <rename_file+0x76>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <link@plt>
cmp    $0x0,%rax
jge    11a2 <rename_file+0x82>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b3 <rename_file+0x93>
jmp    11a7 <rename_file+0x87>
mov    -0x10(%rbp),%rdi
call   1030 <unlink@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rename_file>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
call   1040 <link@plt>
test   %rax,%rax
js     1143 <rename_file+0x23>
mov    %rbx,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1030 <unlink@plt>
mov    0x2e8e(%rip),%rax
mov    (%rax),%rax
mov    0x2e8c(%rip),%rcx
cmp    (%rcx),%rax
jne    1175 <rename_file+0x55>
mov    %r14,%rdi
call   1030 <unlink@plt>
test   %eax,%eax
js     1175 <rename_file+0x55>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1040 <link@plt>
test   %rax,%rax
jns    1134 <rename_file+0x14>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@errno = global i64 0
@EEXIST = global i64 0
define i32 @rename_file(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 = load i8*, i8** %5
%8 = call i64 @link(i8* %6, i8* %7)
%9 = icmp slt i64 %8, 0
br i1 %9, label %10, label %26
10: ; preds = %2
%11 = load i64, i64* @errno
%12 = load i64, i64* @EEXIST
%13 = icmp ne i64 %11, %12
br i1 %13, label %14, label %15
14: ; preds = %10
store i32 -1, i32* %3
br label %29
15: ; preds = %10
%16 = load i8*, i8** %5
%17 = call i32 @unlink(i8* %16)
%18 = icmp slt i32 %17, 0
br i1 %18, label %24, label %19
19: ; preds = %15
%20 = load i8*, i8** %4
%21 = load i8*, i8** %5
%22 = call i64 @link(i8* %20, i8* %21)
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %19, %15
store i32 -1, i32* %3
br label %29
25: ; preds = %19
br label %26
26: ; preds = %25, %2
%27 = load i8*, i8** %4
%28 = call i32 @unlink(i8* %27)
store i32 %28, i32* %3
br label %29
29: ; preds = %26, %24, %14
%30 = load i32, i32* %3
ret i32 %30
}

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