#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 WCONTINUED;
int WEXITSTATUS(int);
scalar_t__ WIFCONTINUED(int);
scalar_t__ WIFEXITED(int);
scalar_t__ WIFSIGNALED(int);
scalar_t__ WIFSTOPPED(int);
int WSTOPSIG(int);
int WTERMSIG(int);
int WUNTRACED;
int /*<<< orphan*/ child();
int fork();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int, ...);
int waitpid(int, int *, int);
int main(int argc, char **argv) {
  pid_t c;
  int wstatus;
  switch ((c = fork())) {
  case 0:
    child();
    break;
  case -1:
    perror("fork()");
    return 0;
  default:
    break;
  }
  printf("Child: %d\n", c);
  do {
    pid_t w = waitpid(c, &wstatus, WUNTRACED | WCONTINUED);
    if (w == -1) {
      perror("waitpid()");
      return 1;
    }
    if (WIFEXITED(wstatus)) {
      printf("[%d] Exit, status: %d\n", c, WEXITSTATUS(wstatus));
    } else if (WIFSIGNALED(wstatus)) {
      printf("[%d] Killed by signal: %d\n", c, WTERMSIG(wstatus));
    } else if (WIFSTOPPED(wstatus)) {
      printf("[%d] Stopped by signal: %d\n", c, WSTOPSIG(wstatus));
    } else if (WIFCONTINUED(wstatus)) {
      printf("[%d] Continued\n", c);
    }
  } while (!WIFEXITED(wstatus) && !WIFSIGNALED(wstatus));
  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)
xor    %eax,%eax
call   10e0 <fork@plt>
mov    %eax,-0x14(%rbp)
mov    %eax,%ecx
mov    %ecx,-0x20(%rbp)
sub    $0xffffffff,%eax
je     120f <main+0x4f>
jmp    11f3 <main+0x33>
mov    -0x20(%rbp),%eax
test   %eax,%eax
jne    1227 <main+0x67>
jmp    1203 <main+0x43>
mov    $0x0,%al
call   1030 <child@plt>
jmp    122c <main+0x6c>
lea    0xdea(%rip),%rdi
call   10b0 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    139f <main+0x1df>
jmp    122c <main+0x6c>
mov    -0x14(%rbp),%esi
lea    0xdd1(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x14(%rbp),%edi
mov    0x2d99(%rip),%rax
mov    (%rax),%edx
mov    0x2d80(%rip),%rax
or     (%rax),%edx
lea    -0x18(%rbp),%rsi
call   10a0 <waitpid@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    1280 <main+0xc0>
lea    0xda3(%rip),%rdi
call   10b0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    139f <main+0x1df>
mov    -0x18(%rbp),%edi
call   10c0 <WIFEXITED@plt>
cmp    $0x0,%rax
je     12b8 <main+0xf8>
mov    -0x14(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x18(%rbp),%edi
call   1090 <WEXITSTATUS@plt>
mov    -0x24(%rbp),%esi
mov    %eax,%edx
lea    0xd70(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    135a <main+0x19a>
mov    -0x18(%rbp),%edi
call   1050 <WIFSIGNALED@plt>
cmp    $0x0,%rax
je     12f0 <main+0x130>
mov    -0x14(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x18(%rbp),%edi
call   1080 <WTERMSIG@plt>
mov    -0x28(%rbp),%esi
mov    %eax,%edx
lea    0xd4f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1355 <main+0x195>
mov    -0x18(%rbp),%edi
call   1070 <WIFSTOPPED@plt>
cmp    $0x0,%rax
je     1328 <main+0x168>
mov    -0x14(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x18(%rbp),%edi
call   10d0 <WSTOPSIG@plt>
mov    -0x2c(%rbp),%esi
mov    %eax,%edx
lea    0xd32(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1350 <main+0x190>
mov    -0x18(%rbp),%edi
call   1060 <WIFCONTINUED@plt>
cmp    $0x0,%rax
je     134b <main+0x18b>
mov    -0x14(%rbp),%esi
lea    0xd26(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1350 <main+0x190>
jmp    1355 <main+0x195>
jmp    135a <main+0x19a>
jmp    135f <main+0x19f>
mov    -0x18(%rbp),%edi
call   10c0 <WIFEXITED@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    $0x0,%rcx
mov    %al,-0x2d(%rbp)
jne    138d <main+0x1cd>
mov    -0x18(%rbp),%edi
call   1050 <WIFSIGNALED@plt>
cmp    $0x0,%rax
setne  %al
xor    $0xff,%al
mov    %al,-0x2d(%rbp)
mov    -0x2d(%rbp),%al
test   $0x1,%al
jne    123d <main+0x7d>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
xor    %eax,%eax
call   10e0 <fork@plt>
mov    %eax,%ebx
test   %eax,%eax
je     11f1 <main+0x31>
cmp    $0xffffffff,%ebx
jne    11f8 <main+0x38>
lea    0xe1c(%rip),%rdi
call   10b0 <perror@plt>
xor    %r15d,%r15d
jmp    130b <main+0x14b>
xor    %eax,%eax
call   1030 <child@plt>
lea    0xe08(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x2dd1(%rip),%r12
mov    0x2dba(%rip),%r13
lea    0x4(%rsp),%r14
lea    0xdfa(%rip),%rbp
xor    %r15d,%r15d
cs nopw 0x0(%rax,%rax,1)
nop
mov    0x0(%r13),%edx
or     (%r12),%edx
mov    %ebx,%edi
mov    %r14,%rsi
call   10a0 <waitpid@plt>
cmp    $0xffffffff,%eax
je     12f9 <main+0x139>
mov    0x4(%rsp),%edi
call   10c0 <WIFEXITED@plt>
mov    0x4(%rsp),%edi
test   %rax,%rax
je     1270 <main+0xb0>
call   1090 <WEXITSTATUS@plt>
mov    %rbp,%rdi
jmp    12b0 <main+0xf0>
nopw   0x0(%rax,%rax,1)
call   1050 <WIFSIGNALED@plt>
mov    0x4(%rsp),%edi
test   %rax,%rax
je     128c <main+0xcc>
call   1080 <WTERMSIG@plt>
lea    0xda9(%rip),%rdi
jmp    12b0 <main+0xf0>
call   1070 <WIFSTOPPED@plt>
mov    0x4(%rsp),%edi
test   %rax,%rax
je     12dd <main+0x11d>
call   10d0 <WSTOPSIG@plt>
lea    0xda8(%rip),%rdi
cs nopw 0x0(%rax,%rax,1)
mov    %ebx,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x4(%rsp),%edi
call   10c0 <WIFEXITED@plt>
test   %rax,%rax
jne    130b <main+0x14b>
mov    0x4(%rsp),%edi
call   1050 <WIFSIGNALED@plt>
test   %rax,%rax
je     1230 <main+0x70>
jmp    130b <main+0x14b>
call   1060 <WIFCONTINUED@plt>
test   %rax,%rax
je     12bb <main+0xfb>
lea    0xd7c(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    12bb <main+0xfb>
lea    0xd12(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%r15d
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [7 x i8] c"fork()\00"
@.str.1 = [11 x i8] c"Child: %d\0A\00"
@WUNTRACED = global i32 0
@WCONTINUED = global i32 0
@.str.2 = [10 x i8] c"waitpid()\00"
@.str.3 = [23 x i8] c"[%d] Exit, status: %d\0A\00"
@.str.4 = [27 x i8] c"[%d] Killed by signal: %d\0A\00"
@.str.5 = [28 x i8] c"[%d] Stopped by signal: %d\0A\00"
@.str.6 = [16 x i8] c"[%d] Continued\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
switch i32 %9, label %14 [
i32 0, label %10
i32 -1, label %12
]
10: ; preds = %2
%11 = call i32 (...) @child()
br label %15
12: ; preds = %2
%13 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %3
br label %78
14: ; preds = %2
br label %15
15: ; preds = %14, %10
%16 = load i32, i32* %6
%17 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 %16)
br label %18
18: ; preds = %75, %15
%19 = load i32, i32* %6
%20 = load i32, i32* @WUNTRACED
%21 = load i32, i32* @WCONTINUED
%22 = or i32 %20, %21
%23 = call i32 @waitpid(i32 %19, i32* %7, i32 %22)
store i32 %23, i32* %8
%24 = load i32, i32* %8
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %28
26: ; preds = %18
%27 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %3
br label %78
28: ; preds = %18
%29 = load i32, i32* %7
%30 = call i64 @WIFEXITED(i32 %29)
%31 = icmp ne i64 %30, 0
br i1 %31, label %32, label %37
32: ; preds = %28
%33 = load i32, i32* %6
%34 = load i32, i32* %7
%35 = call i32 @WEXITSTATUS(i32 %34)
%36 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i32 %33, i32 %35)
br label %65
37: ; preds = %28
%38 = load i32, i32* %7
%39 = call i64 @WIFSIGNALED(i32 %38)
%40 = icmp ne i64 %39, 0
br i1 %40, label %41, label %46
41: ; preds = %37
%42 = load i32, i32* %6
%43 = load i32, i32* %7
%44 = call i32 @WTERMSIG(i32 %43)
%45 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.4, i64 0, i64 0), i32 %42, i32 %44)
br label %64
46: ; preds = %37
%47 = load i32, i32* %7
%48 = call i64 @WIFSTOPPED(i32 %47)
%49 = icmp ne i64 %48, 0
br i1 %49, label %50, label %55
50: ; preds = %46
%51 = load i32, i32* %6
%52 = load i32, i32* %7
%53 = call i32 @WSTOPSIG(i32 %52)
%54 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.5, i64 0, i64 0), i32 %51, i32 %53)
br label %63
55: ; preds = %46
%56 = load i32, i32* %7
%57 = call i64 @WIFCONTINUED(i32 %56)
%58 = icmp ne i64 %57, 0
br i1 %58, label %59, label %62
59: ; preds = %55
%60 = load i32, i32* %6
%61 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.6, i64 0, i64 0), i32 %60)
br label %62
62: ; preds = %59, %55
br label %63
63: ; preds = %62, %50
br label %64
64: ; preds = %63, %41
br label %65
65: ; preds = %64, %32
br label %66
66: ; preds = %65
%67 = load i32, i32* %7
%68 = call i64 @WIFEXITED(i32 %67)
%69 = icmp ne i64 %68, 0
br i1 %69, label %75, label %70
70: ; preds = %66
%71 = load i32, i32* %7
%72 = call i64 @WIFSIGNALED(i32 %71)
%73 = icmp ne i64 %72, 0
%74 = xor i1 %73, true
br label %75
75: ; preds = %70, %66
%76 = phi i1 [ false, %66 ], [ %74, %70 ]
br i1 %76, label %18, label %77
77: ; preds = %75
store i32 0, i32* %3
br label %78
78: ; preds = %77, %26, %12
%79 = load i32, i32* %3
ret i32 %79
}

LLVM IR (-O2):
@.str = [7 x i8] c"fork()\00"
@.str.1 = [11 x i8] c"Child: %d\0A\00"
@WUNTRACED = global i32 0
@WCONTINUED = global i32 0
@.str.2 = [10 x i8] c"waitpid()\00"
@.str.3 = [23 x i8] c"[%d] Exit, status: %d\0A\00"
@.str.4 = [27 x i8] c"[%d] Killed by signal: %d\0A\00"
@.str.5 = [28 x i8] c"[%d] Stopped by signal: %d\0A\00"
@.str.6 = [16 x i8] c"[%d] Continued\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()
switch i32 %5, label %10 [
i32 0, label %6
i32 -1, label %8
]
6: ; preds = %2
%7 = tail call i32 (...) @child()
br label %10
8: ; preds = %2
%9 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %55
10: ; preds = %2, %6
%11 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 %5)
br label %12
12: ; preds = %51, %10
%13 = load i32, i32* @WUNTRACED
%14 = load i32, i32* @WCONTINUED
%15 = or i32 %14, %13
%16 = call i32 @waitpid(i32 %5, i32* %3, i32 %15)
%17 = icmp eq i32 %16, -1
br i1 %17, label %45, label %18
18: ; preds = %12
%19 = load i32, i32* %3
%20 = call i64 @WIFEXITED(i32 %19)
%21 = icmp eq i64 %20, 0
%22 = load i32, i32* %3
br i1 %21, label %26, label %23
23: ; preds = %18
%24 = call i32 @WEXITSTATUS(i32 %22)
%25 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i32 %5, i32 %24)
br label %47
26: ; preds = %18
%27 = call i64 @WIFSIGNALED(i32 %22)
%28 = icmp eq i64 %27, 0
%29 = load i32, i32* %3
br i1 %28, label %33, label %30
30: ; preds = %26
%31 = call i32 @WTERMSIG(i32 %29)
%32 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.4, i64 0, i64 0), i32 %5, i32 %31)
br label %47
33: ; preds = %26
%34 = call i64 @WIFSTOPPED(i32 %29)
%35 = icmp eq i64 %34, 0
%36 = load i32, i32* %3
br i1 %35, label %40, label %37
37: ; preds = %33
%38 = call i32 @WSTOPSIG(i32 %36)
%39 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.5, i64 0, i64 0), i32 %5, i32 %38)
br label %47
40: ; preds = %33
%41 = call i64 @WIFCONTINUED(i32 %36)
%42 = icmp eq i64 %41, 0
br i1 %42, label %47, label %43
43: ; preds = %40
%44 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.6, i64 0, i64 0), i32 %5)
br label %47
45: ; preds = %12
%46 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
br label %55
47: ; preds = %23, %37, %43, %40, %30
%48 = load i32, i32* %3
%49 = call i64 @WIFEXITED(i32 %48)
%50 = icmp eq i64 %49, 0
br i1 %50, label %51, label %55
51: ; preds = %47
%52 = load i32, i32* %3
%53 = call i64 @WIFSIGNALED(i32 %52)
%54 = icmp eq i64 %53, 0
br i1 %54, label %12, label %55
55: ; preds = %51, %47, %45, %8
%56 = phi i32 [ 1, %45 ], [ 0, %8 ], [ 0, %47 ], [ 0, %51 ]
ret i32 %56
}