#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 */
struct timespec {
  int tv_nsec;
  scalar_t__ tv_sec;
};
struct flock {
  scalar_t__ l_len;
  void *l_whence;
  scalar_t__ l_start;
  int /*<<< orphan*/ l_type;
};

/* Variables and functions */
int /*<<< orphan*/ F_SETLK;
int /*<<< orphan*/ F_SETLKW;
int /*<<< orphan*/ F_UNLCK;
int /*<<< orphan*/ F_WRLCK;
int O_APPEND;
int O_CREAT;
int O_RDWR;
void *SEEK_SET;
int /*<<< orphan*/ fcntl(int, int /*<<< orphan*/, struct flock *);
int fork();
int /*<<< orphan*/ nanosleep(struct timespec *, int /*<<< orphan*/ *);
int open(char *, int, int);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
scalar_t__ strlen(char *);
int /*<<< orphan*/ unlink(char *);
int /*<<< orphan*/ wait(int /*<<< orphan*/ *);
int /*<<< orphan*/ write(int, char *, int);
int main(void) {
  unlink("fcntl-lock.temp");
  int child = fork();
  char buf[1024];
  if (child == 0)
    strcpy(buf, "aaa aaa aaa aaa\n");
  else
    strcpy(buf, "bbb bbb bbb bbb\n");
  int sharedFd = open("fcntl-lock.temp", O_RDWR | O_APPEND | O_CREAT, 0600);
  for (int j = 0; j < 1000; j++) {
    struct flock lock;
    lock.l_type = F_WRLCK;
    lock.l_start = 0;
    lock.l_whence = SEEK_SET;
    lock.l_len = 0;
    fcntl(sharedFd, F_SETLKW, &lock);
    for (int i = 0; i < (int)strlen(buf); i++)
      write(sharedFd, &(buf[i]), 1);
    lock.l_type = F_UNLCK;
    lock.l_start = 0;
    lock.l_whence = SEEK_SET;
    lock.l_len = 0;
    fcntl(sharedFd, F_SETLK, &lock);
    struct timespec slptm;
    slptm.tv_sec = 0;
    slptm.tv_nsec = 1000000000 / 1000;
    nanosleep(&slptm, NULL);
  }
  if (child > 0) {
    wait(NULL);
    printf("Look in %s\n", "fcntl-lock.temp");
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x460,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe47(%rip),%rdi
call   1030 <unlink@plt>
mov    $0x0,%al
call   10c0 <fork@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jne    11ea <main+0x4a>
lea    -0x410(%rbp),%rdi
lea    0xe30(%rip),%rsi
call   1040 <strcpy@plt>
jmp    11fd <main+0x5d>
lea    -0x410(%rbp),%rdi
lea    0xe29(%rip),%rsi
call   1040 <strcpy@plt>
mov    0x2ddc(%rip),%rax
mov    (%rax),%esi
mov    0x2d93(%rip),%rax
or     (%rax),%esi
mov    0x2de2(%rip),%rax
or     (%rax),%esi
lea    0xde1(%rip),%rdi
mov    $0x180,%edx
call   10a0 <open@plt>
mov    %eax,-0x414(%rbp)
movl   $0x0,-0x418(%rbp)
cmpl   $0x3e8,-0x418(%rbp)
jge    138c <main+0x1ec>
mov    0x2d60(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x420(%rbp)
movq   $0x0,-0x428(%rbp)
mov    0x2d4e(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x430(%rbp)
movq   $0x0,-0x438(%rbp)
mov    -0x414(%rbp),%edi
mov    0x2d4c(%rip),%rax
mov    (%rax),%esi
lea    -0x438(%rbp),%rdx
call   1050 <fcntl@plt>
movl   $0x0,-0x43c(%rbp)
mov    -0x43c(%rbp),%eax
mov    %eax,-0x454(%rbp)
lea    -0x410(%rbp),%rdi
call   1070 <strlen@plt>
mov    %rax,%rcx
mov    -0x454(%rbp),%eax
cmp    %ecx,%eax
jge    1302 <main+0x162>
mov    -0x414(%rbp),%edi
movslq -0x43c(%rbp),%rax
lea    -0x410(%rbp),%rsi
add    %rax,%rsi
mov    $0x1,%edx
call   1060 <write@plt>
mov    -0x43c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x43c(%rbp)
jmp    12a4 <main+0x104>
mov    0x2cbf(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x420(%rbp)
movq   $0x0,-0x428(%rbp)
mov    0x2c95(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x430(%rbp)
movq   $0x0,-0x438(%rbp)
mov    -0x414(%rbp),%edi
mov    0x2c8b(%rip),%rax
mov    (%rax),%esi
lea    -0x438(%rbp),%rdx
call   1050 <fcntl@plt>
movq   $0x0,-0x448(%rbp)
movl   $0xf4240,-0x450(%rbp)
lea    -0x450(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1090 <nanosleep@plt>
mov    -0x418(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x418(%rbp)
jmp    1239 <main+0x99>
cmpl   $0x0,-0x8(%rbp)
jle    13b2 <main+0x212>
xor    %eax,%eax
mov    %eax,%edi
call   10b0 <wait@plt>
lea    0xc8c(%rip),%rdi
lea    0xc53(%rip),%rsi
call   1080 <printf@plt>
xor    %eax,%eax
add    $0x460,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x448,%rsp
lea    0xe48(%rip),%rbp
mov    %rbp,%rdi
call   1030 <unlink@plt>
xor    %r13d,%r13d
xor    %eax,%eax
call   10c0 <fork@plt>
mov    %eax,0xc(%rsp)
test   %eax,%eax
lea    0xe39(%rip),%rax
lea    0xe43(%rip),%rsi
cmove  %rax,%rsi
lea    0x40(%rsp),%rbx
mov    %rbx,%rdi
call   1040 <strcpy@plt>
mov    0x2dea(%rip),%rax
mov    0x2da3(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2df0(%rip),%rax
or     (%rax),%esi
mov    %rbp,%rdi
mov    $0x180,%edx
call   10a0 <open@plt>
mov    %eax,%ebp
mov    0x2d98(%rip),%r14
lea    0x10(%rsp),%r15
jmp    1298 <main+0xf8>
nopw   0x0(%rax,%rax,1)
mov    0x2d91(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x28(%rsp)
movq   $0x0,0x20(%rsp)
mov    (%r14),%rax
mov    %rax,0x18(%rsp)
movq   $0x0,0x10(%rsp)
mov    0x2d72(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
mov    %r15,%rdx
call   1050 <fcntl@plt>
movq   $0x0,0x38(%rsp)
movl   $0xf4240,0x30(%rsp)
lea    0x30(%rsp),%rdi
xor    %esi,%esi
call   1090 <nanosleep@plt>
add    $0x1,%r13d
cmp    $0x3e8,%r13d
je     131c <main+0x17c>
mov    0x2d11(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x28(%rsp)
movq   $0x0,0x20(%rsp)
mov    (%r14),%rax
mov    %rax,0x18(%rsp)
movq   $0x0,0x10(%rsp)
mov    0x2d12(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
mov    %r15,%rdx
call   1050 <fcntl@plt>
mov    %rbx,%rdi
call   1070 <strlen@plt>
test   %eax,%eax
jle    1230 <main+0x90>
xor    %r12d,%r12d
cs nopw 0x0(%rax,%rax,1)
nop
lea    (%rsp,%r12,1),%rsi
add    $0x40,%rsi
mov    %ebp,%edi
mov    $0x1,%edx
call   1060 <write@plt>
add    $0x1,%r12
mov    %rbx,%rdi
call   1070 <strlen@plt>
cltq
cmp    %rax,%r12
jl     12f0 <main+0x150>
jmp    1230 <main+0x90>
cmpl   $0x0,0xc(%rsp)
jle    133d <main+0x19d>
xor    %edi,%edi
call   10b0 <wait@plt>
lea    0xd01(%rip),%rdi
lea    0xcc8(%rip),%rsi
call   1080 <printf@plt>
xor    %eax,%eax
add    $0x448,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.flock = type { i64, i8*, i64, i32 }
%struct.timespec = type { i32, i64 }
@.str = [16 x i8] c"fcntl-lock.temp\00"
@.str.1 = [17 x i8] c"aaa aaa aaa aaa\0A\00"
@.str.2 = [17 x i8] c"bbb bbb bbb bbb\0A\00"
@O_RDWR = global i32 0
@O_APPEND = global i32 0
@O_CREAT = global i32 0
@F_WRLCK = global i32 0
@SEEK_SET = global i8* null
@F_SETLKW = global i32 0
@F_UNLCK = global i32 0
@F_SETLK = global i32 0
@.str.3 = [12 x i8] c"Look in %s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [1024 x i8]
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.flock
%7 = alloca i32
%8 = alloca %struct.timespec
store i32 0, i32* %1
%9 = call i32 @unlink(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%10 = call i32 (...) @fork()
store i32 %10, i32* %2
%11 = load i32, i32* %2
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %0
%14 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%15 = call i32 @strcpy(i8* %14, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
br label %19
16: ; preds = %0
%17 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%18 = call i32 @strcpy(i8* %17, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
br label %19
19: ; preds = %16, %13
%20 = load i32, i32* @O_RDWR
%21 = load i32, i32* @O_APPEND
%22 = or i32 %20, %21
%23 = load i32, i32* @O_CREAT
%24 = or i32 %22, %23
%25 = call i32 @open(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %24, i32 384)
store i32 %25, i32* %4
store i32 0, i32* %5
br label %26
26: ; preds = %67, %19
%27 = load i32, i32* %5
%28 = icmp slt i32 %27, 1000
br i1 %28, label %29, label %70
29: ; preds = %26
%30 = load i32, i32* @F_WRLCK
%31 = getelementptr inbounds %struct.flock, %struct.flock* %6, i32 0, i32 3
store i32 %30, i32* %31
%32 = getelementptr inbounds %struct.flock, %struct.flock* %6, i32 0, i32 2
store i64 0, i64* %32
%33 = load i8*, i8** @SEEK_SET
%34 = getelementptr inbounds %struct.flock, %struct.flock* %6, i32 0, i32 1
store i8* %33, i8** %34
%35 = getelementptr inbounds %struct.flock, %struct.flock* %6, i32 0, i32 0
store i64 0, i64* %35
%36 = load i32, i32* %4
%37 = load i32, i32* @F_SETLKW
%38 = call i32 @fcntl(i32 %36, i32 %37, %struct.flock* %6)
store i32 0, i32* %7
br label %39
39: ; preds = %51, %29
%40 = load i32, i32* %7
%41 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%42 = call i64 @strlen(i8* %41)
%43 = trunc i64 %42 to i32
%44 = icmp slt i32 %40, %43
br i1 %44, label %45, label %54
45: ; preds = %39
%46 = load i32, i32* %4
%47 = load i32, i32* %7
%48 = sext i32 %47 to i64
%49 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 %48
%50 = call i32 @write(i32 %46, i8* %49, i32 1)
br label %51
51: ; preds = %45
%52 = load i32, i32* %7
%53 = add nsw i32 %52, 1
store i32 %53, i32* %7
br label %39
54: ; preds = %39
%55 = load i32, i32* @F_UNLCK
%56 = getelementptr inbounds %struct.flock, %struct.flock* %6, i32 0, i32 3
store i32 %55, i32* %56
%57 = getelementptr inbounds %struct.flock, %struct.flock* %6, i32 0, i32 2
store i64 0, i64* %57
%58 = load i8*, i8** @SEEK_SET
%59 = getelementptr inbounds %struct.flock, %struct.flock* %6, i32 0, i32 1
store i8* %58, i8** %59
%60 = getelementptr inbounds %struct.flock, %struct.flock* %6, i32 0, i32 0
store i64 0, i64* %60
%61 = load i32, i32* %4
%62 = load i32, i32* @F_SETLK
%63 = call i32 @fcntl(i32 %61, i32 %62, %struct.flock* %6)
%64 = getelementptr inbounds %struct.timespec, %struct.timespec* %8, i32 0, i32 1
store i64 0, i64* %64
%65 = getelementptr inbounds %struct.timespec, %struct.timespec* %8, i32 0, i32 0
store i32 1000000, i32* %65
%66 = call i32 @nanosleep(%struct.timespec* %8, i32* null)
br label %67
67: ; preds = %54
%68 = load i32, i32* %5
%69 = add nsw i32 %68, 1
store i32 %69, i32* %5
br label %26
70: ; preds = %26
%71 = load i32, i32* %2
%72 = icmp sgt i32 %71, 0
br i1 %72, label %73, label %76
73: ; preds = %70
%74 = call i32 @wait(i32* null)
%75 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
br label %76
76: ; preds = %73, %70
ret i32 0
}

LLVM IR (-O2):
%struct.flock = type { i64, i8*, i64, i32 }
%struct.timespec = type { i32, i64 }
@.str = [16 x i8] c"fcntl-lock.temp\00"
@.str.1 = [17 x i8] c"aaa aaa aaa aaa\0A\00"
@.str.2 = [17 x i8] c"bbb bbb bbb bbb\0A\00"
@O_RDWR = global i32 0
@O_APPEND = global i32 0
@O_CREAT = global i32 0
@F_WRLCK = global i32 0
@SEEK_SET = global i8* null
@F_SETLKW = global i32 0
@F_UNLCK = global i32 0
@F_SETLK = global i32 0
@.str.3 = [12 x i8] c"Look in %s\0A\00"
define i32 @main() {
%1 = alloca [1024 x i8]
%2 = alloca %struct.flock
%3 = alloca %struct.timespec
%4 = tail call i32 @unlink(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%5 = tail call i32 (...) @fork()
%6 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
%7 = icmp eq i32 %5, 0
%8 = select i1 %7, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0)
%9 = call i32 @strcpy(i8* %6, i8* %8)
%10 = load i32, i32* @O_RDWR
%11 = load i32, i32* @O_APPEND
%12 = or i32 %11, %10
%13 = load i32, i32* @O_CREAT
%14 = or i32 %12, %13
%15 = call i32 @open(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %14, i32 384)
%16 = bitcast %struct.flock* %2 to i8*
%17 = getelementptr inbounds %struct.flock, %struct.flock* %2, i64 0, i32 3
%18 = getelementptr inbounds %struct.flock, %struct.flock* %2, i64 0, i32 2
%19 = getelementptr inbounds %struct.flock, %struct.flock* %2, i64 0, i32 1
%20 = getelementptr inbounds %struct.flock, %struct.flock* %2, i64 0, i32 0
%21 = bitcast %struct.timespec* %3 to i8*
%22 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 1
%23 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 0
br label %26
24: ; preds = %35
%25 = icmp sgt i32 %5, 0
br i1 %25, label %52, label %55
26: ; preds = %0, %35
%27 = phi i32 [ 0, %0 ], [ %41, %35 ]
%28 = load i32, i32* @F_WRLCK
store i32 %28, i32* %17
store i64 0, i64* %18
%29 = load i8*, i8** @SEEK_SET
store i8* %29, i8** %19
store i64 0, i64* %20
%30 = load i32, i32* @F_SETLKW
%31 = call i32 @fcntl(i32 %15, i32 %30, %struct.flock* %2)
%32 = call i64 @strlen(i8* %6)
%33 = trunc i64 %32 to i32
%34 = icmp sgt i32 %33, 0
br i1 %34, label %43, label %35
35: ; preds = %43, %26
%36 = load i32, i32* @F_UNLCK
store i32 %36, i32* %17
store i64 0, i64* %18
%37 = load i8*, i8** @SEEK_SET
store i8* %37, i8** %19
store i64 0, i64* %20
%38 = load i32, i32* @F_SETLK
%39 = call i32 @fcntl(i32 %15, i32 %38, %struct.flock* %2)
store i64 0, i64* %22
store i32 1000000, i32* %23
%40 = call i32 @nanosleep(%struct.timespec* %3, i32* null)
%41 = add nuw nsw i32 %27, 1
%42 = icmp eq i32 %41, 1000
br i1 %42, label %24, label %26
43: ; preds = %26, %43
%44 = phi i64 [ %47, %43 ], [ 0, %26 ]
%45 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 %44
%46 = call i32 @write(i32 %15, i8* %45, i32 1)
%47 = add nuw nsw i64 %44, 1
%48 = call i64 @strlen(i8* %6)
%49 = shl i64 %48, 32
%50 = ashr exact i64 %49, 32
%51 = icmp slt i64 %47, %50
br i1 %51, label %43, label %35
52: ; preds = %24
%53 = call i32 @wait(i32* null)
%54 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
br label %55
55: ; preds = %52, %24
ret i32 0
}