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


x86 assembly (clang, -O0):
<test_child_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    0x2dd3(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
mov    $0x64,%edx
call   1040 <lock@plt>
mov    $0x0,%al
call   10f0 <fork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jge    1232 <test_child_wait+0x62>
mov    $0x1,%edi
lea    0xdea(%rip),%rsi
call   1070 <err@plt>
cmpq   $0x0,-0x18(%rbp)
jne    128c <test_child_wait+0xbc>
mov    0x2d8c(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
mov    $0x64,%edx
call   1040 <lock@plt>
mov    0x2d87(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
mov    $0x64,%edx
call   1080 <lock_fail@plt>
lea    -0x10(%rbp),%rdi
call   1090 <write_pipe@plt>
mov    0x2d69(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
mov    $0x64,%edx
call   10e0 <lock_wait_ok@plt>
xor    %edi,%edi
call   10a0 <exit@plt>
lea    -0x10(%rbp),%rdi
call   10b0 <read_pipe@plt>
xor    %edi,%edi
mov    $0x64,%esi
call   1030 <unlock@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_child_wait>:
push   %rbx
sub    $0x10,%rsp
lea    0xe29(%rip),%rdi
call   1040 <puts@plt>
xor    %edi,%edi
xor    %esi,%esi
call   1030 <unlock@plt>
mov    %rsp,%rdi
call   1060 <open_pipes@plt>
mov    0x2dd7(%rip),%rbx
mov    (%rbx),%edi
xor    %esi,%esi
mov    $0x64,%edx
call   1050 <lock@plt>
xor    %eax,%eax
call   10f0 <fork@plt>
test   %rax,%rax
js     1255 <test_child_wait+0x85>
jne    1266 <test_child_wait+0x96>
mov    (%rbx),%edi
xor    %esi,%esi
mov    $0x64,%edx
call   1050 <lock@plt>
mov    0x2db6(%rip),%rbx
mov    (%rbx),%edi
xor    %esi,%esi
mov    $0x64,%edx
call   1080 <lock_fail@plt>
mov    %rsp,%rdi
call   1090 <write_pipe@plt>
mov    (%rbx),%edi
xor    %esi,%esi
mov    $0x64,%edx
call   10e0 <lock_wait_ok@plt>
xor    %edi,%edi
call   10a0 <exit@plt>
lea    0xda4(%rip),%rsi
mov    $0x1,%edi
call   1070 <err@plt>
mov    %rsp,%rbx
mov    %rbx,%rdi
call   10b0 <read_pipe@plt>
xor    %edi,%edi
mov    $0x64,%esi
call   1030 <unlock@plt>
xor    %eax,%eax
call   10d0 <wait_for_child@plt>
mov    %rbx,%rdi
call   10c0 <close_pipes@plt>
add    $0x10,%rsp
pop    %rbx
ret

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

LLVM IR (-O2):
@F_RDLCK = global i32 0
@.str.1 = [5 x i8] c"fork\00"
@F_WRLCK = global i32 0
@str = [22 x i8] c"testing child wait...\00"
define void @test_child_wait() {
%1 = alloca [2 x [2 x i32]]
%2 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 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 = load i32, i32* @F_RDLCK
%8 = call i32 @lock(i32 %7, i32 0, i32 100)
%9 = call i64 (...) @fork()
%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 %25
13: ; preds = %0
%14 = icmp eq i64 %9, 0
br i1 %14, label %15, label %25
15: ; preds = %13
%16 = load i32, i32* @F_RDLCK
%17 = call i32 @lock(i32 %16, i32 0, i32 100)
%18 = load i32, i32* @F_WRLCK
%19 = call i32 @lock_fail(i32 %18, i32 0, i32 100)
%20 = getelementptr inbounds [2 x [2 x i32]], [2 x [2 x i32]]* %1, i64 0, i64 0, i64 0
%21 = call i32 @write_pipe(i32* %20)
%22 = load i32, i32* @F_WRLCK
%23 = call i32 @lock_wait_ok(i32 %22, i32 0, i32 100)
%24 = call i32 @exit(i32 0)
unreachable
25: ; preds = %11, %13
%26 = getelementptr inbounds [2 x [2 x i32]], [2 x [2 x i32]]* %1, i64 0, i64 0, i64 0
%27 = call i32 @read_pipe(i32* %26)
%28 = call i32 @unlock(i32 0, i32 100)
%29 = call i32 (...) @wait_for_child()
%30 = call i32 @close_pipes(i32** %5)
ret void
}