#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 */

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
int atoi(char *);
int /*<<< orphan*/ err_sys(char *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ lockabyte(char *, int, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ sleep(int);
int main(int argc, char *argv[]) {
  int fd;
  if (argc != 3) {
    printf("Usage: flockdeadpair path child(0|1)\n");
    exit(-1);
  }
  if ((fd = open(argv[1], O_RDWR)) < 0)
    err_sys("open error");
  if (atoi(argv[2]) == 1) {
    lockabyte("child", fd, 0);
    sleep(1);
    lockabyte("child", fd, 1);
  } else {
    lockabyte("parent", fd, 1);
    sleep(1);
    lockabyte("parent", fd, 0);
  }
  sleep(1);
  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)
cmpl   $0x3,-0x8(%rbp)
je     11a6 <main+0x36>
lea    0xe69(%rip),%rdi
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e33(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    11d4 <main+0x64>
lea    0xe57(%rip),%rdi
call   1030 <err_sys@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1070 <atoi@plt>
cmp    $0x1,%eax
jne    121e <main+0xae>
mov    -0x14(%rbp),%esi
lea    0xe3d(%rip),%rdi
xor    %edx,%edx
call   1050 <lockabyte@plt>
mov    $0x1,%edi
call   1090 <sleep@plt>
mov    -0x14(%rbp),%esi
lea    0xe22(%rip),%rdi
mov    $0x1,%edx
call   1050 <lockabyte@plt>
jmp    124d <main+0xdd>
mov    -0x14(%rbp),%esi
lea    0xe0f(%rip),%rdi
mov    $0x1,%edx
call   1050 <lockabyte@plt>
mov    $0x1,%edi
call   1090 <sleep@plt>
mov    -0x14(%rbp),%esi
lea    0xdf1(%rip),%rdi
xor    %edx,%edx
call   1050 <lockabyte@plt>
mov    $0x1,%edi
call   1090 <sleep@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
cmp    $0x3,%edi
jne    1219 <main+0xa9>
mov    %rsi,%rbx
mov    0x8(%rsi),%rdi
mov    0x2e5e(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    11a3 <main+0x33>
lea    0xe62(%rip),%rdi
call   1040 <err_sys@plt>
mov    0x10(%rbx),%rdi
call   1070 <atoi@plt>
cmp    $0x1,%eax
jne    11da <main+0x6a>
lea    0xe53(%rip),%rbx
mov    %rbx,%rdi
mov    %ebp,%esi
xor    %edx,%edx
call   1050 <lockabyte@plt>
mov    $0x1,%edi
call   1090 <sleep@plt>
mov    %rbx,%rdi
mov    %ebp,%esi
mov    $0x1,%edx
jmp    1201 <main+0x91>
lea    0xe30(%rip),%rbx
mov    %rbx,%rdi
mov    %ebp,%esi
mov    $0x1,%edx
call   1050 <lockabyte@plt>
mov    $0x1,%edi
call   1090 <sleep@plt>
mov    %rbx,%rdi
mov    %ebp,%esi
xor    %edx,%edx
call   1050 <lockabyte@plt>
mov    $0x1,%edi
call   1090 <sleep@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xdf8(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [38 x i8] c"Usage: flockdeadpair path child(0|1)\0A\00"
@O_RDWR = global i32 0
@.str.1 = [11 x i8] c"open error\00"
@.str.2 = [6 x i8] c"child\00"
@.str.3 = [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
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%7 = load i32, i32* %4
%8 = icmp ne i32 %7, 3
br i1 %8, label %9, label %12
9: ; preds = %2
%10 = call i32 @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0))
%11 = call i32 @exit(i32 -1)
unreachable
12: ; preds = %2
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 1
%15 = load i8*, i8** %14
%16 = load i32, i32* @O_RDWR
%17 = call i32 @open(i8* %15, i32 %16)
store i32 %17, i32* %6
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %12
%20 = call i32 @err_sys(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
br label %21
21: ; preds = %19, %12
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i64 2
%24 = load i8*, i8** %23
%25 = call i32 @atoi(i8* %24)
%26 = icmp eq i32 %25, 1
br i1 %26, label %27, label %33
27: ; preds = %21
%28 = load i32, i32* %6
%29 = call i32 @lockabyte(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 %28, i32 0)
%30 = call i32 @sleep(i32 1)
%31 = load i32, i32* %6
%32 = call i32 @lockabyte(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 %31, i32 1)
br label %39
33: ; preds = %21
%34 = load i32, i32* %6
%35 = call i32 @lockabyte(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i32 %34, i32 1)
%36 = call i32 @sleep(i32 1)
%37 = load i32, i32* %6
%38 = call i32 @lockabyte(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i32 %37, i32 0)
br label %39
39: ; preds = %33, %27
%40 = call i32 @sleep(i32 1)
ret i32 0
}

LLVM IR (-O2):
@O_RDWR = global i32 0
@.str.1 = [11 x i8] c"open error\00"
@.str.2 = [6 x i8] c"child\00"
@.str.3 = [7 x i8] c"parent\00"
@str = [37 x i8] c"Usage: flockdeadpair path child(0|1)\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 3
br i1 %3, label %7, label %4
4: ; preds = %2
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @str, i64 0, i64 0))
%6 = tail call i32 @exit(i32 -1)
unreachable
7: ; preds = %2
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
%10 = load i32, i32* @O_RDWR
%11 = tail call i32 @open(i8* %9, i32 %10)
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %7
%14 = tail call i32 @err_sys(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
br label %15
15: ; preds = %13, %7
%16 = getelementptr inbounds i8*, i8** %1, i64 2
%17 = load i8*, i8** %16
%18 = tail call i32 @atoi(i8* %17)
%19 = icmp eq i32 %18, 1
br i1 %19, label %20, label %24
20: ; preds = %15
%21 = tail call i32 @lockabyte(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 %11, i32 0)
%22 = tail call i32 @sleep(i32 1)
%23 = tail call i32 @lockabyte(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 %11, i32 1)
br label %28
24: ; preds = %15
%25 = tail call i32 @lockabyte(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i32 %11, i32 1)
%26 = tail call i32 @sleep(i32 1)
%27 = tail call i32 @lockabyte(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i32 %11, i32 0)
br label %28
28: ; preds = %24, %20
%29 = tail call i32 @sleep(i32 1)
ret i32 0
}