#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*/ fsync(int);
int open(char *, int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ pwrite(int, char *, int, int /*<<< orphan*/);
int main(int ac, char **av) {
  int fd = open(av[1], O_RDWR | O_CREAT, 0666);
  if (fd < 0)
    perror("open");
  for (int i = 0; i < 100000; i++) {
    char buf[4096];
    pwrite(fd, buf, 4096, 0);
    fsync(fd);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1020,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e78(%rip),%rax
mov    (%rax),%esi
mov    0x2e87(%rip),%rax
or     (%rax),%esi
mov    $0x1b6,%edx
call   1050 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1196 <main+0x56>
lea    0xe6f(%rip),%rdi
call   1060 <perror@plt>
movl   $0x0,-0x18(%rbp)
cmpl   $0x186a0,-0x18(%rbp)
jge    11d6 <main+0x96>
mov    -0x14(%rbp),%edi
lea    -0x1020(%rbp),%rsi
mov    $0x1000,%edx
xor    %ecx,%ecx
call   1030 <pwrite@plt>
mov    -0x14(%rbp),%edi
call   1040 <fsync@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    119d <main+0x5d>
mov    -0x4(%rbp),%eax
add    $0x1020,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x1000,%rsp
mov    0x8(%rsi),%rdi
mov    0x2e8a(%rip),%rax
mov    0x2e9b(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    $0x1b6,%edx
call   1050 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
js     11ab <main+0x6b>
mov    $0x186a0,%ebp
mov    %rsp,%r14
nopl   0x0(%rax)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x1000,%edx
xor    %ecx,%ecx
call   1030 <pwrite@plt>
mov    %ebx,%edi
call   1040 <fsync@plt>
add    $0xffffffff,%ebp
jne    1180 <main+0x40>
xor    %eax,%eax
add    $0x1000,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe4e(%rip),%rdi
call   1060 <perror@plt>
jmp    1171 <main+0x31>

LLVM IR (-O0):
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@.str = [5 x i8] c"open\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca [4096 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i8**, i8*** %5
%10 = getelementptr inbounds i8*, i8** %9, i64 1
%11 = load i8*, i8** %10
%12 = load i32, i32* @O_RDWR
%13 = load i32, i32* @O_CREAT
%14 = or i32 %12, %13
%15 = call i32 @open(i8* %11, i32 %14, i32 438)
store i32 %15, i32* %6
%16 = load i32, i32* %6
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %2
%19 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %20
20: ; preds = %18, %2
store i32 0, i32* %7
br label %21
21: ; preds = %30, %20
%22 = load i32, i32* %7
%23 = icmp slt i32 %22, 100000
br i1 %23, label %24, label %33
24: ; preds = %21
%25 = load i32, i32* %6
%26 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 0
%27 = call i32 @pwrite(i32 %25, i8* %26, i32 4096, i32 0)
%28 = load i32, i32* %6
%29 = call i32 @fsync(i32 %28)
br label %30
30: ; preds = %24
%31 = load i32, i32* %7
%32 = add nsw i32 %31, 1
store i32 %32, i32* %7
br label %21
33: ; preds = %21
%34 = load i32, i32* %3
ret i32 %34
}

LLVM IR (-O2):
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@.str = [5 x i8] c"open\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [4096 x i8]
%4 = getelementptr inbounds i8*, i8** %1, i64 1
%5 = load i8*, i8** %4
%6 = load i32, i32* @O_RDWR
%7 = load i32, i32* @O_CREAT
%8 = or i32 %7, %6
%9 = tail call i32 @open(i8* %5, i32 %8, i32 438)
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %13
13: ; preds = %11, %2
%14 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
br label %16
15: ; preds = %16
ret i32 0
16: ; preds = %13, %16
%17 = phi i32 [ 0, %13 ], [ %20, %16 ]
%18 = call i32 @pwrite(i32 %9, i8* %14, i32 4096, i32 0)
%19 = tail call i32 @fsync(i32 %9)
%20 = add nuw nsw i32 %17, 1
%21 = icmp eq i32 %20, 100000
br i1 %21, label %15, label %16
}