#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*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sleep(int);
scalar_t__ wait(int /*<<< orphan*/ *);
int main(void) {
  pid_t child, child_pid;
  child = fork();
  if (child < 0) {
    perror("Error in fork");
  } else if (child == 0) {
    printf("I am the child process, ID = %i\n", getpid());
    sleep(10);
    printf("sleep over\n");
  } else {
    printf("I am the parent process, ID = %i\n", getpid());
    child_pid = wait(NULL);
    printf("I catched a child process with pid = %i\n", child_pid);
  }
  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)
jge    1196 <main+0x36>
lea    0xe74(%rip),%rdi
call   1050 <perror@plt>
jmp    1210 <main+0xb0>
cmpq   $0x0,-0x10(%rbp)
jne    11d5 <main+0x75>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe5d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0xa,%edi
call   1060 <sleep@plt>
lea    0xe66(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    120b <main+0xab>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe56(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1070 <wait@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rsi
lea    0xe59(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1210 <main+0xb0>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
js     11de <main+0x6e>
mov    %rax,%rbx
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,%esi
test   %rbx,%rbx
je     11b6 <main+0x46>
lea    0xe9a(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %edi,%edi
call   1080 <wait@plt>
lea    0xea7(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    11da <main+0x6a>
lea    0xe51(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0xa,%edi
call   1070 <sleep@plt>
lea    0xea5(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
pop    %rbx
ret
lea    0xe1b(%rip),%rdi
call   1060 <perror@plt>
jmp    11da <main+0x6a>

LLVM IR (-O0):
@.str = [14 x i8] c"Error in fork\00"
@.str.1 = [33 x i8] c"I am the child process, ID = %i\0A\00"
@.str.2 = [12 x i8] c"sleep over\0A\00"
@.str.3 = [34 x i8] c"I am the parent process, ID = %i\0A\00"
@.str.4 = [41 x i8] c"I catched a child process with pid = %i\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca i64
store i32 0, i32* %1
%4 = call i64 (...) @fork()
store i64 %4, i64* %2
%5 = load i64, i64* %2
%6 = icmp slt i64 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %0
%8 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %24
9: ; preds = %0
%10 = load i64, i64* %2
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %17
12: ; preds = %9
%13 = call i32 (...) @getpid()
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i32 %13)
%15 = call i32 @sleep(i32 10)
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
br label %23
17: ; preds = %9
%18 = call i32 (...) @getpid()
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0), i32 %18)
%20 = call i64 @wait(i32* null)
store i64 %20, i64* %3
%21 = load i64, i64* %3
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.4, i64 0, i64 0), i64 %21)
br label %23
23: ; preds = %17, %12
br label %24
24: ; preds = %23, %7
ret i32 0
}

LLVM IR (-O2):
@.str = [14 x i8] c"Error in fork\00"
@.str.1 = [33 x i8] c"I am the child process, ID = %i\0A\00"
@.str.3 = [34 x i8] c"I am the parent process, ID = %i\0A\00"
@.str.4 = [41 x i8] c"I catched a child process with pid = %i\0A\00"
@str = [11 x i8] c"sleep over\00"
define i32 @main() {
%1 = tail call i64 (...) @fork()
%2 = icmp slt i64 %1, 0
br i1 %2, label %3, label %5
3: ; preds = %0
%4 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %16
5: ; preds = %0
%6 = icmp eq i64 %1, 0
%7 = tail call i32 (...) @getpid()
br i1 %6, label %8, label %12
8: ; preds = %5
%9 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i32 %7)
%10 = tail call i32 @sleep(i32 10)
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str, i64 0, i64 0))
br label %16
12: ; preds = %5
%13 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0), i32 %7)
%14 = tail call i64 @wait(i32* null)
%15 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @.str.4, i64 0, i64 0), i64 %14)
br label %16
16: ; preds = %8, %12, %3
ret i32 0
}