#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;
  int ret;
  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/watipid.c",
              21, "fork", strerror(errno));
      exit(EXIT_FAILURE);
    } while (0);
  if (pid == 0) {
    printf("i am a chile process\n");
    sleep(2);
  } else {
    if (-1 == (ret = waitpid(-1, NULL, 0)))
      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/watipid.c",
                27, "waitpid", strerror(errno));
        exit(EXIT_FAILURE);
      } while (0);
    printf("i am a parent process");
  }
  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>
lea    0xeb6(%rip),%rdi
call   1030 <printf@plt>
mov    $0x2,%edi
call   1080 <sleep@plt>
jmp    1289 <main+0x119>
mov    $0xffffffff,%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1050 <waitpid@plt>
mov    %eax,%ecx
mov    %ecx,-0xc(%rbp)
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    127d <main+0x10d>
jmp    1230 <main+0xc0>
mov    0x2da9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    0x2d95(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x14(%rbp),%edi
mov    %rax,%r9
lea    0xda9(%rip),%rsi
lea    0xdb2(%rip),%rdx
mov    $0x1b,%ecx
lea    0xe57(%rip),%r8
call   1040 <fprintf@plt>
mov    0x2d5a(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
lea    0xe45(%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   10a0 <fork@plt>
test   %eax,%eax
je     11b2 <main+0x32>
cmp    $0xffffffff,%eax
je     11cc <main+0x4c>
mov    $0xffffffff,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1060 <waitpid@plt>
cmp    $0xffffffff,%eax
je     1201 <main+0x81>
lea    0xf08(%rip),%rdi
call   1040 <printf@plt>
jmp    11c8 <main+0x48>
lea    0xf10(%rip),%rdi
call   1030 <puts@plt>
mov    $0x2,%edi
call   1090 <sleep@plt>
xor    %eax,%eax
pop    %rbx
ret
mov    0x2e0d(%rip),%rax
mov    (%rax),%ebx
mov    0x2dfc(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xe16(%rip),%rsi
lea    0xe1f(%rip),%rdx
lea    0xeae(%rip),%r8
mov    %ebx,%edi
mov    $0x15,%ecx
jmp    1234 <main+0xb4>
mov    0x2dd8(%rip),%rax
mov    (%rax),%ebx
mov    0x2dc7(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xde1(%rip),%rsi
lea    0xdea(%rip),%rdx
lea    0xe7e(%rip),%r8
mov    %ebx,%edi
mov    $0x1b,%ecx
mov    %rax,%r9
call   1050 <fprintf@plt>
mov    0x2d8d(%rip),%rax
mov    (%rax),%edi
call   1070 <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/watipid.c\00"
@.str.2 = [5 x i8] c"fork\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
@.str.3 = [22 x i8] c"i am a chile process\0A\00"
@.str.4 = [8 x i8] c"waitpid\00"
@.str.5 = [22 x i8] c"i am a parent process\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 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
%20 = call i32 @sleep(i32 2)
br label %35
21: ; preds = %15
%22 = call i32 @waitpid(i32 -1, i32* null, i32 0)
store i32 %22, i32* %3
%23 = icmp eq i32 -1, %22
br i1 %23, label %24, label %33
24: ; preds = %21
br label %25
25: ; preds = %24
%26 = load i32, i32* @stderr
%27 = load i32, i32* @errno
%28 = call i8* @strerror(i32 %27)
%29 = call i32 @fprintf(i32 %26, 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 27, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i8* %28)
%30 = load i32, i32* @EXIT_FAILURE
%31 = call i32 @exit(i32 %30)
unreachable
32: ; No predecessors!
br label %33
33: ; preds = %32, %21
%34 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0))
br label %35
35: ; preds = %33, %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/watipid.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.5 = [22 x i8] c"i am a parent process\00"
@str = [21 x i8] c"i am a chile process\00"
define i32 @main() {
%1 = tail call i32 (...) @fork()
switch i32 %1, label %12 [
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 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str, i64 0, i64 0))
%11 = tail call i32 @sleep(i32 2)
br label %24
12: ; preds = %0
%13 = tail call i32 @waitpid(i32 -1, i32* null, i32 0)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %22
15: ; preds = %12
%16 = load i32, i32* @stderr
%17 = load i32, i32* @errno
%18 = tail call i8* @strerror(i32 %17)
%19 = tail call i32 @fprintf(i32 %16, 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 27, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i8* %18)
%20 = load i32, i32* @EXIT_FAILURE
%21 = tail call i32 @exit(i32 %20)
unreachable
22: ; preds = %12
%23 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0))
br label %24
24: ; preds = %22, %9
ret i32 0
}