#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 open(char const *, int, int);
int rename(char const *, char const *);
int rename_noreplace(int olddirfd, const char *oldpath, int newdirfd,
                     const char *newpath) {
  int ret;
  ret = open(newpath, O_EXCL | O_CREAT, 0700);
  if (ret < 0)
    return ret;
  close(ret);
  return rename(oldpath, newpath);
}


x86 assembly (clang, -O0):
<rename_noreplace>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    0x2e87(%rip),%rax
mov    (%rax),%esi
mov    0x2e9e(%rip),%rax
or     (%rax),%esi
mov    $0x1c0,%edx
call   1040 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    117e <rename_noreplace+0x4e>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1196 <rename_noreplace+0x66>
mov    -0x24(%rbp),%edi
call   1030 <close@plt>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1050 <rename@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rename_noreplace>:
push   %r14
push   %rbx
push   %rax
mov    %rcx,%rbx
mov    %rsi,%r14
mov    0x2e97(%rip),%rax
mov    0x2eb0(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rbx,%rdi
mov    $0x1c0,%edx
call   1040 <open@plt>
test   %eax,%eax
js     1176 <rename_noreplace+0x46>
mov    %eax,%edi
call   1030 <close@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1050 <rename@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

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