#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*/ assert(int);
scalar_t__ find_dynamo_library();
scalar_t__ fork();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
scalar_t__ waitpid(scalar_t__, int /*<<< orphan*/ *, int /*<<< orphan*/);
int main(int argc, char **argv) {
  pid_t child;
  if (find_dynamo_library())
    printf("parent is running under DynamoRIO\n");
  else
    printf("parent is running natively\n");
  child = fork();
  if (child < 0) {
    perror("ERROR on fork");
  } else if (child > 0) {
    pid_t result;
    printf("parent waiting for child\n");
    result = waitpid(child, NULL, 0);
    assert(result == child);
    printf("child has exited\n");
  } else {
    if (find_dynamo_library())
      printf("child is running under DynamoRIO\n");
    else
      printf("child is running natively\n");
  }
}


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)
mov    $0x0,%al
call   1050 <find_dynamo_library@plt>
cmp    $0x0,%rax
je     1198 <main+0x38>
lea    0xe72(%rip),%rdi
call   1030 <printf@plt>
jmp    11a4 <main+0x44>
lea    0xe84(%rip),%rdi
call   1030 <printf@plt>
mov    $0x0,%al
call   1080 <fork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jge    11cb <main+0x6b>
lea    0xe7e(%rip),%rdi
call   1070 <perror@plt>
jmp    1253 <main+0xf3>
cmpq   $0x0,-0x18(%rbp)
jle    121b <main+0xbb>
lea    0xe70(%rip),%rdi
call   1030 <printf@plt>
mov    -0x18(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1060 <waitpid@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
lea    0xe56(%rip),%rdi
call   1030 <printf@plt>
jmp    124e <main+0xee>
mov    $0x0,%al
call   1050 <find_dynamo_library@plt>
cmp    $0x0,%rax
je     123d <main+0xdd>
lea    0xe46(%rip),%rdi
call   1030 <printf@plt>
jmp    1249 <main+0xe9>
lea    0xe57(%rip),%rdi
call   1030 <printf@plt>
jmp    124e <main+0xee>
jmp    1253 <main+0xf3>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
xor    %eax,%eax
call   1050 <find_dynamo_library@plt>
test   %rax,%rax
lea    0xe9c(%rip),%rax
lea    0xf15(%rip),%rdi
cmove  %rax,%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   1080 <fork@plt>
test   %rax,%rax
js     11e7 <main+0x87>
je     11c2 <main+0x62>
mov    %rax,%rbx
lea    0xeca(%rip),%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
xor    %esi,%esi
xor    %edx,%edx
call   1060 <waitpid@plt>
xor    %edi,%edi
cmp    %rbx,%rax
sete   %dil
call   1040 <assert@plt>
lea    0xebd(%rip),%rdi
jmp    11de <main+0x7e>
xor    %eax,%eax
call   1050 <find_dynamo_library@plt>
test   %rax,%rax
je     11d7 <main+0x77>
lea    0xe6e(%rip),%rdi
jmp    11de <main+0x7e>
lea    0xe4b(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
pop    %rbx
ret
lea    0xe12(%rip),%rdi
call   1070 <perror@plt>
jmp    11e3 <main+0x83>

LLVM IR (-O0):
@.str = [35 x i8] c"parent is running under DynamoRIO\0A\00"
@.str.1 = [28 x i8] c"parent is running natively\0A\00"
@.str.2 = [14 x i8] c"ERROR on fork\00"
@.str.3 = [26 x i8] c"parent waiting for child\0A\00"
@.str.4 = [18 x i8] c"child has exited\0A\00"
@.str.5 = [34 x i8] c"child is running under DynamoRIO\0A\00"
@.str.6 = [27 x i8] c"child is running natively\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = call i64 (...) @find_dynamo_library()
%9 = icmp ne i64 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0))
br label %14
12: ; preds = %2
%13 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
br label %14
14: ; preds = %12, %10
%15 = call i64 (...) @fork()
store i64 %15, i64* %6
%16 = load i64, i64* %6
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %14
%19 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
br label %42
20: ; preds = %14
%21 = load i64, i64* %6
%22 = icmp sgt i64 %21, 0
br i1 %22, label %23, label %33
23: ; preds = %20
%24 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0))
%25 = load i64, i64* %6
%26 = call i64 @waitpid(i64 %25, i32* null, i32 0)
store i64 %26, i64* %7
%27 = load i64, i64* %7
%28 = load i64, i64* %6
%29 = icmp eq i64 %27, %28
%30 = zext i1 %29 to i32
%31 = call i32 @assert(i32 %30)
%32 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0))
br label %41
33: ; preds = %20
%34 = call i64 (...) @find_dynamo_library()
%35 = icmp ne i64 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %33
%37 = call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.5, i64 0, i64 0))
br label %40
38: ; preds = %33
%39 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.6, i64 0, i64 0))
br label %40
40: ; preds = %38, %36
br label %41
41: ; preds = %40, %23
br label %42
42: ; preds = %41, %18
%43 = load i32, i32* %3
ret i32 %43
}

LLVM IR (-O2):
@.str.2 = [14 x i8] c"ERROR on fork\00"
@str = [27 x i8] c"parent is running natively\00"
@str.7 = [26 x i8] c"child is running natively\00"
@str.8 = [33 x i8] c"child is running under DynamoRIO\00"
@str.9 = [25 x i8] c"parent waiting for child\00"
@str.10 = [17 x i8] c"child has exited\00"
@str.11 = [34 x i8] c"parent is running under DynamoRIO\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i64 (...) @find_dynamo_library()
%4 = icmp eq i64 %3, 0
%5 = select i1 %4, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @str, i64 0, i64 0), i8* getelementptr inbounds ([34 x i8], [34 x i8]* @str.11, i64 0, i64 0)
%6 = tail call i32 @puts(i8* dereferenceable(1) %5)
%7 = tail call i64 (...) @fork()
%8 = icmp slt i64 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %2
%10 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
br label %27
11: ; preds = %2
%12 = icmp eq i64 %7, 0
br i1 %12, label %20, label %13
13: ; preds = %11
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str.9, i64 0, i64 0))
%15 = tail call i64 @waitpid(i64 %7, i32* null, i32 0)
%16 = icmp eq i64 %15, %7
%17 = zext i1 %16 to i32
%18 = tail call i32 @assert(i32 %17)
%19 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str.10, i64 0, i64 0))
br label %27
20: ; preds = %11
%21 = tail call i64 (...) @find_dynamo_library()
%22 = icmp eq i64 %21, 0
br i1 %22, label %25, label %23
23: ; preds = %20
%24 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @str.8, i64 0, i64 0))
br label %27
25: ; preds = %20
%26 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str.7, i64 0, i64 0))
br label %27
27: ; preds = %13, %25, %23, %9
ret i32 0
}