#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(int argc, char const *argv[]) {
  pid_t pid;
  int fd;
  if ((fd = creat("templock", S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) < 0) {
    fprintf(stderr, "creat error\n");
    exit(1);
  }
  if (write(fd, "ab", 2) != 2) {
    fprintf(stderr, "write error\n");
    exit(1);
  }
  TELL_WAIT();
  if ((pid = fork()) < 0) {
    fprintf(stderr, "fork error\n");
    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);
  }
  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    0x2de3(%rip),%rax
mov    (%rax),%esi
mov    0x2e02(%rip),%rax
or     (%rax),%esi
mov    0x2de1(%rip),%rax
or     (%rax),%esi
mov    0x2dd0(%rip),%rax
or     (%rax),%esi
lea    0xdff(%rip),%rdi
call   10a0 <creat@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    1231 <main+0x71>
mov    0x2dbf(%rip),%rax
mov    (%rax),%edi
lea    0xde7(%rip),%rsi
call   1090 <fprintf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x1c(%rbp),%edi
lea    0xddb(%rip),%rsi
mov    $0x2,%edx
call   1040 <write@plt>
cmp    $0x2,%eax
je     126d <main+0xad>
mov    0x2d83(%rip),%rax
mov    (%rax),%edi
lea    0xdbb(%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,-0x18(%rbp)
cmp    $0x0,%rax
jge    12a8 <main+0xe8>
mov    0x2d48(%rip),%rax
mov    (%rax),%edi
lea    0xd8d(%rip),%rsi
call   1090 <fprintf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
cmpq   $0x0,-0x18(%rbp)
jne    12f2 <main+0x132>
mov    -0x1c(%rbp),%esi
lea    0xd75(%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    -0x1c(%rbp),%esi
lea    0xd4f(%rip),%rdi
mov    $0x1,%edx
call   1080 <lockabyte@plt>
jmp    1327 <main+0x167>
mov    -0x1c(%rbp),%esi
lea    0xd3c(%rip),%rdi
mov    $0x1,%edx
call   1080 <lockabyte@plt>
mov    -0x18(%rbp),%rdi
call   1050 <TELL_CHILD@plt>
mov    $0x0,%al
call   1030 <WAIT_CHILD@plt>
mov    -0x1c(%rbp),%esi
lea    0xd18(%rip),%rdi
xor    %edx,%edx
call   1080 <lockabyte@plt>
jmp    132c <main+0x16c>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
mov    0x2df5(%rip),%rax
mov    0x2e16(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2df3(%rip),%rax
or     (%rax),%esi
mov    0x2de2(%rip),%rax
or     (%rax),%esi
lea    0xe11(%rip),%rdi
call   10a0 <creat@plt>
test   %eax,%eax
js     12a2 <main+0xe2>
mov    %eax,%ebp
lea    0xe11(%rip),%rsi
mov    %eax,%edi
mov    $0x2,%edx
call   1040 <write@plt>
cmp    $0x2,%eax
jne    12b4 <main+0xf4>
xor    %eax,%eax
call   1070 <TELL_WAIT@plt>
xor    %eax,%eax
call   10e0 <fork@plt>
test   %rax,%rax
js     12c6 <main+0x106>
je     1264 <main+0xa4>
mov    %rax,%rbx
lea    0xdfb(%rip),%r14
mov    %r14,%rdi
mov    %ebp,%esi
mov    $0x1,%edx
call   1080 <lockabyte@plt>
mov    %rbx,%rdi
call   1050 <TELL_CHILD@plt>
xor    %eax,%eax
call   1030 <WAIT_CHILD@plt>
mov    %r14,%rdi
mov    %ebp,%esi
xor    %edx,%edx
jmp    1296 <main+0xd6>
lea    0xdc7(%rip),%rbx
mov    %rbx,%rdi
mov    %ebp,%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    %rbx,%rdi
mov    %ebp,%esi
mov    $0x1,%edx
call   1080 <lockabyte@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2d2f(%rip),%rax
mov    (%rax),%edi
lea    0xd57(%rip),%rsi
jmp    12d6 <main+0x116>
mov    0x2d1d(%rip),%rax
mov    (%rax),%edi
lea    0xd55(%rip),%rsi
jmp    12d6 <main+0x116>
mov    0x2d0b(%rip),%rax
mov    (%rax),%edi
lea    0xd50(%rip),%rsi
call   1090 <fprintf@plt>
mov    $0x1,%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 = [13 x i8] c"creat error\0A\00"
@.str.2 = [3 x i8] c"ab\00"
@.str.3 = [13 x i8] c"write error\0A\00"
@.str.4 = [12 x i8] c"fork error\0A\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 i64
%7 = alloca i32
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* %7
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %21
17: ; preds = %2
%18 = load i32, i32* @stderr
%19 = call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%20 = call i32 @exit(i32 1)
unreachable
21: ; preds = %2
%22 = load i32, i32* %7
%23 = call i32 @write(i32 %22, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 2)
%24 = icmp ne i32 %23, 2
br i1 %24, label %25, label %29
25: ; preds = %21
%26 = load i32, i32* @stderr
%27 = call i32 @fprintf(i32 %26, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
%28 = call i32 @exit(i32 1)
unreachable
29: ; preds = %21
%30 = call i32 (...) @TELL_WAIT()
%31 = call i64 (...) @fork()
store i64 %31, i64* %6
%32 = icmp slt i64 %31, 0
br i1 %32, label %33, label %37
33: ; preds = %29
%34 = load i32, i32* @stderr
%35 = call i32 @fprintf(i32 %34, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0))
%36 = call i32 @exit(i32 1)
unreachable
37: ; preds = %29
%38 = load i64, i64* %6
%39 = icmp eq i64 %38, 0
br i1 %39, label %40, label %48
40: ; preds = %37
%41 = load i32, i32* %7
%42 = call i32 @lockabyte(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0), i32 %41, i32 0)
%43 = call i32 (...) @getppid()
%44 = call i32 @TELL_PARENT(i32 %43)
%45 = call i32 (...) @WAIT_PARENT()
%46 = load i32, i32* %7
%47 = call i32 @lockabyte(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0), i32 %46, i32 1)
br label %56
48: ; preds = %37
%49 = load i32, i32* %7
%50 = call i32 @lockabyte(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0), i32 %49, i32 1)
%51 = load i64, i64* %6
%52 = call i32 @TELL_CHILD(i64 %51)
%53 = call i32 (...) @WAIT_CHILD()
%54 = load i32, i32* %7
%55 = call i32 @lockabyte(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0), i32 %54, i32 0)
br label %56
56: ; preds = %48, %40
br label %57
57: ; preds = %56
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
@stderr = global i32 0
@.str.1 = [13 x i8] c"creat error\0A\00"
@.str.2 = [3 x i8] c"ab\00"
@.str.3 = [13 x i8] c"write error\0A\00"
@.str.4 = [12 x i8] c"fork error\0A\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 %16
12: ; preds = %2
%13 = load i32, i32* @stderr
%14 = tail call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%15 = tail call i32 @exit(i32 1)
unreachable
16: ; preds = %2
%17 = tail call i32 @write(i32 %10, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 2)
%18 = icmp eq i32 %17, 2
br i1 %18, label %23, label %19
19: ; preds = %16
%20 = load i32, i32* @stderr
%21 = tail call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
%22 = tail call i32 @exit(i32 1)
unreachable
23: ; preds = %16
%24 = tail call i32 (...) @TELL_WAIT()
%25 = tail call i64 (...) @fork()
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %31
27: ; preds = %23
%28 = load i32, i32* @stderr
%29 = tail call i32 @fprintf(i32 %28, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0))
%30 = tail call i32 @exit(i32 1)
unreachable
31: ; preds = %23
%32 = icmp eq i64 %25, 0
br i1 %32, label %33, label %39
33: ; preds = %31
%34 = tail call i32 @lockabyte(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0), i32 %10, i32 0)
%35 = tail call i32 (...) @getppid()
%36 = tail call i32 @TELL_PARENT(i32 %35)
%37 = tail call i32 (...) @WAIT_PARENT()
%38 = 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 %44
39: ; preds = %31
%40 = tail call i32 @lockabyte(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0), i32 %10, i32 1)
%41 = tail call i32 @TELL_CHILD(i64 %25)
%42 = tail call i32 (...) @WAIT_CHILD()
%43 = 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 %44
44: ; preds = %33, %39
ret i32 0
}