#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 S_IRGRP;
int S_IROTH;
int S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ TELL_CHILD(scalar_t__);
int /*<<< orphan*/ TELL_PARENT(int /*<<< orphan*/);
int /*<<< orphan*/ TELL_WAIT();
int /*<<< orphan*/ WAIT_CHILD();
int /*<<< orphan*/ WAIT_PARENT();
int creat(char *, int);
int /*<<< orphan*/ err_sys(char *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ getppid();
int /*<<< orphan*/ lockabyte(char *, int, int);
int write(int, char *, int);
int main(void) {
  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("write error");
  TELL_WAIT();
  if ((pid = fork()) < 0) {
    err_sys("fork error");
  } else if (pid == 0) {
    lockabyte("child", fd, 0);
    TELL_PARENT(getppid());
    WAIT_PARENT();
    lockabyte("child", fd, 1);
  } else {
    lockabyte("parent", fd, 1);
    TELL_CHILD(pid);
    WAIT_CHILD();
    lockabyte("parent", fd, 0);
  }
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2df2(%rip),%rax
mov    (%rax),%esi
mov    0x2e09(%rip),%rax
or     (%rax),%esi
mov    0x2df0(%rip),%rax
or     (%rax),%esi
mov    0x2ddf(%rip),%rax
or     (%rax),%esi
lea    0xe06(%rip),%rdi
call   10a0 <creat@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jge    1217 <main+0x57>
lea    0xdf7(%rip),%rdi
call   1040 <err_sys@plt>
mov    -0x8(%rbp),%edi
lea    0xdf4(%rip),%rsi
mov    $0x2,%edx
call   1050 <write@plt>
cmp    $0x2,%eax
je     1240 <main+0x80>
lea    0xddd(%rip),%rdi
call   1040 <err_sys@plt>
mov    $0x0,%al
call   1080 <TELL_WAIT@plt>
mov    $0x0,%al
call   10e0 <fork@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jge    126d <main+0xad>
lea    0xdc1(%rip),%rdi
call   1040 <err_sys@plt>
jmp    12f1 <main+0x131>
cmpq   $0x0,-0x10(%rbp)
jne    12b7 <main+0xf7>
mov    -0x8(%rbp),%esi
lea    0xdad(%rip),%rdi
xor    %edx,%edx
call   1090 <lockabyte@plt>
mov    $0x0,%al
call   10b0 <getppid@plt>
mov    %eax,%edi
call   1070 <TELL_PARENT@plt>
mov    $0x0,%al
call   10d0 <WAIT_PARENT@plt>
mov    -0x8(%rbp),%esi
lea    0xd87(%rip),%rdi
mov    $0x1,%edx
call   1090 <lockabyte@plt>
jmp    12ec <main+0x12c>
mov    -0x8(%rbp),%esi
lea    0xd74(%rip),%rdi
mov    $0x1,%edx
call   1090 <lockabyte@plt>
mov    -0x10(%rbp),%rdi
call   1060 <TELL_CHILD@plt>
mov    $0x0,%al
call   1030 <WAIT_CHILD@plt>
mov    -0x8(%rbp),%esi
lea    0xd50(%rip),%rdi
xor    %edx,%edx
call   1090 <lockabyte@plt>
jmp    12f1 <main+0x131>
xor    %edi,%edi
call   10c0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
mov    0x2dfd(%rip),%rax
mov    0x2e16(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2dfb(%rip),%rax
or     (%rax),%esi
mov    0x2dea(%rip),%rax
or     (%rax),%esi
lea    0xe11(%rip),%rdi
call   10a0 <creat@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    1206 <main+0x46>
lea    0xe08(%rip),%rdi
call   1040 <err_sys@plt>
lea    0xe08(%rip),%rsi
mov    %ebp,%edi
mov    $0x2,%edx
call   1050 <write@plt>
cmp    $0x2,%eax
je     122a <main+0x6a>
lea    0xdf3(%rip),%rdi
call   1040 <err_sys@plt>
xor    %eax,%eax
call   1080 <TELL_WAIT@plt>
xor    %eax,%eax
call   10e0 <fork@plt>
test   %rax,%rax
jns    1250 <main+0x90>
lea    0xde0(%rip),%rdi
call   1040 <err_sys@plt>
xor    %edi,%edi
call   10c0 <exit@plt>
jne    1286 <main+0xc6>
lea    0xdd6(%rip),%rbx
mov    %rbx,%rdi
mov    %ebp,%esi
xor    %edx,%edx
call   1090 <lockabyte@plt>
xor    %eax,%eax
call   10b0 <getppid@plt>
mov    %eax,%edi
call   1070 <TELL_PARENT@plt>
xor    %eax,%eax
call   10d0 <WAIT_PARENT@plt>
mov    %rbx,%rdi
mov    %ebp,%esi
mov    $0x1,%edx
jmp    12b5 <main+0xf5>
mov    %rax,%rbx
lea    0xda5(%rip),%r14
mov    %r14,%rdi
mov    %ebp,%esi
mov    $0x1,%edx
call   1090 <lockabyte@plt>
mov    %rbx,%rdi
call   1060 <TELL_CHILD@plt>
xor    %eax,%eax
call   1030 <WAIT_CHILD@plt>
mov    %r14,%rdi
mov    %ebp,%esi
xor    %edx,%edx
call   1090 <lockabyte@plt>
xor    %edi,%edi
call   10c0 <exit@plt>

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 = [12 x i8] c"write 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() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i64
store i32 0, i32* %1
%4 = load i32, i32* @S_IRUSR
%5 = load i32, i32* @S_IWUSR
%6 = or i32 %4, %5
%7 = load i32, i32* @S_IRGRP
%8 = or i32 %6, %7
%9 = load i32, i32* @S_IROTH
%10 = or i32 %8, %9
%11 = call i32 @creat(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %10)
store i32 %11, i32* %2
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %0
%14 = call i32 @err_sys(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
br label %15
15: ; preds = %13, %0
%16 = load i32, i32* %2
%17 = call i32 @write(i32 %16, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 2)
%18 = icmp ne i32 %17, 2
br i1 %18, label %19, label %21
19: ; preds = %15
%20 = call i32 @err_sys(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
br label %21
21: ; preds = %19, %15
%22 = call i32 (...) @TELL_WAIT()
%23 = call i64 (...) @fork()
store i64 %23, i64* %3
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %21
%26 = call i32 @err_sys(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0))
br label %47
27: ; preds = %21
%28 = load i64, i64* %3
%29 = icmp eq i64 %28, 0
br i1 %29, label %30, label %38
30: ; preds = %27
%31 = load i32, i32* %2
%32 = call i32 @lockabyte(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0), i32 %31, i32 0)
%33 = call i32 (...) @getppid()
%34 = call i32 @TELL_PARENT(i32 %33)
%35 = call i32 (...) @WAIT_PARENT()
%36 = load i32, i32* %2
%37 = call i32 @lockabyte(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0), i32 %36, i32 1)
br label %46
38: ; preds = %27
%39 = load i32, i32* %2
%40 = call i32 @lockabyte(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0), i32 %39, i32 1)
%41 = load i64, i64* %3
%42 = call i32 @TELL_CHILD(i64 %41)
%43 = call i32 (...) @WAIT_CHILD()
%44 = load i32, i32* %2
%45 = call i32 @lockabyte(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0), i32 %44, i32 0)
br label %46
46: ; preds = %38, %30
br label %47
47: ; preds = %46, %25
%48 = call i32 @exit(i32 0)
unreachable
}

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 = [12 x i8] c"write 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() {
%1 = load i32, i32* @S_IRUSR
%2 = load i32, i32* @S_IWUSR
%3 = or i32 %2, %1
%4 = load i32, i32* @S_IRGRP
%5 = or i32 %3, %4
%6 = load i32, i32* @S_IROTH
%7 = or i32 %5, %6
%8 = tail call i32 @creat(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %7)
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %0
%11 = tail call i32 @err_sys(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
br label %12
12: ; preds = %10, %0
%13 = tail call i32 @write(i32 %8, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 2)
%14 = icmp eq i32 %13, 2
br i1 %14, label %17, label %15
15: ; preds = %12
%16 = tail call i32 @err_sys(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
br label %17
17: ; preds = %15, %12
%18 = tail call i32 (...) @TELL_WAIT()
%19 = tail call i64 (...) @fork()
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %17
%22 = tail call i32 @err_sys(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0))
br label %36
23: ; preds = %17
%24 = icmp eq i64 %19, 0
br i1 %24, label %25, label %31
25: ; preds = %23
%26 = tail call i32 @lockabyte(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0), i32 %8, i32 0)
%27 = tail call i32 (...) @getppid()
%28 = tail call i32 @TELL_PARENT(i32 %27)
%29 = tail call i32 (...) @WAIT_PARENT()
%30 = tail call i32 @lockabyte(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0), i32 %8, i32 1)
br label %36
31: ; preds = %23
%32 = tail call i32 @lockabyte(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0), i32 %8, i32 1)
%33 = tail call i32 @TELL_CHILD(i64 %19)
%34 = tail call i32 (...) @WAIT_CHILD()
%35 = tail call i32 @lockabyte(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0), i32 %8, i32 0)
br label %36
36: ; preds = %25, %31, %21
%37 = tail call i32 @exit(i32 0)
unreachable
}