#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 int pid_t;

/* Variables and functions */
int /*<<< orphan*/ MAP_NORESERVE;
int WEXITSTATUS(int);
scalar_t__ WIFEXITED(int);
int WTERMSIG(int);
int /*<<< orphan*/ do_dlopen(char *);
int /*<<< orphan*/ do_map(int /*<<< orphan*/);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
int waitpid(int, int *, int /*<<< orphan*/);
int main(int argc, const char *argv[]) {
  do_map(MAP_NORESERVE);
  do_dlopen("libm.so");
  do_map(0);
  do_map(0);
  do_map(MAP_NORESERVE);
  do_dlopen("liby.so");
  do_map(MAP_NORESERVE);
  do_map(0);
  do_map(0);
  do_map(MAP_NORESERVE);
  do_map(MAP_NORESERVE);
  do_dlopen("libz.so");
  do_map(MAP_NORESERVE);
  do_map(MAP_NORESERVE);
  do_map(0);
  pid_t pid = fork();
  if (pid == -1) {
    perror("fork");
    exit(1);
  }
  if (pid == 0) {
    do_map(MAP_NORESERVE);
    do_map(0);
    do_map(0);
    do_dlopen("libw.so");
    do_map(0);
    do_map(MAP_NORESERVE);
    do_map(MAP_NORESERVE);
    do_map(0);
    printf("CHILD: PASSED\n");
    fflush(stdout);
    return 0;
  }
  int status;
  if (waitpid(pid, &status, 0) != pid) {
    perror("waitpid");
  } else if ((WIFEXITED(status) != 0) && (WEXITSTATUS(status) == 0)) {
    printf("PASSED\n");
  } else {
    fprintf(stderr, "FAILED: child exited with unexpected status %s %d\n",
            WIFEXITED(status) ? "exit" : "signal",
            WIFEXITED(status) ? WEXITSTATUS(status) : WTERMSIG(status));
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e0b(%rip),%rax
mov    (%rax),%edi
call   1070 <do_map@plt>
lea    0xe15(%rip),%rdi
call   10e0 <do_dlopen@plt>
xor    %edi,%edi
call   1070 <do_map@plt>
xor    %edi,%edi
call   1070 <do_map@plt>
mov    0x2de3(%rip),%rax
mov    (%rax),%edi
call   1070 <do_map@plt>
lea    0xdf5(%rip),%rdi
call   10e0 <do_dlopen@plt>
mov    0x2dc9(%rip),%rax
mov    (%rax),%edi
call   1070 <do_map@plt>
xor    %edi,%edi
call   1070 <do_map@plt>
xor    %edi,%edi
call   1070 <do_map@plt>
mov    0x2dad(%rip),%rax
mov    (%rax),%edi
call   1070 <do_map@plt>
mov    0x2d9f(%rip),%rax
mov    (%rax),%edi
call   1070 <do_map@plt>
lea    0xdb9(%rip),%rdi
call   10e0 <do_dlopen@plt>
mov    0x2d85(%rip),%rax
mov    (%rax),%edi
call   1070 <do_map@plt>
mov    0x2d77(%rip),%rax
mov    (%rax),%edi
call   1070 <do_map@plt>
xor    %edi,%edi
call   1070 <do_map@plt>
mov    $0x0,%al
call   10d0 <fork@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    12a9 <main+0xe9>
lea    0xd7e(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
cmpl   $0x0,-0x14(%rbp)
jne    132b <main+0x16b>
mov    0x2d2e(%rip),%rax
mov    (%rax),%edi
call   1070 <do_map@plt>
xor    %edi,%edi
call   1070 <do_map@plt>
xor    %edi,%edi
call   1070 <do_map@plt>
lea    0xd47(%rip),%rdi
call   10e0 <do_dlopen@plt>
xor    %edi,%edi
call   1070 <do_map@plt>
mov    0x2cff(%rip),%rax
mov    (%rax),%edi
call   1070 <do_map@plt>
mov    0x2cf1(%rip),%rax
mov    (%rax),%edi
call   1070 <do_map@plt>
xor    %edi,%edi
call   1070 <do_map@plt>
lea    0xd19(%rip),%rdi
call   1030 <printf@plt>
mov    0x2cb8(%rip),%rax
mov    (%rax),%edi
call   1050 <fflush@plt>
movl   $0x0,-0x4(%rbp)
jmp    1407 <main+0x247>
mov    -0x14(%rbp),%edi
lea    -0x18(%rbp),%rsi
xor    %edx,%edx
call   1090 <waitpid@plt>
cmp    -0x14(%rbp),%eax
je     1353 <main+0x193>
lea    0xceb(%rip),%rdi
call   10a0 <perror@plt>
jmp    1400 <main+0x240>
mov    -0x18(%rbp),%edi
call   10b0 <WIFEXITED@plt>
cmp    $0x0,%rax
je     1387 <main+0x1c7>
mov    -0x18(%rbp),%edi
call   1080 <WEXITSTATUS@plt>
cmp    $0x0,%eax
jne    1387 <main+0x1c7>
lea    0xcaf(%rip),%rdi
call   1030 <printf@plt>
jmp    13fb <main+0x23b>
mov    0x2c4a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x18(%rbp),%edi
call   10b0 <WIFEXITED@plt>
mov    %rax,%rdx
lea    0xccf(%rip),%rax
lea    0xcc3(%rip),%rcx
cmp    $0x0,%rdx
cmovne %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%edi
call   10b0 <WIFEXITED@plt>
cmp    $0x0,%rax
je     13da <main+0x21a>
mov    -0x18(%rbp),%edi
call   1080 <WEXITSTATUS@plt>
mov    %eax,-0x28(%rbp)
jmp    13e5 <main+0x225>
mov    -0x18(%rbp),%edi
call   1060 <WTERMSIG@plt>
mov    %eax,-0x28(%rbp)
mov    -0x20(%rbp),%rdx
mov    -0x24(%rbp),%edi
mov    -0x28(%rbp),%ecx
lea    0xc46(%rip),%rsi
call   1040 <fprintf@plt>
jmp    1400 <main+0x240>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
mov    0x2e1e(%rip),%rbp
mov    0x0(%rbp),%edi
call   1070 <do_map@plt>
lea    0xe27(%rip),%rdi
call   10e0 <do_dlopen@plt>
xor    %edi,%edi
call   1070 <do_map@plt>
xor    %edi,%edi
call   1070 <do_map@plt>
mov    0x0(%rbp),%edi
call   1070 <do_map@plt>
lea    0xe0d(%rip),%rdi
call   10e0 <do_dlopen@plt>
mov    0x0(%rbp),%edi
call   1070 <do_map@plt>
xor    %edi,%edi
call   1070 <do_map@plt>
xor    %edi,%edi
call   1070 <do_map@plt>
mov    0x0(%rbp),%edi
call   1070 <do_map@plt>
mov    0x0(%rbp),%edi
call   1070 <do_map@plt>
lea    0xde3(%rip),%rdi
call   10e0 <do_dlopen@plt>
mov    0x0(%rbp),%edi
call   1070 <do_map@plt>
mov    0x0(%rbp),%edi
call   1070 <do_map@plt>
xor    %edi,%edi
call   1070 <do_map@plt>
xor    %eax,%eax
call   10d0 <fork@plt>
test   %eax,%eax
je     12d8 <main+0x118>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     1371 <main+0x1b1>
lea    0x4(%rsp),%rsi
mov    %ebx,%edi
xor    %edx,%edx
call   1090 <waitpid@plt>
cmp    %ebx,%eax
jne    135c <main+0x19c>
mov    0x4(%rsp),%edi
call   10b0 <WIFEXITED@plt>
test   %rax,%rax
je     1298 <main+0xd8>
mov    0x4(%rsp),%edi
call   1080 <WEXITSTATUS@plt>
test   %eax,%eax
je     134e <main+0x18e>
mov    0x2d39(%rip),%rax
mov    (%rax),%ebp
mov    0x4(%rsp),%edi
call   10b0 <WIFEXITED@plt>
test   %rax,%rax
lea    0xdb1(%rip),%rax
lea    0xda5(%rip),%rbx
cmove  %rax,%rbx
mov    0x4(%rsp),%edi
call   10b0 <WIFEXITED@plt>
mov    0x4(%rsp),%edi
test   %rax,%rax
je     1334 <main+0x174>
call   1080 <WEXITSTATUS@plt>
jmp    1339 <main+0x179>
mov    0x0(%rbp),%edi
call   1070 <do_map@plt>
xor    %edi,%edi
call   1070 <do_map@plt>
xor    %edi,%edi
call   1070 <do_map@plt>
lea    0xd28(%rip),%rdi
call   10e0 <do_dlopen@plt>
xor    %edi,%edi
call   1070 <do_map@plt>
mov    0x0(%rbp),%edi
call   1070 <do_map@plt>
mov    0x0(%rbp),%edi
call   1070 <do_map@plt>
xor    %edi,%edi
call   1070 <do_map@plt>
lea    0xd4d(%rip),%rdi
call   1030 <puts@plt>
mov    0x2ca5(%rip),%rax
mov    (%rax),%edi
call   1050 <fflush@plt>
jmp    1368 <main+0x1a8>
call   1060 <WTERMSIG@plt>
lea    0xced(%rip),%rsi
mov    %ebp,%edi
mov    %rbx,%rdx
mov    %eax,%ecx
call   1040 <fprintf@plt>
jmp    1368 <main+0x1a8>
lea    0xd1e(%rip),%rdi
call   1030 <puts@plt>
jmp    1368 <main+0x1a8>
lea    0xcc2(%rip),%rdi
call   10a0 <perror@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xca0(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
@MAP_NORESERVE = global i32 0
@.str = [8 x i8] c"libm.so\00"
@.str.1 = [8 x i8] c"liby.so\00"
@.str.2 = [8 x i8] c"libz.so\00"
@.str.3 = [5 x i8] c"fork\00"
@.str.4 = [8 x i8] c"libw.so\00"
@.str.5 = [15 x i8] c"CHILD: PASSED\0A\00"
@stdout = global i32 0
@.str.6 = [8 x i8] c"waitpid\00"
@.str.7 = [8 x i8] c"PASSED\0A\00"
@stderr = global i32 0
@.str.8 = [51 x i8] c"FAILED: child exited with unexpected status %s %d\0A\00"
@.str.9 = [5 x i8] c"exit\00"
@.str.10 = [7 x i8] c"signal\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = load i32, i32* @MAP_NORESERVE
%9 = call i32 @do_map(i32 %8)
%10 = call i32 @do_dlopen(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%11 = call i32 @do_map(i32 0)
%12 = call i32 @do_map(i32 0)
%13 = load i32, i32* @MAP_NORESERVE
%14 = call i32 @do_map(i32 %13)
%15 = call i32 @do_dlopen(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%16 = load i32, i32* @MAP_NORESERVE
%17 = call i32 @do_map(i32 %16)
%18 = call i32 @do_map(i32 0)
%19 = call i32 @do_map(i32 0)
%20 = load i32, i32* @MAP_NORESERVE
%21 = call i32 @do_map(i32 %20)
%22 = load i32, i32* @MAP_NORESERVE
%23 = call i32 @do_map(i32 %22)
%24 = call i32 @do_dlopen(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
%25 = load i32, i32* @MAP_NORESERVE
%26 = call i32 @do_map(i32 %25)
%27 = load i32, i32* @MAP_NORESERVE
%28 = call i32 @do_map(i32 %27)
%29 = call i32 @do_map(i32 0)
%30 = call i32 (...) @fork()
store i32 %30, i32* %6
%31 = load i32, i32* %6
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %36
33: ; preds = %2
%34 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%35 = call i32 @exit(i32 1)
unreachable
36: ; preds = %2
%37 = load i32, i32* %6
%38 = icmp eq i32 %37, 0
br i1 %38, label %39, label %54
39: ; preds = %36
%40 = load i32, i32* @MAP_NORESERVE
%41 = call i32 @do_map(i32 %40)
%42 = call i32 @do_map(i32 0)
%43 = call i32 @do_map(i32 0)
%44 = call i32 @do_dlopen(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0))
%45 = call i32 @do_map(i32 0)
%46 = load i32, i32* @MAP_NORESERVE
%47 = call i32 @do_map(i32 %46)
%48 = load i32, i32* @MAP_NORESERVE
%49 = call i32 @do_map(i32 %48)
%50 = call i32 @do_map(i32 0)
%51 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0))
%52 = load i32, i32* @stdout
%53 = call i32 @fflush(i32 %52)
store i32 0, i32* %3
br label %92
54: ; preds = %36
%55 = load i32, i32* %6
%56 = call i32 @waitpid(i32 %55, i32* %7, i32 0)
%57 = load i32, i32* %6
%58 = icmp ne i32 %56, %57
br i1 %58, label %59, label %61
59: ; preds = %54
%60 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.6, i64 0, i64 0))
br label %91
61: ; preds = %54
%62 = load i32, i32* %7
%63 = call i64 @WIFEXITED(i32 %62)
%64 = icmp ne i64 %63, 0
br i1 %64, label %65, label %71
65: ; preds = %61
%66 = load i32, i32* %7
%67 = call i32 @WEXITSTATUS(i32 %66)
%68 = icmp eq i32 %67, 0
br i1 %68, label %69, label %71
69: ; preds = %65
%70 = call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.7, i64 0, i64 0))
br label %90
71: ; preds = %65, %61
%72 = load i32, i32* @stderr
%73 = load i32, i32* %7
%74 = call i64 @WIFEXITED(i32 %73)
%75 = icmp ne i64 %74, 0
%76 = zext i1 %75 to i64
%77 = select i1 %75, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.9, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.10, i64 0, i64 0)
%78 = load i32, i32* %7
%79 = call i64 @WIFEXITED(i32 %78)
%80 = icmp ne i64 %79, 0
br i1 %80, label %81, label %84
81: ; preds = %71
%82 = load i32, i32* %7
%83 = call i32 @WEXITSTATUS(i32 %82)
br label %87
84: ; preds = %71
%85 = load i32, i32* %7
%86 = call i32 @WTERMSIG(i32 %85)
br label %87
87: ; preds = %84, %81
%88 = phi i32 [ %83, %81 ], [ %86, %84 ]
%89 = call i32 @fprintf(i32 %72, i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.8, i64 0, i64 0), i8* %77, i32 %88)
br label %90
90: ; preds = %87, %69
br label %91
91: ; preds = %90, %59
store i32 0, i32* %3
br label %92
92: ; preds = %91, %39
%93 = load i32, i32* %3
ret i32 %93
}

LLVM IR (-O2):
@MAP_NORESERVE = global i32 0
@.str = [8 x i8] c"libm.so\00"
@.str.1 = [8 x i8] c"liby.so\00"
@.str.2 = [8 x i8] c"libz.so\00"
@.str.3 = [5 x i8] c"fork\00"
@.str.4 = [8 x i8] c"libw.so\00"
@stdout = global i32 0
@.str.6 = [8 x i8] c"waitpid\00"
@stderr = global i32 0
@.str.8 = [51 x i8] c"FAILED: child exited with unexpected status %s %d\0A\00"
@.str.9 = [5 x i8] c"exit\00"
@.str.10 = [7 x i8] c"signal\00"
@str = [7 x i8] c"PASSED\00"
@str.11 = [14 x i8] c"CHILD: PASSED\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i32
%4 = load i32, i32* @MAP_NORESERVE
%5 = tail call i32 @do_map(i32 %4)
%6 = tail call i32 @do_dlopen(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%7 = tail call i32 @do_map(i32 0)
%8 = tail call i32 @do_map(i32 0)
%9 = load i32, i32* @MAP_NORESERVE
%10 = tail call i32 @do_map(i32 %9)
%11 = tail call i32 @do_dlopen(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%12 = load i32, i32* @MAP_NORESERVE
%13 = tail call i32 @do_map(i32 %12)
%14 = tail call i32 @do_map(i32 0)
%15 = tail call i32 @do_map(i32 0)
%16 = load i32, i32* @MAP_NORESERVE
%17 = tail call i32 @do_map(i32 %16)
%18 = load i32, i32* @MAP_NORESERVE
%19 = tail call i32 @do_map(i32 %18)
%20 = tail call i32 @do_dlopen(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
%21 = load i32, i32* @MAP_NORESERVE
%22 = tail call i32 @do_map(i32 %21)
%23 = load i32, i32* @MAP_NORESERVE
%24 = tail call i32 @do_map(i32 %23)
%25 = tail call i32 @do_map(i32 0)
%26 = tail call i32 (...) @fork()
switch i32 %26, label %45 [
i32 -1, label %27
i32 0, label %30
]
27: ; preds = %2
%28 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%29 = tail call i32 @exit(i32 1)
unreachable
30: ; preds = %2
%31 = load i32, i32* @MAP_NORESERVE
%32 = tail call i32 @do_map(i32 %31)
%33 = tail call i32 @do_map(i32 0)
%34 = tail call i32 @do_map(i32 0)
%35 = tail call i32 @do_dlopen(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0))
%36 = tail call i32 @do_map(i32 0)
%37 = load i32, i32* @MAP_NORESERVE
%38 = tail call i32 @do_map(i32 %37)
%39 = load i32, i32* @MAP_NORESERVE
%40 = tail call i32 @do_map(i32 %39)
%41 = tail call i32 @do_map(i32 0)
%42 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str.11, i64 0, i64 0))
%43 = load i32, i32* @stdout
%44 = tail call i32 @fflush(i32 %43)
br label %79
45: ; preds = %2
%46 = bitcast i32* %3 to i8*
%47 = call i32 @waitpid(i32 %26, i32* %3, i32 0)
%48 = icmp eq i32 %47, %26
br i1 %48, label %51, label %49
49: ; preds = %45
%50 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.6, i64 0, i64 0))
br label %78
51: ; preds = %45
%52 = load i32, i32* %3
%53 = call i64 @WIFEXITED(i32 %52)
%54 = icmp eq i64 %53, 0
br i1 %54, label %61, label %55
55: ; preds = %51
%56 = load i32, i32* %3
%57 = call i32 @WEXITSTATUS(i32 %56)
%58 = icmp eq i32 %57, 0
br i1 %58, label %59, label %61
59: ; preds = %55
%60 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @str, i64 0, i64 0))
br label %78
61: ; preds = %55, %51
%62 = load i32, i32* @stderr
%63 = load i32, i32* %3
%64 = call i64 @WIFEXITED(i32 %63)
%65 = icmp eq i64 %64, 0
%66 = select i1 %65, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.10, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.9, i64 0, i64 0)
%67 = load i32, i32* %3
%68 = call i64 @WIFEXITED(i32 %67)
%69 = icmp eq i64 %68, 0
%70 = load i32, i32* %3
br i1 %69, label %73, label %71
71: ; preds = %61
%72 = call i32 @WEXITSTATUS(i32 %70)
br label %75
73: ; preds = %61
%74 = call i32 @WTERMSIG(i32 %70)
br label %75
75: ; preds = %73, %71
%76 = phi i32 [ %72, %71 ], [ %74, %73 ]
%77 = call i32 @fprintf(i32 %62, i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.8, i64 0, i64 0), i8* %66, i32 %76)
br label %78
78: ; preds = %59, %75, %49
br label %79
79: ; preds = %78, %30
ret i32 0
}