#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 fork();
int getpid();
int /*<<< orphan*/ printf(char *, int, ...);
int /*<<< orphan*/ sleep(int);
int main(void) {
  pid_t pid1;
  pid_t pid2;
  pid_t pid3;
  int i;
  pid1 = getpid();
  printf("pid1 = %d\n", pid1);
  pid2 = fork();
  if (pid2 < 0) {
    printf("Error!: pid2 = %d\n", pid2);
    return -1;
  } else if (pid2 == 0) {
    pid3 = getpid();
    for (i = 0; i < 5; i++) {
      printf("I am child!, pid3 = %d.\n", pid3);
      sleep(1);
    }
  } else {
    pid3 = getpid();
    for (i = 0; i < 5; i++) {
      printf("I am parent!, pid3 = %d, my child pid2 = %d\n", pid3, pid2);
      sleep(1);
    }
  }
  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,-0x8(%rbp)
mov    -0x8(%rbp),%esi
lea    0xe9d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1060 <fork@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    119b <main+0x5b>
mov    -0xc(%rbp),%esi
lea    0xe83(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1246 <main+0x106>
cmpl   $0x0,-0xc(%rbp)
jne    11ee <main+0xae>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
cmpl   $0x5,-0x14(%rbp)
jge    11e9 <main+0xa9>
mov    -0x10(%rbp),%esi
lea    0xe54(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   1050 <sleep@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11b6 <main+0x76>
jmp    123a <main+0xfa>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
cmpl   $0x5,-0x14(%rbp)
jge    1235 <main+0xf5>
mov    -0x10(%rbp),%esi
mov    -0xc(%rbp),%edx
lea    0xe21(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   1050 <sleep@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11ff <main+0xbf>
jmp    123a <main+0xfa>
jmp    123f <main+0xff>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xeab(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
call   1060 <fork@plt>
mov    %eax,%ebx
test   %eax,%eax
js     11fd <main+0xbd>
xor    %r14d,%r14d
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %eax,%ebp
test   %ebx,%ebx
je     1215 <main+0xd5>
lea    0xead(%rip),%r15
mov    %r15,%rdi
mov    %ebp,%esi
mov    %ebx,%edx
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x1,%edi
call   1050 <sleep@plt>
mov    %r15,%rdi
mov    %ebp,%esi
mov    %ebx,%edx
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x1,%edi
call   1050 <sleep@plt>
mov    %r15,%rdi
mov    %ebp,%esi
mov    %ebx,%edx
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x1,%edi
call   1050 <sleep@plt>
mov    %r15,%rdi
mov    %ebp,%esi
mov    %ebx,%edx
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x1,%edi
call   1050 <sleep@plt>
mov    %r15,%rdi
mov    %ebp,%esi
mov    %ebx,%edx
xor    %eax,%eax
call   1040 <printf@plt>
jmp    1283 <main+0x143>
lea    0xe07(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0xffffffff,%r14d
jmp    128d <main+0x14d>
lea    0xe02(%rip),%rbx
xor    %r14d,%r14d
mov    %rbx,%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x1,%edi
call   1050 <sleep@plt>
mov    %rbx,%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x1,%edi
call   1050 <sleep@plt>
mov    %rbx,%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x1,%edi
call   1050 <sleep@plt>
mov    %rbx,%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x1,%edi
call   1050 <sleep@plt>
mov    %rbx,%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x1,%edi
call   1050 <sleep@plt>
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [11 x i8] c"pid1 = %d\0A\00"
@.str.1 = [19 x i8] c"Error!: pid2 = %d\0A\00"
@.str.2 = [25 x i8] c"I am child!, pid3 = %d.\0A\00"
@.str.3 = [45 x i8] c"I am parent!, pid3 = %d, my child pid2 = %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 0, i32* %1
%6 = call i32 (...) @getpid()
store i32 %6, i32* %2
%7 = load i32, i32* %2
%8 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %7)
%9 = call i32 (...) @fork()
store i32 %9, i32* %3
%10 = load i32, i32* %3
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %0
%13 = load i32, i32* %3
%14 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i32 %13)
store i32 -1, i32* %1
br label %47
15: ; preds = %0
%16 = load i32, i32* %3
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %31
18: ; preds = %15
%19 = call i32 (...) @getpid()
store i32 %19, i32* %4
store i32 0, i32* %5
br label %20
20: ; preds = %27, %18
%21 = load i32, i32* %5
%22 = icmp slt i32 %21, 5
br i1 %22, label %23, label %30
23: ; preds = %20
%24 = load i32, i32* %4
%25 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i32 %24)
%26 = call i32 @sleep(i32 1)
br label %27
27: ; preds = %23
%28 = load i32, i32* %5
%29 = add nsw i32 %28, 1
store i32 %29, i32* %5
br label %20
30: ; preds = %20
br label %45
31: ; preds = %15
%32 = call i32 (...) @getpid()
store i32 %32, i32* %4
store i32 0, i32* %5
br label %33
33: ; preds = %41, %31
%34 = load i32, i32* %5
%35 = icmp slt i32 %34, 5
br i1 %35, label %36, label %44
36: ; preds = %33
%37 = load i32, i32* %4
%38 = load i32, i32* %3
%39 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.3, i64 0, i64 0), i32 %37, i32 %38)
%40 = call i32 @sleep(i32 1)
br label %41
41: ; preds = %36
%42 = load i32, i32* %5
%43 = add nsw i32 %42, 1
store i32 %43, i32* %5
br label %33
44: ; preds = %33
br label %45
45: ; preds = %44, %30
br label %46
46: ; preds = %45
store i32 0, i32* %1
br label %47
47: ; preds = %46, %12
%48 = load i32, i32* %1
ret i32 %48
}

LLVM IR (-O2):
@.str = [11 x i8] c"pid1 = %d\0A\00"
@.str.1 = [19 x i8] c"Error!: pid2 = %d\0A\00"
@.str.2 = [25 x i8] c"I am child!, pid3 = %d.\0A\00"
@.str.3 = [45 x i8] c"I am parent!, pid3 = %d, my child pid2 = %d\0A\00"
define i32 @main() {
%1 = tail call i32 (...) @getpid()
%2 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %1)
%3 = tail call i32 (...) @fork()
%4 = icmp slt i32 %3, 0
br i1 %4, label %5, label %7
5: ; preds = %0
%6 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i32 %3)
br label %32
7: ; preds = %0
%8 = icmp eq i32 %3, 0
%9 = tail call i32 (...) @getpid()
br i1 %8, label %10, label %21
10: ; preds = %7
%11 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i32 %9)
%12 = tail call i32 @sleep(i32 1)
%13 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i32 %9)
%14 = tail call i32 @sleep(i32 1)
%15 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i32 %9)
%16 = tail call i32 @sleep(i32 1)
%17 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i32 %9)
%18 = tail call i32 @sleep(i32 1)
%19 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i32 %9)
%20 = tail call i32 @sleep(i32 1)
br label %32
21: ; preds = %7
%22 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.3, i64 0, i64 0), i32 %9, i32 %3)
%23 = tail call i32 @sleep(i32 1)
%24 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.3, i64 0, i64 0), i32 %9, i32 %3)
%25 = tail call i32 @sleep(i32 1)
%26 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.3, i64 0, i64 0), i32 %9, i32 %3)
%27 = tail call i32 @sleep(i32 1)
%28 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.3, i64 0, i64 0), i32 %9, i32 %3)
%29 = tail call i32 @sleep(i32 1)
%30 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.3, i64 0, i64 0), i32 %9, i32 %3)
%31 = tail call i32 @sleep(i32 1)
br label %32
32: ; preds = %21, %10, %5
%33 = phi i32 [ -1, %5 ], [ 0, %10 ], [ 0, %21 ]
ret i32 %33
}