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

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int WEXITSTATUS(int);
scalar_t__ WIFEXITED(int);
scalar_t__ WIFSIGNALED(int);
int WTERMSIG(int);
int /*<<< orphan*/ abort();
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, int, char *,
                           char *);
int getpid();
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int wait(int *);
int main(void) {
  pid_t pid;
  if ((pid = fork()) == -1)
    do {
      fprintf(stderr, "[%s][%d]:%s %s\n",
              "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
              "2022-01-22/02-19-57/repos/drsin/linuxCoreLearn/refs/heads/"
              "master/apue/day06/wait.c",
              21, "fork", strerror(errno));
      exit(EXIT_FAILURE);
    } while (0);
  if (pid == 0) {
    sleep(2);
    printf("this is child! %d\n", getpid());
    abort();
  }
  int status;
  pid_t cid;
  cid = wait(&status);
  if (cid == -1)
    do {
      fprintf(stderr, "[%s][%d]:%s %s\n",
              "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
              "2022-01-22/02-19-57/repos/drsin/linuxCoreLearn/refs/heads/"
              "master/apue/day06/wait.c",
              34, "wait", strerror(errno));
      exit(EXIT_FAILURE);
    } while (0);
  printf("this is parent!cid=%d\n", cid);
  if (WIFEXITED(status))
    printf("status=%hhd\n", WEXITSTATUS(status));
  else if (WIFSIGNALED(status)) {
    printf("exit abnormal! %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   10f0 <fork@plt>
mov    %eax,-0x8(%rbp)
cmp    $0xffffffff,%eax
jne    1244 <main+0x74>
jmp    11f7 <main+0x27>
mov    0x2de2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    0x2dce(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    -0x14(%rbp),%edi
mov    %rax,%r9
lea    0xde2(%rip),%rsi
lea    0xdeb(%rip),%rdx
mov    $0x15,%ecx
lea    0xe72(%rip),%r8
call   1070 <fprintf@plt>
mov    0x2d93(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
cmpl   $0x0,-0x8(%rbp)
jne    1274 <main+0xa4>
mov    $0x2,%edi
call   10d0 <sleep@plt>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%esi
lea    0xe40(%rip),%rdi
call   1050 <printf@plt>
mov    $0x0,%al
call   1030 <abort@plt>
lea    -0xc(%rbp),%rdi
call   10e0 <wait@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
jne    12dc <main+0x10c>
jmp    128f <main+0xbf>
mov    0x2d4a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2d36(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    -0x18(%rbp),%edi
mov    %rax,%r9
lea    0xd4a(%rip),%rsi
lea    0xd53(%rip),%rdx
mov    $0x22,%ecx
lea    0xdf2(%rip),%r8
call   1070 <fprintf@plt>
mov    0x2cfb(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    -0x10(%rbp),%esi
lea    0xdda(%rip),%rdi
call   1050 <printf@plt>
mov    -0xc(%rbp),%edi
call   10a0 <WIFEXITED@plt>
cmp    $0x0,%rax
je     1318 <main+0x148>
mov    -0xc(%rbp),%edi
call   1090 <WEXITSTATUS@plt>
mov    %eax,%esi
lea    0xdc9(%rip),%rdi
call   1050 <printf@plt>
jmp    1345 <main+0x175>
mov    -0xc(%rbp),%edi
call   1060 <WIFSIGNALED@plt>
cmp    $0x0,%rax
je     1340 <main+0x170>
mov    -0xc(%rbp),%edi
call   1080 <WTERMSIG@plt>
mov    %eax,%esi
lea    0xda9(%rip),%rdi
call   1050 <printf@plt>
jmp    1345 <main+0x175>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
xor    %eax,%eax
call   10f0 <fork@plt>
test   %eax,%eax
je     1253 <main+0x83>
cmp    $0xffffffff,%eax
je     1279 <main+0xa9>
lea    0xc(%rsp),%rdi
call   10e0 <wait@plt>
cmp    $0xffffffff,%eax
je     12ae <main+0xde>
lea    0xebd(%rip),%rdi
mov    %eax,%esi
call   1050 <printf@plt>
mov    0xc(%rsp),%edi
call   10a0 <WIFEXITED@plt>
mov    0xc(%rsp),%edi
test   %rax,%rax
je     122a <main+0x5a>
call   1090 <WEXITSTATUS@plt>
lea    0xeaf(%rip),%rdi
jmp    1244 <main+0x74>
call   1060 <WIFSIGNALED@plt>
test   %rax,%rax
je     124b <main+0x7b>
mov    0xc(%rsp),%edi
call   1080 <WTERMSIG@plt>
lea    0xea0(%rip),%rdi
mov    %eax,%esi
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret
mov    $0x2,%edi
call   10d0 <sleep@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe3d(%rip),%rdi
mov    %eax,%esi
call   1050 <printf@plt>
xor    %eax,%eax
call   1030 <abort@plt>
mov    0x2d60(%rip),%rax
mov    (%rax),%ebx
mov    0x2d4f(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
lea    0xd69(%rip),%rsi
lea    0xd72(%rip),%rdx
lea    0xdfe(%rip),%r8
mov    %ebx,%edi
mov    $0x15,%ecx
jmp    12e1 <main+0x111>
mov    0x2d2b(%rip),%rax
mov    (%rax),%ebx
mov    0x2d1a(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
lea    0xd34(%rip),%rsi
lea    0xd3d(%rip),%rdx
lea    0xde1(%rip),%r8
mov    %ebx,%edi
mov    $0x22,%ecx
mov    %rax,%r9
call   1070 <fprintf@plt>
mov    0x2ce0(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [16 x i8] c"[%s][%d]:%s %s\0A\00"
@.str.1 = [147 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/drsin/linuxCoreLearn/refs/heads/master/apue/day06/wait.c\00"
@.str.2 = [5 x i8] c"fork\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
@.str.3 = [19 x i8] c"this is child! %d\0A\00"
@.str.4 = [5 x i8] c"wait\00"
@.str.5 = [23 x i8] c"this is parent!cid=%d\0A\00"
@.str.6 = [13 x i8] c"status=%hhd\0A\00"
@.str.7 = [19 x i8] c"exit abnormal! %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 0, i32* %1
%5 = call i32 (...) @fork()
store i32 %5, i32* %2
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %16
7: ; preds = %0
br label %8
8: ; preds = %7
%9 = load i32, i32* @stderr
%10 = load i32, i32* @errno
%11 = call i8* @strerror(i32 %10)
%12 = call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([147 x i8], [147 x i8]* @.str.1, i64 0, i64 0), i32 21, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8* %11)
%13 = load i32, i32* @EXIT_FAILURE
%14 = call i32 @exit(i32 %13)
unreachable
15: ; No predecessors!
br label %16
16: ; preds = %15, %0
%17 = load i32, i32* %2
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %24
19: ; preds = %16
%20 = call i32 @sleep(i32 2)
%21 = call i32 (...) @getpid()
%22 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i32 %21)
%23 = call i32 (...) @abort()
unreachable
24: ; preds = %16
%25 = call i32 @wait(i32* %3)
store i32 %25, i32* %4
%26 = load i32, i32* %4
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %37
28: ; preds = %24
br label %29
29: ; preds = %28
%30 = load i32, i32* @stderr
%31 = load i32, i32* @errno
%32 = call i8* @strerror(i32 %31)
%33 = call i32 @fprintf(i32 %30, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([147 x i8], [147 x i8]* @.str.1, i64 0, i64 0), i32 34, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i8* %32)
%34 = load i32, i32* @EXIT_FAILURE
%35 = call i32 @exit(i32 %34)
unreachable
36: ; No predecessors!
br label %37
37: ; preds = %36, %24
%38 = load i32, i32* %4
%39 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.5, i64 0, i64 0), i32 %38)
%40 = load i32, i32* %3
%41 = call i64 @WIFEXITED(i32 %40)
%42 = icmp ne i64 %41, 0
br i1 %42, label %43, label %47
43: ; preds = %37
%44 = load i32, i32* %3
%45 = call i32 @WEXITSTATUS(i32 %44)
%46 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.6, i64 0, i64 0), i32 %45)
br label %56
47: ; preds = %37
%48 = load i32, i32* %3
%49 = call i64 @WIFSIGNALED(i32 %48)
%50 = icmp ne i64 %49, 0
br i1 %50, label %51, label %55
51: ; preds = %47
%52 = load i32, i32* %3
%53 = call i32 @WTERMSIG(i32 %52)
%54 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.7, i64 0, i64 0), i32 %53)
br label %55
55: ; preds = %51, %47
br label %56
56: ; preds = %55, %43
ret i32 0
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [16 x i8] c"[%s][%d]:%s %s\0A\00"
@.str.1 = [147 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/drsin/linuxCoreLearn/refs/heads/master/apue/day06/wait.c\00"
@.str.2 = [5 x i8] c"fork\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
@.str.3 = [19 x i8] c"this is child! %d\0A\00"
@.str.4 = [5 x i8] c"wait\00"
@.str.5 = [23 x i8] c"this is parent!cid=%d\0A\00"
@.str.6 = [13 x i8] c"status=%hhd\0A\00"
@.str.7 = [19 x i8] c"exit abnormal! %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = tail call i32 (...) @fork()
switch i32 %2, label %15 [
i32 -1, label %3
i32 0, label %10
]
3: ; preds = %0
%4 = load i32, i32* @stderr
%5 = load i32, i32* @errno
%6 = tail call i8* @strerror(i32 %5)
%7 = tail call i32 @fprintf(i32 %4, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([147 x i8], [147 x i8]* @.str.1, i64 0, i64 0), i32 21, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8* %6)
%8 = load i32, i32* @EXIT_FAILURE
%9 = tail call i32 @exit(i32 %8)
unreachable
10: ; preds = %0
%11 = tail call i32 @sleep(i32 2)
%12 = tail call i32 (...) @getpid()
%13 = tail call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i32 %12)
%14 = tail call i32 (...) @abort()
unreachable
15: ; preds = %0
%16 = bitcast i32* %1 to i8*
%17 = call i32 @wait(i32* %1)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %26
19: ; preds = %15
%20 = load i32, i32* @stderr
%21 = load i32, i32* @errno
%22 = call i8* @strerror(i32 %21)
%23 = call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([147 x i8], [147 x i8]* @.str.1, i64 0, i64 0), i32 34, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i8* %22)
%24 = load i32, i32* @EXIT_FAILURE
%25 = call i32 @exit(i32 %24)
unreachable
26: ; preds = %15
%27 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.5, i64 0, i64 0), i32 %17)
%28 = load i32, i32* %1
%29 = call i64 @WIFEXITED(i32 %28)
%30 = icmp eq i64 %29, 0
%31 = load i32, i32* %1
br i1 %30, label %35, label %32
32: ; preds = %26
%33 = call i32 @WEXITSTATUS(i32 %31)
%34 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.6, i64 0, i64 0), i32 %33)
br label %42
35: ; preds = %26
%36 = call i64 @WIFSIGNALED(i32 %31)
%37 = icmp eq i64 %36, 0
br i1 %37, label %42, label %38
38: ; preds = %35
%39 = load i32, i32* %1
%40 = call i32 @WTERMSIG(i32 %39)
%41 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.7, i64 0, i64 0), i32 %40)
br label %42
42: ; preds = %35, %38, %32
ret i32 0
}