#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*/ exit(int);
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ getppid();
int /*<<< orphan*/ lockabyte(char *, int, int);
int /*<<< orphan*/ stderr;
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) {
    fprintf(stderr, "creat error");
    exit(1);
  }
  if (write(fd, "ab", 2) != 2) {
    fprintf(stderr, "write error");
    exit(1);
  }
  TELL_WAIT();
  if ((pid = fork()) < 0) {
    fprintf(stderr, "fork error");
    exit(1);
  } 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    0x2dea(%rip),%rax
mov    (%rax),%esi
mov    0x2e09(%rip),%rax
or     (%rax),%esi
mov    0x2de8(%rip),%rax
or     (%rax),%esi
mov    0x2dd7(%rip),%rax
or     (%rax),%esi
lea    0xe06(%rip),%rdi
call   10a0 <creat@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jge    122a <main+0x6a>
mov    0x2dc6(%rip),%rax
mov    (%rax),%edi
lea    0xdee(%rip),%rsi
call   1090 <fprintf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x8(%rbp),%edi
lea    0xde1(%rip),%rsi
mov    $0x2,%edx
call   1040 <write@plt>
cmp    $0x2,%eax
je     1266 <main+0xa6>
mov    0x2d8a(%rip),%rax
mov    (%rax),%edi
lea    0xdc1(%rip),%rsi
call   1090 <fprintf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    $0x0,%al
call   1070 <TELL_WAIT@plt>
mov    $0x0,%al
call   10e0 <fork@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jge    12a1 <main+0xe1>
mov    0x2d4f(%rip),%rax
mov    (%rax),%edi
lea    0xd92(%rip),%rsi
call   1090 <fprintf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
cmpq   $0x0,-0x10(%rbp)
jne    12eb <main+0x12b>
mov    -0x8(%rbp),%esi
lea    0xd79(%rip),%rdi
xor    %edx,%edx
call   1080 <lockabyte@plt>
mov    $0x0,%al
call   10b0 <getppid@plt>
mov    %eax,%edi
call   1060 <TELL_PARENT@plt>
mov    $0x0,%al
call   10d0 <WAIT_PARENT@plt>
mov    -0x8(%rbp),%esi
lea    0xd53(%rip),%rdi
mov    $0x1,%edx
call   1080 <lockabyte@plt>
jmp    1320 <main+0x160>
mov    -0x8(%rbp),%esi
lea    0xd40(%rip),%rdi
mov    $0x1,%edx
call   1080 <lockabyte@plt>
mov    -0x10(%rbp),%rdi
call   1050 <TELL_CHILD@plt>
mov    $0x0,%al
call   1030 <WAIT_CHILD@plt>
mov    -0x8(%rbp),%esi
lea    0xd1c(%rip),%rdi
xor    %edx,%edx
call   1080 <lockabyte@plt>
jmp    1325 <main+0x165>
xor    %edi,%edi
call   10c0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    0x2df2(%rip),%rax
mov    0x2e13(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2df0(%rip),%rax
or     (%rax),%esi
mov    0x2ddf(%rip),%rax
or     (%rax),%esi
lea    0xe0e(%rip),%rdi
call   10a0 <creat@plt>
test   %eax,%eax
jns    120d <main+0x4d>
mov    0x2dd6(%rip),%rax
mov    (%rax),%edi
lea    0xdfe(%rip),%rsi
jmp    125c <main+0x9c>
mov    %eax,%ebx
lea    0xdff(%rip),%rsi
mov    %eax,%edi
mov    $0x2,%edx
call   1040 <write@plt>
cmp    $0x2,%eax
jne    124c <main+0x8c>
xor    %eax,%eax
call   1070 <TELL_WAIT@plt>
xor    %eax,%eax
call   10e0 <fork@plt>
test   %rax,%rax
jns    126b <main+0xab>
mov    0x2d97(%rip),%rax
mov    (%rax),%edi
lea    0xdda(%rip),%rsi
jmp    125c <main+0x9c>
mov    0x2d85(%rip),%rax
mov    (%rax),%edi
lea    0xdbc(%rip),%rsi
call   1090 <fprintf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
jne    129c <main+0xdc>
lea    0xdbb(%rip),%r14
mov    %r14,%rdi
mov    %ebx,%esi
xor    %edx,%edx
call   1080 <lockabyte@plt>
xor    %eax,%eax
call   10b0 <getppid@plt>
mov    %eax,%edi
call   1060 <TELL_PARENT@plt>
xor    %eax,%eax
call   10d0 <WAIT_PARENT@plt>
mov    $0x1,%ebp
jmp    12c6 <main+0x106>
mov    %rax,%r15
lea    0xd8f(%rip),%r14
mov    %r14,%rdi
mov    %ebx,%esi
mov    $0x1,%edx
call   1080 <lockabyte@plt>
mov    %r15,%rdi
call   1050 <TELL_CHILD@plt>
xor    %ebp,%ebp
xor    %eax,%eax
call   1030 <WAIT_CHILD@plt>
mov    %r14,%rdi
mov    %ebx,%esi
mov    %ebp,%edx
call   1080 <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
@stderr = 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 %17
13: ; preds = %0
%14 = load i32, i32* @stderr
%15 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%16 = call i32 @exit(i32 1)
unreachable
17: ; preds = %0
%18 = load i32, i32* %2
%19 = call i32 @write(i32 %18, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 2)
%20 = icmp ne i32 %19, 2
br i1 %20, label %21, label %25
21: ; preds = %17
%22 = load i32, i32* @stderr
%23 = call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
%24 = call i32 @exit(i32 1)
unreachable
25: ; preds = %17
%26 = call i32 (...) @TELL_WAIT()
%27 = call i64 (...) @fork()
store i64 %27, i64* %3
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %33
29: ; preds = %25
%30 = load i32, i32* @stderr
%31 = call i32 @fprintf(i32 %30, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0))
%32 = call i32 @exit(i32 1)
unreachable
33: ; preds = %25
%34 = load i64, i64* %3
%35 = icmp eq i64 %34, 0
br i1 %35, label %36, label %44
36: ; preds = %33
%37 = load i32, i32* %2
%38 = call i32 @lockabyte(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0), i32 %37, i32 0)
%39 = call i32 (...) @getppid()
%40 = call i32 @TELL_PARENT(i32 %39)
%41 = call i32 (...) @WAIT_PARENT()
%42 = load i32, i32* %2
%43 = call i32 @lockabyte(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0), i32 %42, i32 1)
br label %52
44: ; preds = %33
%45 = load i32, i32* %2
%46 = call i32 @lockabyte(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0), i32 %45, i32 1)
%47 = load i64, i64* %3
%48 = call i32 @TELL_CHILD(i64 %47)
%49 = call i32 (...) @WAIT_CHILD()
%50 = load i32, i32* %2
%51 = call i32 @lockabyte(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0), i32 %50, i32 0)
br label %52
52: ; preds = %44, %36
br label %53
53: ; preds = %52
%54 = 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
@stderr = 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 %14
10: ; preds = %0
%11 = load i32, i32* @stderr
%12 = tail call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%13 = tail call i32 @exit(i32 1)
unreachable
14: ; preds = %0
%15 = tail call i32 @write(i32 %8, 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 %21, label %17
17: ; preds = %14
%18 = load i32, i32* @stderr
%19 = tail call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
%20 = tail call i32 @exit(i32 1)
unreachable
21: ; preds = %14
%22 = tail call i32 (...) @TELL_WAIT()
%23 = tail call i64 (...) @fork()
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %29
25: ; preds = %21
%26 = load i32, i32* @stderr
%27 = tail call i32 @fprintf(i32 %26, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0))
%28 = tail call i32 @exit(i32 1)
unreachable
29: ; preds = %21
%30 = icmp eq i64 %23, 0
br i1 %30, label %31, label %36
31: ; preds = %29
%32 = tail call i32 @lockabyte(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0), i32 %8, i32 0)
%33 = tail call i32 (...) @getppid()
%34 = tail call i32 @TELL_PARENT(i32 %33)
%35 = tail call i32 (...) @WAIT_PARENT()
br label %40
36: ; preds = %29
%37 = tail call i32 @lockabyte(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0), i32 %8, i32 1)
%38 = tail call i32 @TELL_CHILD(i64 %23)
%39 = tail call i32 (...) @WAIT_CHILD()
br label %40
40: ; preds = %31, %36
%41 = phi i32 [ 1, %31 ], [ 0, %36 ]
%42 = phi i8* [ getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0), %31 ], [ getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0), %36 ]
%43 = tail call i32 @lockabyte(i8* %42, i32 %8, i32 %41)
%44 = tail call i32 @exit(i32 0)
unreachable
}