#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 scalar_t__ pid_t;

/* Variables and functions */
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 /*<<< orphan*/ err_msg(char *);
int /*<<< orphan*/ err_sys(char *);
scalar_t__ fork();
int getpid();
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sleep(int);
scalar_t__ wait(int *);
int main(int argc, char *argv[]) {
  pid_t pid = 0;
  if ((pid = fork()) < 0)
    err_sys("fork error");
  else if (pid == 0)
    printf("this is child %d\n", getpid());
  else
    printf("before wait any child\n");
  pid_t cld = 0;
  int status = 0;
  cld = wait(&status);
  if (cld == -1) {
    err_msg("wait[p][id] failed");
    sleep(20);
    return -1;
  } else {
    printf("wait child %d, ret = 0x%x\n", cld, status);
    if (WIFEXITED(status))
      printf("exit code = %d\n", WEXITSTATUS(status));
    else if (WIFSIGNALED(status))
      printf("signaled by %d\n", WTERMSIG(status));
    else if (WIFSTOPPED(status))
      printf("stopped by %d\n", WSTOPSIG(status));
    else if (WIFCONTINUED(status))
      printf("contined\n");
    else
      printf("unknow process status\n");
  }
  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)
movq   $0x0,-0x18(%rbp)
mov    $0x0,%al
call   1100 <fork@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jge    1224 <main+0x44>
lea    0xde6(%rip),%rdi
call   1030 <err_sys@plt>
jmp    125e <main+0x7e>
cmpq   $0x0,-0x18(%rbp)
jne    124b <main+0x6b>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%esi
lea    0xdcc(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1259 <main+0x79>
lea    0xdcb(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    125e <main+0x7e>
movq   $0x0,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
lea    -0x24(%rbp),%rdi
call   10f0 <wait@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0xffffffffffffffff,-0x20(%rbp)
jne    12a7 <main+0xc7>
lea    0xda8(%rip),%rdi
call   10b0 <err_msg@plt>
mov    $0x14,%edi
call   10d0 <sleep@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1397 <main+0x1b7>
mov    -0x20(%rbp),%rsi
mov    -0x24(%rbp),%edx
lea    0xd92(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x24(%rbp),%edi
call   10c0 <WIFEXITED@plt>
cmp    $0x0,%rax
je     12eb <main+0x10b>
mov    -0x24(%rbp),%edi
call   10a0 <WEXITSTATUS@plt>
mov    %eax,%esi
lea    0xd83(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    138b <main+0x1ab>
mov    -0x24(%rbp),%edi
call   1060 <WIFSIGNALED@plt>
cmp    $0x0,%rax
je     131a <main+0x13a>
mov    -0x24(%rbp),%edi
call   1090 <WTERMSIG@plt>
mov    %eax,%esi
lea    0xd64(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1386 <main+0x1a6>
mov    -0x24(%rbp),%edi
call   1080 <WIFSTOPPED@plt>
cmp    $0x0,%rax
je     1349 <main+0x169>
mov    -0x24(%rbp),%edi
call   10e0 <WSTOPSIG@plt>
mov    %eax,%esi
lea    0xd45(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1381 <main+0x1a1>
mov    -0x24(%rbp),%edi
call   1070 <WIFCONTINUED@plt>
cmp    $0x0,%rax
je     136e <main+0x18e>
lea    0xd2f(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    137c <main+0x19c>
lea    0xd26(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1381 <main+0x1a1>
jmp    1386 <main+0x1a6>
jmp    138b <main+0x1ab>
jmp    1390 <main+0x1b0>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
xor    %eax,%eax
call   1110 <fork@plt>
test   %rax,%rax
js     1211 <main+0x21>
je     121f <main+0x2f>
lea    0xe70(%rip),%rdi
call   1030 <puts@plt>
jmp    1236 <main+0x46>
lea    0xde8(%rip),%rdi
call   1040 <err_sys@plt>
jmp    1236 <main+0x46>
xor    %eax,%eax
call   1050 <getpid@plt>
lea    0xdde(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
movl   $0x0,0xc(%rsp)
lea    0xc(%rsp),%rdi
call   1100 <wait@plt>
cmp    $0xffffffffffffffff,%rax
je     1285 <main+0x95>
mov    0xc(%rsp),%edx
lea    0xdd7(%rip),%rdi
xor    %ebx,%ebx
mov    %rax,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0xc(%rsp),%edi
call   10d0 <WIFEXITED@plt>
mov    0xc(%rsp),%edi
test   %rax,%rax
je     12a2 <main+0xb2>
call   10b0 <WEXITSTATUS@plt>
lea    0xdc8(%rip),%rdi
jmp    12da <main+0xea>
lea    0xd91(%rip),%rdi
call   10c0 <err_msg@plt>
mov    $0x14,%edi
call   10e0 <sleep@plt>
mov    $0xffffffff,%ebx
jmp    12e3 <main+0xf3>
call   1070 <WIFSIGNALED@plt>
mov    0xc(%rsp),%edi
test   %rax,%rax
je     12be <main+0xce>
call   10a0 <WTERMSIG@plt>
lea    0xd9f(%rip),%rdi
jmp    12d8 <main+0xe8>
call   1090 <WIFSTOPPED@plt>
mov    0xc(%rsp),%edi
test   %rax,%rax
je     12eb <main+0xfb>
call   10f0 <WSTOPSIG@plt>
lea    0xd93(%rip),%rdi
xor    %ebx,%ebx
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
ret
call   1080 <WIFCONTINUED@plt>
test   %rax,%rax
je     12fe <main+0x10e>
lea    0xdaa(%rip),%rdi
jmp    1305 <main+0x115>
lea    0xd8b(%rip),%rdi
call   1030 <puts@plt>
jmp    12e3 <main+0xf3>

LLVM IR (-O0):
@.str = [11 x i8] c"fork error\00"
@.str.1 = [18 x i8] c"this is child %d\0A\00"
@.str.2 = [23 x i8] c"before wait any child\0A\00"
@.str.3 = [19 x i8] c"wait[p][id] failed\00"
@.str.4 = [27 x i8] c"wait child %d, ret = 0x%x\0A\00"
@.str.5 = [16 x i8] c"exit code = %d\0A\00"
@.str.6 = [16 x i8] c"signaled by %d\0A\00"
@.str.7 = [15 x i8] c"stopped by %d\0A\00"
@.str.8 = [10 x i8] c"contined\0A\00"
@.str.9 = [23 x i8] c"unknow process status\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i64
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i64 0, i64* %6
%9 = call i64 (...) @fork()
store i64 %9, i64* %6
%10 = icmp slt i64 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = call i32 @err_sys(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
br label %22
13: ; preds = %2
%14 = load i64, i64* %6
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %13
%17 = call i32 (...) @getpid()
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i32 %17)
br label %21
19: ; preds = %13
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
br label %21
21: ; preds = %19, %16
br label %22
22: ; preds = %21, %11
store i64 0, i64* %7
store i32 0, i32* %8
%23 = call i64 @wait(i32* %8)
store i64 %23, i64* %7
%24 = load i64, i64* %7
%25 = icmp eq i64 %24, -1
br i1 %25, label %26, label %29
26: ; preds = %22
%27 = call i32 @err_msg(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0))
%28 = call i32 @sleep(i32 20)
store i32 -1, i32* %3
br label %69
29: ; preds = %22
%30 = load i64, i64* %7
%31 = load i32, i32* %8
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.4, i64 0, i64 0), i64 %30, i32 %31)
%33 = load i32, i32* %8
%34 = call i64 @WIFEXITED(i32 %33)
%35 = icmp ne i64 %34, 0
br i1 %35, label %36, label %40
36: ; preds = %29
%37 = load i32, i32* %8
%38 = call i32 @WEXITSTATUS(i32 %37)
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0), i32 %38)
br label %67
40: ; preds = %29
%41 = load i32, i32* %8
%42 = call i64 @WIFSIGNALED(i32 %41)
%43 = icmp ne i64 %42, 0
br i1 %43, label %44, label %48
44: ; preds = %40
%45 = load i32, i32* %8
%46 = call i32 @WTERMSIG(i32 %45)
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.6, i64 0, i64 0), i32 %46)
br label %66
48: ; preds = %40
%49 = load i32, i32* %8
%50 = call i64 @WIFSTOPPED(i32 %49)
%51 = icmp ne i64 %50, 0
br i1 %51, label %52, label %56
52: ; preds = %48
%53 = load i32, i32* %8
%54 = call i32 @WSTOPSIG(i32 %53)
%55 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.7, i64 0, i64 0), i32 %54)
br label %65
56: ; preds = %48
%57 = load i32, i32* %8
%58 = call i64 @WIFCONTINUED(i32 %57)
%59 = icmp ne i64 %58, 0
br i1 %59, label %60, label %62
60: ; preds = %56
%61 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.8, i64 0, i64 0))
br label %64
62: ; preds = %56
%63 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.9, i64 0, i64 0))
br label %64
64: ; preds = %62, %60
br label %65
65: ; preds = %64, %52
br label %66
66: ; preds = %65, %44
br label %67
67: ; preds = %66, %36
br label %68
68: ; preds = %67
store i32 0, i32* %3
br label %69
69: ; preds = %68, %26
%70 = load i32, i32* %3
ret i32 %70
}

LLVM IR (-O2):
@.str = [11 x i8] c"fork error\00"
@.str.1 = [18 x i8] c"this is child %d\0A\00"
@.str.3 = [19 x i8] c"wait[p][id] failed\00"
@.str.4 = [27 x i8] c"wait child %d, ret = 0x%x\0A\00"
@.str.5 = [16 x i8] c"exit code = %d\0A\00"
@.str.6 = [16 x i8] c"signaled by %d\0A\00"
@.str.7 = [15 x i8] c"stopped by %d\0A\00"
@str = [22 x i8] c"before wait any child\00"
@str.10 = [22 x i8] c"unknow process status\00"
@str.11 = [9 x i8] c"contined\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i32
%4 = tail call i64 (...) @fork()
%5 = icmp slt i64 %4, 0
br i1 %5, label %6, label %8
6: ; preds = %2
%7 = tail call i32 @err_sys(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
br label %15
8: ; preds = %2
%9 = icmp eq i64 %4, 0
br i1 %9, label %10, label %13
10: ; preds = %8
%11 = tail call i32 (...) @getpid()
%12 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i32 %11)
br label %15
13: ; preds = %8
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str, i64 0, i64 0))
br label %15
15: ; preds = %10, %13, %6
%16 = bitcast i32* %3 to i8*
store i32 0, i32* %3
%17 = call i64 @wait(i32* %3)
%18 = icmp eq i64 %17, -1
br i1 %18, label %19, label %22
19: ; preds = %15
%20 = call i32 @err_msg(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0))
%21 = call i32 @sleep(i32 20)
br label %53
22: ; preds = %15
%23 = load i32, i32* %3
%24 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.4, i64 0, i64 0), i64 %17, i32 %23)
%25 = load i32, i32* %3
%26 = call i64 @WIFEXITED(i32 %25)
%27 = icmp eq i64 %26, 0
%28 = load i32, i32* %3
br i1 %27, label %32, label %29
29: ; preds = %22
%30 = call i32 @WEXITSTATUS(i32 %28)
%31 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0), i32 %30)
br label %53
32: ; preds = %22
%33 = call i64 @WIFSIGNALED(i32 %28)
%34 = icmp eq i64 %33, 0
%35 = load i32, i32* %3
br i1 %34, label %39, label %36
36: ; preds = %32
%37 = call i32 @WTERMSIG(i32 %35)
%38 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.6, i64 0, i64 0), i32 %37)
br label %53
39: ; preds = %32
%40 = call i64 @WIFSTOPPED(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 @WSTOPSIG(i32 %42)
%45 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.7, i64 0, i64 0), i32 %44)
br label %53
46: ; preds = %39
%47 = call i64 @WIFCONTINUED(i32 %42)
%48 = icmp eq i64 %47, 0
br i1 %48, label %51, label %49
49: ; preds = %46
%50 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @str.11, i64 0, i64 0))
br label %53
51: ; preds = %46
%52 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str.10, i64 0, i64 0))
br label %53
53: ; preds = %36, %49, %51, %43, %29, %19
%54 = phi i32 [ -1, %19 ], [ 0, %29 ], [ 0, %43 ], [ 0, %51 ], [ 0, %49 ], [ 0, %36 ]
ret i32 %54
}