#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();
scalar_t__ getpid();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, long);
int main(int argc, char *argv[]) {
  pid_t child_pid;
  child_pid = fork();
  if (child_pid == -1) {
    perror("Fork failed!");
    return -1;
  }
  int i;
  const int loopMax = 20;
  for (i = 1; i <= loopMax; i++) {
    if (child_pid == 0) {
      printf("Child (%ld): ", (long)getpid());
    } else {
      printf("Parent (%ld): ", (long)getpid());
    }
    printf("Count = %d\n", i);
  }
  if (child_pid == 0) {
    printf("Child (%ld) exiting...\n", (long)getpid());
  } else {
    printf("Parent (%ld) exiting...\n", (long)getpid());
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x0,%al
call   1060 <fork@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1182 <main+0x42>
lea    0xe8f(%rip),%rdi
call   1050 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1235 <main+0xf5>
movl   $0x14,-0x1c(%rbp)
movl   $0x1,-0x18(%rbp)
cmpl   $0x14,-0x18(%rbp)
jg     11f3 <main+0xb3>
cmpl   $0x0,-0x14(%rbp)
jne    11bf <main+0x7f>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %rax,%rsi
lea    0xe58(%rip),%rdi
call   1040 <printf@plt>
jmp    11d5 <main+0x95>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %rax,%rsi
lea    0xe4b(%rip),%rdi
call   1040 <printf@plt>
movslq -0x18(%rbp),%rsi
lea    0xe4a(%rip),%rdi
call   1040 <printf@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1190 <main+0x50>
cmpl   $0x0,-0x14(%rbp)
jne    1218 <main+0xd8>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %rax,%rsi
lea    0xe28(%rip),%rdi
call   1040 <printf@plt>
jmp    122e <main+0xee>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %rax,%rsi
lea    0xe25(%rip),%rdi
call   1040 <printf@plt>
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   %r12
push   %rbx
xor    %eax,%eax
call   1060 <fork@plt>
cmp    $0xffffffff,%eax
je     11fd <main+0xbd>
mov    %eax,%ebp
mov    $0x1,%ebx
lea    0xea7(%rip),%r14
lea    0xebd(%rip),%r15
lea    0xea7(%rip),%r12
jmp    119d <main+0x5d>
cs nopw 0x0(%rax,%rax,1)
mov    %r14,%rdi
call   1040 <printf@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
call   1040 <printf@plt>
add    $0x1,%rbx
cmp    $0x15,%rbx
je     11da <main+0x9a>
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %rax,%rsi
test   %ebp,%ebp
je     1180 <main+0x40>
mov    %r12,%rdi
call   1040 <printf@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
call   1040 <printf@plt>
add    $0x1,%rbx
cmp    $0x15,%rbx
jne    119d <main+0x5d>
xor    %ebx,%ebx
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe76(%rip),%rdi
jmp    11ea <main+0xaa>
xor    %ebx,%ebx
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe4c(%rip),%rdi
mov    %rax,%rsi
call   1040 <printf@plt>
mov    %ebx,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdfc(%rip),%rdi
call   1050 <perror@plt>
mov    $0xffffffff,%ebx
jmp    11f2 <main+0xb2>

LLVM IR (-O0):
@.str = [13 x i8] c"Fork failed!\00"
@.str.1 = [14 x i8] c"Child (%ld): \00"
@.str.2 = [15 x i8] c"Parent (%ld): \00"
@.str.3 = [12 x i8] c"Count = %d\0A\00"
@.str.4 = [24 x i8] c"Child (%ld) exiting...\0A\00"
@.str.5 = [25 x i8] c"Parent (%ld) exiting...\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = call i32 (...) @fork()
store i32 %9, i32* %6
%10 = load i32, i32* %6
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %44
14: ; preds = %2
store i32 20, i32* %8
store i32 1, i32* %7
br label %15
15: ; preds = %31, %14
%16 = load i32, i32* %7
%17 = icmp sle i32 %16, 20
br i1 %17, label %18, label %34
18: ; preds = %15
%19 = load i32, i32* %6
%20 = icmp eq i32 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %18
%22 = call i64 (...) @getpid()
%23 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i64 %22)
br label %27
24: ; preds = %18
%25 = call i64 (...) @getpid()
%26 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i64 %25)
br label %27
27: ; preds = %24, %21
%28 = load i32, i32* %7
%29 = sext i32 %28 to i64
%30 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i64 %29)
br label %31
31: ; preds = %27
%32 = load i32, i32* %7
%33 = add nsw i32 %32, 1
store i32 %33, i32* %7
br label %15
34: ; preds = %15
%35 = load i32, i32* %6
%36 = icmp eq i32 %35, 0
br i1 %36, label %37, label %40
37: ; preds = %34
%38 = call i64 (...) @getpid()
%39 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0), i64 %38)
br label %43
40: ; preds = %34
%41 = call i64 (...) @getpid()
%42 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0), i64 %41)
br label %43
43: ; preds = %40, %37
store i32 0, i32* %3
br label %44
44: ; preds = %43, %12
%45 = load i32, i32* %3
ret i32 %45
}

LLVM IR (-O2):
@.str = [13 x i8] c"Fork failed!\00"
@.str.1 = [14 x i8] c"Child (%ld): \00"
@.str.2 = [15 x i8] c"Parent (%ld): \00"
@.str.3 = [12 x i8] c"Count = %d\0A\00"
@.str.4 = [24 x i8] c"Child (%ld) exiting...\0A\00"
@.str.5 = [25 x i8] c"Parent (%ld) exiting...\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i32 (...) @fork()
%4 = icmp eq i32 %3, -1
br i1 %4, label %7, label %5
5: ; preds = %2
%6 = icmp eq i32 %3, 0
br label %9
7: ; preds = %2
%8 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
br label %30
9: ; preds = %17, %5
%10 = phi i64 [ 1, %5 ], [ %18, %17 ]
%11 = tail call i64 (...) @getpid()
br i1 %6, label %12, label %19
12: ; preds = %9
%13 = tail call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i64 %11)
%14 = tail call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i64 %10)
%15 = add nuw nsw i64 %10, 1
%16 = icmp eq i64 %15, 21
br i1 %16, label %24, label %17
17: ; preds = %12, %19
%18 = phi i64 [ %15, %12 ], [ %22, %19 ]
br label %9
19: ; preds = %9
%20 = tail call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i64 %11)
%21 = tail call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i64 %10)
%22 = add nuw nsw i64 %10, 1
%23 = icmp eq i64 %22, 21
br i1 %23, label %27, label %17
24: ; preds = %12
%25 = tail call i64 (...) @getpid()
%26 = tail call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0), i64 %25)
br label %30
27: ; preds = %19
%28 = tail call i64 (...) @getpid()
%29 = tail call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0), i64 %28)
br label %30
30: ; preds = %24, %27, %7
%31 = phi i32 [ -1, %7 ], [ 0, %27 ], [ 0, %24 ]
ret i32 %31
}