#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_DIRECTORY;
int O_EXCL;
int O_RDONLY;
int O_WRONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fsync(int);
int open(char *, int, ...);
int /*<<< orphan*/ rename(char *, char *);
int /*<<< orphan*/ unlink(char *);
int main(int ac, char **av) {
  int fd = open("test.one", O_CREAT | O_EXCL | O_WRONLY, 0666);
  close(fd);
  int dfd = open(".", O_RDONLY | O_DIRECTORY);
  for (int i = 0; i < 100; i++) {
    rename("test.one", "test.two");
    fsync(dfd);
    rename("test.two", "test.one");
    fsync(dfd);
  }
  unlink("test.one");
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e8b(%rip),%rax
mov    (%rax),%esi
mov    0x2e52(%rip),%rax
or     (%rax),%esi
mov    0x2e39(%rip),%rax
or     (%rax),%esi
lea    0xe78(%rip),%rdi
mov    $0x1b6,%edx
mov    $0x0,%al
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
mov    0x2e3a(%rip),%rax
mov    (%rax),%esi
mov    0x2e21(%rip),%rax
or     (%rax),%esi
lea    0xe51(%rip),%rdi
mov    $0x0,%al
call   1060 <open@plt>
mov    %eax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
cmpl   $0x64,-0x1c(%rbp)
jge    1217 <main+0xc7>
lea    0xe26(%rip),%rdi
lea    0xe2a(%rip),%rsi
call   1070 <rename@plt>
mov    -0x18(%rbp),%edi
call   1050 <fsync@plt>
lea    0xe16(%rip),%rdi
lea    0xe04(%rip),%rsi
call   1070 <rename@plt>
mov    -0x18(%rbp),%edi
call   1050 <fsync@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11c9 <main+0x79>
lea    0xde2(%rip),%rdi
call   1030 <unlink@plt>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    0x2e9a(%rip),%rax
mov    0x2e63(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e48(%rip),%rax
or     (%rax),%esi
lea    0xe87(%rip),%r14
mov    %r14,%rdi
mov    $0x1b6,%edx
xor    %eax,%eax
call   1060 <open@plt>
mov    %eax,%edi
call   1040 <close@plt>
mov    0x2e4a(%rip),%rax
mov    0x2e33(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe61(%rip),%rdi
xor    %eax,%eax
call   1060 <open@plt>
mov    %eax,%ebp
mov    $0x64,%ebx
lea    0xe4e(%rip),%r15
nopl   (%rax)
mov    %r14,%rdi
mov    %r15,%rsi
call   1070 <rename@plt>
mov    %ebp,%edi
call   1050 <fsync@plt>
mov    %r15,%rdi
mov    %r14,%rsi
call   1070 <rename@plt>
mov    %ebp,%edi
call   1050 <fsync@plt>
add    $0xffffffff,%ebx
jne    11c0 <main+0x70>
lea    0xe10(%rip),%rdi
call   1030 <unlink@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [9 x i8] c"test.one\00"
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@O_WRONLY = global i32 0
@.str.1 = [2 x i8] c".\00"
@O_RDONLY = global i32 0
@O_DIRECTORY = global i32 0
@.str.2 = [9 x i8] c"test.two\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* @O_CREAT
%10 = load i32, i32* @O_EXCL
%11 = or i32 %9, %10
%12 = load i32, i32* @O_WRONLY
%13 = or i32 %11, %12
%14 = call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %13, i32 438)
store i32 %14, i32* %6
%15 = load i32, i32* %6
%16 = call i32 @close(i32 %15)
%17 = load i32, i32* @O_RDONLY
%18 = load i32, i32* @O_DIRECTORY
%19 = or i32 %17, %18
%20 = call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 %19)
store i32 %20, i32* %7
store i32 0, i32* %8
br label %21
21: ; preds = %31, %2
%22 = load i32, i32* %8
%23 = icmp slt i32 %22, 100
br i1 %23, label %24, label %34
24: ; preds = %21
%25 = call i32 @rename(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
%26 = load i32, i32* %7
%27 = call i32 @fsync(i32 %26)
%28 = call i32 @rename(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
%29 = load i32, i32* %7
%30 = call i32 @fsync(i32 %29)
br label %31
31: ; preds = %24
%32 = load i32, i32* %8
%33 = add nsw i32 %32, 1
store i32 %33, i32* %8
br label %21
34: ; preds = %21
%35 = call i32 @unlink(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
%36 = load i32, i32* %3
ret i32 %36
}

LLVM IR (-O2):
@.str = [9 x i8] c"test.one\00"
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@O_WRONLY = global i32 0
@.str.1 = [2 x i8] c".\00"
@O_RDONLY = global i32 0
@O_DIRECTORY = global i32 0
@.str.2 = [9 x i8] c"test.two\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = load i32, i32* @O_CREAT
%4 = load i32, i32* @O_EXCL
%5 = or i32 %4, %3
%6 = load i32, i32* @O_WRONLY
%7 = or i32 %5, %6
%8 = tail call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %7, i32 438)
%9 = tail call i32 @close(i32 %8)
%10 = load i32, i32* @O_RDONLY
%11 = load i32, i32* @O_DIRECTORY
%12 = or i32 %11, %10
%13 = tail call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 %12)
br label %16
14: ; preds = %16
%15 = tail call i32 @unlink(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
ret i32 0
16: ; preds = %2, %16
%17 = phi i32 [ 0, %2 ], [ %22, %16 ]
%18 = tail call i32 @rename(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
%19 = tail call i32 @fsync(i32 %13)
%20 = tail call i32 @rename(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
%21 = tail call i32 @fsync(i32 %13)
%22 = add nuw nsw i32 %17, 1
%23 = icmp eq i32 %22, 100
br i1 %23, label %14, label %16
}