#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);
scalar_t__ WIFEXITED(int);
scalar_t__ fork();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ wait(int *);
int main(int argc, char **argv) {
  pid_t pid = fork();
  if (pid == 0) {
    printf("Hello from child!\n");
    return 9;
  } else if (pid > 0) {
    printf("Hello from parent!\n");
    printf("Waiting for child process..\n");
    int status = 0;
    wait(&status);
    if (WIFEXITED(status)) {
      int exit_status = WEXITSTATUS(status);
      printf(
          "Child process '%d' terminated normally with exit status code: %d\n",
          pid, exit_status);
    } else {
      printf("Child process exited abnormally!\n");
      return 1;
    }
  } else {
    perror("fork");
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x0,%al
call   1080 <fork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11a6 <main+0x46>
lea    0xe6d(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x9,-0x4(%rbp)
jmp    1257 <main+0xf7>
cmpq   $0x0,-0x18(%rbp)
jle    1233 <main+0xd3>
lea    0xe5b(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xe61(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x1c(%rbp)
lea    -0x1c(%rbp),%rdi
call   1070 <wait@plt>
mov    -0x1c(%rbp),%edi
call   1060 <WIFEXITED@plt>
cmp    $0x0,%rax
je     1214 <main+0xb4>
mov    -0x1c(%rbp),%edi
call   1040 <WEXITSTATUS@plt>
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%edx
lea    0xe3c(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    122e <main+0xce>
lea    0xe6b(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1257 <main+0xf7>
jmp    124b <main+0xeb>
lea    0xe6e(%rip),%rdi
call   1050 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1257 <main+0xf7>
jmp    1250 <main+0xf0>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
je     11e0 <main+0x70>
jle    120d <main+0x9d>
mov    %rax,%rbx
lea    0xeb8(%rip),%rdi
call   1030 <puts@plt>
lea    0xebf(%rip),%rdi
call   1030 <puts@plt>
movl   $0x0,0x4(%rsp)
lea    0x4(%rsp),%rdi
call   1080 <wait@plt>
mov    0x4(%rsp),%edi
call   1070 <WIFEXITED@plt>
test   %rax,%rax
je     11f3 <main+0x83>
mov    0x4(%rsp),%edi
call   1050 <WEXITSTATUS@plt>
lea    0xe30(%rip),%rdi
xor    %ebp,%ebp
mov    %rbx,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <printf@plt>
jmp    1204 <main+0x94>
lea    0xeb0(%rip),%rdi
call   1030 <puts@plt>
mov    $0x9,%ebp
jmp    1204 <main+0x94>
lea    0xe7c(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe2e(%rip),%rdi
call   1060 <perror@plt>
jmp    11ff <main+0x8f>

LLVM IR (-O0):
@.str = [19 x i8] c"Hello from child!\0A\00"
@.str.1 = [20 x i8] c"Hello from parent!\0A\00"
@.str.2 = [29 x i8] c"Waiting for child process..\0A\00"
@.str.3 = [66 x i8] c"Child process '%d' terminated normally with exit status code: %d\0A\00"
@.str.4 = [34 x i8] c"Child process exited abnormally!\0A\00"
@.str.5 = [5 x i8] c"fork\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i32
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = call i64 (...) @fork()
store i64 %9, i64* %6
%10 = load i64, i64* %6
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
store i32 9, i32* %3
br label %37
14: ; preds = %2
%15 = load i64, i64* %6
%16 = icmp sgt i64 %15, 0
br i1 %16, label %17, label %33
17: ; preds = %14
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %7
%20 = call i32 @wait(i32* %7)
%21 = load i32, i32* %7
%22 = call i64 @WIFEXITED(i32 %21)
%23 = icmp ne i64 %22, 0
br i1 %23, label %24, label %30
24: ; preds = %17
%25 = load i32, i32* %7
%26 = call i32 @WEXITSTATUS(i32 %25)
store i32 %26, i32* %8
%27 = load i64, i64* %6
%28 = load i32, i32* %8
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([66 x i8], [66 x i8]* @.str.3, i64 0, i64 0), i64 %27, i32 %28)
br label %32
30: ; preds = %17
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %3
br label %37
32: ; preds = %24
br label %35
33: ; preds = %14
%34 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
store i32 1, i32* %3
br label %37
35: ; preds = %32
br label %36
36: ; preds = %35
store i32 0, i32* %3
br label %37
37: ; preds = %36, %33, %30, %12
%38 = load i32, i32* %3
ret i32 %38
}

LLVM IR (-O2):
@.str.3 = [66 x i8] c"Child process '%d' terminated normally with exit status code: %d\0A\00"
@.str.5 = [5 x i8] c"fork\00"
@str = [19 x i8] c"Hello from parent!\00"
@str.6 = [28 x i8] c"Waiting for child process..\00"
@str.7 = [33 x i8] c"Child process exited abnormally!\00"
@str.8 = [18 x i8] c"Hello from child!\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i32
%4 = tail call i64 (...) @fork()
%5 = icmp eq i64 %4, 0
br i1 %5, label %6, label %8
6: ; preds = %2
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str.8, i64 0, i64 0))
br label %28
8: ; preds = %2
%9 = icmp sgt i64 %4, 0
br i1 %9, label %10, label %26
10: ; preds = %8
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str, i64 0, i64 0))
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str.6, i64 0, i64 0))
%13 = bitcast i32* %3 to i8*
store i32 0, i32* %3
%14 = call i32 @wait(i32* %3)
%15 = load i32, i32* %3
%16 = call i64 @WIFEXITED(i32 %15)
%17 = icmp eq i64 %16, 0
br i1 %17, label %22, label %18
18: ; preds = %10
%19 = load i32, i32* %3
%20 = call i32 @WEXITSTATUS(i32 %19)
%21 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([66 x i8], [66 x i8]* @.str.3, i64 0, i64 0), i64 %4, i32 %20)
br label %24
22: ; preds = %10
%23 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @str.7, i64 0, i64 0))
br label %24
24: ; preds = %18, %22
%25 = phi i32 [ 0, %18 ], [ 1, %22 ]
br label %28
26: ; preds = %8
%27 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
br label %28
28: ; preds = %24, %26, %6
%29 = phi i32 [ 9, %6 ], [ 1, %26 ], [ %25, %24 ]
ret i32 %29
}