#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 /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
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*/ _exit(int /*<<< orphan*/);
int /*<<< orphan*/ atoi(char *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
scalar_t__ getpid();
int /*<<< orphan*/ pause();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int waitpid(int, int *, int);
int main(int argc, char *argv[]) {
  pid_t cpid, w;
  int status;
  cpid = fork();
  if (cpid == -1) {
    perror("fork");
    exit(EXIT_FAILURE);
  }
  if (cpid == 0) {
    printf("Child PID is %ld\n", (long)getpid());
    if (argc == 1)
      pause();
    _exit(atoi(argv[1]));
  } else {
    do {
      w = waitpid(cpid, &status, WUNTRACED | WCONTINUED);
      if (w == -1) {
        perror("waitpid");
        exit(EXIT_FAILURE);
      }
      if (WIFEXITED(status)) {
        printf("exited, status=%d\n", WEXITSTATUS(status));
      } else if (WIFSIGNALED(status)) {
        printf("killed by signal %d\n", WTERMSIG(status));
      } else if (WIFSTOPPED(status)) {
        printf("stopped by signal %d\n", WSTOPSIG(status));
      } else if (WIFCONTINUED(status)) {
        printf("continued\n");
      }
    } while (!WIFEXITED(status) && !WIFSIGNALED(status));
    exit(EXIT_SUCCESS);
  }
}


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   1120 <fork@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1244 <main+0x44>
lea    0xdcf(%rip),%rdi
call   10d0 <perror@plt>
mov    0x2d9b(%rip),%rax
mov    (%rax),%edi
call   1100 <exit@plt>
cmpl   $0x0,-0x14(%rbp)
jne    128b <main+0x8b>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %rax,%rsi
lea    0xda6(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
cmpl   $0x1,-0x8(%rbp)
jne    1277 <main+0x77>
mov    $0x0,%al
call   1090 <pause@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10e0 <atoi@plt>
mov    %eax,%edi
call   1030 <_exit@plt>
jmp    1290 <main+0x90>
mov    -0x14(%rbp),%edi
mov    0x2d46(%rip),%rax
mov    (%rax),%edx
mov    0x2d1d(%rip),%rax
or     (%rax),%edx
lea    -0x1c(%rbp),%rsi
call   10c0 <waitpid@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    12d5 <main+0xd5>
lea    0xd55(%rip),%rdi
call   10d0 <perror@plt>
mov    0x2d0a(%rip),%rax
mov    (%rax),%edi
call   1100 <exit@plt>
mov    -0x1c(%rbp),%edi
call   10f0 <WIFEXITED@plt>
cmp    $0x0,%rax
je     1304 <main+0x104>
mov    -0x1c(%rbp),%edi
call   10b0 <WEXITSTATUS@plt>
mov    %eax,%esi
lea    0xd27(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1391 <main+0x191>
mov    -0x1c(%rbp),%edi
call   1060 <WIFSIGNALED@plt>
cmp    $0x0,%rax
je     1333 <main+0x133>
mov    -0x1c(%rbp),%edi
call   10a0 <WTERMSIG@plt>
mov    %eax,%esi
lea    0xd0b(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    138c <main+0x18c>
mov    -0x1c(%rbp),%edi
call   1080 <WIFSTOPPED@plt>
cmp    $0x0,%rax
je     1362 <main+0x162>
mov    -0x1c(%rbp),%edi
call   1110 <WSTOPSIG@plt>
mov    %eax,%esi
lea    0xcf1(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1387 <main+0x187>
mov    -0x1c(%rbp),%edi
call   1070 <WIFCONTINUED@plt>
cmp    $0x0,%rax
je     1382 <main+0x182>
lea    0xce2(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1387 <main+0x187>
jmp    138c <main+0x18c>
jmp    1391 <main+0x191>
jmp    1396 <main+0x196>
mov    -0x1c(%rbp),%edi
call   10f0 <WIFEXITED@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    $0x0,%rcx
mov    %al,-0x1d(%rbp)
jne    13c4 <main+0x1c4>
mov    -0x1c(%rbp),%edi
call   1060 <WIFSIGNALED@plt>
cmp    $0x0,%rax
setne  %al
xor    $0xff,%al
mov    %al,-0x1d(%rbp)
mov    -0x1d(%rbp),%al
test   $0x1,%al
jne    1290 <main+0x90>
mov    0x2bfa(%rip),%rax
mov    (%rax),%edi
call   1100 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %edi,%ebp
xor    %eax,%eax
call   1130 <fork@plt>
cmp    $0xffffffff,%eax
je     1266 <main+0x56>
mov    %eax,%ebx
test   %eax,%eax
jne    1272 <main+0x62>
xor    %eax,%eax
call   1050 <getpid@plt>
lea    0xdc5(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
cmp    $0x1,%ebp
jne    1256 <main+0x46>
xor    %eax,%eax
call   10a0 <pause@plt>
mov    0x8(%r14),%rdi
call   10f0 <atoi@plt>
mov    %eax,%edi
call   1030 <_exit@plt>
lea    0xd93(%rip),%rdi
jmp    1378 <main+0x168>
mov    0x2d67(%rip),%r15
mov    0x2d40(%rip),%r12
lea    0x4(%rsp),%r14
lea    0xda6(%rip),%r13
lea    0xd8c(%rip),%rbp
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    (%r12),%edx
or     (%r15),%edx
mov    %ebx,%edi
mov    %r14,%rsi
call   10d0 <waitpid@plt>
cmp    $0xffffffff,%eax
je     1371 <main+0x161>
mov    0x4(%rsp),%edi
call   1100 <WIFEXITED@plt>
mov    0x4(%rsp),%edi
test   %rax,%rax
je     12e0 <main+0xd0>
call   10c0 <WEXITSTATUS@plt>
mov    %rbp,%rdi
jmp    1320 <main+0x110>
cs nopw 0x0(%rax,%rax,1)
call   1070 <WIFSIGNALED@plt>
mov    0x4(%rsp),%edi
test   %rax,%rax
je     12f8 <main+0xe8>
call   10b0 <WTERMSIG@plt>
mov    %r13,%rdi
jmp    1320 <main+0x110>
call   1090 <WIFSTOPPED@plt>
mov    0x4(%rsp),%edi
test   %rax,%rax
je     134b <main+0x13b>
call   1120 <WSTOPSIG@plt>
lea    0xd35(%rip),%rdi
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x4(%rsp),%edi
call   1100 <WIFEXITED@plt>
test   %rax,%rax
jne    1363 <main+0x153>
mov    0x4(%rsp),%edi
call   1070 <WIFSIGNALED@plt>
test   %rax,%rax
je     12a0 <main+0x90>
jmp    1363 <main+0x153>
call   1080 <WIFCONTINUED@plt>
test   %rax,%rax
je     1329 <main+0x119>
lea    0xd01(%rip),%rdi
call   1040 <puts@plt>
jmp    1329 <main+0x119>
mov    0x2c66(%rip),%rax
mov    (%rax),%edi
call   1110 <exit@plt>
lea    0xc9f(%rip),%rdi
call   10e0 <perror@plt>
mov    0x2c54(%rip),%rax
mov    (%rax),%edi
call   1110 <exit@plt>

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

LLVM IR (-O2):
@.str = [5 x i8] c"fork\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [18 x i8] c"Child PID is %ld\0A\00"
@WUNTRACED = global i32 0
@WCONTINUED = global i32 0
@.str.2 = [8 x i8] c"waitpid\00"
@.str.3 = [19 x i8] c"exited, status=%d\0A\00"
@.str.4 = [21 x i8] c"killed by signal %d\0A\00"
@.str.5 = [22 x i8] c"stopped by signal %d\0A\00"
@EXIT_SUCCESS = global i32 0
@str = [10 x i8] c"continued\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = tail call i32 (...) @fork()
switch i32 %5, label %21 [
i32 -1, label %6
i32 0, label %10
]
6: ; preds = %2
%7 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%8 = load i32, i32* @EXIT_FAILURE
%9 = tail call i32 @exit(i32 %8)
unreachable
10: ; preds = %2
%11 = tail call i64 (...) @getpid()
%12 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i64 %11)
%13 = icmp eq i32 %0, 1
br i1 %13, label %14, label %16
14: ; preds = %10
%15 = tail call i32 (...) @pause()
br label %16
16: ; preds = %14, %10
%17 = getelementptr inbounds i8*, i8** %1, i64 1
%18 = load i8*, i8** %17
%19 = tail call i32 @atoi(i8* %18)
%20 = tail call i32 @_exit(i32 %19)
unreachable
21: ; preds = %2, %62
%22 = load i32, i32* @WUNTRACED
%23 = load i32, i32* @WCONTINUED
%24 = or i32 %23, %22
%25 = call i32 @waitpid(i32 %5, i32* %3, i32 %24)
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %31
27: ; preds = %21
%28 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
%29 = load i32, i32* @EXIT_FAILURE
%30 = call i32 @exit(i32 %29)
unreachable
31: ; preds = %21
%32 = load i32, i32* %3
%33 = call i64 @WIFEXITED(i32 %32)
%34 = icmp eq i64 %33, 0
%35 = load i32, i32* %3
br i1 %34, label %39, label %36
36: ; preds = %31
%37 = call i32 @WEXITSTATUS(i32 %35)
%38 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i32 %37)
br label %58
39: ; preds = %31
%40 = call i64 @WIFSIGNALED(i32 %35)
%41 = icmp eq i64 %40, 0
%42 = load i32, i32* %3
br i1 %41, label %46, label %43
43: ; preds = %39
%44 = call i32 @WTERMSIG(i32 %42)
%45 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0), i32 %44)
br label %58
46: ; preds = %39
%47 = call i64 @WIFSTOPPED(i32 %42)
%48 = icmp eq i64 %47, 0
%49 = load i32, i32* %3
br i1 %48, label %53, label %50
50: ; preds = %46
%51 = call i32 @WSTOPSIG(i32 %49)
%52 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0), i32 %51)
br label %58
53: ; preds = %46
%54 = call i64 @WIFCONTINUED(i32 %49)
%55 = icmp eq i64 %54, 0
br i1 %55, label %58, label %56
56: ; preds = %53
%57 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @str, i64 0, i64 0))
br label %58
58: ; preds = %36, %50, %56, %53, %43
%59 = load i32, i32* %3
%60 = call i64 @WIFEXITED(i32 %59)
%61 = icmp eq i64 %60, 0
br i1 %61, label %62, label %66
62: ; preds = %58
%63 = load i32, i32* %3
%64 = call i64 @WIFSIGNALED(i32 %63)
%65 = icmp eq i64 %64, 0
br i1 %65, label %21, label %66
66: ; preds = %58, %62
%67 = load i32, i32* @EXIT_SUCCESS
%68 = call i32 @exit(i32 %67)
unreachable
}