#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 EXIT_SUCCESS;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
scalar_t__ getpid();
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ stderr;
int main() {
  pid_t child_pid;
  printf("the main program process id is %d\n", (int)getpid());
  child_pid = fork();
  switch (child_pid) {
  case -1:
    fprintf(stderr, "Error: fork()");
    exit(EXIT_FAILURE);
  case 0:
    printf("this is the child process, with id %d\n", (int)getpid());
    sleep(60);
    break;
  default:
    printf("this is the parent process, with id %d\n", (int)getpid());
    printf("the child's process id is %d\n", (int)child_pid);
    sleep(30);
  };
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
xor    %eax,%eax
mov    %al,-0xd(%rbp)
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe7e(%rip),%rdi
call   1040 <printf@plt>
mov    -0xd(%rbp),%al
call   1080 <fork@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0xc(%rbp)
sub    $0xffffffff,%eax
je     11b6 <main+0x56>
jmp    11a6 <main+0x46>
mov    -0xc(%rbp),%eax
test   %eax,%eax
je     11d9 <main+0x79>
jmp    11fd <main+0x9d>
mov    0x2e23(%rip),%rax
mov    (%rax),%edi
lea    0xe5d(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x2e06(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe48(%rip),%rdi
call   1040 <printf@plt>
mov    $0x3c,%edi
call   1070 <sleep@plt>
jmp    122b <main+0xcb>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe4b(%rip),%rdi
call   1040 <printf@plt>
mov    -0x8(%rbp),%esi
lea    0xe64(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1e,%edi
call   1070 <sleep@plt>
mov    0x2d9e(%rip),%rax
mov    (%rax),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe91(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
xor    %eax,%eax
call   1080 <fork@plt>
test   %eax,%eax
je     11b2 <main+0x52>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     11dc <main+0x7c>
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xec2(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
lea    0xedc(%rip),%rdi
mov    %ebx,%esi
call   1040 <printf@plt>
mov    $0x1e,%edi
jmp    11cc <main+0x6c>
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe71(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
mov    $0x3c,%edi
call   1070 <sleep@plt>
mov    0x2df8(%rip),%rax
mov    (%rax),%eax
pop    %rbx
ret
mov    0x2dfd(%rip),%rax
mov    (%rax),%edi
lea    0xe37(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x2de0(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@.str = [35 x i8] c"the main program process id is %d\0A\00"
@stderr = global i32 0
@.str.1 = [14 x i8] c"Error: fork()\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [39 x i8] c"this is the child process, with id %d\0A\00"
@.str.3 = [40 x i8] c"this is the parent process, with id %d\0A\00"
@.str.4 = [30 x i8] c"the child's process id is %d\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
store i32 0, i32* %1
%3 = call i64 (...) @getpid()
%4 = trunc i64 %3 to i32
%5 = call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i32 %4)
%6 = call i32 (...) @fork()
store i32 %6, i32* %2
%7 = load i32, i32* %2
switch i32 %7, label %18 [
i32 -1, label %8
i32 0, label %13
]
8: ; preds = %0
%9 = load i32, i32* @stderr
%10 = call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%11 = load i32, i32* @EXIT_FAILURE
%12 = call i32 @exit(i32 %11)
unreachable
13: ; preds = %0
%14 = call i64 (...) @getpid()
%15 = trunc i64 %14 to i32
%16 = call i32 @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0), i32 %15)
%17 = call i32 @sleep(i32 60)
br label %25
18: ; preds = %0
%19 = call i64 (...) @getpid()
%20 = trunc i64 %19 to i32
%21 = call i32 @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.3, i64 0, i64 0), i32 %20)
%22 = load i32, i32* %2
%23 = call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0), i32 %22)
%24 = call i32 @sleep(i32 30)
br label %25
25: ; preds = %18, %13
%26 = load i32, i32* @EXIT_SUCCESS
ret i32 %26
}

LLVM IR (-O2):
@.str = [35 x i8] c"the main program process id is %d\0A\00"
@stderr = global i32 0
@.str.1 = [14 x i8] c"Error: fork()\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [39 x i8] c"this is the child process, with id %d\0A\00"
@.str.3 = [40 x i8] c"this is the parent process, with id %d\0A\00"
@.str.4 = [30 x i8] c"the child's process id is %d\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = tail call i64 (...) @getpid()
%2 = trunc i64 %1 to i32
%3 = tail call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i32 %2)
%4 = tail call i32 (...) @fork()
switch i32 %4, label %14 [
i32 -1, label %5
i32 0, label %10
]
5: ; preds = %0
%6 = load i32, i32* @stderr
%7 = tail call i32 @fprintf(i32 %6, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%8 = load i32, i32* @EXIT_FAILURE
%9 = tail call i32 @exit(i32 %8)
unreachable
10: ; preds = %0
%11 = tail call i64 (...) @getpid()
%12 = trunc i64 %11 to i32
%13 = tail call i32 @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0), i32 %12)
br label %19
14: ; preds = %0
%15 = tail call i64 (...) @getpid()
%16 = trunc i64 %15 to i32
%17 = tail call i32 @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.3, i64 0, i64 0), i32 %16)
%18 = tail call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0), i32 %4)
br label %19
19: ; preds = %14, %10
%20 = phi i32 [ 30, %14 ], [ 60, %10 ]
%21 = tail call i32 @sleep(i32 %20)
%22 = load i32, i32* @EXIT_SUCCESS
ret i32 %22
}