#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 O_CREAT;
int O_EXCL;
int /*<<< orphan*/ close(int);
int errno;
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 ((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    -0x18(%rbp),%rdi
mov    0x2e9d(%rip),%rax
mov    (%rax),%esi
mov    0x2e74(%rip),%rax
or     (%rax),%esi
mov    $0x1b6,%edx
call   1050 <open@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0xffffffff,%eax
jne    1188 <o_rename+0x48>
movl   $0xffffffff,-0x4(%rbp)
jmp    11da <o_rename+0x9a>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1060 <rename@plt>
cmp    $0xffffffff,%eax
jne    11d3 <o_rename+0x93>
mov    0x2e23(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <unlink@plt>
mov    -0x20(%rbp),%ecx
mov    0x2e0b(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    11da <o_rename+0x9a>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<o_rename>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%rbx
mov    %rdi,%r14
mov    0x2ea7(%rip),%rax
mov    0x2e80(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rbx,%rdi
mov    $0x1b6,%edx
call   1050 <open@plt>
mov    %eax,%edi
mov    $0xffffffff,%eax
cmp    $0xffffffff,%edi
je     11a8 <o_rename+0x68>
call   1040 <close@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1060 <rename@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0xffffffff,%ecx
jne    11a8 <o_rename+0x68>
mov    0x2e3b(%rip),%r14
mov    (%r14),%ebp
mov    %rbx,%rdi
call   1030 <unlink@plt>
mov    %ebp,(%r14)
mov    $0xffffffff,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@errno = global i32 0
define i32 @o_rename(i8* nocapture %0, i8* nocapture %1) {
%3 = load i32, i32* @O_CREAT
%4 = load i32, i32* @O_EXCL
%5 = or i32 %4, %3
%6 = tail call i32 @open(i8* %1, i32 %5, i32 438)
%7 = icmp eq i32 %6, -1
br i1 %7, label %15, label %8
8: ; preds = %2
%9 = tail call i32 @close(i32 %6)
%10 = tail call i32 @rename(i8* %0, i8* %1)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %15
12: ; preds = %8
%13 = load i32, i32* @errno
%14 = tail call i32 @unlink(i8* %1)
store i32 %13, i32* @errno
br label %15
15: ; preds = %8, %2, %12
%16 = phi i32 [ -1, %12 ], [ -1, %2 ], [ 0, %8 ]
ret i32 %16
}