#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 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);
int fork();
int getpid();
int getppid();
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sleep(int);
int waitpid(int, int *, int);
int main() {
  pid_t child_pid, wpid;
  int status;
  child_pid = fork();
  if (child_pid == -1) {
    printf("fork error\n");
    exit(EXIT_FAILURE);
  }
  if (child_pid == 0) {
    printf("in c child_pid = %d\n", child_pid);
    printf("c id = %d\n", getpid());
    printf("p id = %d\n", getppid());
    sleep(3);
    exit(4);
  } else {
    printf("p2 id = %d\n", getpid());
    do {
      printf("child_pid = %d\n", child_pid);
      wpid = waitpid(child_pid, &status, WUNTRACED | WCONTINUED);
      if (wpid == -1) {
        printf("waitpid error\n");
        exit(EXIT_FAILURE);
      }
      if (WIFEXITED(status)) {
        printf("child exited, status = %d\n", WEXITSTATUS(status));
      } else if (WIFSIGNALED(status)) {
        printf("child killed (signal %d) \n", WTERMSIG(status));
      } else if (WIFSTOPPED(status)) {
        printf("child stopped (signal %d) \n", WSTOPSIG(status));
      } else if (WIFCONTINUED(status)) {
        printf("child continued\n");
      } else {
        printf("Unexpected status (0x%x) \n", status);
      }
    } while (!WIFEXITED(status) && !WIFSIGNALED(status));
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1100 <fork@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    121f <main+0x3f>
lea    0xdf6(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2dc0(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
cmpl   $0x0,-0x8(%rbp)
jne    127c <main+0x9c>
mov    -0x8(%rbp),%esi
lea    0xdd9(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xdd7(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   10c0 <getppid@plt>
mov    %eax,%esi
lea    0xdcb(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x3,%edi
call   10e0 <sleep@plt>
mov    $0x4,%edi
call   10d0 <exit@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xdab(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%esi
lea    0xd74(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%edi
mov    0x2d32(%rip),%rax
mov    (%rax),%edx
mov    0x2d11(%rip),%rax
or     (%rax),%edx
lea    -0x10(%rbp),%rsi
call   10a0 <waitpid@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    12eb <main+0x10b>
lea    0xd6d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2cf4(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
mov    -0x10(%rbp),%edi
call   10b0 <WIFEXITED@plt>
cmp    $0x0,%rax
je     131a <main+0x13a>
mov    -0x10(%rbp),%edi
call   1090 <WEXITSTATUS@plt>
mov    %eax,%esi
lea    0xd44(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    13bd <main+0x1dd>
mov    -0x10(%rbp),%edi
call   1050 <WIFSIGNALED@plt>
cmp    $0x0,%rax
je     1349 <main+0x169>
mov    -0x10(%rbp),%edi
call   1080 <WTERMSIG@plt>
mov    %eax,%esi
lea    0xd30(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    13b8 <main+0x1d8>
mov    -0x10(%rbp),%edi
call   1070 <WIFSTOPPED@plt>
cmp    $0x0,%rax
je     1378 <main+0x198>
mov    -0x10(%rbp),%edi
call   10f0 <WSTOPSIG@plt>
mov    %eax,%esi
lea    0xd1c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    13b3 <main+0x1d3>
mov    -0x10(%rbp),%edi
call   1060 <WIFCONTINUED@plt>
cmp    $0x0,%rax
je     139d <main+0x1bd>
lea    0xd13(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    13ae <main+0x1ce>
mov    -0x10(%rbp),%esi
lea    0xd0e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    13b3 <main+0x1d3>
jmp    13b8 <main+0x1d8>
jmp    13bd <main+0x1dd>
jmp    13c2 <main+0x1e2>
mov    -0x10(%rbp),%edi
call   10b0 <WIFEXITED@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    $0x0,%rcx
mov    %al,-0x11(%rbp)
jne    13f0 <main+0x210>
mov    -0x10(%rbp),%edi
call   1050 <WIFSIGNALED@plt>
cmp    $0x0,%rax
setne  %al
xor    $0xff,%al
mov    %al,-0x11(%rbp)
mov    -0x11(%rbp),%al
test   $0x1,%al
jne    1293 <main+0xb3>
jmp    1400 <main+0x220>
xor    %eax,%eax
add    $0x20,%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   1110 <fork@plt>
test   %eax,%eax
je     135b <main+0x16b>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     13ad <main+0x1bd>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe08(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xdd2(%rip),%r14
mov    0x2da6(%rip),%rbp
mov    0x2d87(%rip),%r12
lea    0x4(%rsp),%r15
lea    0xdea(%rip),%r13
nopl   (%rax)
mov    %r14,%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    (%r12),%edx
or     0x0(%rbp),%edx
mov    %ebx,%edi
mov    %r15,%rsi
call   10b0 <waitpid@plt>
cmp    $0xffffffff,%eax
je     1341 <main+0x151>
mov    0x4(%rsp),%edi
call   10c0 <WIFEXITED@plt>
mov    0x4(%rsp),%edi
test   %rax,%rax
je     12a0 <main+0xb0>
call   10a0 <WEXITSTATUS@plt>
mov    %r13,%rdi
jmp    12e0 <main+0xf0>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
call   1060 <WIFSIGNALED@plt>
mov    0x4(%rsp),%edi
test   %rax,%rax
je     12bc <main+0xcc>
call   1090 <WTERMSIG@plt>
lea    0xd98(%rip),%rdi
jmp    12e0 <main+0xf0>
call   1080 <WIFSTOPPED@plt>
mov    0x4(%rsp),%edi
test   %rax,%rax
je     130b <main+0x11b>
call   1100 <WSTOPSIG@plt>
lea    0xd97(%rip),%rdi
cs nopw 0x0(%rax,%rax,1)
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x4(%rsp),%edi
call   10c0 <WIFEXITED@plt>
test   %rax,%rax
jne    1330 <main+0x140>
mov    0x4(%rsp),%edi
call   1060 <WIFSIGNALED@plt>
test   %rax,%rax
je     1250 <main+0x60>
jmp    1330 <main+0x140>
call   1070 <WIFCONTINUED@plt>
test   %rax,%rax
je     1323 <main+0x133>
lea    0xd88(%rip),%rdi
call   1030 <puts@plt>
jmp    12e9 <main+0xf9>
mov    0x4(%rsp),%esi
lea    0xd5b(%rip),%rdi
jmp    12e2 <main+0xf2>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd6c(%rip),%rdi
call   1030 <puts@plt>
mov    0x2c84(%rip),%rax
mov    (%rax),%edi
call   10e0 <exit@plt>
lea    0xc9e(%rip),%rdi
xor    %esi,%esi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xc9c(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
call   10d0 <getppid@plt>
lea    0xc90(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x3,%edi
call   10f0 <sleep@plt>
mov    $0x4,%edi
call   10e0 <exit@plt>
lea    0xd0e(%rip),%rdi
jmp    1348 <main+0x158>

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

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