#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 /*<<< orphan*/);
int /*<<< orphan*/ wait_for_child();
int /*<<< orphan*/ write_pipe(int *);
void test_child_exit() {
  printf("testing child exit...\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_WRLCK, 0, 100);
    write_pipe(pipes[0]);
    read_pipe(pipes[1]);
    exit(0);
  }
  read_pipe(pipes[0]);
  lock_fail(F_RDLCK, 0, 100);
  write_pipe(pipes[1]);
  lock_wait_ok(F_RDLCK, 0, 100);
  wait_for_child();
  close_pipes(pipes);
}


x86 assembly (clang, -O0):
<test_child_exit>:
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_child_exit+0x4d>
mov    $0x1,%edi
lea    0xdff(%rip),%rsi
call   1070 <err@plt>
cmpq   $0x0,-0x18(%rbp)
jne    125a <test_child_exit+0x8a>
mov    0x2db1(%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
call   10a0 <exit@plt>
lea    -0x10(%rbp),%rdi
call   10b0 <read_pipe@plt>
mov    0x2d66(%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    0x2d44(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
mov    $0x64,%edx
call   10e0 <lock_wait_ok@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_exit>:
push   %r14
push   %rbx
sub    $0x18,%rsp
lea    0xe27(%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     1230 <test_child_exit+0x60>
jne    1241 <test_child_exit+0x71>
mov    0x2dd7(%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),%rdi
call   10b0 <read_pipe@plt>
xor    %edi,%edi
call   10a0 <exit@plt>
lea    0xdc9(%rip),%rsi
mov    $0x1,%edi
call   1070 <err@plt>
mov    %rsp,%r14
mov    %r14,%rdi
call   10b0 <read_pipe@plt>
mov    0x2d7d(%rip),%rbx
mov    (%rbx),%edi
xor    %esi,%esi
mov    $0x64,%edx
call   1080 <lock_fail@plt>
lea    0x8(%rsp),%rdi
call   1090 <write_pipe@plt>
mov    (%rbx),%edi
xor    %esi,%esi
mov    $0x64,%edx
call   10e0 <lock_wait_ok@plt>
xor    %eax,%eax
call   10d0 <wait_for_child@plt>
mov    %r14,%rdi
call   10c0 <close_pipes@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [23 x i8] c"testing child exit...\0A\00"
@.str.1 = [5 x i8] c"fork\00"
@F_WRLCK = global i32 0
@F_RDLCK = global i32 0
define void @test_child_exit() {
%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 = 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 %26
16: ; preds = %13
%17 = load i32, i32* @F_WRLCK
%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 @exit(i32 0)
unreachable
26: ; preds = %13
%27 = getelementptr inbounds [2 x [2 x i32]], [2 x [2 x i32]]* %1, i64 0, i64 0
%28 = getelementptr inbounds [2 x i32], [2 x i32]* %27, i64 0, i64 0
%29 = call i32 @read_pipe(i32* %28)
%30 = load i32, i32* @F_RDLCK
%31 = call i32 @lock_fail(i32 %30, i32 0, i32 100)
%32 = getelementptr inbounds [2 x [2 x i32]], [2 x [2 x i32]]* %1, i64 0, i64 1
%33 = getelementptr inbounds [2 x i32], [2 x i32]* %32, i64 0, i64 0
%34 = call i32 @write_pipe(i32* %33)
%35 = load i32, i32* @F_RDLCK
%36 = call i32 @lock_wait_ok(i32 %35, i32 0, i32 100)
%37 = call i32 (...) @wait_for_child()
%38 = getelementptr inbounds [2 x [2 x i32]], [2 x [2 x i32]]* %1, i64 0, i64 0
%39 = bitcast [2 x i32]* %38 to i32**
%40 = call i32 @close_pipes(i32** %39)
ret void
}

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