#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 /*<<< orphan*/ O_DIRECTORY;
int S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ assert(int);
int close(int);
int creat(char *, int);
int fsync(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, int);
int rename(char *, char *);
int write(int, char *, int);
int main() {
  int fd = creat("file.new", S_IRUSR | S_IWUSR);
  printf("creat -> %d \n", fd);
  assert(fd >= 0);
  char buf[255];
  int err = write(fd, buf, 255);
  printf("write -> %d \n", err);
  assert(err == 255);
  err = fsync(fd);
  printf("fsync -> %d \n", err);
  assert(err == 0);
  err = close(fd);
  printf("close -> %d \n", err);
  assert(err == 0);
  int dirfd = open(".", O_DIRECTORY);
  printf("open on dir -> %d \n", dirfd);
  assert(dirfd >= 0);
  err = fsync(dirfd);
  printf("fsync on dir -> %d \n", err);
  assert(err == 0);
  err = close(dirfd);
  printf("close on dir -> %d \n", err);
  assert(err == 0);
  err = rename("file.new", "file");
  printf("rename -> %d \n", err);
  assert(err == 0);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e37(%rip),%rax
mov    (%rax),%esi
mov    0x2e46(%rip),%rax
or     (%rax),%esi
lea    0xe55(%rip),%rdi
call   10a0 <creat@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%esi
lea    0xe4c(%rip),%rdi
call   1040 <printf@plt>
cmpl   $0x0,-0x8(%rbp)
setge  %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    -0x8(%rbp),%edi
lea    -0x110(%rbp),%rsi
mov    $0xff,%edx
call   1030 <write@plt>
mov    %eax,-0x114(%rbp)
mov    -0x114(%rbp),%esi
lea    0xe1d(%rip),%rdi
call   1040 <printf@plt>
cmpl   $0xff,-0x114(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    -0x8(%rbp),%edi
call   1070 <fsync@plt>
mov    %eax,-0x114(%rbp)
mov    -0x114(%rbp),%esi
lea    0xdf4(%rip),%rdi
call   1040 <printf@plt>
cmpl   $0x0,-0x114(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
mov    %eax,-0x114(%rbp)
mov    -0x114(%rbp),%esi
lea    0xdce(%rip),%rdi
call   1040 <printf@plt>
cmpl   $0x0,-0x114(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    0x2d53(%rip),%rax
mov    (%rax),%esi
lea    0xdb3(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x118(%rbp)
mov    -0x118(%rbp),%esi
lea    0xd9d(%rip),%rdi
call   1040 <printf@plt>
cmpl   $0x0,-0x118(%rbp)
setge  %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    -0x118(%rbp),%edi
call   1070 <fsync@plt>
mov    %eax,-0x114(%rbp)
mov    -0x114(%rbp),%esi
lea    0xd7a(%rip),%rdi
call   1040 <printf@plt>
cmpl   $0x0,-0x114(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    -0x118(%rbp),%edi
call   1050 <close@plt>
mov    %eax,-0x114(%rbp)
mov    -0x114(%rbp),%esi
lea    0xd58(%rip),%rdi
call   1040 <printf@plt>
cmpl   $0x0,-0x114(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
lea    0xccc(%rip),%rdi
lea    0xd46(%rip),%rsi
call   1090 <rename@plt>
mov    %eax,-0x114(%rbp)
mov    -0x114(%rbp),%esi
lea    0xd33(%rip),%rdi
call   1040 <printf@plt>
cmpl   $0x0,-0x114(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
xor    %eax,%eax
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x100,%rsp
mov    0x2e3e(%rip),%rax
mov    0x2e4f(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe5c(%rip),%r14
mov    %r14,%rdi
call   10a0 <creat@plt>
mov    %eax,%ebp
lea    0xe54(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
mov    %ebp,%edi
not    %edi
shr    $0x1f,%edi
call   1060 <assert@plt>
mov    %rsp,%rsi
mov    %ebp,%edi
mov    $0xff,%edx
call   1030 <write@plt>
mov    %eax,%ebx
lea    0xe37(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
xor    %edi,%edi
cmp    $0xff,%ebx
sete   %dil
call   1060 <assert@plt>
mov    %ebp,%edi
call   1070 <fsync@plt>
mov    %eax,%ebx
lea    0xe1d(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
xor    %edi,%edi
test   %ebx,%ebx
sete   %dil
call   1060 <assert@plt>
mov    %ebp,%edi
call   1050 <close@plt>
mov    %eax,%ebx
lea    0xe07(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
xor    %edi,%edi
test   %ebx,%ebx
sete   %dil
call   1060 <assert@plt>
mov    0x2d91(%rip),%rax
mov    (%rax),%esi
lea    0xdf1(%rip),%rdi
call   1080 <open@plt>
mov    %eax,%ebp
lea    0xde5(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
mov    %ebp,%edi
not    %edi
shr    $0x1f,%edi
call   1060 <assert@plt>
mov    %ebp,%edi
call   1070 <fsync@plt>
mov    %eax,%ebx
lea    0xdd6(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
xor    %edi,%edi
test   %ebx,%ebx
sete   %dil
call   1060 <assert@plt>
mov    %ebp,%edi
call   1050 <close@plt>
mov    %eax,%ebx
lea    0xdc7(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
xor    %edi,%edi
test   %ebx,%ebx
sete   %dil
call   1060 <assert@plt>
lea    0xdc1(%rip),%rsi
mov    %r14,%rdi
call   1090 <rename@plt>
mov    %eax,%ebx
lea    0xdb5(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
xor    %edi,%edi
test   %ebx,%ebx
sete   %dil
call   1060 <assert@plt>
xor    %eax,%eax
add    $0x100,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [9 x i8] c"file.new\00"
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str.1 = [14 x i8] c"creat -> %d \0A\00"
@.str.2 = [14 x i8] c"write -> %d \0A\00"
@.str.3 = [14 x i8] c"fsync -> %d \0A\00"
@.str.4 = [14 x i8] c"close -> %d \0A\00"
@.str.5 = [2 x i8] c".\00"
@O_DIRECTORY = global i32 0
@.str.6 = [20 x i8] c"open on dir -> %d \0A\00"
@.str.7 = [21 x i8] c"fsync on dir -> %d \0A\00"
@.str.8 = [21 x i8] c"close on dir -> %d \0A\00"
@.str.9 = [5 x i8] c"file\00"
@.str.10 = [15 x i8] c"rename -> %d \0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [255 x i8]
%4 = alloca i32
%5 = alloca i32
store i32 0, i32* %1
%6 = load i32, i32* @S_IRUSR
%7 = load i32, i32* @S_IWUSR
%8 = or i32 %6, %7
%9 = call i32 @creat(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %8)
store i32 %9, i32* %2
%10 = load i32, i32* %2
%11 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 %10)
%12 = load i32, i32* %2
%13 = icmp sge i32 %12, 0
%14 = zext i1 %13 to i32
%15 = call i32 @assert(i32 %14)
%16 = load i32, i32* %2
%17 = getelementptr inbounds [255 x i8], [255 x i8]* %3, i64 0, i64 0
%18 = call i32 @write(i32 %16, i8* %17, i32 255)
store i32 %18, i32* %4
%19 = load i32, i32* %4
%20 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 %19)
%21 = load i32, i32* %4
%22 = icmp eq i32 %21, 255
%23 = zext i1 %22 to i32
%24 = call i32 @assert(i32 %23)
%25 = load i32, i32* %2
%26 = call i32 @fsync(i32 %25)
store i32 %26, i32* %4
%27 = load i32, i32* %4
%28 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i32 %27)
%29 = load i32, i32* %4
%30 = icmp eq i32 %29, 0
%31 = zext i1 %30 to i32
%32 = call i32 @assert(i32 %31)
%33 = load i32, i32* %2
%34 = call i32 @close(i32 %33)
store i32 %34, i32* %4
%35 = load i32, i32* %4
%36 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0), i32 %35)
%37 = load i32, i32* %4
%38 = icmp eq i32 %37, 0
%39 = zext i1 %38 to i32
%40 = call i32 @assert(i32 %39)
%41 = load i32, i32* @O_DIRECTORY
%42 = call i32 @open(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0), i32 %41)
store i32 %42, i32* %5
%43 = load i32, i32* %5
%44 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.6, i64 0, i64 0), i32 %43)
%45 = load i32, i32* %5
%46 = icmp sge i32 %45, 0
%47 = zext i1 %46 to i32
%48 = call i32 @assert(i32 %47)
%49 = load i32, i32* %5
%50 = call i32 @fsync(i32 %49)
store i32 %50, i32* %4
%51 = load i32, i32* %4
%52 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.7, i64 0, i64 0), i32 %51)
%53 = load i32, i32* %4
%54 = icmp eq i32 %53, 0
%55 = zext i1 %54 to i32
%56 = call i32 @assert(i32 %55)
%57 = load i32, i32* %5
%58 = call i32 @close(i32 %57)
store i32 %58, i32* %4
%59 = load i32, i32* %4
%60 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.8, i64 0, i64 0), i32 %59)
%61 = load i32, i32* %4
%62 = icmp eq i32 %61, 0
%63 = zext i1 %62 to i32
%64 = call i32 @assert(i32 %63)
%65 = call i32 @rename(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.9, i64 0, i64 0))
store i32 %65, i32* %4
%66 = load i32, i32* %4
%67 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.10, i64 0, i64 0), i32 %66)
%68 = load i32, i32* %4
%69 = icmp eq i32 %68, 0
%70 = zext i1 %69 to i32
%71 = call i32 @assert(i32 %70)
ret i32 0
}

LLVM IR (-O2):
@.str = [9 x i8] c"file.new\00"
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str.1 = [14 x i8] c"creat -> %d \0A\00"
@.str.2 = [14 x i8] c"write -> %d \0A\00"
@.str.3 = [14 x i8] c"fsync -> %d \0A\00"
@.str.4 = [14 x i8] c"close -> %d \0A\00"
@.str.5 = [2 x i8] c".\00"
@O_DIRECTORY = global i32 0
@.str.6 = [20 x i8] c"open on dir -> %d \0A\00"
@.str.7 = [21 x i8] c"fsync on dir -> %d \0A\00"
@.str.8 = [21 x i8] c"close on dir -> %d \0A\00"
@.str.9 = [5 x i8] c"file\00"
@.str.10 = [15 x i8] c"rename -> %d \0A\00"
define i32 @main() {
%1 = alloca [255 x i8]
%2 = load i32, i32* @S_IRUSR
%3 = load i32, i32* @S_IWUSR
%4 = or i32 %3, %2
%5 = tail call i32 @creat(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %4)
%6 = tail call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 %5)
%7 = xor i32 %5, -1
%8 = lshr i32 %7, 31
%9 = tail call i32 @assert(i32 %8)
%10 = getelementptr inbounds [255 x i8], [255 x i8]* %1, i64 0, i64 0
%11 = call i32 @write(i32 %5, i8* %10, i32 255)
%12 = tail call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 %11)
%13 = icmp eq i32 %11, 255
%14 = zext i1 %13 to i32
%15 = tail call i32 @assert(i32 %14)
%16 = tail call i32 @fsync(i32 %5)
%17 = tail call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i32 %16)
%18 = icmp eq i32 %16, 0
%19 = zext i1 %18 to i32
%20 = tail call i32 @assert(i32 %19)
%21 = tail call i32 @close(i32 %5)
%22 = tail call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0), i32 %21)
%23 = icmp eq i32 %21, 0
%24 = zext i1 %23 to i32
%25 = tail call i32 @assert(i32 %24)
%26 = load i32, i32* @O_DIRECTORY
%27 = tail call i32 @open(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0), i32 %26)
%28 = tail call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.6, i64 0, i64 0), i32 %27)
%29 = xor i32 %27, -1
%30 = lshr i32 %29, 31
%31 = tail call i32 @assert(i32 %30)
%32 = tail call i32 @fsync(i32 %27)
%33 = tail call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.7, i64 0, i64 0), i32 %32)
%34 = icmp eq i32 %32, 0
%35 = zext i1 %34 to i32
%36 = tail call i32 @assert(i32 %35)
%37 = tail call i32 @close(i32 %27)
%38 = tail call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.8, i64 0, i64 0), i32 %37)
%39 = icmp eq i32 %37, 0
%40 = zext i1 %39 to i32
%41 = tail call i32 @assert(i32 %40)
%42 = tail call i32 @rename(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.9, i64 0, i64 0))
%43 = tail call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.10, i64 0, i64 0), i32 %42)
%44 = icmp eq i32 %42, 0
%45 = zext i1 %44 to i32
%46 = tail call i32 @assert(i32 %45)
ret i32 0
}