#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 */
scalar_t__ fork();
int getpid();
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ wait(int *);
int main(void) {
  printf("Process %d started\n", getpid());
  pid_t pid = fork();
  if (pid < 0) {
    printf("Error while forking.\n");
    return -1;
  }
  if (pid == 0) {
    printf("I am a baby process and my pid is %d \n.", getpid());
  } else {
    printf("I am a proud parent of process %d. I still have the pid %d \n.",
           pid, getpid());
  }
  printf("Press CTRL+C to stop us.\n");
  while (1) {
    printf("%d: ZZZzzz\n", getpid());
    sleep(5);
  }
  if (pid != 0) {
    int status;
    wait(&status);
    printf("Child %d exited with status %d:\n", pid, status);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xea1(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1060 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jge    1195 <main+0x55>
lea    0xe91(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0xffffffff,%eax
add    $0x20,%rsp
pop    %rbp
ret
cmpq   $0x0,-0x10(%rbp)
jne    11bc <main+0x7c>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe7a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    11df <main+0x9f>
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0x18(%rbp),%rsi
mov    %eax,%edx
lea    0xe7a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xeaa(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xead(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x5,%edi
call   1050 <sleep@plt>
jmp    11ed <main+0xad>

x86 assembly (clang, -O2):
<main>:
push   %rbx
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xea1(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
call   1070 <fork@plt>
test   %rax,%rax
js     1198 <main+0x48>
mov    %rax,%rbx
xor    %eax,%eax
call   1040 <getpid@plt>
test   %rbx,%rbx
je     11ab <main+0x5b>
lea    0xeb2(%rip),%rdi
mov    %rbx,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1050 <printf@plt>
jmp    11bb <main+0x6b>
lea    0xf00(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%eax
pop    %rbx
ret
lea    0xe62(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xec4(%rip),%rdi
call   1030 <puts@plt>
lea    0xeac(%rip),%rbx
xchg   %ax,%ax
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %rbx,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x5,%edi
call   1060 <sleep@plt>
jmp    11d0 <main+0x80>

LLVM IR (-O0):
@.str = [20 x i8] c"Process %d started\0A\00"
@.str.1 = [22 x i8] c"Error while forking.\0A\00"
@.str.2 = [40 x i8] c"I am a baby process and my pid is %d \0A.\00"
@.str.3 = [62 x i8] c"I am a proud parent of process %d. I still have the pid %d \0A.\00"
@.str.4 = [26 x i8] c"Press CTRL+C to stop us.\0A\00"
@.str.5 = [12 x i8] c"%d: ZZZzzz\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
store i32 0, i32* %1
%3 = call i32 (...) @getpid()
%4 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = call i64 (...) @fork()
store i64 %5, i64* %2
%6 = load i64, i64* %2
%7 = icmp slt i64 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %0
%9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
ret i32 -1
10: ; preds = %0
%11 = load i64, i64* %2
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %10
%14 = call i32 (...) @getpid()
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0), i32 %14)
br label %20
16: ; preds = %10
%17 = load i64, i64* %2
%18 = call i32 (...) @getpid()
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([62 x i8], [62 x i8]* @.str.3, i64 0, i64 0), i64 %17, i32 %18)
br label %20
20: ; preds = %16, %13
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0))
br label %22
22: ; preds = %20, %22
%23 = call i32 (...) @getpid()
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0), i32 %23)
%25 = call i32 @sleep(i32 5)
br label %22
}

LLVM IR (-O2):
@.str = [20 x i8] c"Process %d started\0A\00"
@.str.2 = [40 x i8] c"I am a baby process and my pid is %d \0A.\00"
@.str.3 = [62 x i8] c"I am a proud parent of process %d. I still have the pid %d \0A.\00"
@.str.5 = [12 x i8] c"%d: ZZZzzz\0A\00"
@str = [25 x i8] c"Press CTRL+C to stop us.\00"
@str.6 = [21 x i8] c"Error while forking.\00"
define i32 @main() {
%1 = tail call i32 (...) @getpid()
%2 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i32 %1)
%3 = tail call i64 (...) @fork()
%4 = icmp slt i64 %3, 0
br i1 %4, label %5, label %7
5: ; preds = %0
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str.6, i64 0, i64 0))
ret i32 -1
7: ; preds = %0
%8 = icmp eq i64 %3, 0
%9 = tail call i32 (...) @getpid()
br i1 %8, label %10, label %12
10: ; preds = %7
%11 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0), i32 %9)
br label %14
12: ; preds = %7
%13 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([62 x i8], [62 x i8]* @.str.3, i64 0, i64 0), i64 %3, i32 %9)
br label %14
14: ; preds = %12, %10
%15 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str, i64 0, i64 0))
br label %16
16: ; preds = %16, %14
%17 = tail call i32 (...) @getpid()
%18 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0), i32 %17)
%19 = tail call i32 @sleep(i32 5)
br label %16
}