#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 /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int WEXITSTATUS(int);
scalar_t__ WIFEXITED(int);
scalar_t__ WIFSIGNALED(int);
int WTERMSIG(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
scalar_t__ wait(int *);
void forkc(void (*child)(), int wflg) {
  pid_t pid, pidch;
  int status;
  if ((pid = fork()) < 0) {
    perror("fork()");
    exit(EXIT_FAILURE);
  } else if (pid == 0) {
    child();
  } else {
    if (wflg) {
      if ((pidch = wait(&status)) < 0) {
        perror("wait()");
        exit(EXIT_FAILURE);
      }
      if (WIFEXITED(status)) {
        printf("親プロセス : 子プロセスは終了ステータス%dで正常終了しました\n",
               WEXITSTATUS(status));
      } else if (WIFSIGNALED(status)) {
        printf("親プロセス : 子プロセスはシグナル番号%dで終了しました\n",
               WTERMSIG(status));
      } else {
        exit(EXIT_SUCCESS);
      }
    } else {
      exit(EXIT_SUCCESS);
    }
  }
}


x86 assembly (clang, -O0):
<forkc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jge    11ce <forkc+0x3e>
lea    0xe45(%rip),%rdi
call   1070 <perror@plt>
mov    0x2e19(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
cmpq   $0x0,-0x18(%rbp)
jne    11e3 <forkc+0x53>
mov    $0x0,%al
call   *-0x8(%rbp)
jmp    12a3 <forkc+0x113>
cmpl   $0x0,-0xc(%rbp)
je     1290 <forkc+0x100>
lea    -0x24(%rbp),%rdi
call   10a0 <wait@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jge    121e <forkc+0x8e>
lea    0xdfc(%rip),%rdi
call   1070 <perror@plt>
mov    0x2dc9(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    -0x24(%rbp),%edi
call   1080 <WIFEXITED@plt>
cmp    $0x0,%rax
je     124b <forkc+0xbb>
mov    -0x24(%rbp),%edi
call   1060 <WEXITSTATUS@plt>
mov    %eax,%esi
lea    0xdcd(%rip),%rdi
call   1030 <printf@plt>
jmp    128b <forkc+0xfb>
mov    -0x24(%rbp),%edi
call   1040 <WIFSIGNALED@plt>
cmp    $0x0,%rax
je     1278 <forkc+0xe8>
mov    -0x24(%rbp),%edi
call   1050 <WTERMSIG@plt>
mov    %eax,%esi
lea    0xdf8(%rip),%rdi
call   1030 <printf@plt>
jmp    1286 <forkc+0xf6>
mov    0x2d59(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
jmp    128b <forkc+0xfb>
jmp    129e <forkc+0x10e>
mov    0x2d41(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
jmp    12a3 <forkc+0x113>
jmp    12a8 <forkc+0x118>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<forkc>:
push   %r14
push   %rbx
push   %rax
mov    %esi,%ebx
mov    %rdi,%r14
xor    %eax,%eax
call   10b0 <fork@plt>
test   %rax,%rax
js     1211 <forkc+0x81>
je     11da <forkc+0x4a>
test   %ebx,%ebx
je     121a <forkc+0x8a>
lea    0x4(%rsp),%rdi
call   10a0 <wait@plt>
test   %rax,%rax
js     1228 <forkc+0x98>
mov    0x4(%rsp),%edi
call   1080 <WIFEXITED@plt>
mov    0x4(%rsp),%edi
test   %rax,%rax
je     11e8 <forkc+0x58>
call   1060 <WEXITSTATUS@plt>
lea    0xe36(%rip),%rdi
jmp    1202 <forkc+0x72>
xor    %eax,%eax
mov    %r14,%rcx
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    *%rcx
call   1040 <WIFSIGNALED@plt>
test   %rax,%rax
je     121a <forkc+0x8a>
mov    0x4(%rsp),%edi
call   1050 <WTERMSIG@plt>
lea    0xe64(%rip),%rdi
mov    %eax,%esi
call   1030 <printf@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xde8(%rip),%rdi
jmp    122f <forkc+0x9f>
mov    0x2db7(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
lea    0xdd8(%rip),%rdi
call   1070 <perror@plt>
mov    0x2da5(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [7 x i8] c"fork()\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [7 x i8] c"wait()\00"
@.str.2 = [88 x i8] c"\E8\A6\AA\E3\83\97\E3\83\AD\E3\82\BB\E3\82\B9 : \E5\AD\90\E3\83\97\E3\83\AD\E3\82\BB\E3\82\B9\E3\81\AF\E7\B5\82\E4\BA\86\E3\82\B9\E3\83\86\E3\83\BC\E3\82\BF\E3\82\B9%d\E3\81\A7\E6\AD\A3\E5\B8\B8\E7\B5\82\E4\BA\86\E3\81\97\E3\81\BE\E3\81\97\E3\81\9F\0A\00"
@.str.3 = [79 x i8] c"\E8\A6\AA\E3\83\97\E3\83\AD\E3\82\BB\E3\82\B9 : \E5\AD\90\E3\83\97\E3\83\AD\E3\82\BB\E3\82\B9\E3\81\AF\E3\82\B7\E3\82\B0\E3\83\8A\E3\83\AB\E7\95\AA\E5\8F\B7%d\E3\81\A7\E7\B5\82\E4\BA\86\E3\81\97\E3\81\BE\E3\81\97\E3\81\9F\0A\00"
@EXIT_SUCCESS = global i32 0
define void @forkc(void (...)* %0, i32 %1) {
%3 = alloca void (...)*
%4 = alloca i32
%5 = alloca i64
%6 = alloca i64
%7 = alloca i32
store void (...)* %0, void (...)** %3
store i32 %1, i32* %4
%8 = call i64 (...) @fork()
store i64 %8, i64* %5
%9 = icmp slt i64 %8, 0
br i1 %9, label %10, label %14
10: ; preds = %2
%11 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%12 = load i32, i32* @EXIT_FAILURE
%13 = call i32 @exit(i32 %12)
unreachable
14: ; preds = %2
%15 = load i64, i64* %5
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %14
%18 = load void (...)*, void (...)** %3
br label %54
19: ; preds = %14
%20 = load i32, i32* %4
%21 = icmp ne i32 %20, 0
br i1 %21, label %22, label %50
22: ; preds = %19
%23 = call i64 @wait(i32* %7)
store i64 %23, i64* %6
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %29
25: ; preds = %22
%26 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%27 = load i32, i32* @EXIT_FAILURE
%28 = call i32 @exit(i32 %27)
unreachable
29: ; preds = %22
%30 = load i32, i32* %7
%31 = call i64 @WIFEXITED(i32 %30)
%32 = icmp ne i64 %31, 0
br i1 %32, label %33, label %37
33: ; preds = %29
%34 = load i32, i32* %7
%35 = call i32 @WEXITSTATUS(i32 %34)
%36 = call i32 @printf(i8* getelementptr inbounds ([88 x i8], [88 x i8]* @.str.2, i64 0, i64 0), i32 %35)
br label %49
37: ; preds = %29
%38 = load i32, i32* %7
%39 = call i64 @WIFSIGNALED(i32 %38)
%40 = icmp ne i64 %39, 0
br i1 %40, label %41, label %45
41: ; preds = %37
%42 = load i32, i32* %7
%43 = call i32 @WTERMSIG(i32 %42)
%44 = call i32 @printf(i8* getelementptr inbounds ([79 x i8], [79 x i8]* @.str.3, i64 0, i64 0), i32 %43)
br label %48
45: ; preds = %37
%46 = load i32, i32* @EXIT_SUCCESS
%47 = call i32 @exit(i32 %46)
unreachable
48: ; preds = %41
br label %49
49: ; preds = %48, %33
br label %53
50: ; preds = %19
%51 = load i32, i32* @EXIT_SUCCESS
%52 = call i32 @exit(i32 %51)
unreachable
53: ; preds = %49
br label %54
54: ; preds = %53, %17
br label %55
55: ; preds = %54
ret void
}

LLVM IR (-O2):
@.str = [7 x i8] c"fork()\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [7 x i8] c"wait()\00"
@.str.2 = [88 x i8] c"\E8\A6\AA\E3\83\97\E3\83\AD\E3\82\BB\E3\82\B9 : \E5\AD\90\E3\83\97\E3\83\AD\E3\82\BB\E3\82\B9\E3\81\AF\E7\B5\82\E4\BA\86\E3\82\B9\E3\83\86\E3\83\BC\E3\82\BF\E3\82\B9%d\E3\81\A7\E6\AD\A3\E5\B8\B8\E7\B5\82\E4\BA\86\E3\81\97\E3\81\BE\E3\81\97\E3\81\9F\0A\00"
@.str.3 = [79 x i8] c"\E8\A6\AA\E3\83\97\E3\83\AD\E3\82\BB\E3\82\B9 : \E5\AD\90\E3\83\97\E3\83\AD\E3\82\BB\E3\82\B9\E3\81\AF\E3\82\B7\E3\82\B0\E3\83\8A\E3\83\AB\E7\95\AA\E5\8F\B7%d\E3\81\A7\E7\B5\82\E4\BA\86\E3\81\97\E3\81\BE\E3\81\97\E3\81\9F\0A\00"
@EXIT_SUCCESS = global i32 0
define void @forkc(void (...)* nocapture %0, i32 %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 %11
7: ; preds = %2
%8 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%9 = load i32, i32* @EXIT_FAILURE
%10 = tail call i32 @exit(i32 %9)
unreachable
11: ; preds = %2
%12 = icmp eq i64 %5, 0
br i1 %12, label %13, label %14
13: ; preds = %11
tail call void (...) %0()
br label %44
14: ; preds = %11
%15 = icmp eq i32 %1, 0
br i1 %15, label %41, label %16
16: ; preds = %14
%17 = call i64 @wait(i32* %3)
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %23
19: ; preds = %16
%20 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%21 = load i32, i32* @EXIT_FAILURE
%22 = call i32 @exit(i32 %21)
unreachable
23: ; preds = %16
%24 = load i32, i32* %3
%25 = call i64 @WIFEXITED(i32 %24)
%26 = icmp eq i64 %25, 0
%27 = load i32, i32* %3
br i1 %26, label %31, label %28
28: ; preds = %23
%29 = call i32 @WEXITSTATUS(i32 %27)
%30 = call i32 @printf(i8* getelementptr inbounds ([88 x i8], [88 x i8]* @.str.2, i64 0, i64 0), i32 %29)
br label %44
31: ; preds = %23
%32 = call i64 @WIFSIGNALED(i32 %27)
%33 = icmp eq i64 %32, 0
br i1 %33, label %38, label %34
34: ; preds = %31
%35 = load i32, i32* %3
%36 = call i32 @WTERMSIG(i32 %35)
%37 = call i32 @printf(i8* getelementptr inbounds ([79 x i8], [79 x i8]* @.str.3, i64 0, i64 0), i32 %36)
br label %44
38: ; preds = %31
%39 = load i32, i32* @EXIT_SUCCESS
%40 = call i32 @exit(i32 %39)
unreachable
41: ; preds = %14
%42 = load i32, i32* @EXIT_SUCCESS
%43 = tail call i32 @exit(i32 %42)
unreachable
44: ; preds = %13, %28, %34
ret void
}