#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_RDWR;
int O_TRUNC;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fsync(int);
int /*<<< orphan*/ lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
int open(char *, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ read(int, void *, int);
int /*<<< orphan*/ write(int, void *, int);
int main() {
  char my_write_str[] = "1234567890";
  char my_read_str[100];
  char my_filename[] = "/mnt/reflex/flush_test.txt";
  int my_file_descriptor, close_err;
  my_file_descriptor = open(my_filename, O_RDWR | O_CREAT | O_TRUNC);
  write(my_file_descriptor, (void *)my_write_str, 10);
  printf("Do fsync()\n");
  fsync(my_file_descriptor);
  printf("fsync done\n");
  lseek(my_file_descriptor, 0, SEEK_SET);
  read(my_file_descriptor, (void *)my_read_str, 10);
  my_read_str[10] = '\0';
  printf("String read = %s.\n", my_read_str);
  close(my_file_descriptor);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
movl   $0x0,-0x4(%rbp)
mov    0xe77(%rip),%rax
mov    %rax,-0xf(%rbp)
mov    0xe74(%rip),%ax
mov    %ax,-0x7(%rbp)
mov    0xe6c(%rip),%al
mov    %al,-0x5(%rbp)
mov    0xe98(%rip),%rax
mov    %rax,-0xa0(%rbp)
mov    0xe92(%rip),%rax
mov    %rax,-0x98(%rbp)
mov    0xe8c(%rip),%rax
mov    %rax,-0x90(%rbp)
mov    0xe86(%rip),%ax
mov    %ax,-0x88(%rbp)
mov    0xe7b(%rip),%al
mov    %al,-0x86(%rbp)
lea    -0xa0(%rbp),%rdi
mov    0x2de5(%rip),%rax
mov    (%rax),%esi
mov    0x2dfc(%rip),%rax
or     (%rax),%esi
mov    0x2deb(%rip),%rax
or     (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0xa4(%rbp)
mov    -0xa4(%rbp),%edi
lea    -0xf(%rbp),%rsi
mov    $0xa,%edx
call   1030 <write@plt>
lea    0xdde(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0xa4(%rbp),%edi
call   1080 <fsync@plt>
lea    0xdd1(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0xa4(%rbp),%edi
mov    0x2d6e(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1050 <lseek@plt>
mov    -0xa4(%rbp),%edi
lea    -0x80(%rbp),%rsi
mov    $0xa,%edx
call   1070 <read@plt>
movb   $0x0,-0x76(%rbp)
lea    -0x80(%rbp),%rsi
lea    0xd9d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0xa4(%rbp),%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0xb0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x98,%rsp
movabs $0x3837363534333231,%rax
36 37 38
mov    %rax,(%rsp)
movl   $0x303938,0x7(%rsp)
movups 0xea4(%rip),%xmm0
movups %xmm0,0x1b(%rsp)
movaps 0xe8d(%rip),%xmm0
mov    0x2e1e(%rip),%rax
mov    0x2e37(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
movaps %xmm0,0x10(%rsp)
mov    0x2e1f(%rip),%rax
or     (%rax),%esi
lea    0x10(%rsp),%rdi
call   10a0 <open@plt>
mov    %eax,%ebx
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0xa,%edx
call   1040 <write@plt>
lea    0xe29(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%edi
call   1090 <fsync@plt>
lea    0xe21(%rip),%rdi
call   1030 <puts@plt>
mov    0x2db4(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
xor    %esi,%esi
call   1060 <lseek@plt>
lea    0x30(%rsp),%r14
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0xa,%edx
call   1080 <read@plt>
movb   $0x0,0x3a(%rsp)
lea    0xdcc(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebx,%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0x98,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@__const.main.my_write_str = [11 x i8] c"1234567890\00"
@__const.main.my_filename = [27 x i8] c"/mnt/reflex/flush_test.txt\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@.str = [12 x i8] c"Do fsync()\0A\00"
@.str.1 = [12 x i8] c"fsync done\0A\00"
@SEEK_SET = global i32 0
@.str.2 = [19 x i8] c"String read = %s.\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [11 x i8]
%3 = alloca [100 x i8]
%4 = alloca [27 x i8]
%5 = alloca i32
%6 = alloca i32
store i32 0, i32* %1
%7 = bitcast [11 x i8]* %2 to i8*
%8 = bitcast [27 x i8]* %4 to i8*
%9 = getelementptr inbounds [27 x i8], [27 x i8]* %4, i64 0, i64 0
%10 = load i32, i32* @O_RDWR
%11 = load i32, i32* @O_CREAT
%12 = or i32 %10, %11
%13 = load i32, i32* @O_TRUNC
%14 = or i32 %12, %13
%15 = call i32 @open(i8* %9, i32 %14)
store i32 %15, i32* %5
%16 = load i32, i32* %5
%17 = getelementptr inbounds [11 x i8], [11 x i8]* %2, i64 0, i64 0
%18 = call i32 @write(i32 %16, i8* %17, i32 10)
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%20 = load i32, i32* %5
%21 = call i32 @fsync(i32 %20)
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%23 = load i32, i32* %5
%24 = load i32, i32* @SEEK_SET
%25 = call i32 @lseek(i32 %23, i32 0, i32 %24)
%26 = load i32, i32* %5
%27 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%28 = call i32 @read(i32 %26, i8* %27, i32 10)
%29 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 10
store i8 0, i8* %29
%30 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i8* %30)
%32 = load i32, i32* %5
%33 = call i32 @close(i32 %32)
ret i32 0
}

LLVM IR (-O2):
@__const.main.my_write_str = [11 x i8] c"1234567890\00"
@__const.main.my_filename = [27 x i8] c"/mnt/reflex/flush_test.txt\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@SEEK_SET = global i32 0
@.str.2 = [19 x i8] c"String read = %s.\0A\00"
@str = [11 x i8] c"Do fsync()\00"
@str.3 = [11 x i8] c"fsync done\00"
define i32 @main() {
%1 = alloca [11 x i8]
%2 = alloca [100 x i8]
%3 = alloca [27 x i8]
%4 = getelementptr inbounds [11 x i8], [11 x i8]* %1, i64 0, i64 0
%5 = getelementptr inbounds [100 x i8], [100 x i8]* %2, i64 0, i64 0
%6 = getelementptr inbounds [27 x i8], [27 x i8]* %3, i64 0, i64 0
%7 = load i32, i32* @O_RDWR
%8 = load i32, i32* @O_CREAT
%9 = or i32 %8, %7
%10 = load i32, i32* @O_TRUNC
%11 = or i32 %9, %10
%12 = call i32 @open(i8* %6, i32 %11)
%13 = call i32 @write(i32 %12, i8* %4, i32 10)
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str, i64 0, i64 0))
%15 = tail call i32 @fsync(i32 %12)
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str.3, i64 0, i64 0))
%17 = load i32, i32* @SEEK_SET
%18 = tail call i32 @lseek(i32 %12, i32 0, i32 %17)
%19 = call i32 @read(i32 %12, i8* %5, i32 10)
%20 = getelementptr inbounds [100 x i8], [100 x i8]* %2, i64 0, i64 10
store i8 0, i8* %20
%21 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i8* %5)
%22 = call i32 @close(i32 %12)
ret i32 0
}