#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*/ F_RDLCK;
int /*<<< orphan*/ F_WRLCK;
int /*<<< orphan*/ close_pipes(int **);
int /*<<< orphan*/ err(int, char *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ lock(int /*<<< orphan*/, int /*<<< orphan*/, int);
int /*<<< orphan*/ lock_fail(int /*<<< orphan*/, int /*<<< orphan*/, int);
int /*<<< orphan*/ lock_wait_ok(int /*<<< orphan*/, int /*<<< orphan*/, int);
int /*<<< orphan*/ open_pipes(int **);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ read_pipe(int *);
int /*<<< orphan*/ unlock(int /*<<< orphan*/, int);
int /*<<< orphan*/ wait_for_child();
int /*<<< orphan*/ write_pipe(int *);
void test_parent_wait() {
  printf("testing parent wait...\n");
  unlock(0, 0);
  int pipes[2][2];
  open_pipes(pipes);
  pid_t pid = fork();
  if (pid < 0)
    err(1, "fork");
  if (pid == 0) {
    lock(F_RDLCK, 0, 100);
    write_pipe(pipes[0]);
    read_pipe(pipes[1]);
    unlock(0, 100);
    read_pipe(pipes[1]);
    exit(0);
  }
  read_pipe(pipes[0]);
  lock(F_RDLCK, 0, 100);
  lock_fail(F_WRLCK, 0, 100);
  write_pipe(pipes[1]);
  lock_wait_ok(F_WRLCK, 0, 100);
  write_pipe(pipes[1]);
  wait_for_child();
  close_pipes(pipes);
}


x86 assembly (clang, -O0):
<test_parent_wait>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    0xe21(%rip),%rdi
call   1050 <printf@plt>
xor    %esi,%esi
mov    %esi,%edi
call   1030 <unlock@plt>
lea    -0x10(%rbp),%rdi
call   1060 <open_pipes@plt>
mov    $0x0,%al
call   10f0 <fork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jge    121d <test_parent_wait+0x4d>
mov    $0x1,%edi
lea    0xe00(%rip),%rsi
call   1070 <err@plt>
cmpq   $0x0,-0x18(%rbp)
jne    1273 <test_parent_wait+0xa3>
mov    0x2da1(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
mov    $0x64,%edx
call   1040 <lock@plt>
lea    -0x10(%rbp),%rdi
call   1090 <write_pipe@plt>
lea    -0x10(%rbp),%rdi
add    $0x8,%rdi
call   10b0 <read_pipe@plt>
xor    %edi,%edi
mov    $0x64,%esi
call   1030 <unlock@plt>
lea    -0x10(%rbp),%rdi
add    $0x8,%rdi
call   10b0 <read_pipe@plt>
xor    %edi,%edi
call   10a0 <exit@plt>
lea    -0x10(%rbp),%rdi
call   10b0 <read_pipe@plt>
mov    0x2d4d(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
mov    $0x64,%edx
call   1040 <lock@plt>
mov    0x2d48(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
mov    $0x64,%edx
call   1080 <lock_fail@plt>
lea    -0x10(%rbp),%rdi
add    $0x8,%rdi
call   1090 <write_pipe@plt>
mov    0x2d26(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
mov    $0x64,%edx
call   10e0 <lock_wait_ok@plt>
lea    -0x10(%rbp),%rdi
add    $0x8,%rdi
call   1090 <write_pipe@plt>
mov    $0x0,%al
call   10d0 <wait_for_child@plt>
lea    -0x10(%rbp),%rdi
call   10c0 <close_pipes@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_parent_wait>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
lea    0xe25(%rip),%rdi
call   1040 <puts@plt>
xor    %edi,%edi
xor    %esi,%esi
call   1030 <unlock@plt>
mov    %rsp,%rdi
call   1060 <open_pipes@plt>
xor    %eax,%eax
call   10f0 <fork@plt>
test   %rax,%rax
js     1249 <test_parent_wait+0x79>
jne    125a <test_parent_wait+0x8a>
mov    0x2dc5(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
mov    $0x64,%edx
call   1050 <lock@plt>
mov    %rsp,%rdi
call   1090 <write_pipe@plt>
lea    0x8(%rsp),%rbx
mov    %rbx,%rdi
call   10b0 <read_pipe@plt>
xor    %edi,%edi
mov    $0x64,%esi
call   1030 <unlock@plt>
mov    %rbx,%rdi
call   10b0 <read_pipe@plt>
xor    %edi,%edi
call   10a0 <exit@plt>
lea    0xdb0(%rip),%rsi
mov    $0x1,%edi
call   1070 <err@plt>
mov    %rsp,%r14
mov    %r14,%rdi
call   10b0 <read_pipe@plt>
mov    0x2d64(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
mov    $0x64,%edx
call   1050 <lock@plt>
mov    0x2d5f(%rip),%r15
mov    (%r15),%edi
xor    %esi,%esi
mov    $0x64,%edx
call   1080 <lock_fail@plt>
lea    0x8(%rsp),%rbx
mov    %rbx,%rdi
call   1090 <write_pipe@plt>
mov    (%r15),%edi
xor    %esi,%esi
mov    $0x64,%edx
call   10e0 <lock_wait_ok@plt>
mov    %rbx,%rdi
call   1090 <write_pipe@plt>
xor    %eax,%eax
call   10d0 <wait_for_child@plt>
mov    %r14,%rdi
call   10c0 <close_pipes@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [24 x i8] c"testing parent wait...\0A\00"
@.str.1 = [5 x i8] c"fork\00"
@F_RDLCK = global i32 0
@F_WRLCK = global i32 0
define void @test_parent_wait() {
%1 = alloca [2 x [2 x i32]]
%2 = alloca i64
%3 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
%4 = call i32 @unlock(i32 0, i32 0)
%5 = getelementptr inbounds [2 x [2 x i32]], [2 x [2 x i32]]* %1, i64 0, i64 0
%6 = bitcast [2 x i32]* %5 to i32**
%7 = call i32 @open_pipes(i32** %6)
%8 = call i64 (...) @fork()
store i64 %8, i64* %2
%9 = load i64, i64* %2
%10 = icmp slt i64 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %0
%12 = call i32 @err(i32 1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %13
13: ; preds = %11, %0
%14 = load i64, i64* %2
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %30
16: ; preds = %13
%17 = load i32, i32* @F_RDLCK
%18 = call i32 @lock(i32 %17, i32 0, i32 100)
%19 = getelementptr inbounds [2 x [2 x i32]], [2 x [2 x i32]]* %1, i64 0, i64 0
%20 = getelementptr inbounds [2 x i32], [2 x i32]* %19, i64 0, i64 0
%21 = call i32 @write_pipe(i32* %20)
%22 = getelementptr inbounds [2 x [2 x i32]], [2 x [2 x i32]]* %1, i64 0, i64 1
%23 = getelementptr inbounds [2 x i32], [2 x i32]* %22, i64 0, i64 0
%24 = call i32 @read_pipe(i32* %23)
%25 = call i32 @unlock(i32 0, i32 100)
%26 = getelementptr inbounds [2 x [2 x i32]], [2 x [2 x i32]]* %1, i64 0, i64 1
%27 = getelementptr inbounds [2 x i32], [2 x i32]* %26, i64 0, i64 0
%28 = call i32 @read_pipe(i32* %27)
%29 = call i32 @exit(i32 0)
unreachable
30: ; preds = %13
%31 = getelementptr inbounds [2 x [2 x i32]], [2 x [2 x i32]]* %1, i64 0, i64 0
%32 = getelementptr inbounds [2 x i32], [2 x i32]* %31, i64 0, i64 0
%33 = call i32 @read_pipe(i32* %32)
%34 = load i32, i32* @F_RDLCK
%35 = call i32 @lock(i32 %34, i32 0, i32 100)
%36 = load i32, i32* @F_WRLCK
%37 = call i32 @lock_fail(i32 %36, i32 0, i32 100)
%38 = getelementptr inbounds [2 x [2 x i32]], [2 x [2 x i32]]* %1, i64 0, i64 1
%39 = getelementptr inbounds [2 x i32], [2 x i32]* %38, i64 0, i64 0
%40 = call i32 @write_pipe(i32* %39)
%41 = load i32, i32* @F_WRLCK
%42 = call i32 @lock_wait_ok(i32 %41, i32 0, i32 100)
%43 = getelementptr inbounds [2 x [2 x i32]], [2 x [2 x i32]]* %1, i64 0, i64 1
%44 = getelementptr inbounds [2 x i32], [2 x i32]* %43, i64 0, i64 0
%45 = call i32 @write_pipe(i32* %44)
%46 = call i32 (...) @wait_for_child()
%47 = getelementptr inbounds [2 x [2 x i32]], [2 x [2 x i32]]* %1, i64 0, i64 0
%48 = bitcast [2 x i32]* %47 to i32**
%49 = call i32 @close_pipes(i32** %48)
ret void
}

LLVM IR (-O2):
@.str.1 = [5 x i8] c"fork\00"
@F_RDLCK = global i32 0
@F_WRLCK = global i32 0
@str = [23 x i8] c"testing parent wait...\00"
define void @test_parent_wait() {
%1 = alloca [2 x [2 x i32]]
%2 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str, i64 0, i64 0))
%3 = tail call i32 @unlock(i32 0, i32 0)
%4 = bitcast [2 x [2 x i32]]* %1 to i8*
%5 = bitcast [2 x [2 x i32]]* %1 to i32**
%6 = call i32 @open_pipes(i32** %5)
%7 = call i64 (...) @fork()
%8 = icmp slt i64 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %0
%10 = call i32 @err(i32 1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %23
11: ; preds = %0
%12 = icmp eq i64 %7, 0
br i1 %12, label %13, label %23
13: ; preds = %11
%14 = load i32, i32* @F_RDLCK
%15 = call i32 @lock(i32 %14, i32 0, i32 100)
%16 = getelementptr inbounds [2 x [2 x i32]], [2 x [2 x i32]]* %1, i64 0, i64 0, i64 0
%17 = call i32 @write_pipe(i32* %16)
%18 = getelementptr inbounds [2 x [2 x i32]], [2 x [2 x i32]]* %1, i64 0, i64 1, i64 0
%19 = call i32 @read_pipe(i32* %18)
%20 = call i32 @unlock(i32 0, i32 100)
%21 = call i32 @read_pipe(i32* %18)
%22 = call i32 @exit(i32 0)
unreachable
23: ; preds = %9, %11
%24 = getelementptr inbounds [2 x [2 x i32]], [2 x [2 x i32]]* %1, i64 0, i64 0, i64 0
%25 = call i32 @read_pipe(i32* %24)
%26 = load i32, i32* @F_RDLCK
%27 = call i32 @lock(i32 %26, i32 0, i32 100)
%28 = load i32, i32* @F_WRLCK
%29 = call i32 @lock_fail(i32 %28, i32 0, i32 100)
%30 = getelementptr inbounds [2 x [2 x i32]], [2 x [2 x i32]]* %1, i64 0, i64 1, i64 0
%31 = call i32 @write_pipe(i32* %30)
%32 = load i32, i32* @F_WRLCK
%33 = call i32 @lock_wait_ok(i32 %32, i32 0, i32 100)
%34 = call i32 @write_pipe(i32* %30)
%35 = call i32 (...) @wait_for_child()
%36 = call i32 @close_pipes(i32** %5)
ret void
}