#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(1);
    while (!(wpid = wait4(pid, 0, 0, 0))) {
      fprintf(stderr, "parent: continuing to wait...\n");
      sleep(3);
    }
    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(5);
    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     1239 <main+0xd9>
mov    0x2e50(%rip),%rax
mov    (%rax),%edi
mov    -0x14(%rbp),%edx
lea    0xe64(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
mov    $0x1,%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    11f9 <main+0x99>
mov    0x2e06(%rip),%rax
mov    (%rax),%edi
lea    0xe3e(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
mov    $0x3,%edi
call   1070 <sleep@plt>
jmp    11ad <main+0x4d>
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jne    1223 <main+0xc3>
mov    0x2dd4(%rip),%rax
mov    (%rax),%edi
lea    0xe2b(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
xor    %edi,%edi
call   1060 <exit@plt>
lea    0xe2d(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    0x2da0(%rip),%rax
mov    (%rax),%edi
lea    0xe1b(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
mov    $0x5,%edi
call   1070 <sleep@plt>
mov    0x2d7f(%rip),%rax
mov    (%rax),%edi
lea    0xe0b(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
xor    %edi,%edi
call   1060 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
xor    %eax,%eax
call   1080 <fork@plt>
mov    0x2e6d(%rip),%r15
mov    (%r15),%edi
test   %eax,%eax
jne    119e <main+0x3e>
lea    0xee3(%rip),%rsi
xor    %eax,%eax
call   1030 <fprintf@plt>
mov    $0x5,%edi
call   1070 <sleep@plt>
mov    (%r15),%edi
lea    0xed9(%rip),%rsi
jmp    1216 <main+0xb6>
mov    %eax,%ebx
lea    0xe59(%rip),%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1030 <fprintf@plt>
mov    $0x1,%edi
call   1070 <sleep@plt>
mov    %ebx,%edi
xor    %esi,%esi
xor    %edx,%edx
xor    %ecx,%ecx
call   1040 <wait4@plt>
test   %eax,%eax
jne    1208 <main+0xa8>
lea    0xe4f(%rip),%r14
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    (%r15),%edi
mov    %r14,%rsi
xor    %eax,%eax
call   1030 <fprintf@plt>
mov    $0x3,%edi
call   1070 <sleep@plt>
mov    %ebx,%edi
xor    %esi,%esi
xor    %edx,%edx
xor    %ecx,%ecx
call   1040 <wait4@plt>
test   %eax,%eax
je     11e0 <main+0x80>
cmp    %ebx,%eax
jne    1224 <main+0xc4>
mov    (%r15),%edi
lea    0xe2a(%rip),%rsi
xor    %eax,%eax
call   1030 <fprintf@plt>
xor    %edi,%edi
call   1060 <exit@plt>
lea    0xe2c(%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 = [31 x i8] c"parent: continuing to wait...\0A\00"
@.str.2 = [23 x i8] c"parent: waited on kid\0A\00"
@.str.3 = [13 x i8] c"wait4 failed\00"
@.str.4 = [17 x i8] c"child: I'm here\0A\00"
@.str.5 = [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 %35
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 1)
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 %24
20: ; preds = %15
%21 = load i32, i32* @stderr
%22 = call i32 (i32, i8*, ...) @fprintf(i32 %21, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0))
%23 = call i32 @sleep(i32 3)
br label %15
24: ; preds = %15
%25 = load i32, i32* %7
%26 = load i32, i32* %6
%27 = icmp eq i32 %25, %26
br i1 %27, label %28, label %32
28: ; preds = %24
%29 = load i32, i32* @stderr
%30 = call i32 (i32, i8*, ...) @fprintf(i32 %29, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
%31 = call i32 @exit(i32 0)
unreachable
32: ; preds = %24
%33 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
%34 = call i32 @exit(i32 1)
unreachable
35: ; preds = %2
%36 = load i32, i32* @stderr
%37 = call i32 (i32, i8*, ...) @fprintf(i32 %36, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0))
%38 = call i32 @sleep(i32 5)
%39 = load i32, i32* @stderr
%40 = call i32 (i32, i8*, ...) @fprintf(i32 %39, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0))
%41 = 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 = [31 x i8] c"parent: continuing to wait...\0A\00"
@.str.2 = [23 x i8] c"parent: waited on kid\0A\00"
@.str.3 = [13 x i8] c"wait4 failed\00"
@.str.4 = [17 x i8] c"child: I'm here\0A\00"
@.str.5 = [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 %27, 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 1)
%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 %17
11: ; preds = %6, %11
%12 = load i32, i32* @stderr
%13 = tail call i32 (i32, i8*, ...) @fprintf(i32 %12, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0))
%14 = tail call i32 @sleep(i32 3)
%15 = tail call i32 @wait4(i32 %3, i32 0, i32 0, i32 0)
%16 = icmp eq i32 %15, 0
br i1 %16, label %11, label %17
17: ; preds = %11, %6
%18 = phi i32 [ %9, %6 ], [ %15, %11 ]
%19 = icmp eq i32 %18, %3
br i1 %19, label %20, label %24
20: ; preds = %17
%21 = load i32, i32* @stderr
%22 = tail call i32 (i32, i8*, ...) @fprintf(i32 %21, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
%23 = tail call i32 @exit(i32 0)
unreachable
24: ; preds = %17
%25 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
%26 = tail call i32 @exit(i32 1)
unreachable
27: ; preds = %2
%28 = tail call i32 (i32, i8*, ...) @fprintf(i32 %5, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0))
%29 = tail call i32 @sleep(i32 5)
%30 = load i32, i32* @stderr
%31 = tail call i32 (i32, i8*, ...) @fprintf(i32 %30, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0))
%32 = tail call i32 @exit(i32 0)
unreachable
}