#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 /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fsync(int);
int /*<<< orphan*/ lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ malloc(int);
int /*<<< orphan*/ memset(char *, int, int);
int open(char *, int);
int /*<<< orphan*/ sprintf(char *, char *, int);
int /*<<< orphan*/ usleep(int);
int /*<<< orphan*/ write(int, char *, int);
void withSync() {
  int idx = 0;
  int g = 0;
  int f[9];
  for (idx = 0; idx <= 9; idx++) {
    char filename[20];
    sprintf(filename, "f_%d.txt", idx);
    if (idx == 0) {
      g = open(filename, O_RDWR | O_CREAT);
      lseek(g, 0, SEEK_END);
      int records = 1;
      int i = 0;
      char *buf = NULL;
      buf = (char *)malloc(1024 * 64);
      memset(buf, 0xcafe, 1024 * 64);
      for (i = 0; i < records; i++) {
        write(g, buf, 1 * 1024);
      }
    } else {
      f[idx] = open(filename, O_RDWR | O_CREAT);
      lseek(f[idx], 0, SEEK_END);
      int records = 1;
      int i = 0;
      char *buf = NULL;
      buf = (char *)malloc(1024 * 64);
      memset(buf, 0xcafe, 1024 * 64);
      for (i = 0; i < records; i++) {
        write(f[idx], buf, 1 * 1024);
      }
      usleep(1000);
      if (idx == 9) {
        fsync(f[idx]);
      }
      close(f[idx]);
    }
  }
  close(g);
}


x86 assembly (clang, -O0):
<withSync>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
movl   $0x0,-0x4(%rbp)
cmpl   $0x9,-0x4(%rbp)
jg     135f <withSync+0x1cf>
lea    -0x50(%rbp),%rdi
mov    -0x4(%rbp),%edx
lea    0xe3b(%rip),%rsi
call   10a0 <sprintf@plt>
cmpl   $0x0,-0x4(%rbp)
jne    1273 <withSync+0xe3>
lea    -0x50(%rbp),%rdi
mov    0x2e01(%rip),%rax
mov    (%rax),%esi
mov    0x2e10(%rip),%rax
or     (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2ddc(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1040 <lseek@plt>
movl   $0x1,-0x54(%rbp)
movl   $0x0,-0x58(%rbp)
movq   $0x0,-0x60(%rbp)
mov    $0x10000,%edi
call   1070 <malloc@plt>
mov    %rax,-0x60(%rbp)
mov    -0x60(%rbp),%rdi
mov    $0xcafe,%esi
mov    $0x10000,%edx
call   1050 <memset@plt>
movl   $0x0,-0x58(%rbp)
mov    -0x58(%rbp),%eax
cmp    -0x54(%rbp),%eax
jge    126e <withSync+0xde>
mov    -0x8(%rbp),%edi
mov    -0x60(%rbp),%rsi
mov    $0x400,%edx
call   1030 <write@plt>
mov    -0x58(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x58(%rbp)
jmp    1243 <withSync+0xb3>
jmp    134c <withSync+0x1bc>
lea    -0x50(%rbp),%rdi
mov    0x2d62(%rip),%rax
mov    (%rax),%esi
mov    0x2d71(%rip),%rax
or     (%rax),%esi
call   1090 <open@plt>
mov    %eax,%ecx
movslq -0x4(%rbp),%rax
mov    %ecx,-0x30(%rbp,%rax,4)
movslq -0x4(%rbp),%rax
mov    -0x30(%rbp,%rax,4),%edi
mov    0x2d31(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1040 <lseek@plt>
movl   $0x1,-0x64(%rbp)
movl   $0x0,-0x68(%rbp)
movq   $0x0,-0x70(%rbp)
mov    $0x10000,%edi
call   1070 <malloc@plt>
mov    %rax,-0x70(%rbp)
mov    -0x70(%rbp),%rdi
mov    $0xcafe,%esi
mov    $0x10000,%edx
call   1050 <memset@plt>
movl   $0x0,-0x68(%rbp)
mov    -0x68(%rbp),%eax
cmp    -0x64(%rbp),%eax
jge    131e <withSync+0x18e>
movslq -0x4(%rbp),%rax
mov    -0x30(%rbp,%rax,4),%edi
mov    -0x70(%rbp),%rsi
mov    $0x400,%edx
call   1030 <write@plt>
mov    -0x68(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x68(%rbp)
jmp    12ee <withSync+0x15e>
mov    $0x3e8,%edi
call   10b0 <usleep@plt>
cmpl   $0x9,-0x4(%rbp)
jne    133f <withSync+0x1af>
movslq -0x4(%rbp),%rax
mov    -0x30(%rbp,%rax,4),%edi
call   1080 <fsync@plt>
movslq -0x4(%rbp),%rax
mov    -0x30(%rbp,%rax,4),%edi
call   1060 <close@plt>
jmp    1351 <withSync+0x1c1>
mov    -0x4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4(%rbp)
jmp    11ad <withSync+0x1d>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<withSync>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
lea    0xe5b(%rip),%rsi
lea    0x10(%rsp),%r12
mov    %r12,%rdi
xor    %edx,%edx
call   10a0 <sprintf@plt>
mov    0x2e25(%rip),%rax
mov    0x2e36(%rip),%r14
mov    (%r14),%esi
or     (%rax),%esi
mov    %r12,%rdi
call   1090 <open@plt>
mov    %eax,%ebp
mov    0x2e00(%rip),%r15
mov    (%r15),%edx
mov    %eax,%edi
xor    %esi,%esi
call   1040 <lseek@plt>
mov    $0x10000,%edi
call   1070 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    $0xcafe,%esi
mov    $0x10000,%edx
call   1050 <memset@plt>
mov    %ebp,0xc(%rsp)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x400,%edx
call   1030 <write@plt>
mov    $0x1,%r13d
nopl   0x0(%rax)
mov    %r12,%rdi
lea    0xdd6(%rip),%rsi
mov    %r13d,%edx
call   10a0 <sprintf@plt>
mov    (%r14),%esi
mov    0x2da4(%rip),%rax
or     (%rax),%esi
mov    %r12,%rdi
call   1090 <open@plt>
mov    %eax,%ebx
mov    (%r15),%edx
mov    %eax,%edi
xor    %esi,%esi
call   1040 <lseek@plt>
mov    $0x10000,%edi
call   1070 <malloc@plt>
mov    %rax,%rbp
mov    %rax,%rdi
mov    $0xcafe,%esi
mov    $0x10000,%edx
call   1050 <memset@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x400,%edx
call   1030 <write@plt>
mov    $0x3e8,%edi
call   10b0 <usleep@plt>
cmp    $0x9,%r13d
je     129f <withSync+0x10f>
mov    %ebx,%edi
call   1060 <close@plt>
add    $0x1,%r13d
jmp    1220 <withSync+0x90>
mov    %ebx,%edi
call   1080 <fsync@plt>
mov    %ebx,%edi
call   1060 <close@plt>
mov    0xc(%rsp),%edi
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1060 <close@plt>

LLVM IR (-O0):
@.str = [9 x i8] c"f_%d.txt\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@SEEK_END = global i32 0
define void @withSync() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [9 x i32]
%4 = alloca [20 x i8]
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
store i32 0, i32* %1
store i32 0, i32* %2
store i32 0, i32* %1
br label %11
11: ; preds = %95, %0
%12 = load i32, i32* %1
%13 = icmp sle i32 %12, 9
br i1 %13, label %14, label %98
14: ; preds = %11
%15 = getelementptr inbounds [20 x i8], [20 x i8]* %4, i64 0, i64 0
%16 = load i32, i32* %1
%17 = call i32 @sprintf(i8* %15, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %16)
%18 = load i32, i32* %1
%19 = icmp eq i32 %18, 0
br i1 %19, label %20, label %45
20: ; preds = %14
%21 = getelementptr inbounds [20 x i8], [20 x i8]* %4, i64 0, i64 0
%22 = load i32, i32* @O_RDWR
%23 = load i32, i32* @O_CREAT
%24 = or i32 %22, %23
%25 = call i32 @open(i8* %21, i32 %24)
store i32 %25, i32* %2
%26 = load i32, i32* %2
%27 = load i32, i32* @SEEK_END
%28 = call i32 @lseek(i32 %26, i32 0, i32 %27)
store i32 1, i32* %5
store i32 0, i32* %6
store i8* null, i8** %7
%29 = call i64 @malloc(i32 65536)
%30 = inttoptr i64 %29 to i8*
store i8* %30, i8** %7
%31 = load i8*, i8** %7
%32 = call i32 @memset(i8* %31, i32 51966, i32 65536)
store i32 0, i32* %6
br label %33
33: ; preds = %41, %20
%34 = load i32, i32* %6
%35 = load i32, i32* %5
%36 = icmp slt i32 %34, %35
br i1 %36, label %37, label %44
37: ; preds = %33
%38 = load i32, i32* %2
%39 = load i8*, i8** %7
%40 = call i32 @write(i32 %38, i8* %39, i32 1024)
br label %41
41: ; preds = %37
%42 = load i32, i32* %6
%43 = add nsw i32 %42, 1
store i32 %43, i32* %6
br label %33
44: ; preds = %33
br label %94
45: ; preds = %14
%46 = getelementptr inbounds [20 x i8], [20 x i8]* %4, i64 0, i64 0
%47 = load i32, i32* @O_RDWR
%48 = load i32, i32* @O_CREAT
%49 = or i32 %47, %48
%50 = call i32 @open(i8* %46, i32 %49)
%51 = load i32, i32* %1
%52 = sext i32 %51 to i64
%53 = getelementptr inbounds [9 x i32], [9 x i32]* %3, i64 0, i64 %52
store i32 %50, i32* %53
%54 = load i32, i32* %1
%55 = sext i32 %54 to i64
%56 = getelementptr inbounds [9 x i32], [9 x i32]* %3, i64 0, i64 %55
%57 = load i32, i32* %56
%58 = load i32, i32* @SEEK_END
%59 = call i32 @lseek(i32 %57, i32 0, i32 %58)
store i32 1, i32* %8
store i32 0, i32* %9
store i8* null, i8** %10
%60 = call i64 @malloc(i32 65536)
%61 = inttoptr i64 %60 to i8*
store i8* %61, i8** %10
%62 = load i8*, i8** %10
%63 = call i32 @memset(i8* %62, i32 51966, i32 65536)
store i32 0, i32* %9
br label %64
64: ; preds = %75, %45
%65 = load i32, i32* %9
%66 = load i32, i32* %8
%67 = icmp slt i32 %65, %66
br i1 %67, label %68, label %78
68: ; preds = %64
%69 = load i32, i32* %1
%70 = sext i32 %69 to i64
%71 = getelementptr inbounds [9 x i32], [9 x i32]* %3, i64 0, i64 %70
%72 = load i32, i32* %71
%73 = load i8*, i8** %10
%74 = call i32 @write(i32 %72, i8* %73, i32 1024)
br label %75
75: ; preds = %68
%76 = load i32, i32* %9
%77 = add nsw i32 %76, 1
store i32 %77, i32* %9
br label %64
78: ; preds = %64
%79 = call i32 @usleep(i32 1000)
%80 = load i32, i32* %1
%81 = icmp eq i32 %80, 9
br i1 %81, label %82, label %88
82: ; preds = %78
%83 = load i32, i32* %1
%84 = sext i32 %83 to i64
%85 = getelementptr inbounds [9 x i32], [9 x i32]* %3, i64 0, i64 %84
%86 = load i32, i32* %85
%87 = call i32 @fsync(i32 %86)
br label %88
88: ; preds = %82, %78
%89 = load i32, i32* %1
%90 = sext i32 %89 to i64
%91 = getelementptr inbounds [9 x i32], [9 x i32]* %3, i64 0, i64 %90
%92 = load i32, i32* %91
%93 = call i32 @close(i32 %92)
br label %94
94: ; preds = %88, %44
br label %95
95: ; preds = %94
%96 = load i32, i32* %1
%97 = add nsw i32 %96, 1
store i32 %97, i32* %1
br label %11
98: ; preds = %11
%99 = load i32, i32* %2
%100 = call i32 @close(i32 %99)
ret void
}

LLVM IR (-O2):
@.str = [9 x i8] c"f_%d.txt\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@SEEK_END = global i32 0
define void @withSync() {
%1 = alloca [20 x i8]
%2 = getelementptr inbounds [20 x i8], [20 x i8]* %1, i64 0, i64 0
%3 = call i32 @sprintf(i8* %2, i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 0)
%4 = load i32, i32* @O_RDWR
%5 = load i32, i32* @O_CREAT
%6 = or i32 %5, %4
%7 = call i32 @open(i8* %2, i32 %6)
%8 = load i32, i32* @SEEK_END
%9 = tail call i32 @lseek(i32 %7, i32 0, i32 %8)
%10 = tail call i64 @malloc(i32 65536)
%11 = inttoptr i64 %10 to i8*
%12 = tail call i32 @memset(i8* %11, i32 51966, i32 65536)
%13 = tail call i32 @write(i32 %7, i8* %11, i32 1024)
br label %14
14: ; preds = %29, %0
%15 = phi i32 [ 1, %0 ], [ %31, %29 ]
%16 = call i32 @sprintf(i8* %2, i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %15)
%17 = load i32, i32* @O_RDWR
%18 = load i32, i32* @O_CREAT
%19 = or i32 %18, %17
%20 = call i32 @open(i8* %2, i32 %19)
%21 = load i32, i32* @SEEK_END
%22 = tail call i32 @lseek(i32 %20, i32 0, i32 %21)
%23 = tail call i64 @malloc(i32 65536)
%24 = inttoptr i64 %23 to i8*
%25 = tail call i32 @memset(i8* %24, i32 51966, i32 65536)
%26 = tail call i32 @write(i32 %20, i8* %24, i32 1024)
%27 = tail call i32 @usleep(i32 1000)
%28 = icmp eq i32 %15, 9
br i1 %28, label %32, label %29
29: ; preds = %14
%30 = tail call i32 @close(i32 %20)
%31 = add nuw nsw i32 %15, 1
br label %14
32: ; preds = %14
%33 = tail call i32 @fsync(i32 %20)
%34 = tail call i32 @close(i32 %20)
%35 = tail call i32 @close(i32 %7)
ret void
}