#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_RDWR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ error(char *);
int /*<<< orphan*/ exit(int);
int fsync(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ sleep(int);
int write(int, char *, int);
int main(void) {
  int fd;
  int i;
  fd = open("test.txt", O_RDWR);
  if (fd == -1) {
    error("fail to open");
    exit(1);
  }
  i = 0;
  while (i < 3) {
    sleep(5);
    printf("hello\n");
    if (write(fd, "hello\n", 6) == -1) {
      perror("fail to write");
      exit(1);
    }
    if (fsync(fd) == -1) {
      perror("fail to fsync");
      exit(1);
    }
    i++;
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e42(%rip),%rax
mov    (%rax),%esi
lea    0xe51(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    11d7 <main+0x47>
lea    0xe41(%rip),%rdi
call   1060 <error@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
movl   $0x0,-0xc(%rbp)
cmpl   $0x3,-0xc(%rbp)
jge    1266 <main+0xd6>
mov    $0x5,%edi
call   10b0 <sleep@plt>
lea    0xe1d(%rip),%rdi
call   1040 <printf@plt>
mov    -0x8(%rbp),%edi
lea    0xe0e(%rip),%rsi
mov    $0x6,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    1231 <main+0xa1>
lea    0xdfb(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x8(%rbp),%edi
call   1070 <fsync@plt>
cmp    $0xffffffff,%eax
jne    1258 <main+0xc8>
lea    0xde2(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    11de <main+0x4e>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
mov    0x2e50(%rip),%rax
mov    (%rax),%esi
lea    0xe5f(%rip),%rdi
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     1291 <main+0x101>
mov    %eax,%ebx
mov    $0x5,%edi
call   10b0 <sleep@plt>
lea    0xe77(%rip),%rdi
call   1030 <puts@plt>
lea    0xe48(%rip),%rsi
mov    %ebx,%edi
mov    $0x6,%edx
call   1040 <write@plt>
cmp    $0xffffffff,%eax
je     1272 <main+0xe2>
mov    %ebx,%edi
call   1070 <fsync@plt>
cmp    $0xffffffff,%eax
je     127b <main+0xeb>
mov    $0x5,%edi
call   10b0 <sleep@plt>
lea    0xe35(%rip),%rdi
call   1030 <puts@plt>
lea    0xe06(%rip),%rsi
mov    %ebx,%edi
mov    $0x6,%edx
call   1040 <write@plt>
cmp    $0xffffffff,%eax
je     1272 <main+0xe2>
mov    %ebx,%edi
call   1070 <fsync@plt>
cmp    $0xffffffff,%eax
je     127b <main+0xeb>
mov    $0x5,%edi
call   10b0 <sleep@plt>
lea    0xdfb(%rip),%rdi
call   1030 <puts@plt>
lea    0xdcc(%rip),%rsi
mov    %ebx,%edi
mov    $0x6,%edx
call   1040 <write@plt>
cmp    $0xffffffff,%eax
je     1272 <main+0xe2>
mov    %ebx,%edi
call   1070 <fsync@plt>
cmp    $0xffffffff,%eax
je     127b <main+0xeb>
mov    %ebx,%edi
call   1050 <close@plt>
xor    %eax,%eax
pop    %rbx
ret
lea    0xda4(%rip),%rdi
jmp    1282 <main+0xf2>
lea    0xda9(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
lea    0xd71(%rip),%rdi
call   1060 <error@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [9 x i8] c"test.txt\00"
@O_RDWR = global i32 0
@.str.1 = [13 x i8] c"fail to open\00"
@.str.2 = [7 x i8] c"hello\0A\00"
@.str.3 = [14 x i8] c"fail to write\00"
@.str.4 = [14 x i8] c"fail to fsync\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
store i32 0, i32* %1
%4 = load i32, i32* @O_RDWR
%5 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %4)
store i32 %5, i32* %2
%6 = load i32, i32* %2
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %11
8: ; preds = %0
%9 = call i32 @error(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%10 = call i32 @exit(i32 1)
unreachable
11: ; preds = %0
store i32 0, i32* %3
br label %12
12: ; preds = %31, %11
%13 = load i32, i32* %3
%14 = icmp slt i32 %13, 3
br i1 %14, label %15, label %34
15: ; preds = %12
%16 = call i32 @sleep(i32 5)
%17 = call i32 @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%18 = load i32, i32* %2
%19 = call i32 @write(i32 %18, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32 6)
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %24
21: ; preds = %15
%22 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
%23 = call i32 @exit(i32 1)
unreachable
24: ; preds = %15
%25 = load i32, i32* %2
%26 = call i32 @fsync(i32 %25)
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %31
28: ; preds = %24
%29 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
%30 = call i32 @exit(i32 1)
unreachable
31: ; preds = %24
%32 = load i32, i32* %3
%33 = add nsw i32 %32, 1
store i32 %33, i32* %3
br label %12
34: ; preds = %12
%35 = load i32, i32* %2
%36 = call i32 @close(i32 %35)
ret i32 0
}

LLVM IR (-O2):
@.str = [9 x i8] c"test.txt\00"
@O_RDWR = global i32 0
@.str.1 = [13 x i8] c"fail to open\00"
@.str.2 = [7 x i8] c"hello\0A\00"
@.str.3 = [14 x i8] c"fail to write\00"
@.str.4 = [14 x i8] c"fail to fsync\00"
@str = [6 x i8] c"hello\00"
define i32 @main() {
%1 = load i32, i32* @O_RDWR
%2 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %1)
%3 = icmp eq i32 %2, -1
br i1 %3, label %9, label %4
4: ; preds = %0
%5 = tail call i32 @sleep(i32 5)
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @str, i64 0, i64 0))
%7 = tail call i32 @write(i32 %2, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32 6)
%8 = icmp eq i32 %7, -1
br i1 %8, label %30, label %33
9: ; preds = %0
%10 = tail call i32 @error(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%11 = tail call i32 @exit(i32 1)
unreachable
12: ; preds = %33
%13 = tail call i32 @sleep(i32 5)
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @str, i64 0, i64 0))
%15 = tail call i32 @write(i32 %2, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32 6)
%16 = icmp eq i32 %15, -1
br i1 %16, label %30, label %17
17: ; preds = %12
%18 = tail call i32 @fsync(i32 %2)
%19 = icmp eq i32 %18, -1
br i1 %19, label %36, label %20
20: ; preds = %17
%21 = tail call i32 @sleep(i32 5)
%22 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @str, i64 0, i64 0))
%23 = tail call i32 @write(i32 %2, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32 6)
%24 = icmp eq i32 %23, -1
br i1 %24, label %30, label %25
25: ; preds = %20
%26 = tail call i32 @fsync(i32 %2)
%27 = icmp eq i32 %26, -1
br i1 %27, label %36, label %28
28: ; preds = %25
%29 = tail call i32 @close(i32 %2)
ret i32 0
30: ; preds = %20, %12, %4
%31 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
%32 = tail call i32 @exit(i32 1)
unreachable
33: ; preds = %4
%34 = tail call i32 @fsync(i32 %2)
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %12
36: ; preds = %25, %17, %33
%37 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
%38 = tail call i32 @exit(i32 1)
unreachable
}