#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_APPEND;
int O_CREAT;
int O_WRONLY;
int /*<<< orphan*/ close(int);
int open(char *, int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ write(int, char *, size_t);
void test_synchronization(char *output, char *text, size_t length,
                          int (*fcnt)(int)) {
  int fd = open(output, O_WRONLY | O_CREAT | O_APPEND, 0660);
  if (fd < 0) {
    perror("Cannot get page size");
    return;
  }
  int i;
  for (i = 0; i < 10000; i++)
    write(fd, text, length);
  fcnt(fd);
  close(fd);
  sleep(5);
}


x86 assembly (clang, -O0):
<test_synchronization>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e5d(%rip),%rax
mov    (%rax),%esi
mov    0x2e7c(%rip),%rax
or     (%rax),%esi
mov    0x2e43(%rip),%rax
or     (%rax),%esi
mov    $0x1b0,%edx
call   1050 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    11af <test_synchronization+0x5f>
lea    0xe5b(%rip),%rdi
call   1060 <perror@plt>
jmp    11fc <test_synchronization+0xac>
movl   $0x0,-0x28(%rbp)
cmpl   $0x2710,-0x28(%rbp)
jge    11e1 <test_synchronization+0x91>
mov    -0x24(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <write@plt>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    11b6 <test_synchronization+0x66>
mov    -0x20(%rbp),%rax
mov    -0x24(%rbp),%edi
call   *%rax
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
mov    $0x5,%edi
call   1070 <sleep@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_synchronization>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    0x2e71(%rip),%rbp
mov    0x2e92(%rip),%rax
mov    (%rax),%eax
or     0x0(%rbp),%eax
mov    %rcx,%r14
mov    %rdx,%r15
mov    %rsi,%r12
mov    0x2e4d(%rip),%rcx
or     (%rcx),%eax
mov    %eax,%esi
mov    $0x1b0,%edx
call   1050 <open@plt>
test   %eax,%eax
js     11d0 <test_synchronization+0x80>
mov    %eax,%ebp
mov    $0x2710,%ebx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %ebp,%edi
mov    %r12,%rsi
mov    %r15,%rdx
call   1030 <write@plt>
add    $0xffffffff,%ebx
jne    11a0 <test_synchronization+0x50>
mov    %ebp,%edi
call   *%r14
mov    %ebp,%edi
call   1040 <close@plt>
mov    $0x5,%edi
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1070 <sleep@plt>
lea    0xe29(%rip),%rdi
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1060 <perror@plt>

LLVM IR (-O0):
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_APPEND = global i32 0
@.str = [21 x i8] c"Cannot get page size\00"
define void @test_synchronization(i8* %0, i8* %1, i64 %2, i32 (i32)* %3) {
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32 (i32)*
%9 = alloca i32
%10 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i64 %2, i64* %7
store i32 (i32)* %3, i32 (i32)** %8
%11 = load i8*, i8** %5
%12 = load i32, i32* @O_WRONLY
%13 = load i32, i32* @O_CREAT
%14 = or i32 %12, %13
%15 = load i32, i32* @O_APPEND
%16 = or i32 %14, %15
%17 = call i32 @open(i8* %11, i32 %16, i32 432)
store i32 %17, i32* %9
%18 = load i32, i32* %9
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %4
%21 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
br label %41
22: ; preds = %4
store i32 0, i32* %10
br label %23
23: ; preds = %31, %22
%24 = load i32, i32* %10
%25 = icmp slt i32 %24, 10000
br i1 %25, label %26, label %34
26: ; preds = %23
%27 = load i32, i32* %9
%28 = load i8*, i8** %6
%29 = load i64, i64* %7
%30 = call i32 @write(i32 %27, i8* %28, i64 %29)
br label %31
31: ; preds = %26
%32 = load i32, i32* %10
%33 = add nsw i32 %32, 1
store i32 %33, i32* %10
br label %23
34: ; preds = %23
%35 = load i32 (i32)*, i32 (i32)** %8
%36 = load i32, i32* %9
%37 = call i32 %35(i32 %36)
%38 = load i32, i32* %9
%39 = call i32 @close(i32 %38)
%40 = call i32 @sleep(i32 5)
br label %41
41: ; preds = %34, %20
ret void
}

LLVM IR (-O2):
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_APPEND = global i32 0
@.str = [21 x i8] c"Cannot get page size\00"
define void @test_synchronization(i8* nocapture %0, i8* nocapture %1, i64 %2, i32 (i32)* nocapture %3) {
%5 = load i32, i32* @O_WRONLY
%6 = load i32, i32* @O_CREAT
%7 = or i32 %6, %5
%8 = load i32, i32* @O_APPEND
%9 = or i32 %7, %8
%10 = tail call i32 @open(i8* %0, i32 %9, i32 432)
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %4
%13 = tail call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
br label %23
14: ; preds = %4, %14
%15 = phi i32 [ %17, %14 ], [ 0, %4 ]
%16 = tail call i32 @write(i32 %10, i8* %1, i64 %2)
%17 = add nuw nsw i32 %15, 1
%18 = icmp eq i32 %17, 10000
br i1 %18, label %19, label %14
19: ; preds = %14
%20 = tail call i32 %3(i32 %10)
%21 = tail call i32 @close(i32 %10)
%22 = tail call i32 @sleep(i32 5)
br label %23
23: ; preds = %19, %12
ret void
}