#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);
int WIFEXITED(int);
int WIFSIGNALED(int);
int WTERMSIG(int);
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int wait(int *);
int main(void) {
  pid_t pid;
  int num, status;
  pid = fork();
  if (pid < 0) {
    perror("fail to fork");
    exit(1);
  } else if (0 == pid) {
    printf("the first, exit normally\n");
    exit(0);
  } else {
    if (wait(&status) == -1) {
      perror("fail to wait");
      exit(1);
    }
    if (WIFEXITED(status) == 1)
      printf("the status of first is : %d\n", WEXITSTATUS(status));
  }
  pid = fork();
  if (pid < 0) {
    perror("fail to fork");
    exit(1);
  } else if (0 == pid) {
    printf("the second, exit abnormally\n");
    num = 1 / 0;
  } else {
    if (wait(&status) == -1) {
      perror("fail to wait");
      exit(1);
    }
    if (WIFSIGNALED(status) == 1)
      printf("the terminated signal is : %d\n", WTERMSIG(status));
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jge    11cb <main+0x3b>
lea    0xe44(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
xor    %eax,%eax
cmp    -0x10(%rbp),%rax
jne    11ec <main+0x5c>
lea    0xe2f(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %edi,%edi
call   1090 <exit@plt>
lea    -0x18(%rbp),%rdi
call   10a0 <wait@plt>
cmp    $0xffffffff,%eax
jne    1214 <main+0x84>
lea    0xe22(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x18(%rbp),%edi
call   1080 <WIFEXITED@plt>
cmp    $0x1,%eax
jne    123d <main+0xad>
mov    -0x18(%rbp),%edi
call   1060 <WEXITSTATUS@plt>
mov    %eax,%esi
lea    0xdfe(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1242 <main+0xb2>
jmp    1247 <main+0xb7>
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jge    1273 <main+0xe3>
lea    0xd9c(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
xor    %eax,%eax
cmp    -0x10(%rbp),%rax
jne    1295 <main+0x105>
lea    0xdcb(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    %eax,-0x14(%rbp)
jmp    12eb <main+0x15b>
lea    -0x18(%rbp),%rdi
call   10a0 <wait@plt>
cmp    $0xffffffff,%eax
jne    12bd <main+0x12d>
lea    0xd79(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x18(%rbp),%edi
call   1040 <WIFSIGNALED@plt>
cmp    $0x1,%eax
jne    12e6 <main+0x156>
mov    -0x18(%rbp),%edi
call   1050 <WTERMSIG@plt>
mov    %eax,%esi
lea    0xd8f(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    12eb <main+0x15b>
jmp    12f0 <main+0x160>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rax
xor    %eax,%eax
call   10c0 <fork@plt>
test   %rax,%rax
js     1247 <main+0xa7>
je     1266 <main+0xc6>
lea    0x4(%rsp),%rdi
call   10b0 <wait@plt>
cmp    $0xffffffff,%eax
je     1250 <main+0xb0>
mov    0x4(%rsp),%edi
call   1090 <WIFEXITED@plt>
cmp    $0x1,%eax
jne    11f1 <main+0x51>
mov    0x4(%rsp),%edi
call   1070 <WEXITSTATUS@plt>
lea    0xe32(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
call   10c0 <fork@plt>
test   %rax,%rax
js     1247 <main+0xa7>
je     1237 <main+0x97>
lea    0x4(%rsp),%rdi
call   10b0 <wait@plt>
cmp    $0xffffffff,%eax
je     1250 <main+0xb0>
mov    0x4(%rsp),%edi
call   1050 <WIFSIGNALED@plt>
cmp    $0x1,%eax
jne    1243 <main+0xa3>
mov    0x4(%rsp),%edi
call   1060 <WTERMSIG@plt>
lea    0xe0b(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    1243 <main+0xa3>
lea    0xe18(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
pop    %rcx
ret
lea    0xdb2(%rip),%rdi
jmp    1257 <main+0xb7>
lea    0xdb6(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
lea    0xe05(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [13 x i8] c"fail to fork\00"
@.str.1 = [26 x i8] c"the first, exit normally\0A\00"
@.str.2 = [13 x i8] c"fail to wait\00"
@.str.3 = [29 x i8] c"the status of first is : %d\0A\00"
@.str.4 = [29 x i8] c"the second, exit abnormally\0A\00"
@.str.5 = [31 x i8] c"the terminated signal is : %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca i32
%4 = alloca i32
store i32 0, i32* %1
%5 = call i64 (...) @fork()
store i64 %5, i64* %2
%6 = load i64, i64* %2
%7 = icmp slt i64 %6, 0
br i1 %7, label %8, label %11
8: ; preds = %0
%9 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%10 = call i32 @exit(i32 1)
unreachable
11: ; preds = %0
%12 = load i64, i64* %2
%13 = icmp eq i64 0, %12
br i1 %13, label %14, label %17
14: ; preds = %11
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
%16 = call i32 @exit(i32 0)
unreachable
17: ; preds = %11
%18 = call i32 @wait(i32* %4)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %23
20: ; preds = %17
%21 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%22 = call i32 @exit(i32 1)
unreachable
23: ; preds = %17
%24 = load i32, i32* %4
%25 = call i32 @WIFEXITED(i32 %24)
%26 = icmp eq i32 %25, 1
br i1 %26, label %27, label %31
27: ; preds = %23
%28 = load i32, i32* %4
%29 = call i32 @WEXITSTATUS(i32 %28)
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i32 %29)
br label %31
31: ; preds = %27, %23
br label %32
32: ; preds = %31
br label %33
33: ; preds = %32
%34 = call i64 (...) @fork()
store i64 %34, i64* %2
%35 = load i64, i64* %2
%36 = icmp slt i64 %35, 0
br i1 %36, label %37, label %40
37: ; preds = %33
%38 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%39 = call i32 @exit(i32 1)
unreachable
40: ; preds = %33
%41 = load i64, i64* %2
%42 = icmp eq i64 0, %41
br i1 %42, label %43, label %45
43: ; preds = %40
%44 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0))
store i32 poison, i32* %3
br label %60
45: ; preds = %40
%46 = call i32 @wait(i32* %4)
%47 = icmp eq i32 %46, -1
br i1 %47, label %48, label %51
48: ; preds = %45
%49 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%50 = call i32 @exit(i32 1)
unreachable
51: ; preds = %45
%52 = load i32, i32* %4
%53 = call i32 @WIFSIGNALED(i32 %52)
%54 = icmp eq i32 %53, 1
br i1 %54, label %55, label %59
55: ; preds = %51
%56 = load i32, i32* %4
%57 = call i32 @WTERMSIG(i32 %56)
%58 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.5, i64 0, i64 0), i32 %57)
br label %59
59: ; preds = %55, %51
br label %60
60: ; preds = %59, %43
br label %61
61: ; preds = %60
ret i32 0
}

LLVM IR (-O2):
@.str = [13 x i8] c"fail to fork\00"
@.str.2 = [13 x i8] c"fail to wait\00"
@.str.3 = [29 x i8] c"the status of first is : %d\0A\00"
@.str.5 = [31 x i8] c"the terminated signal is : %d\0A\00"
@str = [28 x i8] c"the second, exit abnormally\00"
@str.6 = [25 x i8] c"the first, exit normally\00"
define i32 @main() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
%3 = tail call i64 (...) @fork()
%4 = icmp slt i64 %3, 0
br i1 %4, label %5, label %8
5: ; preds = %0
%6 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%7 = tail call i32 @exit(i32 1)
unreachable
8: ; preds = %0
%9 = icmp eq i64 %3, 0
br i1 %9, label %10, label %13
10: ; preds = %8
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str.6, i64 0, i64 0))
%12 = tail call i32 @exit(i32 0)
unreachable
13: ; preds = %8
%14 = call i32 @wait(i32* %1)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %19
16: ; preds = %13
%17 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %13
%20 = load i32, i32* %1
%21 = call i32 @WIFEXITED(i32 %20)
%22 = icmp eq i32 %21, 1
br i1 %22, label %23, label %27
23: ; preds = %19
%24 = load i32, i32* %1
%25 = call i32 @WEXITSTATUS(i32 %24)
%26 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i32 %25)
br label %27
27: ; preds = %23, %19
%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, 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 %37
35: ; preds = %33
%36 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str, i64 0, i64 0))
br label %51
37: ; preds = %33
%38 = call i32 @wait(i32* %1)
%39 = icmp eq i32 %38, -1
br i1 %39, label %40, label %43
40: ; preds = %37
%41 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%42 = call i32 @exit(i32 1)
unreachable
43: ; preds = %37
%44 = load i32, i32* %1
%45 = call i32 @WIFSIGNALED(i32 %44)
%46 = icmp eq i32 %45, 1
br i1 %46, label %47, label %51
47: ; preds = %43
%48 = load i32, i32* %1
%49 = call i32 @WTERMSIG(i32 %48)
%50 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.5, i64 0, i64 0), i32 %49)
br label %51
51: ; preds = %35, %47, %43
ret i32 0
}