#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__ _unlink(char const *);
scalar_t__ errno;
int rename(char const *, char const *);
int sys_rename(const char *from, const char *to) {
  int retval = rename(from, to);
  if ((retval < 0) && (errno == EEXIST)) {
    if (_unlink(to) == 0)
      retval = rename(from, to);
  }
  return retval;
}


x86 assembly (clang, -O0):
<sys_rename>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <rename@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    118c <sys_rename+0x6c>
mov    0x2e87(%rip),%rax
mov    (%rax),%rax
mov    0x2e85(%rip),%rcx
cmp    (%rcx),%rax
jne    118c <sys_rename+0x6c>
mov    -0x10(%rbp),%rdi
call   1030 <_unlink@plt>
cmp    $0x0,%rax
jne    1187 <sys_rename+0x67>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <rename@plt>
mov    %eax,-0x14(%rbp)
jmp    118c <sys_rename+0x6c>
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sys_rename>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%rbx
mov    %rdi,%r14
call   1040 <rename@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    1158 <sys_rename+0x38>
mov    0x2e9c(%rip),%rax
mov    (%rax),%rax
mov    0x2e9a(%rip),%rcx
cmp    (%rcx),%rax
jne    1158 <sys_rename+0x38>
mov    %rbx,%rdi
call   1030 <_unlink@plt>
test   %rax,%rax
je     115f <sys_rename+0x3f>
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    %r14,%rdi
mov    %rbx,%rsi
pop    %rbx
pop    %r14
pop    %rbp
jmp    1040 <rename@plt>

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

LLVM IR (-O2):
@errno = global i64 0
@EEXIST = global i64 0
define i32 @sys_rename(i8* nocapture %0, i8* %1) {
%3 = tail call i32 @rename(i8* %0, i8* %1)
%4 = icmp slt i32 %3, 0
br i1 %4, label %5, label %14
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 %14
9: ; preds = %5
%10 = tail call i64 @_unlink(i8* %1)
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %9
%13 = tail call i32 @rename(i8* %0, i8* %1)
br label %14
14: ; preds = %9, %12, %5, %2
%15 = phi i32 [ %13, %12 ], [ %3, %9 ], [ %3, %5 ], [ %3, %2 ]
ret i32 %15
}