#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 EXIT_FAILURE;
int 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*/ execv(char *, char **);
int /*<<< orphan*/ exit(int);
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();
    }
    execv(argv[1], argv + 1);
    exit(127);
  } 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   1110 <fork@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1234 <main+0x44>
lea    0xddf(%rip),%rdi
call   10c0 <perror@plt>
mov    0x2dab(%rip),%rax
mov    (%rax),%edi
call   10e0 <exit@plt>
cmpl   $0x0,-0x14(%rbp)
jne    1286 <main+0x96>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %rax,%rsi
lea    0xdb6(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
cmpl   $0x1,-0x8(%rbp)
jne    1267 <main+0x77>
mov    $0x0,%al
call   1080 <pause@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x10(%rbp),%rsi
add    $0x8,%rsi
call   1100 <execv@plt>
mov    $0x7f,%edi
call   10e0 <exit@plt>
jmp    128b <main+0x9b>
mov    -0x14(%rbp),%edi
mov    0x2d4b(%rip),%rax
mov    (%rax),%edx
mov    0x2d22(%rip),%rax
or     (%rax),%edx
lea    -0x1c(%rbp),%rsi
call   10b0 <waitpid@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    12d0 <main+0xe0>
lea    0xd5a(%rip),%rdi
call   10c0 <perror@plt>
mov    0x2d0f(%rip),%rax
mov    (%rax),%edi
call   10e0 <exit@plt>
mov    -0x1c(%rbp),%edi
call   10d0 <WIFEXITED@plt>
cmp    $0x0,%rax
je     12ff <main+0x10f>
mov    -0x1c(%rbp),%edi
call   10a0 <WEXITSTATUS@plt>
mov    %eax,%esi
lea    0xd2c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    138c <main+0x19c>
mov    -0x1c(%rbp),%edi
call   1050 <WIFSIGNALED@plt>
cmp    $0x0,%rax
je     132e <main+0x13e>
mov    -0x1c(%rbp),%edi
call   1090 <WTERMSIG@plt>
mov    %eax,%esi
lea    0xd10(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1387 <main+0x197>
mov    -0x1c(%rbp),%edi
call   1070 <WIFSTOPPED@plt>
cmp    $0x0,%rax
je     135d <main+0x16d>
mov    -0x1c(%rbp),%edi
call   10f0 <WSTOPSIG@plt>
mov    %eax,%esi
lea    0xcf6(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1382 <main+0x192>
mov    -0x1c(%rbp),%edi
call   1060 <WIFCONTINUED@plt>
cmp    $0x0,%rax
je     137d <main+0x18d>
lea    0xce7(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1382 <main+0x192>
jmp    1387 <main+0x197>
jmp    138c <main+0x19c>
jmp    1391 <main+0x1a1>
mov    -0x1c(%rbp),%edi
call   10d0 <WIFEXITED@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    $0x0,%rcx
mov    %al,-0x1d(%rbp)
jne    13bf <main+0x1cf>
mov    -0x1c(%rbp),%edi
call   1050 <WIFSIGNALED@plt>
cmp    $0x0,%rax
setne  %al
xor    $0xff,%al
mov    %al,-0x1d(%rbp)
mov    -0x1d(%rbp),%al
test   $0x1,%al
jne    128b <main+0x9b>
mov    0x2bff(%rip),%rax
mov    (%rax),%edi
call   10e0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%rbp
mov    %edi,%r14d
xor    %eax,%eax
call   1120 <fork@plt>
cmp    $0xffffffff,%eax
je     1262 <main+0x62>
mov    %eax,%ebx
test   %eax,%eax
jne    126e <main+0x6e>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xdd4(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
cmp    $0x1,%r14d
jne    1248 <main+0x48>
xor    %eax,%eax
call   1090 <pause@plt>
mov    0x8(%rbp),%rdi
add    $0x8,%rbp
mov    %rbp,%rsi
call   1110 <execv@plt>
mov    $0x7f,%edi
call   10f0 <exit@plt>
lea    0xd97(%rip),%rdi
jmp    1368 <main+0x168>
mov    0x2d6b(%rip),%r15
mov    0x2d44(%rip),%r12
lea    0x4(%rsp),%r14
lea    0xdaa(%rip),%r13
lea    0xd90(%rip),%rbp
nop
mov    (%r12),%edx
or     (%r15),%edx
mov    %ebx,%edi
mov    %r14,%rsi
call   10c0 <waitpid@plt>
cmp    $0xffffffff,%eax
je     1361 <main+0x161>
mov    0x4(%rsp),%edi
call   10e0 <WIFEXITED@plt>
mov    0x4(%rsp),%edi
test   %rax,%rax
je     12d0 <main+0xd0>
call   10b0 <WEXITSTATUS@plt>
mov    %rbp,%rdi
jmp    1310 <main+0x110>
cs nopw 0x0(%rax,%rax,1)
call   1060 <WIFSIGNALED@plt>
mov    0x4(%rsp),%edi
test   %rax,%rax
je     12e8 <main+0xe8>
call   10a0 <WTERMSIG@plt>
mov    %r13,%rdi
jmp    1310 <main+0x110>
call   1080 <WIFSTOPPED@plt>
mov    0x4(%rsp),%edi
test   %rax,%rax
je     133b <main+0x13b>
call   1100 <WSTOPSIG@plt>
lea    0xd45(%rip),%rdi
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x4(%rsp),%edi
call   10e0 <WIFEXITED@plt>
test   %rax,%rax
jne    1353 <main+0x153>
mov    0x4(%rsp),%edi
call   1060 <WIFSIGNALED@plt>
test   %rax,%rax
je     1290 <main+0x90>
jmp    1353 <main+0x153>
call   1070 <WIFCONTINUED@plt>
test   %rax,%rax
je     1319 <main+0x119>
lea    0xd11(%rip),%rdi
call   1030 <puts@plt>
jmp    1319 <main+0x119>
mov    0x2c76(%rip),%rax
mov    (%rax),%edi
call   10f0 <exit@plt>
lea    0xcaf(%rip),%rdi
call   10d0 <perror@plt>
mov    0x2c64(%rip),%rax
mov    (%rax),%edi
call   10f0 <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 %34
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 = load i8**, i8*** %5
%31 = getelementptr inbounds i8*, i8** %30, i64 1
%32 = call i32 @execv(i8* %29, i8** %31)
%33 = call i32 @exit(i32 127)
unreachable
34: ; preds = %16
br label %35
35: ; preds = %90, %34
%36 = load i32, i32* %6
%37 = load i32, i32* @WUNTRACED
%38 = load i32, i32* @WCONTINUED
%39 = or i32 %37, %38
%40 = call i32 @waitpid(i32 %36, i32* %8, i32 %39)
store i32 %40, i32* %7
%41 = load i32, i32* %7
%42 = icmp eq i32 %41, -1
br i1 %42, label %43, label %47
43: ; preds = %35
%44 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
%45 = load i32, i32* @EXIT_FAILURE
%46 = call i32 @exit(i32 %45)
unreachable
47: ; preds = %35
%48 = load i32, i32* %8
%49 = call i64 @WIFEXITED(i32 %48)
%50 = icmp ne i64 %49, 0
br i1 %50, label %51, label %55
51: ; preds = %47
%52 = load i32, i32* %8
%53 = call i32 @WEXITSTATUS(i32 %52)
%54 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i32 %53)
br label %80
55: ; preds = %47
%56 = load i32, i32* %8
%57 = call i64 @WIFSIGNALED(i32 %56)
%58 = icmp ne i64 %57, 0
br i1 %58, label %59, label %63
59: ; preds = %55
%60 = load i32, i32* %8
%61 = call i32 @WTERMSIG(i32 %60)
%62 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0), i32 %61)
br label %79
63: ; preds = %55
%64 = load i32, i32* %8
%65 = call i64 @WIFSTOPPED(i32 %64)
%66 = icmp ne i64 %65, 0
br i1 %66, label %67, label %71
67: ; preds = %63
%68 = load i32, i32* %8
%69 = call i32 @WSTOPSIG(i32 %68)
%70 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0), i32 %69)
br label %78
71: ; preds = %63
%72 = load i32, i32* %8
%73 = call i64 @WIFCONTINUED(i32 %72)
%74 = icmp ne i64 %73, 0
br i1 %74, label %75, label %77
75: ; preds = %71
%76 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.6, i64 0, i64 0))
br label %77
77: ; preds = %75, %71
br label %78
78: ; preds = %77, %67
br label %79
79: ; preds = %78, %59
br label %80
80: ; preds = %79, %51
br label %81
81: ; preds = %80
%82 = load i32, i32* %8
%83 = call i64 @WIFEXITED(i32 %82)
%84 = icmp ne i64 %83, 0
br i1 %84, label %90, label %85
85: ; preds = %81
%86 = load i32, i32* %8
%87 = call i64 @WIFSIGNALED(i32 %86)
%88 = icmp ne i64 %87, 0
%89 = xor i1 %88, true
br label %90
90: ; preds = %85, %81
%91 = phi i1 [ false, %81 ], [ %89, %85 ]
br i1 %91, label %35, label %92
92: ; preds = %90
%93 = load i32, i32* @EXIT_SUCCESS
%94 = call i32 @exit(i32 %93)
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** %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 @execv(i8* %18, i8** %17)
%20 = tail call i32 @exit(i32 127)
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
}