#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 */
typedef scalar_t__ pid_t;

/* Variables and functions */
int /*<<< orphan*/ SYNC_INIT();
int /*<<< orphan*/ SYNC_TELL(scalar_t__, int);
int /*<<< orphan*/ SYNC_WAIT(int);
int S_IRGRP;
int S_IROTH;
int S_IRUSR;
int S_IWUSR;
int creat(char *, int);
int /*<<< orphan*/ err_sys(char *);
scalar_t__ fork();
scalar_t__ getppid();
int /*<<< orphan*/ lockabyte(char *, int, int);
int /*<<< orphan*/ sleep(int);
int write(int, char *, int);
int main(int argc, char *argv[]) {
  int fd;
  pid_t pid;
  if ((fd = creat("templock", (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH))) < 0)
    err_sys("creat error");
  if (write(fd, "ab", 2) != 2)
    err_sys("writer error");
  SYNC_INIT();
  if ((pid = fork()) < 0)
    err_sys("fork error");
  else if (pid == 0) {
    lockabyte("child", fd, 0);
    SYNC_TELL(getppid(), 0);
    SYNC_WAIT(1);
    lockabyte("child", fd, 1);
  } else {
    lockabyte("parent", fd, 1);
    SYNC_WAIT(0);
    SYNC_TELL(pid, 1);
    lockabyte("parent", fd, 0);
  }
  sleep(2);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e0b(%rip),%rax
mov    (%rax),%esi
mov    0x2e22(%rip),%rax
or     (%rax),%esi
mov    0x2e09(%rip),%rax
or     (%rax),%esi
mov    0x2df8(%rip),%rax
or     (%rax),%esi
lea    0xe1f(%rip),%rdi
call   1090 <creat@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    11fe <main+0x5e>
lea    0xe10(%rip),%rdi
call   1030 <err_sys@plt>
mov    -0x14(%rbp),%edi
lea    0xe0d(%rip),%rsi
mov    $0x2,%edx
call   1040 <write@plt>
cmp    $0x2,%eax
je     1227 <main+0x87>
lea    0xdf6(%rip),%rdi
call   1030 <err_sys@plt>
mov    $0x0,%al
call   1070 <SYNC_INIT@plt>
mov    $0x0,%al
call   10c0 <fork@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jge    1254 <main+0xb4>
lea    0xddb(%rip),%rdi
call   1030 <err_sys@plt>
jmp    12e3 <main+0x143>
cmpq   $0x0,-0x20(%rbp)
jne    12a4 <main+0x104>
mov    -0x14(%rbp),%esi
lea    0xdc7(%rip),%rdi
xor    %edx,%edx
call   1060 <lockabyte@plt>
mov    $0x0,%al
call   10a0 <getppid@plt>
mov    %rax,%rdi
xor    %esi,%esi
call   1050 <SYNC_TELL@plt>
mov    $0x1,%edi
call   1080 <SYNC_WAIT@plt>
mov    -0x14(%rbp),%esi
lea    0xd9b(%rip),%rdi
mov    $0x1,%edx
call   1060 <lockabyte@plt>
jmp    12de <main+0x13e>
mov    -0x14(%rbp),%esi
lea    0xd88(%rip),%rdi
mov    $0x1,%edx
call   1060 <lockabyte@plt>
xor    %edi,%edi
call   1080 <SYNC_WAIT@plt>
mov    -0x20(%rbp),%rdi
mov    $0x1,%esi
call   1050 <SYNC_TELL@plt>
mov    -0x14(%rbp),%esi
lea    0xd5f(%rip),%rdi
xor    %edx,%edx
call   1060 <lockabyte@plt>
jmp    12e3 <main+0x143>
mov    $0x2,%edi
call   10b0 <sleep@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
mov    0x2e1d(%rip),%rax
mov    0x2e36(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e1b(%rip),%rax
or     (%rax),%esi
mov    0x2e0a(%rip),%rax
or     (%rax),%esi
lea    0xe31(%rip),%rdi
call   1090 <creat@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    11e6 <main+0x46>
lea    0xe28(%rip),%rdi
call   1030 <err_sys@plt>
lea    0xe28(%rip),%rsi
mov    %ebp,%edi
mov    $0x2,%edx
call   1040 <write@plt>
cmp    $0x2,%eax
je     120a <main+0x6a>
lea    0xe13(%rip),%rdi
call   1030 <err_sys@plt>
xor    %eax,%eax
call   1070 <SYNC_INIT@plt>
xor    %eax,%eax
call   10c0 <fork@plt>
test   %rax,%rax
js     1255 <main+0xb5>
je     1263 <main+0xc3>
mov    %rax,%rbx
lea    0xe0d(%rip),%r14
mov    %r14,%rdi
mov    %ebp,%esi
mov    $0x1,%edx
call   1060 <lockabyte@plt>
xor    %edi,%edi
call   1080 <SYNC_WAIT@plt>
mov    %rbx,%rdi
mov    $0x1,%esi
call   1050 <SYNC_TELL@plt>
mov    %r14,%rdi
mov    %ebp,%esi
xor    %edx,%edx
jmp    129b <main+0xfb>
lea    0xdc9(%rip),%rdi
call   1030 <err_sys@plt>
jmp    12a0 <main+0x100>
lea    0xdc6(%rip),%rbx
mov    %rbx,%rdi
mov    %ebp,%esi
xor    %edx,%edx
call   1060 <lockabyte@plt>
xor    %eax,%eax
call   10a0 <getppid@plt>
mov    %rax,%rdi
xor    %esi,%esi
call   1050 <SYNC_TELL@plt>
mov    $0x1,%edi
call   1080 <SYNC_WAIT@plt>
mov    %rbx,%rdi
mov    %ebp,%esi
mov    $0x1,%edx
call   1060 <lockabyte@plt>
mov    $0x2,%edi
call   10b0 <sleep@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [9 x i8] c"templock\00"
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
@.str.1 = [12 x i8] c"creat error\00"
@.str.2 = [3 x i8] c"ab\00"
@.str.3 = [13 x i8] c"writer error\00"
@.str.4 = [11 x i8] c"fork error\00"
@.str.5 = [6 x i8] c"child\00"
@.str.6 = [7 x i8] c"parent\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = load i32, i32* @S_IRUSR
%9 = load i32, i32* @S_IWUSR
%10 = or i32 %8, %9
%11 = load i32, i32* @S_IRGRP
%12 = or i32 %10, %11
%13 = load i32, i32* @S_IROTH
%14 = or i32 %12, %13
%15 = call i32 @creat(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %14)
store i32 %15, i32* %6
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %2
%18 = call i32 @err_sys(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
br label %19
19: ; preds = %17, %2
%20 = load i32, i32* %6
%21 = call i32 @write(i32 %20, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 2)
%22 = icmp ne i32 %21, 2
br i1 %22, label %23, label %25
23: ; preds = %19
%24 = call i32 @err_sys(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
br label %25
25: ; preds = %23, %19
%26 = call i32 (...) @SYNC_INIT()
%27 = call i64 (...) @fork()
store i64 %27, i64* %7
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %25
%30 = call i32 @err_sys(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0))
br label %51
31: ; preds = %25
%32 = load i64, i64* %7
%33 = icmp eq i64 %32, 0
br i1 %33, label %34, label %42
34: ; preds = %31
%35 = load i32, i32* %6
%36 = call i32 @lockabyte(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0), i32 %35, i32 0)
%37 = call i64 (...) @getppid()
%38 = call i32 @SYNC_TELL(i64 %37, i32 0)
%39 = call i32 @SYNC_WAIT(i32 1)
%40 = load i32, i32* %6
%41 = call i32 @lockabyte(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0), i32 %40, i32 1)
br label %50
42: ; preds = %31
%43 = load i32, i32* %6
%44 = call i32 @lockabyte(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0), i32 %43, i32 1)
%45 = call i32 @SYNC_WAIT(i32 0)
%46 = load i64, i64* %7
%47 = call i32 @SYNC_TELL(i64 %46, i32 1)
%48 = load i32, i32* %6
%49 = call i32 @lockabyte(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0), i32 %48, i32 0)
br label %50
50: ; preds = %42, %34
br label %51
51: ; preds = %50, %29
%52 = call i32 @sleep(i32 2)
ret i32 0
}

LLVM IR (-O2):
@.str = [9 x i8] c"templock\00"
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
@.str.1 = [12 x i8] c"creat error\00"
@.str.2 = [3 x i8] c"ab\00"
@.str.3 = [13 x i8] c"writer error\00"
@.str.4 = [11 x i8] c"fork error\00"
@.str.5 = [6 x i8] c"child\00"
@.str.6 = [7 x i8] c"parent\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = load i32, i32* @S_IRUSR
%4 = load i32, i32* @S_IWUSR
%5 = or i32 %4, %3
%6 = load i32, i32* @S_IRGRP
%7 = or i32 %5, %6
%8 = load i32, i32* @S_IROTH
%9 = or i32 %7, %8
%10 = tail call i32 @creat(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = tail call i32 @err_sys(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
br label %14
14: ; preds = %12, %2
%15 = tail call i32 @write(i32 %10, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 2)
%16 = icmp eq i32 %15, 2
br i1 %16, label %19, label %17
17: ; preds = %14
%18 = tail call i32 @err_sys(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
br label %19
19: ; preds = %17, %14
%20 = tail call i32 (...) @SYNC_INIT()
%21 = tail call i64 (...) @fork()
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %19
%24 = tail call i32 @err_sys(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0))
br label %38
25: ; preds = %19
%26 = icmp eq i64 %21, 0
br i1 %26, label %27, label %33
27: ; preds = %25
%28 = tail call i32 @lockabyte(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0), i32 %10, i32 0)
%29 = tail call i64 (...) @getppid()
%30 = tail call i32 @SYNC_TELL(i64 %29, i32 0)
%31 = tail call i32 @SYNC_WAIT(i32 1)
%32 = tail call i32 @lockabyte(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0), i32 %10, i32 1)
br label %38
33: ; preds = %25
%34 = tail call i32 @lockabyte(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0), i32 %10, i32 1)
%35 = tail call i32 @SYNC_WAIT(i32 0)
%36 = tail call i32 @SYNC_TELL(i64 %21, i32 1)
%37 = tail call i32 @lockabyte(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0), i32 %10, i32 0)
br label %38
38: ; preds = %27, %33, %23
%39 = tail call i32 @sleep(i32 2)
ret i32 0
}