#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 WCONTINUED;
int WUNTRACED;
int /*<<< orphan*/ abort();
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int getpid();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ prexit(int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sleep(int);
scalar_t__ wait(int *);
scalar_t__ waitpid(scalar_t__, int *, int);
int main(int argc, char *argv[]) {
  pid_t pid;
  int stat;
  if ((pid = fork()) < 0) {
    perror("fork error");
    exit(1);
  } else if (0 == pid)
    exit(7);
  if (wait(&stat) != pid) {
    perror("fork error");
    exit(1);
  }
  prexit(stat);
  if ((pid = fork()) < 0) {
    perror("fork error");
    exit(1);
  } else if (0 == pid)
    abort();
  if (wait(&stat) != pid) {
    perror("wait error");
    exit(1);
  }
  prexit(stat);
  if ((pid = fork()) < 0) {
    perror("fork error");
    exit(1);
  } else if (0 == pid)
    stat = 1 / (1 - 1);
  if (wait(&stat) != pid) {
    perror("wait error");
    exit(1);
  }
  prexit(stat);
  if ((pid = fork()) < 0) {
    perror("fork error");
    exit(1);
  } else if (0 == pid) {
    printf("child pid is %d\n\n", getpid());
    int n = 0;
    while (n++ < 30) {
      printf("child ticking: %d\n", n);
      sleep(1);
    }
    exit(0);
  }
  if (waitpid(pid, &stat, WCONTINUED | WUNTRACED) != pid) {
    perror("wait error");
    exit(1);
  }
  prexit(stat);
  printf("hahah\n");
  if (waitpid(pid, &stat, WCONTINUED | WUNTRACED) != pid) {
    perror("wait error");
    exit(1);
  }
  prexit(stat);
  return 0;
}


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   10c0 <fork@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jge    11e1 <main+0x41>
lea    0xe2e(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
xor    %eax,%eax
cmp    -0x18(%rbp),%rax
jne    11f7 <main+0x57>
mov    $0x7,%edi
call   1090 <exit@plt>
jmp    11fc <main+0x5c>
lea    -0x1c(%rbp),%rdi
call   10b0 <wait@plt>
cmp    -0x18(%rbp),%rax
je     1225 <main+0x85>
lea    0xdea(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x1c(%rbp),%edi
call   1060 <prexit@plt>
mov    $0x0,%al
call   10c0 <fork@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jge    1258 <main+0xb8>
lea    0xdb7(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
xor    %eax,%eax
cmp    -0x18(%rbp),%rax
jne    126b <main+0xcb>
mov    $0x0,%al
call   1030 <abort@plt>
jmp    1270 <main+0xd0>
lea    -0x1c(%rbp),%rdi
call   10b0 <wait@plt>
cmp    -0x18(%rbp),%rax
je     1299 <main+0xf9>
lea    0xd81(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x1c(%rbp),%edi
call   1060 <prexit@plt>
mov    $0x0,%al
call   10c0 <fork@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jge    12cc <main+0x12c>
lea    0xd43(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
xor    %eax,%eax
cmp    -0x18(%rbp),%rax
jne    12db <main+0x13b>
mov    %eax,-0x1c(%rbp)
jmp    12e0 <main+0x140>
lea    -0x1c(%rbp),%rdi
call   10b0 <wait@plt>
cmp    -0x18(%rbp),%rax
je     1309 <main+0x169>
lea    0xd11(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x1c(%rbp),%edi
call   1060 <prexit@plt>
mov    $0x0,%al
call   10c0 <fork@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jge    133c <main+0x19c>
lea    0xcd3(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
xor    %eax,%eax
cmp    -0x18(%rbp),%rax
jne    13a1 <main+0x201>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%esi
lea    0xcbe(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
mov    %eax,%ecx
add    $0x1,%ecx
mov    %ecx,-0x20(%rbp)
cmp    $0x1e,%eax
jge    139a <main+0x1fa>
mov    -0x20(%rbp),%esi
lea    0xca4(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x1,%edi
call   10a0 <sleep@plt>
jmp    1366 <main+0x1c6>
xor    %edi,%edi
call   1090 <exit@plt>
jmp    13a6 <main+0x206>
mov    -0x18(%rbp),%rdi
mov    0x2c1f(%rip),%rax
mov    (%rax),%edx
mov    0x2c26(%rip),%rax
or     (%rax),%edx
lea    -0x1c(%rbp),%rsi
call   1070 <waitpid@plt>
cmp    -0x18(%rbp),%rax
je     13e5 <main+0x245>
lea    0xc35(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x1c(%rbp),%edi
call   1060 <prexit@plt>
lea    0xc47(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x18(%rbp),%rdi
mov    0x2bca(%rip),%rax
mov    (%rax),%edx
mov    0x2bd1(%rip),%rax
or     (%rax),%edx
lea    -0x1c(%rbp),%rsi
call   1070 <waitpid@plt>
cmp    -0x18(%rbp),%rax
je     143a <main+0x29a>
lea    0xbe0(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x1c(%rbp),%edi
call   1060 <prexit@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
xor    %eax,%eax
call   10d0 <fork@plt>
test   %rax,%rax
js     12cf <main+0x11f>
je     12ee <main+0x13e>
mov    %rax,%rbx
lea    0xc(%rsp),%rdi
call   10c0 <wait@plt>
cmp    %rbx,%rax
jne    12cf <main+0x11f>
mov    0xc(%rsp),%edi
call   1070 <prexit@plt>
xor    %eax,%eax
call   10d0 <fork@plt>
test   %rax,%rax
js     12cf <main+0x11f>
je     12f8 <main+0x148>
mov    %rax,%rbx
lea    0xc(%rsp),%rdi
call   10c0 <wait@plt>
cmp    %rbx,%rax
jne    12d8 <main+0x128>
mov    0xc(%rsp),%edi
call   1070 <prexit@plt>
xor    %eax,%eax
call   10d0 <fork@plt>
test   %rax,%rax
js     12cf <main+0x11f>
mov    %rax,%rbx
lea    0xc(%rsp),%rdi
call   10c0 <wait@plt>
cmp    %rbx,%rax
jne    12d8 <main+0x128>
mov    0xc(%rsp),%edi
call   1070 <prexit@plt>
xor    %eax,%eax
call   10d0 <fork@plt>
test   %rax,%rax
js     12cf <main+0x11f>
je     12ff <main+0x14f>
mov    %rax,%rbx
mov    0x2d62(%rip),%r14
mov    0x2d6b(%rip),%r15
mov    (%r15),%edx
or     (%r14),%edx
lea    0xc(%rsp),%rsi
mov    %rax,%rdi
call   1080 <waitpid@plt>
cmp    %rbx,%rax
jne    12d8 <main+0x128>
mov    0xc(%rsp),%edi
call   1070 <prexit@plt>
lea    0xd9e(%rip),%rdi
call   1040 <puts@plt>
mov    (%r15),%edx
or     (%r14),%edx
lea    0xc(%rsp),%rsi
mov    %rbx,%rdi
call   1080 <waitpid@plt>
cmp    %rbx,%rax
jne    12d8 <main+0x128>
mov    0xc(%rsp),%edi
call   1070 <prexit@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xd2a(%rip),%rdi
jmp    12df <main+0x12f>
lea    0xd2c(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    $0x7,%edi
call   10a0 <exit@plt>
xor    %eax,%eax
call   1030 <abort@plt>
xor    %eax,%eax
call   1050 <getpid@plt>
lea    0xd09(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0x1,%ebx
lea    0xd06(%rip),%r14
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r14,%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0x1,%edi
call   10b0 <sleep@plt>
add    $0x1,%ebx
cmp    $0x1f,%ebx
jne    1330 <main+0x180>
xor    %edi,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [11 x i8] c"fork error\00"
@.str.1 = [11 x i8] c"wait error\00"
@.str.2 = [18 x i8] c"child pid is %d\0A\0A\00"
@.str.3 = [19 x i8] c"child ticking: %d\0A\00"
@WCONTINUED = global i32 0
@WUNTRACED = global i32 0
@.str.4 = [7 x i8] c"hahah\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i32
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = call i64 (...) @fork()
store i64 %9, i64* %6
%10 = icmp slt i64 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %2
%12 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%13 = call i32 @exit(i32 1)
unreachable
14: ; preds = %2
%15 = load i64, i64* %6
%16 = icmp eq i64 0, %15
br i1 %16, label %17, label %19
17: ; preds = %14
%18 = call i32 @exit(i32 7)
unreachable
19: ; preds = %14
br label %20
20: ; preds = %19
%21 = call i64 @wait(i32* %7)
%22 = load i64, i64* %6
%23 = icmp ne i64 %21, %22
br i1 %23, label %24, label %27
24: ; preds = %20
%25 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%26 = call i32 @exit(i32 1)
unreachable
27: ; preds = %20
%28 = load i32, i32* %7
%29 = call i32 @prexit(i32 %28)
%30 = call i64 (...) @fork()
store i64 %30, i64* %6
%31 = icmp slt i64 %30, 0
br i1 %31, label %32, label %35
32: ; preds = %27
%33 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%34 = call i32 @exit(i32 1)
unreachable
35: ; preds = %27
%36 = load i64, i64* %6
%37 = icmp eq i64 0, %36
br i1 %37, label %38, label %40
38: ; preds = %35
%39 = call i32 (...) @abort()
unreachable
40: ; preds = %35
br label %41
41: ; preds = %40
%42 = call i64 @wait(i32* %7)
%43 = load i64, i64* %6
%44 = icmp ne i64 %42, %43
br i1 %44, label %45, label %48
45: ; preds = %41
%46 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%47 = call i32 @exit(i32 1)
unreachable
48: ; preds = %41
%49 = load i32, i32* %7
%50 = call i32 @prexit(i32 %49)
%51 = call i64 (...) @fork()
store i64 %51, i64* %6
%52 = icmp slt i64 %51, 0
br i1 %52, label %53, label %56
53: ; preds = %48
%54 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%55 = call i32 @exit(i32 1)
unreachable
56: ; preds = %48
%57 = load i64, i64* %6
%58 = icmp eq i64 0, %57
br i1 %58, label %59, label %60
59: ; preds = %56
store i32 poison, i32* %7
br label %60
60: ; preds = %59, %56
br label %61
61: ; preds = %60
%62 = call i64 @wait(i32* %7)
%63 = load i64, i64* %6
%64 = icmp ne i64 %62, %63
br i1 %64, label %65, label %68
65: ; preds = %61
%66 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%67 = call i32 @exit(i32 1)
unreachable
68: ; preds = %61
%69 = load i32, i32* %7
%70 = call i32 @prexit(i32 %69)
%71 = call i64 (...) @fork()
store i64 %71, i64* %6
%72 = icmp slt i64 %71, 0
br i1 %72, label %73, label %76
73: ; preds = %68
%74 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%75 = call i32 @exit(i32 1)
unreachable
76: ; preds = %68
%77 = load i64, i64* %6
%78 = icmp eq i64 0, %77
br i1 %78, label %79, label %92
79: ; preds = %76
%80 = call i32 (...) @getpid()
%81 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i32 %80)
store i32 0, i32* %8
br label %82
82: ; preds = %86, %79
%83 = load i32, i32* %8
%84 = add nsw i32 %83, 1
store i32 %84, i32* %8
%85 = icmp slt i32 %83, 30
br i1 %85, label %86, label %90
86: ; preds = %82
%87 = load i32, i32* %8
%88 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i32 %87)
%89 = call i32 @sleep(i32 1)
br label %82
90: ; preds = %82
%91 = call i32 @exit(i32 0)
unreachable
92: ; preds = %76
br label %93
93: ; preds = %92
%94 = load i64, i64* %6
%95 = load i32, i32* @WCONTINUED
%96 = load i32, i32* @WUNTRACED
%97 = or i32 %95, %96
%98 = call i64 @waitpid(i64 %94, i32* %7, i32 %97)
%99 = load i64, i64* %6
%100 = icmp ne i64 %98, %99
br i1 %100, label %101, label %104
101: ; preds = %93
%102 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%103 = call i32 @exit(i32 1)
unreachable
104: ; preds = %93
%105 = load i32, i32* %7
%106 = call i32 @prexit(i32 %105)
%107 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
%108 = load i64, i64* %6
%109 = load i32, i32* @WCONTINUED
%110 = load i32, i32* @WUNTRACED
%111 = or i32 %109, %110
%112 = call i64 @waitpid(i64 %108, i32* %7, i32 %111)
%113 = load i64, i64* %6
%114 = icmp ne i64 %112, %113
br i1 %114, label %115, label %118
115: ; preds = %104
%116 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%117 = call i32 @exit(i32 1)
unreachable
118: ; preds = %104
%119 = load i32, i32* %7
%120 = call i32 @prexit(i32 %119)
ret i32 0
}

LLVM IR (-O2):
@.str = [11 x i8] c"fork error\00"
@.str.1 = [11 x i8] c"wait error\00"
@.str.2 = [18 x i8] c"child pid is %d\0A\0A\00"
@.str.3 = [19 x i8] c"child ticking: %d\0A\00"
@WCONTINUED = global i32 0
@WUNTRACED = global i32 0
@str = [6 x i8] c"hahah\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = tail call i64 (...) @fork()
%6 = icmp slt i64 %5, 0
br i1 %6, label %7, label %10
7: ; preds = %2
%8 = tail call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%9 = tail call i32 @exit(i32 1)
unreachable
10: ; preds = %2
%11 = icmp eq i64 %5, 0
br i1 %11, label %12, label %14
12: ; preds = %10
%13 = tail call i32 @exit(i32 7)
unreachable
14: ; preds = %10
%15 = call i64 @wait(i32* %3)
%16 = icmp eq i64 %15, %5
br i1 %16, label %20, label %17
17: ; preds = %14
%18 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %14
%21 = load i32, i32* %3
%22 = call i32 @prexit(i32 %21)
%23 = call i64 (...) @fork()
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %20
%26 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%27 = call i32 @exit(i32 1)
unreachable
28: ; preds = %20
%29 = icmp eq i64 %23, 0
br i1 %29, label %30, label %32
30: ; preds = %28
%31 = call i32 (...) @abort()
unreachable
32: ; preds = %28
%33 = call i64 @wait(i32* %3)
%34 = icmp eq i64 %33, %23
br i1 %34, label %38, label %35
35: ; preds = %32
%36 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%37 = call i32 @exit(i32 1)
unreachable
38: ; preds = %32
%39 = load i32, i32* %3
%40 = call i32 @prexit(i32 %39)
%41 = call i64 (...) @fork()
%42 = icmp slt i64 %41, 0
br i1 %42, label %43, label %46
43: ; preds = %38
%44 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%45 = call i32 @exit(i32 1)
unreachable
46: ; preds = %38
%47 = call i64 @wait(i32* %3)
%48 = icmp eq i64 %47, %41
br i1 %48, label %52, label %49
49: ; preds = %46
%50 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%51 = call i32 @exit(i32 1)
unreachable
52: ; preds = %46
%53 = load i32, i32* %3
%54 = call i32 @prexit(i32 %53)
%55 = call i64 (...) @fork()
%56 = icmp slt i64 %55, 0
br i1 %56, label %57, label %60
57: ; preds = %52
%58 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%59 = call i32 @exit(i32 1)
unreachable
60: ; preds = %52
%61 = icmp eq i64 %55, 0
br i1 %61, label %62, label %73
62: ; preds = %60
%63 = call i32 (...) @getpid()
%64 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i32 %63)
br label %65
65: ; preds = %62, %65
%66 = phi i32 [ 1, %62 ], [ %69, %65 ]
%67 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i32 %66)
%68 = call i32 @sleep(i32 1)
%69 = add nuw nsw i32 %66, 1
%70 = icmp eq i32 %69, 31
br i1 %70, label %71, label %65
71: ; preds = %65
%72 = call i32 @exit(i32 0)
unreachable
73: ; preds = %60
%74 = load i32, i32* @WCONTINUED
%75 = load i32, i32* @WUNTRACED
%76 = or i32 %75, %74
%77 = call i64 @waitpid(i64 %55, i32* %3, i32 %76)
%78 = icmp eq i64 %77, %55
br i1 %78, label %82, label %79
79: ; preds = %73
%80 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%81 = call i32 @exit(i32 1)
unreachable
82: ; preds = %73
%83 = load i32, i32* %3
%84 = call i32 @prexit(i32 %83)
%85 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @str, i64 0, i64 0))
%86 = load i32, i32* @WCONTINUED
%87 = load i32, i32* @WUNTRACED
%88 = or i32 %87, %86
%89 = call i64 @waitpid(i64 %55, i32* %3, i32 %88)
%90 = icmp eq i64 %89, %55
br i1 %90, label %94, label %91
91: ; preds = %82
%92 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%93 = call i32 @exit(i32 1)
unreachable
94: ; preds = %82
%95 = load i32, i32* %3
%96 = call i32 @prexit(i32 %95)
ret i32 0
}