#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 /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, int, char *,
                           char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int waitpid(int, int /*<<< orphan*/ *, int /*<<< orphan*/);
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/waitpid.c",
              21, "fork", strerror(errno));
      exit(EXIT_FAILURE);
    } while (0);
  if (pid == 0) {
    sleep(2);
    printf("I'm child!\n");
  } else {
    pid_t ret;
    if ((ret = waitpid(pid, NULL, 0)) == -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/waitpid.c",
                29, "waitpid", strerror(errno));
        exit(EXIT_FAILURE);
      } while (0);
    if (ret == 0)
      printf("no child exit!\n");
    printf("I'm parent!\n");
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1090 <fork@plt>
mov    %eax,-0x8(%rbp)
cmp    $0xffffffff,%eax
jne    11e4 <main+0x74>
jmp    1197 <main+0x27>
mov    0x2e42(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    0x2e2e(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x10(%rbp),%edi
mov    %rax,%r9
lea    0xe42(%rip),%rsi
lea    0xe4b(%rip),%rdx
mov    $0x15,%ecx
lea    0xed5(%rip),%r8
call   1040 <fprintf@plt>
mov    0x2df3(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
cmpl   $0x0,-0x8(%rbp)
jne    1209 <main+0x99>
mov    $0x2,%edi
call   1080 <sleep@plt>
lea    0xeac(%rip),%rdi
call   1030 <printf@plt>
jmp    1297 <main+0x127>
mov    -0x8(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1050 <waitpid@plt>
mov    %eax,-0xc(%rbp)
cmp    $0xffffffff,%eax
jne    1275 <main+0x105>
jmp    1228 <main+0xb8>
mov    0x2db1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    0x2d9d(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x14(%rbp),%edi
mov    %rax,%r9
lea    0xdb1(%rip),%rsi
lea    0xdba(%rip),%rdx
mov    $0x1d,%ecx
lea    0xe55(%rip),%r8
call   1040 <fprintf@plt>
mov    0x2d62(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
cmpl   $0x0,-0xc(%rbp)
jne    128b <main+0x11b>
lea    0xe39(%rip),%rdi
call   1030 <printf@plt>
lea    0xe3d(%rip),%rdi
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
xor    %eax,%eax
call   1090 <fork@plt>
test   %eax,%eax
je     11d1 <main+0x61>
cmp    $0xffffffff,%eax
je     11fc <main+0x8c>
mov    %eax,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1050 <waitpid@plt>
lea    0xf20(%rip),%rbx
test   %eax,%eax
je     11e4 <main+0x74>
cmp    $0xffffffff,%eax
jne    11f0 <main+0x80>
mov    0x2e3d(%rip),%rax
mov    (%rax),%ebx
mov    0x2e2c(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xe46(%rip),%rsi
lea    0xe4f(%rip),%rdx
lea    0xee3(%rip),%r8
mov    %ebx,%edi
mov    $0x1d,%ecx
jmp    122f <main+0xbf>
mov    $0x2,%edi
call   1080 <sleep@plt>
lea    0xeec(%rip),%rbx
jmp    11f0 <main+0x80>
lea    0xed4(%rip),%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
call   1030 <puts@plt>
xor    %eax,%eax
pop    %rbx
ret
mov    0x2ddd(%rip),%rax
mov    (%rax),%ebx
mov    0x2dcc(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xde6(%rip),%rsi
lea    0xdef(%rip),%rdx
lea    0xe7e(%rip),%r8
mov    %ebx,%edi
mov    $0x15,%ecx
mov    %rax,%r9
call   1040 <fprintf@plt>
mov    0x2d92(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [16 x i8] c"[%s][%d]:%s %s\0A\00"
@.str.1 = [150 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/waitpid.c\00"
@.str.2 = [5 x i8] c"fork\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
@.str.3 = [12 x i8] c"I'm child!\0A\00"
@.str.4 = [8 x i8] c"waitpid\00"
@.str.5 = [16 x i8] c"no child exit!\0A\00"
@.str.6 = [13 x i8] c"I'm parent!\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
store i32 0, i32* %1
%4 = call i32 (...) @fork()
store i32 %4, i32* %2
%5 = icmp eq i32 %4, -1
br i1 %5, label %6, label %15
6: ; preds = %0
br label %7
7: ; preds = %6
%8 = load i32, i32* @stderr
%9 = load i32, i32* @errno
%10 = call i8* @strerror(i32 %9)
%11 = call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([150 x i8], [150 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* %10)
%12 = load i32, i32* @EXIT_FAILURE
%13 = call i32 @exit(i32 %12)
unreachable
14: ; No predecessors!
br label %15
15: ; preds = %14, %0
%16 = load i32, i32* %2
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %15
%19 = call i32 @sleep(i32 2)
%20 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
br label %41
21: ; preds = %15
%22 = load i32, i32* %2
%23 = call i32 @waitpid(i32 %22, i32* null, i32 0)
store i32 %23, i32* %3
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %34
25: ; preds = %21
br label %26
26: ; preds = %25
%27 = load i32, i32* @stderr
%28 = load i32, i32* @errno
%29 = call i8* @strerror(i32 %28)
%30 = call i32 @fprintf(i32 %27, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([150 x i8], [150 x i8]* @.str.1, i64 0, i64 0), i32 29, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i8* %29)
%31 = load i32, i32* @EXIT_FAILURE
%32 = call i32 @exit(i32 %31)
unreachable
33: ; No predecessors!
br label %34
34: ; preds = %33, %21
%35 = load i32, i32* %3
%36 = icmp eq i32 %35, 0
br i1 %36, label %37, label %39
37: ; preds = %34
%38 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0))
br label %39
39: ; preds = %37, %34
%40 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.6, i64 0, i64 0))
br label %41
41: ; preds = %39, %18
ret i32 0
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [16 x i8] c"[%s][%d]:%s %s\0A\00"
@.str.1 = [150 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/waitpid.c\00"
@.str.2 = [5 x i8] c"fork\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
@.str.4 = [8 x i8] c"waitpid\00"
@str = [12 x i8] c"I'm parent!\00"
@str.7 = [15 x i8] c"no child exit!\00"
@str.8 = [11 x i8] c"I'm child!\00"
define i32 @main() {
%1 = tail call i32 (...) @fork()
switch i32 %1, label %11 [
i32 -1, label %2
i32 0, label %9
]
2: ; preds = %0
%3 = load i32, i32* @stderr
%4 = load i32, i32* @errno
%5 = tail call i8* @strerror(i32 %4)
%6 = tail call i32 @fprintf(i32 %3, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([150 x i8], [150 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* %5)
%7 = load i32, i32* @EXIT_FAILURE
%8 = tail call i32 @exit(i32 %7)
unreachable
9: ; preds = %0
%10 = tail call i32 @sleep(i32 2)
br label %22
11: ; preds = %0
%12 = tail call i32 @waitpid(i32 %1, i32* null, i32 0)
switch i32 %12, label %22 [
i32 -1, label %13
i32 0, label %20
]
13: ; preds = %11
%14 = load i32, i32* @stderr
%15 = load i32, i32* @errno
%16 = tail call i8* @strerror(i32 %15)
%17 = tail call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([150 x i8], [150 x i8]* @.str.1, i64 0, i64 0), i32 29, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i8* %16)
%18 = load i32, i32* @EXIT_FAILURE
%19 = tail call i32 @exit(i32 %18)
unreachable
20: ; preds = %11
%21 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str.7, i64 0, i64 0))
br label %22
22: ; preds = %20, %11, %9
%23 = phi i8* [ getelementptr inbounds ([11 x i8], [11 x i8]* @str.8, i64 0, i64 0), %9 ], [ getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0), %11 ], [ getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0), %20 ]
%24 = tail call i32 @puts(i8* dereferenceable(1) %23)
ret i32 0
}