#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 /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ sleep(int);
int main() {
  pid_t childpid = 0;
  int count = 0;
  if (childpid == fork()) {
    sleep(5);
    while (1) {
      printf("Child: %d\n", count);
      sleep(1);
      count++;
    }
  } else {
    while (1) {
      printf("parent: %d \n", count);
      sleep(1);
      count++;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
movq   $0x0,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    $0x0,%al
call   1050 <fork@plt>
mov    %rax,%rcx
mov    -0x20(%rbp),%rax
cmp    %rcx,%rax
jne    11a1 <main+0x71>
mov    $0x5,%edi
call   1040 <sleep@plt>
mov    -0x14(%rbp),%esi
lea    0xe7f(%rip),%rdi
call   1030 <printf@plt>
mov    $0x1,%edi
call   1040 <sleep@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
mov    %eax,-0x14(%rbp)
jmp    1177 <main+0x47>
jmp    11a6 <main+0x76>
mov    -0x14(%rbp),%esi
lea    0xe5b(%rip),%rdi
call   1030 <printf@plt>
mov    $0x1,%edi
call   1040 <sleep@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
mov    %eax,-0x14(%rbp)
jmp    11a6 <main+0x76>

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
xor    %ebx,%ebx
xor    %eax,%eax
call   1050 <fork@plt>
test   %rax,%rax
je     1169 <main+0x39>
lea    0xec2(%rip),%r14
nopl   0x0(%rax)
mov    %r14,%rdi
mov    %ebx,%esi
call   1030 <printf@plt>
mov    $0x1,%edi
call   1040 <sleep@plt>
add    $0x1,%ebx
jmp    1150 <main+0x20>
mov    $0x5,%edi
call   1040 <sleep@plt>
xor    %ebx,%ebx
lea    0xe84(%rip),%r14
nopl   0x0(%rax)
mov    %r14,%rdi
mov    %ebx,%esi
call   1030 <printf@plt>
mov    $0x1,%edi
call   1040 <sleep@plt>
add    $0x1,%ebx
jmp    1180 <main+0x50>

LLVM IR (-O0):
@.str = [11 x i8] c"Child: %d\0A\00"
@.str.1 = [13 x i8] c"parent: %d \0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca i32
store i32 0, i32* %1
store i64 0, i64* %2
store i32 0, i32* %3
%4 = load i64, i64* %2
%5 = call i64 (...) @fork()
%6 = icmp eq i64 %4, %5
br i1 %6, label %7, label %15
7: ; preds = %0
%8 = call i32 @sleep(i32 5)
br label %9
9: ; preds = %7, %9
%10 = load i32, i32* %3
%11 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %10)
%12 = call i32 @sleep(i32 1)
%13 = load i32, i32* %3
%14 = add nsw i32 %13, 1
store i32 %14, i32* %3
br label %9
15: ; preds = %0
br label %16
16: ; preds = %15, %16
%17 = load i32, i32* %3
%18 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32 %17)
%19 = call i32 @sleep(i32 1)
%20 = load i32, i32* %3
%21 = add nsw i32 %20, 1
store i32 %21, i32* %3
br label %16
}

LLVM IR (-O2):
@.str = [11 x i8] c"Child: %d\0A\00"
@.str.1 = [13 x i8] c"parent: %d \0A\00"
define i32 @main() {
%1 = tail call i64 (...) @fork()
%2 = icmp eq i64 %1, 0
br i1 %2, label %3, label %10
3: ; preds = %0
%4 = tail call i32 @sleep(i32 5)
br label %5
5: ; preds = %5, %3
%6 = phi i32 [ 0, %3 ], [ %9, %5 ]
%7 = tail call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %6)
%8 = tail call i32 @sleep(i32 1)
%9 = add nuw nsw i32 %6, 1
br label %5
10: ; preds = %0, %10
%11 = phi i32 [ %14, %10 ], [ 0, %0 ]
%12 = tail call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32 %11)
%13 = tail call i32 @sleep(i32 1)
%14 = add nuw nsw i32 %11, 1
br label %10
}