#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 /*<<< orphan*/ pid_t;

/* Variables and functions */
int EXIT_SUCCESS;
int WEXITSTATUS(int);
scalar_t__ WIFEXITED(int);
scalar_t__ WIFSIGNALED(int);
scalar_t__ WIFSTOPPED(int);
int WSTOPSIG(int);
int WTERMSIG(int);
int /*<<< orphan*/ WUNTRACED;
int /*<<< orphan*/ _exit(int /*<<< orphan*/);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fork();
int getpid();
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ waitpid(int /*<<< orphan*/, int *, int /*<<< orphan*/);
int main(void) {
  pid_t pid;
  int status;
  if ((pid = fork()) == 0) {
    printf("I am a child with pid = %d\n", getpid());
    sleep(30);
    printf("child terminates\n");
    _exit(0);
  } else {
    while (1) {
      waitpid(pid, &status, WUNTRACED);
      if (WIFSTOPPED(status)) {
        printf("child stopped, signal(%d)\n", WSTOPSIG(status));
        continue;
      } else if (WIFEXITED(status)) {
        printf("normal termination with status(%d)\n", WEXITSTATUS(status));
        break;
      } else if (WIFSIGNALED(status)) {
        printf("abnormal termination, signal(%d)\n", WTERMSIG(status));
        exit(0);
      }
    }
  }
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   10f0 <fork@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jne    1228 <main+0x58>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%esi
lea    0xdfe(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x1e,%edi
call   10d0 <sleep@plt>
lea    0xe02(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
xor    %edi,%edi
call   1030 <_exit@plt>
jmp    122d <main+0x5d>
mov    -0x8(%rbp),%edi
mov    0x2da9(%rip),%rax
mov    (%rax),%edx
lea    -0xc(%rbp),%rsi
call   10a0 <waitpid@plt>
mov    -0xc(%rbp),%edi
call   1070 <WIFSTOPPED@plt>
cmp    $0x0,%rax
je     1271 <main+0xa1>
mov    -0xc(%rbp),%edi
call   10e0 <WSTOPSIG@plt>
mov    %eax,%esi
lea    0xdc9(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    122d <main+0x5d>
mov    -0xc(%rbp),%edi
call   10b0 <WIFEXITED@plt>
cmp    $0x0,%rax
je     12a0 <main+0xd0>
mov    -0xc(%rbp),%edi
call   1090 <WEXITSTATUS@plt>
mov    %eax,%esi
lea    0xdb5(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    12e0 <main+0x110>
mov    -0xc(%rbp),%edi
call   1060 <WIFSIGNALED@plt>
cmp    $0x0,%rax
je     12d1 <main+0x101>
mov    -0xc(%rbp),%edi
call   1080 <WTERMSIG@plt>
mov    %eax,%esi
lea    0xdaa(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
xor    %edi,%edi
call   10c0 <exit@plt>
jmp    12d6 <main+0x106>
jmp    12db <main+0x10b>
jmp    122d <main+0x5d>
jmp    12e5 <main+0x115>
mov    0x2cec(%rip),%rax
mov    (%rax),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
xor    %eax,%eax
call   1100 <fork@plt>
test   %eax,%eax
je     12a3 <main+0xc3>
mov    %eax,%ebx
mov    0x2de0(%rip),%r12
lea    0x4(%rsp),%r14
lea    0xe10(%rip),%r15
jmp    1221 <main+0x41>
xchg   %ax,%ax
call   10f0 <WSTOPSIG@plt>
mov    %r15,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    (%r12),%edx
mov    %ebx,%edi
mov    %r14,%rsi
call   10b0 <waitpid@plt>
mov    0x4(%rsp),%edi
call   1080 <WIFSTOPPED@plt>
mov    0x4(%rsp),%edi
test   %rax,%rax
jne    1210 <main+0x30>
call   10c0 <WIFEXITED@plt>
mov    0x4(%rsp),%edi
test   %rax,%rax
jne    1279 <main+0x99>
call   1070 <WIFSIGNALED@plt>
test   %rax,%rax
je     1221 <main+0x41>
mov    0x4(%rsp),%edi
call   1090 <WTERMSIG@plt>
lea    0xdf2(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
xor    %edi,%edi
call   10d0 <exit@plt>
call   10a0 <WEXITSTATUS@plt>
lea    0xdb2(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x2d43(%rip),%rax
mov    (%rax),%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
xor    %eax,%eax
call   1050 <getpid@plt>
lea    0xd4f(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0x1e,%edi
call   10e0 <sleep@plt>
lea    0xdb2(%rip),%rdi
call   1040 <puts@plt>
xor    %edi,%edi
call   1030 <_exit@plt>

LLVM IR (-O0):
@.str = [28 x i8] c"I am a child with pid = %d\0A\00"
@.str.1 = [18 x i8] c"child terminates\0A\00"
@WUNTRACED = global i32 0
@.str.2 = [27 x i8] c"child stopped, signal(%d)\0A\00"
@.str.3 = [36 x i8] c"normal termination with status(%d)\0A\00"
@.str.4 = [34 x i8] c"abnormal termination, signal(%d)\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
store i32 0, i32* %1
%4 = call i32 (...) @fork()
store i32 %4, i32* %2
%5 = icmp eq i32 %4, 0
br i1 %5, label %6, label %12
6: ; preds = %0
%7 = call i32 (...) @getpid()
%8 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i32 %7)
%9 = call i32 @sleep(i32 30)
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
%11 = call i32 @_exit(i32 0)
unreachable
12: ; preds = %0
br label %13
13: ; preds = %12, %20, %43
%14 = load i32, i32* %2
%15 = load i32, i32* @WUNTRACED
%16 = call i32 @waitpid(i32 %14, i32* %3, i32 %15)
%17 = load i32, i32* %3
%18 = call i64 @WIFSTOPPED(i32 %17)
%19 = icmp ne i64 %18, 0
br i1 %19, label %20, label %24
20: ; preds = %13
%21 = load i32, i32* %3
%22 = call i32 @WSTOPSIG(i32 %21)
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i32 %22)
br label %13
24: ; preds = %13
%25 = load i32, i32* %3
%26 = call i64 @WIFEXITED(i32 %25)
%27 = icmp ne i64 %26, 0
br i1 %27, label %28, label %32
28: ; preds = %24
%29 = load i32, i32* %3
%30 = call i32 @WEXITSTATUS(i32 %29)
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0), i32 %30)
br label %44
32: ; preds = %24
%33 = load i32, i32* %3
%34 = call i64 @WIFSIGNALED(i32 %33)
%35 = icmp ne i64 %34, 0
br i1 %35, label %36, label %41
36: ; preds = %32
%37 = load i32, i32* %3
%38 = call i32 @WTERMSIG(i32 %37)
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.4, i64 0, i64 0), i32 %38)
%40 = call i32 @exit(i32 0)
unreachable
41: ; preds = %32
br label %42
42: ; preds = %41
br label %43
43: ; preds = %42
br label %13
44: ; preds = %28
br label %45
45: ; preds = %44
%46 = load i32, i32* @EXIT_SUCCESS
ret i32 %46
}

LLVM IR (-O2):
@.str = [28 x i8] c"I am a child with pid = %d\0A\00"
@WUNTRACED = global i32 0
@.str.2 = [27 x i8] c"child stopped, signal(%d)\0A\00"
@.str.3 = [36 x i8] c"normal termination with status(%d)\0A\00"
@.str.4 = [34 x i8] c"abnormal termination, signal(%d)\0A\00"
@EXIT_SUCCESS = global i32 0
@str = [17 x i8] c"child terminates\00"
define i32 @main() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
%3 = tail call i32 (...) @fork()
%4 = icmp eq i32 %3, 0
br i1 %4, label %5, label %11
5: ; preds = %0
%6 = tail call i32 (...) @getpid()
%7 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i32 %6)
%8 = tail call i32 @sleep(i32 30)
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str, i64 0, i64 0))
%10 = tail call i32 @_exit(i32 0)
unreachable
11: ; preds = %0, %21
%12 = load i32, i32* @WUNTRACED
%13 = call i32 @waitpid(i32 %3, i32* %1, i32 %12)
%14 = load i32, i32* %1
%15 = call i64 @WIFSTOPPED(i32 %14)
%16 = icmp eq i64 %15, 0
%17 = load i32, i32* %1
br i1 %16, label %22, label %18
18: ; preds = %11
%19 = call i32 @WSTOPSIG(i32 %17)
%20 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i32 %19)
br label %21
21: ; preds = %18, %30
br label %11
22: ; preds = %11
%23 = call i64 @WIFEXITED(i32 %17)
%24 = icmp eq i64 %23, 0
%25 = load i32, i32* %1
br i1 %24, label %30, label %26
26: ; preds = %22
%27 = call i32 @WEXITSTATUS(i32 %25)
%28 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0), i32 %27)
%29 = load i32, i32* @EXIT_SUCCESS
ret i32 %29
30: ; preds = %22
%31 = call i64 @WIFSIGNALED(i32 %25)
%32 = icmp eq i64 %31, 0
br i1 %32, label %21, label %33
33: ; preds = %30
%34 = load i32, i32* %1
%35 = call i32 @WTERMSIG(i32 %34)
%36 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @.str.4, i64 0, i64 0), i32 %35)
%37 = call i32 @exit(i32 0)
unreachable
}