#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 */

/* Variables and functions */
int /*<<< orphan*/ exit(int);
int fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ stderr;
int wait4(int, int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  int pid, wpid;
  if (pid = fork()) {
    fprintf(stderr, "parent: I see kid with pid = %d\n", pid);
    sleep(3);
    while (!(wpid = wait4(pid, 0, 0, 0)))
      sleep(2);
    if (wpid == pid) {
      fprintf(stderr, "parent: waited on kid\n");
      exit(0);
    } else {
      perror("wait4 failed");
      exit(1);
    }
  } else {
    fprintf(stderr, "child: I'm here\n");
    sleep(1);
    fprintf(stderr, "child: exiting\n");
    exit(0);
  }
}


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   1080 <fork@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
je     1222 <main+0xc2>
mov    0x2e50(%rip),%rax
mov    (%rax),%edi
mov    -0x14(%rbp),%edx
lea    0xe64(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
mov    $0x3,%edi
call   1070 <sleep@plt>
mov    -0x14(%rbp),%edi
xor    %ecx,%ecx
mov    %ecx,%esi
mov    %ecx,%edx
call   1040 <wait4@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    11d3 <main+0x73>
jmp    11e2 <main+0x82>
mov    $0x2,%edi
call   1070 <sleep@plt>
jmp    11ad <main+0x4d>
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jne    120c <main+0xac>
mov    0x2deb(%rip),%rax
mov    (%rax),%edi
lea    0xe23(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
xor    %edi,%edi
call   1060 <exit@plt>
lea    0xe25(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    0x2db7(%rip),%rax
mov    (%rax),%edi
lea    0xe13(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
mov    $0x1,%edi
call   1070 <sleep@plt>
mov    0x2d96(%rip),%rax
mov    (%rax),%edi
lea    0xe03(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
xor    %edi,%edi
call   1060 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
xor    %eax,%eax
call   1080 <fork@plt>
mov    0x2e6e(%rip),%r14
mov    (%r14),%edi
test   %eax,%eax
jne    119d <main+0x3d>
lea    0xec5(%rip),%rsi
xor    %eax,%eax
call   1030 <fprintf@plt>
mov    $0x1,%edi
call   1070 <sleep@plt>
mov    (%r14),%edi
lea    0xebb(%rip),%rsi
jmp    11eb <main+0x8b>
mov    %eax,%ebx
lea    0xe5a(%rip),%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1030 <fprintf@plt>
mov    $0x3,%edi
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
call   1070 <sleep@plt>
mov    %ebx,%edi
xor    %esi,%esi
xor    %edx,%edx
xor    %ecx,%ecx
call   1040 <wait4@plt>
test   %eax,%eax
jne    11dd <main+0x7d>
mov    $0x2,%edi
jmp    11c0 <main+0x60>
cmp    %ebx,%eax
jne    11f9 <main+0x99>
mov    (%r14),%edi
lea    0xe36(%rip),%rsi
xor    %eax,%eax
call   1030 <fprintf@plt>
xor    %edi,%edi
call   1060 <exit@plt>
lea    0xe38(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [33 x i8] c"parent: I see kid with pid = %d\0A\00"
@.str.1 = [23 x i8] c"parent: waited on kid\0A\00"
@.str.2 = [13 x i8] c"wait4 failed\00"
@.str.3 = [17 x i8] c"child: I'm here\0A\00"
@.str.4 = [16 x i8] c"child: exiting\0A\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 = call i32 (...) @fork()
store i32 %8, i32* %6
%9 = icmp ne i32 %8, 0
br i1 %9, label %10, label %33
10: ; preds = %2
%11 = load i32, i32* @stderr
%12 = load i32, i32* %6
%13 = call i32 (i32, i8*, ...) @fprintf(i32 %11, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i32 %12)
%14 = call i32 @sleep(i32 3)
br label %15
15: ; preds = %20, %10
%16 = load i32, i32* %6
%17 = call i32 @wait4(i32 %16, i32 0, i32 0, i32 0)
store i32 %17, i32* %7
%18 = icmp ne i32 %17, 0
%19 = xor i1 %18, true
br i1 %19, label %20, label %22
20: ; preds = %15
%21 = call i32 @sleep(i32 2)
br label %15
22: ; preds = %15
%23 = load i32, i32* %7
%24 = load i32, i32* %6
%25 = icmp eq i32 %23, %24
br i1 %25, label %26, label %30
26: ; preds = %22
%27 = load i32, i32* @stderr
%28 = call i32 (i32, i8*, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
%29 = call i32 @exit(i32 0)
unreachable
30: ; preds = %22
%31 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%32 = call i32 @exit(i32 1)
unreachable
33: ; preds = %2
%34 = load i32, i32* @stderr
%35 = call i32 (i32, i8*, ...) @fprintf(i32 %34, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
%36 = call i32 @sleep(i32 1)
%37 = load i32, i32* @stderr
%38 = call i32 (i32, i8*, ...) @fprintf(i32 %37, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0))
%39 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [33 x i8] c"parent: I see kid with pid = %d\0A\00"
@.str.1 = [23 x i8] c"parent: waited on kid\0A\00"
@.str.2 = [13 x i8] c"wait4 failed\00"
@.str.3 = [17 x i8] c"child: I'm here\0A\00"
@.str.4 = [16 x i8] c"child: exiting\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i32 (...) @fork()
%4 = icmp eq i32 %3, 0
%5 = load i32, i32* @stderr
br i1 %4, label %25, label %6
6: ; preds = %2
%7 = tail call i32 (i32, i8*, ...) @fprintf(i32 %5, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i32 %3)
%8 = tail call i32 @sleep(i32 3)
%9 = tail call i32 @wait4(i32 %3, i32 0, i32 0, i32 0)
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %15
11: ; preds = %6, %11
%12 = tail call i32 @sleep(i32 2)
%13 = tail call i32 @wait4(i32 %3, i32 0, i32 0, i32 0)
%14 = icmp eq i32 %13, 0
br i1 %14, label %11, label %15
15: ; preds = %11, %6
%16 = phi i32 [ %9, %6 ], [ %13, %11 ]
%17 = icmp eq i32 %16, %3
br i1 %17, label %18, label %22
18: ; preds = %15
%19 = load i32, i32* @stderr
%20 = tail call i32 (i32, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
%21 = tail call i32 @exit(i32 0)
unreachable
22: ; preds = %15
%23 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%24 = tail call i32 @exit(i32 1)
unreachable
25: ; preds = %2
%26 = tail call i32 (i32, i8*, ...) @fprintf(i32 %5, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
%27 = tail call i32 @sleep(i32 1)
%28 = load i32, i32* @stderr
%29 = tail call i32 (i32, i8*, ...) @fprintf(i32 %28, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0))
%30 = tail call i32 @exit(i32 0)
unreachable
}