#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 WEXITSTATUS(int);
scalar_t__ WIFEXITED(int);
scalar_t__ WIFSIGNALED(int);
scalar_t__ WIFSTOPPED(int);
int WSTOPSIG(int);
int WTERMSIG(int);
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 *, int /*<<< orphan*/, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  int pid, wpid;
  int status;
  if (pid = fork()) {
    while (!(wpid = wait4(pid, &status, 0, 0))) {
      fprintf(stderr, "parent: continuing to wait...\n");
      sleep(3);
    }
    if (wpid == pid) {
      fprintf(stderr, "parent: waited on kid\n");
      fprintf(stderr, "parent: status = 0x%x\n", status);
      if (WIFSIGNALED(status))
        fprintf(stderr, "parent: signaled with %d\n", WTERMSIG(status));
      else if (WIFEXITED(status))
        fprintf(stderr, "parent: exited with status %d\n", WEXITSTATUS(status));
      else if (WIFSTOPPED(status))
        fprintf(stderr, "parent: stopped with %d\n", WSTOPSIG(status));
      else
        fprintf(stderr, "parent: bad status\n");
      exit(0);
    } else {
      perror("wait4 failed");
      exit(1);
    }
  } else {
    fprintf(stderr, "child: I'm here\n");
    sleep(10);
    fprintf(stderr, "child: exiting\n");
    exit(7);
  }
}


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    $0x0,%al
call   10e0 <fork@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
je     1371 <main+0x1b1>
jmp    11ee <main+0x2e>
mov    -0x14(%rbp),%edi
lea    -0x1c(%rbp),%rsi
xor    %ecx,%ecx
mov    %ecx,%edx
call   1070 <wait4@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    1216 <main+0x56>
jmp    123c <main+0x7c>
mov    0x2dc3(%rip),%rax
mov    (%rax),%edi
lea    0xdda(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    $0x3,%edi
call   10c0 <sleep@plt>
jmp    11ee <main+0x2e>
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jne    135b <main+0x19b>
mov    0x2d91(%rip),%rax
mov    (%rax),%edi
lea    0xdc7(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    0x2d7a(%rip),%rax
mov    (%rax),%edi
mov    -0x1c(%rbp),%edx
lea    0xdc4(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x1c(%rbp),%edi
call   1030 <WIFSIGNALED@plt>
cmp    $0x0,%rax
je     12b7 <main+0xf7>
mov    0x2d4e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%edi
call   1060 <WTERMSIG@plt>
mov    -0x20(%rbp),%edi
mov    %eax,%edx
lea    0xda2(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
jmp    1354 <main+0x194>
mov    -0x1c(%rbp),%edi
call   10a0 <WIFEXITED@plt>
cmp    $0x0,%rax
je     12f5 <main+0x135>
mov    0x2d10(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x1c(%rbp),%edi
call   1080 <WEXITSTATUS@plt>
mov    -0x24(%rbp),%edi
mov    %eax,%edx
lea    0xd7e(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
jmp    134f <main+0x18f>
mov    -0x1c(%rbp),%edi
call   1050 <WIFSTOPPED@plt>
cmp    $0x0,%rax
je     1333 <main+0x173>
mov    0x2cd2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x1c(%rbp),%edi
call   10d0 <WSTOPSIG@plt>
mov    -0x28(%rbp),%edi
mov    %eax,%edx
lea    0xd5f(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
jmp    134a <main+0x18a>
mov    0x2ca6(%rip),%rax
mov    (%rax),%edi
lea    0xd5c(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
jmp    134f <main+0x18f>
jmp    1354 <main+0x194>
xor    %edi,%edi
call   10b0 <exit@plt>
lea    0xd51(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    0x2c68(%rip),%rax
mov    (%rax),%edi
lea    0xd3f(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    $0xa,%edi
call   10c0 <sleep@plt>
mov    0x2c47(%rip),%rax
mov    (%rax),%edi
lea    0xd2f(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    $0x7,%edi
call   10b0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
xor    %eax,%eax
call   10e0 <fork@plt>
test   %eax,%eax
jne    120e <main+0x4e>
mov    0x2e06(%rip),%rbx
mov    (%rbx),%edi
lea    0xedd(%rip),%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    $0xa,%edi
call   10c0 <sleep@plt>
mov    (%rbx),%edi
lea    0xed4(%rip),%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    $0x7,%edi
call   10b0 <exit@plt>
mov    %eax,%ebx
lea    0x4(%rsp),%rsi
mov    %eax,%edi
xor    %edx,%edx
xor    %ecx,%ecx
call   1070 <wait4@plt>
test   %eax,%eax
jne    126a <main+0xaa>
mov    0x2db5(%rip),%r12
lea    0xdce(%rip),%r14
lea    0x4(%rsp),%r15
nopw   0x0(%rax,%rax,1)
mov    (%r12),%edi
mov    %r14,%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    $0x3,%edi
call   10c0 <sleep@plt>
mov    %ebx,%edi
mov    %r15,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
call   1070 <wait4@plt>
test   %eax,%eax
je     1240 <main+0x80>
cmp    %ebx,%eax
jne    12dc <main+0x11c>
mov    0x2d6b(%rip),%rbx
mov    (%rbx),%edi
lea    0xda1(%rip),%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    (%rbx),%edi
mov    0x4(%rsp),%edx
lea    0xda4(%rip),%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    0x4(%rsp),%edi
call   1030 <WIFSIGNALED@plt>
test   %rax,%rax
jne    12f2 <main+0x132>
mov    0x4(%rsp),%edi
call   10a0 <WIFEXITED@plt>
test   %rax,%rax
jne    1306 <main+0x146>
mov    0x4(%rsp),%edi
call   1050 <WIFSTOPPED@plt>
mov    (%rbx),%ebx
test   %rax,%rax
jne    131a <main+0x15a>
lea    0xdd3(%rip),%rsi
mov    %ebx,%edi
xor    %eax,%eax
call   1040 <fprintf@plt>
xor    %edi,%edi
call   10b0 <exit@plt>
lea    0xdd0(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    (%rbx),%ebx
mov    0x4(%rsp),%edi
call   1060 <WTERMSIG@plt>
lea    0xd49(%rip),%rsi
jmp    132a <main+0x16a>
mov    (%rbx),%ebx
mov    0x4(%rsp),%edi
call   1080 <WEXITSTATUS@plt>
lea    0xd4f(%rip),%rsi
jmp    132a <main+0x16a>
mov    0x4(%rsp),%edi
call   10d0 <WSTOPSIG@plt>
lea    0xd5c(%rip),%rsi
mov    %ebx,%edi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <fprintf@plt>
xor    %edi,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [31 x i8] c"parent: continuing to wait...\0A\00"
@.str.1 = [23 x i8] c"parent: waited on kid\0A\00"
@.str.2 = [23 x i8] c"parent: status = 0x%x\0A\00"
@.str.3 = [26 x i8] c"parent: signaled with %d\0A\00"
@.str.4 = [31 x i8] c"parent: exited with status %d\0A\00"
@.str.5 = [25 x i8] c"parent: stopped with %d\0A\00"
@.str.6 = [20 x i8] c"parent: bad status\0A\00"
@.str.7 = [13 x i8] c"wait4 failed\00"
@.str.8 = [17 x i8] c"child: I'm here\0A\00"
@.str.9 = [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
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = call i32 (...) @fork()
store i32 %9, i32* %6
%10 = icmp ne i32 %9, 0
br i1 %10, label %11, label %67
11: ; preds = %2
br label %12
12: ; preds = %17, %11
%13 = load i32, i32* %6
%14 = call i32 @wait4(i32 %13, i32* %8, i32 0, i32 0)
store i32 %14, i32* %7
%15 = icmp ne i32 %14, 0
%16 = xor i1 %15, true
br i1 %16, label %17, label %21
17: ; preds = %12
%18 = load i32, i32* @stderr
%19 = call i32 (i32, i8*, ...) @fprintf(i32 %18, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0))
%20 = call i32 @sleep(i32 3)
br label %12
21: ; preds = %12
%22 = load i32, i32* %7
%23 = load i32, i32* %6
%24 = icmp eq i32 %22, %23
br i1 %24, label %25, label %64
25: ; preds = %21
%26 = load i32, i32* @stderr
%27 = call i32 (i32, i8*, ...) @fprintf(i32 %26, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
%28 = load i32, i32* @stderr
%29 = load i32, i32* %8
%30 = call i32 (i32, i8*, ...) @fprintf(i32 %28, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i32 %29)
%31 = load i32, i32* %8
%32 = call i64 @WIFSIGNALED(i32 %31)
%33 = icmp ne i64 %32, 0
br i1 %33, label %34, label %39
34: ; preds = %25
%35 = load i32, i32* @stderr
%36 = load i32, i32* %8
%37 = call i32 @WTERMSIG(i32 %36)
%38 = call i32 (i32, i8*, ...) @fprintf(i32 %35, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), i32 %37)
br label %62
39: ; preds = %25
%40 = load i32, i32* %8
%41 = call i64 @WIFEXITED(i32 %40)
%42 = icmp ne i64 %41, 0
br i1 %42, label %43, label %48
43: ; preds = %39
%44 = load i32, i32* @stderr
%45 = load i32, i32* %8
%46 = call i32 @WEXITSTATUS(i32 %45)
%47 = call i32 (i32, i8*, ...) @fprintf(i32 %44, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0), i32 %46)
br label %61
48: ; preds = %39
%49 = load i32, i32* %8
%50 = call i64 @WIFSTOPPED(i32 %49)
%51 = icmp ne i64 %50, 0
br i1 %51, label %52, label %57
52: ; preds = %48
%53 = load i32, i32* @stderr
%54 = load i32, i32* %8
%55 = call i32 @WSTOPSIG(i32 %54)
%56 = call i32 (i32, i8*, ...) @fprintf(i32 %53, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0), i32 %55)
br label %60
57: ; preds = %48
%58 = load i32, i32* @stderr
%59 = call i32 (i32, i8*, ...) @fprintf(i32 %58, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.6, i64 0, i64 0))
br label %60
60: ; preds = %57, %52
br label %61
61: ; preds = %60, %43
br label %62
62: ; preds = %61, %34
%63 = call i32 @exit(i32 0)
unreachable
64: ; preds = %21
%65 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.7, i64 0, i64 0))
%66 = call i32 @exit(i32 1)
unreachable
67: ; preds = %2
%68 = load i32, i32* @stderr
%69 = call i32 (i32, i8*, ...) @fprintf(i32 %68, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.8, i64 0, i64 0))
%70 = call i32 @sleep(i32 10)
%71 = load i32, i32* @stderr
%72 = call i32 (i32, i8*, ...) @fprintf(i32 %71, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.9, i64 0, i64 0))
%73 = call i32 @exit(i32 7)
unreachable
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [31 x i8] c"parent: continuing to wait...\0A\00"
@.str.1 = [23 x i8] c"parent: waited on kid\0A\00"
@.str.2 = [23 x i8] c"parent: status = 0x%x\0A\00"
@.str.3 = [26 x i8] c"parent: signaled with %d\0A\00"
@.str.4 = [31 x i8] c"parent: exited with status %d\0A\00"
@.str.5 = [25 x i8] c"parent: stopped with %d\0A\00"
@.str.6 = [20 x i8] c"parent: bad status\0A\00"
@.str.7 = [13 x i8] c"wait4 failed\00"
@.str.8 = [17 x i8] c"child: I'm here\0A\00"
@.str.9 = [16 x i8] c"child: exiting\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = tail call i32 (...) @fork()
%6 = icmp eq i32 %5, 0
br i1 %6, label %58, label %7
7: ; preds = %2
%8 = call i32 @wait4(i32 %5, i32* %3, i32 0, i32 0)
%9 = icmp eq i32 %8, 0
br i1 %9, label %10, label %16
10: ; preds = %7, %10
%11 = load i32, i32* @stderr
%12 = call i32 (i32, i8*, ...) @fprintf(i32 %11, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0))
%13 = call i32 @sleep(i32 3)
%14 = call i32 @wait4(i32 %5, i32* %3, i32 0, i32 0)
%15 = icmp eq i32 %14, 0
br i1 %15, label %10, label %16
16: ; preds = %10, %7
%17 = phi i32 [ %8, %7 ], [ %14, %10 ]
%18 = icmp eq i32 %17, %5
br i1 %18, label %19, label %55
19: ; preds = %16
%20 = load i32, i32* @stderr
%21 = call i32 (i32, i8*, ...) @fprintf(i32 %20, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
%22 = load i32, i32* @stderr
%23 = load i32, i32* %3
%24 = call i32 (i32, i8*, ...) @fprintf(i32 %22, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i32 %23)
%25 = load i32, i32* %3
%26 = call i64 @WIFSIGNALED(i32 %25)
%27 = icmp eq i64 %26, 0
br i1 %27, label %33, label %28
28: ; preds = %19
%29 = load i32, i32* @stderr
%30 = load i32, i32* %3
%31 = call i32 @WTERMSIG(i32 %30)
%32 = call i32 (i32, i8*, ...) @fprintf(i32 %29, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), i32 %31)
br label %53
33: ; preds = %19
%34 = load i32, i32* %3
%35 = call i64 @WIFEXITED(i32 %34)
%36 = icmp eq i64 %35, 0
br i1 %36, label %42, label %37
37: ; preds = %33
%38 = load i32, i32* @stderr
%39 = load i32, i32* %3
%40 = call i32 @WEXITSTATUS(i32 %39)
%41 = call i32 (i32, i8*, ...) @fprintf(i32 %38, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0), i32 %40)
br label %53
42: ; preds = %33
%43 = load i32, i32* %3
%44 = call i64 @WIFSTOPPED(i32 %43)
%45 = icmp eq i64 %44, 0
%46 = load i32, i32* @stderr
br i1 %45, label %51, label %47
47: ; preds = %42
%48 = load i32, i32* %3
%49 = call i32 @WSTOPSIG(i32 %48)
%50 = call i32 (i32, i8*, ...) @fprintf(i32 %46, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0), i32 %49)
br label %53
51: ; preds = %42
%52 = call i32 (i32, i8*, ...) @fprintf(i32 %46, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.6, i64 0, i64 0))
br label %53
53: ; preds = %37, %51, %47, %28
%54 = call i32 @exit(i32 0)
unreachable
55: ; preds = %16
%56 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.7, i64 0, i64 0))
%57 = call i32 @exit(i32 1)
unreachable
58: ; preds = %2
%59 = load i32, i32* @stderr
%60 = tail call i32 (i32, i8*, ...) @fprintf(i32 %59, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.8, i64 0, i64 0))
%61 = tail call i32 @sleep(i32 10)
%62 = load i32, i32* @stderr
%63 = tail call i32 (i32, i8*, ...) @fprintf(i32 %62, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.9, i64 0, i64 0))
%64 = tail call i32 @exit(i32 7)
unreachable
}