#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__ fork();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sleep(int);
int waitpid(scalar_t__, int *, int /*<<< orphan*/);
int main() {
  pid_t fork_result = fork();
  if (fork_result == 0) {
    printf("Hello child\n");
    sleep(1);
    return 5;
  } else if (fork_result > 0) {
    printf("Hello parent");
    sleep(10);
    int status;
    if (waitpid(fork_result, &status, 0) != -1) {
      printf("child process %d", WEXITSTATUS(status));
    }
    return 3;
  } else {
    perror("fork");
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1080 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11a9 <main+0x49>
lea    0xe74(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x1,%edi
call   1070 <sleep@plt>
movl   $0x5,-0x4(%rbp)
jmp    1220 <main+0xc0>
cmpq   $0x0,-0x10(%rbp)
jle    1208 <main+0xa8>
lea    0xe52(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0xa,%edi
call   1070 <sleep@plt>
mov    -0x10(%rbp),%rdi
lea    -0x14(%rbp),%rsi
xor    %edx,%edx
call   1050 <waitpid@plt>
cmp    $0xffffffff,%eax
je     11fc <main+0x9c>
mov    -0x14(%rbp),%edi
call   1040 <WEXITSTATUS@plt>
mov    %eax,%esi
lea    0xe25(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x3,-0x4(%rbp)
jmp    1220 <main+0xc0>
lea    0xe1c(%rip),%rdi
call   1060 <perror@plt>
jmp    1219 <main+0xb9>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
je     11d2 <main+0x62>
jle    11f3 <main+0x83>
mov    %rax,%rbx
lea    0xe73(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0xa,%edi
call   1080 <sleep@plt>
lea    0xc(%rsp),%rsi
mov    %rbx,%rdi
xor    %edx,%edx
call   1060 <waitpid@plt>
cmp    $0xffffffff,%eax
je     11cb <main+0x5b>
mov    0xc(%rsp),%edi
call   1050 <WEXITSTATUS@plt>
lea    0xe4b(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x3,%eax
jmp    11ed <main+0x7d>
lea    0xe4a(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1080 <sleep@plt>
mov    $0x5,%eax
add    $0x10,%rsp
pop    %rbx
ret
lea    0xe24(%rip),%rdi
call   1070 <perror@plt>
xor    %eax,%eax
jmp    11ed <main+0x7d>

LLVM IR (-O0):
@.str = [13 x i8] c"Hello child\0A\00"
@.str.1 = [13 x i8] c"Hello parent\00"
@.str.2 = [17 x i8] c"child process %d\00"
@.str.3 = [5 x i8] c"fork\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca i32
store i32 0, i32* %1
%4 = call i64 (...) @fork()
store i64 %4, i64* %2
%5 = load i64, i64* %2
%6 = icmp eq i64 %5, 0
br i1 %6, label %7, label %10
7: ; preds = %0
%8 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%9 = call i32 @sleep(i32 1)
store i32 5, i32* %1
br label %28
10: ; preds = %0
%11 = load i64, i64* %2
%12 = icmp sgt i64 %11, 0
br i1 %12, label %13, label %24
13: ; preds = %10
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%15 = call i32 @sleep(i32 10)
%16 = load i64, i64* %2
%17 = call i32 @waitpid(i64 %16, i32* %3, i32 0)
%18 = icmp ne i32 %17, -1
br i1 %18, label %19, label %23
19: ; preds = %13
%20 = load i32, i32* %3
%21 = call i32 @WEXITSTATUS(i32 %20)
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i32 %21)
br label %23
23: ; preds = %19, %13
store i32 3, i32* %1
br label %28
24: ; preds = %10
%25 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
br label %26
26: ; preds = %24
br label %27
27: ; preds = %26
store i32 0, i32* %1
br label %28
28: ; preds = %27, %23, %7
%29 = load i32, i32* %1
ret i32 %29
}

LLVM IR (-O2):
@.str.1 = [13 x i8] c"Hello parent\00"
@.str.2 = [17 x i8] c"child process %d\00"
@.str.3 = [5 x i8] c"fork\00"
@str = [12 x i8] c"Hello child\00"
define i32 @main() {
%1 = alloca i32
%2 = tail call i64 (...) @fork()
%3 = icmp eq i64 %2, 0
br i1 %3, label %4, label %7
4: ; preds = %0
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0))
%6 = tail call i32 @sleep(i32 1)
br label %22
7: ; preds = %0
%8 = icmp sgt i64 %2, 0
br i1 %8, label %9, label %20
9: ; preds = %7
%10 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%11 = tail call i32 @sleep(i32 10)
%12 = bitcast i32* %1 to i8*
%13 = call i32 @waitpid(i64 %2, i32* %1, i32 0)
%14 = icmp eq i32 %13, -1
br i1 %14, label %19, label %15
15: ; preds = %9
%16 = load i32, i32* %1
%17 = call i32 @WEXITSTATUS(i32 %16)
%18 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i32 %17)
br label %19
19: ; preds = %15, %9
br label %22
20: ; preds = %7
%21 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
br label %22
22: ; preds = %20, %19, %4
%23 = phi i32 [ 5, %4 ], [ 3, %19 ], [ 0, %20 ]
ret i32 %23
}