#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 */
struct sigaction {
  int /*<<< orphan*/ sa_mask;
  int /*<<< orphan*/ *sa_sigaction;
  int /*<<< orphan*/ sa_flags;
  int /*<<< orphan*/ sa_handler;
};
typedef scalar_t__ pid_t;

/* Variables and functions */
scalar_t__ ECHILD;
int /*<<< orphan*/ SA_NOCLDWAIT;
int /*<<< orphan*/ SIGCHLD;
int /*<<< orphan*/ SIG_DFL;
scalar_t__ errno;
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int sigaction(int /*<<< orphan*/, struct sigaction *, int /*<<< orphan*/ *);
int /*<<< orphan*/ sigemptyset(int /*<<< orphan*/ *);
int /*<<< orphan*/ sleep(int);
int wait(int /*<<< orphan*/ *);
int main(void) {
  struct sigaction act;
  pid_t pid;
  act.sa_handler = SIG_DFL;
  act.sa_flags = SA_NOCLDWAIT;
  act.sa_sigaction = NULL;
  sigemptyset(&act.sa_mask);
  if (sigaction(SIGCHLD, &act, NULL) == -1) {
    perror("fail to set handler for SIGCHILD");
    exit(1);
  }
  pid = fork();
  if (pid < 0) {
    perror("fail to fork");
    exit(1);
  } else if (pid == 0) {
    printf("the 1st child\n");
    exit(0);
  } else {
    pid = fork();
    if (pid < 0) {
      perror("fail to fork");
      exit(1);
    } else if (pid == 0) {
      printf("the 2nd child\n");
      sleep(5);
      exit(0);
    } else {
      if (wait(NULL) == -1)
        if (errno == ECHILD)
          printf("all child quit, no child is zome\n");
      printf("the parent\n");
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e32(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
mov    0x2e46(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
lea    -0x20(%rbp),%rdi
call   1050 <sigemptyset@plt>
mov    0x2e21(%rip),%rax
mov    (%rax),%edi
lea    -0x20(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1030 <sigaction@plt>
cmp    $0xffffffff,%eax
jne    11ed <main+0x6d>
lea    0xe22(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jge    1219 <main+0x99>
lea    0xe17(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
cmpq   $0x0,-0x28(%rbp)
jne    1237 <main+0xb7>
lea    0xe03(%rip),%rdi
call   1040 <printf@plt>
xor    %edi,%edi
call   1070 <exit@plt>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jge    1263 <main+0xe3>
lea    0xdcd(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
cmpq   $0x0,-0x28(%rbp)
jne    128b <main+0x10b>
lea    0xdc8(%rip),%rdi
call   1040 <printf@plt>
mov    $0x5,%edi
call   1080 <sleep@plt>
xor    %edi,%edi
call   1070 <exit@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1090 <wait@plt>
cmp    $0xffffffff,%eax
jne    12c8 <main+0x148>
mov    0x2d2c(%rip),%rax
mov    (%rax),%rax
mov    0x2d12(%rip),%rcx
cmp    (%rcx),%rax
jne    12c3 <main+0x143>
lea    0xd8e(%rip),%rdi
call   1040 <printf@plt>
jmp    12c8 <main+0x148>
lea    0xd9f(%rip),%rdi
call   1040 <printf@plt>
jmp    12d9 <main+0x159>
jmp    12de <main+0x15e>
jmp    12e3 <main+0x163>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x20,%rsp
mov    0x2e3c(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x1c(%rsp)
mov    0x2e4f(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
movq   $0x0,0x10(%rsp)
lea    0x8(%rsp),%rbx
mov    %rbx,%rdi
call   1050 <sigemptyset@plt>
mov    0x2e24(%rip),%rax
mov    (%rax),%edi
mov    %rbx,%rsi
xor    %edx,%edx
call   1040 <sigaction@plt>
cmp    $0xffffffff,%eax
je     1234 <main+0xb4>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
js     122b <main+0xab>
je     124a <main+0xca>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
js     122b <main+0xab>
je     125d <main+0xdd>
xor    %edi,%edi
call   1090 <wait@plt>
cmp    $0xffffffff,%eax
jne    1217 <main+0x97>
mov    0x2dd4(%rip),%rax
mov    (%rax),%rax
mov    0x2dba(%rip),%rcx
cmp    (%rcx),%rax
jne    1217 <main+0x97>
lea    0xe27(%rip),%rdi
call   1030 <puts@plt>
lea    0xe10(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbx
ret
lea    0xdef(%rip),%rdi
jmp    123b <main+0xbb>
lea    0xdc5(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
lea    0xe17(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
call   1070 <exit@plt>
lea    0xdf6(%rip),%rdi
call   1030 <puts@plt>
mov    $0x5,%edi
call   1080 <sleep@plt>
xor    %edi,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
%struct.sigaction = type { i32, i32*, i32, i32 }
@SIG_DFL = global i32 0
@SA_NOCLDWAIT = global i32 0
@SIGCHLD = global i32 0
@.str = [33 x i8] c"fail to set handler for SIGCHILD\00"
@.str.1 = [13 x i8] c"fail to fork\00"
@.str.2 = [15 x i8] c"the 1st child\0A\00"
@.str.3 = [15 x i8] c"the 2nd child\0A\00"
@errno = global i64 0
@ECHILD = global i64 0
@.str.4 = [34 x i8] c"all child quit, no child is zome\0A\00"
@.str.5 = [12 x i8] c"the parent\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca %struct.sigaction
%3 = alloca i64
store i32 0, i32* %1
%4 = load i32, i32* @SIG_DFL
%5 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %2, i32 0, i32 3
store i32 %4, i32* %5
%6 = load i32, i32* @SA_NOCLDWAIT
%7 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %2, i32 0, i32 2
store i32 %6, i32* %7
%8 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %2, i32 0, i32 1
store i32* null, i32** %8
%9 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %2, i32 0, i32 0
%10 = call i32 @sigemptyset(i32* %9)
%11 = load i32, i32* @SIGCHLD
%12 = call i32 @sigaction(i32 %11, %struct.sigaction* %2, i32* null)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %17
14: ; preds = %0
%15 = call i32 @perror(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
%16 = call i32 @exit(i32 1)
unreachable
17: ; preds = %0
%18 = call i64 (...) @fork()
store i64 %18, i64* %3
%19 = load i64, i64* %3
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %17
%22 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%23 = call i32 @exit(i32 1)
unreachable
24: ; preds = %17
%25 = load i64, i64* %3
%26 = icmp eq i64 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %24
%28 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
%29 = call i32 @exit(i32 0)
unreachable
30: ; preds = %24
%31 = call i64 (...) @fork()
store i64 %31, i64* %3
%32 = load i64, i64* %3
%33 = icmp slt i64 %32, 0
br i1 %33, label %34, label %37
34: ; preds = %30
%35 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%36 = call i32 @exit(i32 1)
unreachable
37: ; preds = %30
%38 = load i64, i64* %3
%39 = icmp eq i64 %38, 0
br i1 %39, label %40, label %44
40: ; preds = %37
%41 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
%42 = call i32 @sleep(i32 5)
%43 = call i32 @exit(i32 0)
unreachable
44: ; preds = %37
%45 = call i32 @wait(i32* null)
%46 = icmp eq i32 %45, -1
br i1 %46, label %47, label %54
47: ; preds = %44
%48 = load i64, i64* @errno
%49 = load i64, i64* @ECHILD
%50 = icmp eq i64 %48, %49
br i1 %50, label %51, label %53
51: ; preds = %47
%52 = call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.4, i64 0, i64 0))
br label %53
53: ; preds = %51, %47
br label %54
54: ; preds = %53, %44
%55 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0))
br label %56
56: ; preds = %54
br label %57
57: ; preds = %56
br label %58
58: ; preds = %57
br label %59
59: ; preds = %58
ret i32 0
}

LLVM IR (-O2):
%struct.sigaction = type { i32, i32*, i32, i32 }
@SIG_DFL = global i32 0
@SA_NOCLDWAIT = global i32 0
@SIGCHLD = global i32 0
@.str = [33 x i8] c"fail to set handler for SIGCHILD\00"
@.str.1 = [13 x i8] c"fail to fork\00"
@errno = global i64 0
@ECHILD = global i64 0
@str = [11 x i8] c"the parent\00"
@str.6 = [33 x i8] c"all child quit, no child is zome\00"
@str.7 = [14 x i8] c"the 2nd child\00"
@str.8 = [14 x i8] c"the 1st child\00"
define i32 @main() {
%1 = alloca %struct.sigaction
%2 = bitcast %struct.sigaction* %1 to i8*
%3 = load i32, i32* @SIG_DFL
%4 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %1, i64 0, i32 3
store i32 %3, i32* %4
%5 = load i32, i32* @SA_NOCLDWAIT
%6 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %1, i64 0, i32 2
store i32 %5, i32* %6
%7 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %1, i64 0, i32 1
store i32* null, i32** %7
%8 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %1, i64 0, i32 0
%9 = call i32 @sigemptyset(i32* %8)
%10 = load i32, i32* @SIGCHLD
%11 = call i32 @sigaction(i32 %10, %struct.sigaction* %1, i32* null)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %16
13: ; preds = %0
%14 = call i32 @perror(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
%15 = call i32 @exit(i32 1)
unreachable
16: ; preds = %0
%17 = call i64 (...) @fork()
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %16
%20 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%21 = call i32 @exit(i32 1)
unreachable
22: ; preds = %16
%23 = icmp eq i64 %17, 0
br i1 %23, label %24, label %27
24: ; preds = %22
%25 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str.8, i64 0, i64 0))
%26 = call i32 @exit(i32 0)
unreachable
27: ; preds = %22
%28 = call i64 (...) @fork()
%29 = icmp slt i64 %28, 0
br i1 %29, label %30, label %33
30: ; preds = %27
%31 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%32 = call i32 @exit(i32 1)
unreachable
33: ; preds = %27
%34 = icmp eq i64 %28, 0
br i1 %34, label %35, label %39
35: ; preds = %33
%36 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str.7, i64 0, i64 0))
%37 = call i32 @sleep(i32 5)
%38 = call i32 @exit(i32 0)
unreachable
39: ; preds = %33
%40 = call i32 @wait(i32* null)
%41 = icmp eq i32 %40, -1
br i1 %41, label %42, label %48
42: ; preds = %39
%43 = load i64, i64* @errno
%44 = load i64, i64* @ECHILD
%45 = icmp eq i64 %43, %44
br i1 %45, label %46, label %48
46: ; preds = %42
%47 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @str.6, i64 0, i64 0))
br label %48
48: ; preds = %42, %46, %39
%49 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str, i64 0, i64 0))
ret i32 0
}