#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_RDONLY;
int O_RDWR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
scalar_t__ fdatasync(int);
scalar_t__ fsync(int);
int open(char *, int, ...);
int /*<<< orphan*/ perror(char *);
scalar_t__ rename(char *, char *);
size_t strlen(char *);
size_t write(int, char *, size_t);
int main(int ac, char **av) {
  char *msg = "hello\n";
  int fd = open("temp", O_CREAT | O_RDWR, 0666);
  if (fd < 0) {
    perror("open temp");
    exit(-1);
  }
  size_t cc = strlen(msg);
  if (write(fd, msg, cc) != cc) {
    perror("write");
    exit(-1);
  }
  if (fdatasync(fd) < 0) {
    perror("fdatasync");
    exit(-1);
  }
  close(fd);
  if (rename("temp", "dst") < 0) {
    perror("rename");
    exit(-1);
  }
  fd = open(".", O_DIRECTORY | O_RDONLY);
  if (fd < 0) {
    perror("open dir");
    exit(-1);
  }
  if (fsync(fd) < 0) {
    perror("fsync");
    exit(-1);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe5a(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    0x2e47(%rip),%rax
mov    (%rax),%esi
mov    0x2e26(%rip),%rax
or     (%rax),%esi
lea    0xe49(%rip),%rdi
mov    $0x1b6,%edx
mov    $0x0,%al
call   1080 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11f2 <main+0x62>
lea    0xe24(%rip),%rdi
call   1090 <perror@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x1c(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1040 <write@plt>
cmp    -0x28(%rbp),%rax
je     122f <main+0x9f>
lea    0xdf1(%rip),%rdi
call   1090 <perror@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>
mov    -0x1c(%rbp),%edi
call   1030 <fdatasync@plt>
cmp    $0x0,%rax
jge    1257 <main+0xc7>
lea    0xdcf(%rip),%rdi
call   1090 <perror@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
lea    0xda6(%rip),%rdi
lea    0xdb4(%rip),%rsi
call   10a0 <rename@plt>
cmp    $0x0,%rax
jge    1292 <main+0x102>
lea    0xda2(%rip),%rdi
call   1090 <perror@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>
mov    0x2d2f(%rip),%rax
mov    (%rax),%esi
mov    0x2d36(%rip),%rax
or     (%rax),%esi
lea    0xd81(%rip),%rdi
mov    $0x0,%al
call   1080 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    12d5 <main+0x145>
lea    0xd68(%rip),%rdi
call   1090 <perror@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>
mov    -0x1c(%rbp),%edi
call   1070 <fsync@plt>
cmp    $0x0,%rax
jge    12fd <main+0x16d>
lea    0xd49(%rip),%rdi
call   1090 <perror@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
mov    0x2e70(%rip),%rax
mov    0x2e51(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe72(%rip),%rdi
mov    $0x1b6,%edx
xor    %eax,%eax
call   1070 <open@plt>
test   %eax,%eax
js     1224 <main+0xa4>
mov    %eax,%ebx
lea    0xe4d(%rip),%rsi
mov    $0x6,%edx
mov    %eax,%edi
call   1040 <write@plt>
cmp    $0x6,%rax
jne    122d <main+0xad>
mov    %ebx,%edi
call   1030 <fdatasync@plt>
test   %rax,%rax
js     1236 <main+0xb6>
mov    %ebx,%edi
call   1050 <close@plt>
lea    0xe2d(%rip),%rdi
lea    0xe3b(%rip),%rsi
call   1090 <rename@plt>
test   %rax,%rax
js     123f <main+0xbf>
mov    0x2dd1(%rip),%rax
mov    0x2dda(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe23(%rip),%rdi
xor    %eax,%eax
call   1070 <open@plt>
test   %eax,%eax
js     1248 <main+0xc8>
mov    %eax,%edi
call   1060 <fsync@plt>
test   %rax,%rax
js     1251 <main+0xd1>
xor    %eax,%eax
pop    %rbx
ret
lea    0xddc(%rip),%rdi
jmp    1258 <main+0xd8>
lea    0xddd(%rip),%rdi
jmp    1258 <main+0xd8>
lea    0xdda(%rip),%rdi
jmp    1258 <main+0xd8>
lea    0xddf(%rip),%rdi
jmp    1258 <main+0xd8>
lea    0xddf(%rip),%rdi
jmp    1258 <main+0xd8>
lea    0xddf(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [7 x i8] c"hello\0A\00"
@.str.1 = [5 x i8] c"temp\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@.str.2 = [10 x i8] c"open temp\00"
@.str.3 = [6 x i8] c"write\00"
@.str.4 = [10 x i8] c"fdatasync\00"
@.str.5 = [4 x i8] c"dst\00"
@.str.6 = [7 x i8] c"rename\00"
@.str.7 = [2 x i8] c".\00"
@O_DIRECTORY = global i32 0
@O_RDONLY = global i32 0
@.str.8 = [9 x i8] c"open dir\00"
@.str.9 = [6 x i8] c"fsync\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i8**
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i64
store i32 %0, i32* %3
store i8** %1, i8*** %4
store i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8** %5
%8 = load i32, i32* @O_CREAT
%9 = load i32, i32* @O_RDWR
%10 = or i32 %8, %9
%11 = call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 %10, i32 438)
store i32 %11, i32* %6
%12 = load i32, i32* %6
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %2
%15 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
%16 = call i32 @exit(i32 -1)
unreachable
17: ; preds = %2
%18 = load i8*, i8** %5
%19 = call i64 @strlen(i8* %18)
store i64 %19, i64* %7
%20 = load i32, i32* %6
%21 = load i8*, i8** %5
%22 = load i64, i64* %7
%23 = call i64 @write(i32 %20, i8* %21, i64 %22)
%24 = load i64, i64* %7
%25 = icmp ne i64 %23, %24
br i1 %25, label %26, label %29
26: ; preds = %17
%27 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%28 = call i32 @exit(i32 -1)
unreachable
29: ; preds = %17
%30 = load i32, i32* %6
%31 = call i64 @fdatasync(i32 %30)
%32 = icmp slt i64 %31, 0
br i1 %32, label %33, label %36
33: ; preds = %29
%34 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0))
%35 = call i32 @exit(i32 -1)
unreachable
36: ; preds = %29
%37 = load i32, i32* %6
%38 = call i32 @close(i32 %37)
%39 = call i64 @rename(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0))
%40 = icmp slt i64 %39, 0
br i1 %40, label %41, label %44
41: ; preds = %36
%42 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0))
%43 = call i32 @exit(i32 -1)
unreachable
44: ; preds = %36
%45 = load i32, i32* @O_DIRECTORY
%46 = load i32, i32* @O_RDONLY
%47 = or i32 %45, %46
%48 = call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.7, i64 0, i64 0), i32 %47)
store i32 %48, i32* %6
%49 = load i32, i32* %6
%50 = icmp slt i32 %49, 0
br i1 %50, label %51, label %54
51: ; preds = %44
%52 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.8, i64 0, i64 0))
%53 = call i32 @exit(i32 -1)
unreachable
54: ; preds = %44
%55 = load i32, i32* %6
%56 = call i64 @fsync(i32 %55)
%57 = icmp slt i64 %56, 0
br i1 %57, label %58, label %61
58: ; preds = %54
%59 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.9, i64 0, i64 0))
%60 = call i32 @exit(i32 -1)
unreachable
61: ; preds = %54
ret i32 0
}

LLVM IR (-O2):
@.str = [7 x i8] c"hello\0A\00"
@.str.1 = [5 x i8] c"temp\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@.str.2 = [10 x i8] c"open temp\00"
@.str.3 = [6 x i8] c"write\00"
@.str.4 = [10 x i8] c"fdatasync\00"
@.str.5 = [4 x i8] c"dst\00"
@.str.6 = [7 x i8] c"rename\00"
@.str.7 = [2 x i8] c".\00"
@O_DIRECTORY = global i32 0
@O_RDONLY = global i32 0
@.str.8 = [9 x i8] c"open dir\00"
@.str.9 = [6 x i8] c"fsync\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = load i32, i32* @O_CREAT
%4 = load i32, i32* @O_RDWR
%5 = or i32 %4, %3
%6 = tail call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 %5, i32 438)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %11
8: ; preds = %2
%9 = tail call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
%10 = tail call i32 @exit(i32 -1)
unreachable
11: ; preds = %2
%12 = tail call i64 @write(i32 %6, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i64 6)
%13 = icmp eq i64 %12, 6
br i1 %13, label %17, label %14
14: ; preds = %11
%15 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%16 = tail call i32 @exit(i32 -1)
unreachable
17: ; preds = %11
%18 = tail call i64 @fdatasync(i32 %6)
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %17
%21 = tail call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0))
%22 = tail call i32 @exit(i32 -1)
unreachable
23: ; preds = %17
%24 = tail call i32 @close(i32 %6)
%25 = tail call i64 @rename(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0))
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %23
%28 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0))
%29 = tail call i32 @exit(i32 -1)
unreachable
30: ; preds = %23
%31 = load i32, i32* @O_DIRECTORY
%32 = load i32, i32* @O_RDONLY
%33 = or i32 %32, %31
%34 = tail call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.7, i64 0, i64 0), i32 %33)
%35 = icmp slt i32 %34, 0
br i1 %35, label %36, label %39
36: ; preds = %30
%37 = tail call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.8, i64 0, i64 0))
%38 = tail call i32 @exit(i32 -1)
unreachable
39: ; preds = %30
%40 = tail call i64 @fsync(i32 %34)
%41 = icmp slt i64 %40, 0
br i1 %41, label %42, label %45
42: ; preds = %39
%43 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.9, i64 0, i64 0))
%44 = tail call i32 @exit(i32 -1)
unreachable
45: ; preds = %39
ret i32 0
}