#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 */
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int getpid();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sleep(int);
scalar_t__ waitpid(scalar_t__, int /*<<< orphan*/ *, int /*<<< orphan*/);
int main() {
  pid_t fpid;
  int count = 0;
  while (1) {
    fpid = fork();
    if (fpid < 0)
      printf("error in fork!");
    else if (fpid == 0) {
      printf("Child pid = %d\n", getpid());
      exit(0);
    } else {
      if (waitpid(fpid, NULL, 0) != fpid)
        perror("waitpid error");
      printf("Parent PID = %d\n", getpid());
      sleep(20);
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x14(%rbp)
mov    $0x0,%al
call   1090 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jge    11af <main+0x3f>
lea    0xe5d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1223 <main+0xb3>
cmpq   $0x0,-0x10(%rbp)
jne    11d8 <main+0x68>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe45(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %edi,%edi
call   1070 <exit@plt>
mov    -0x10(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1050 <waitpid@plt>
cmp    -0x10(%rbp),%rax
je     11fd <main+0x8d>
lea    0xe27(%rip),%rdi
call   1060 <perror@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe20(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x14,%edi
call   1080 <sleep@plt>
jmp    1223 <main+0xb3>
jmp    1186 <main+0x16>

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
lea    0xe81(%rip),%r14
lea    0xea7(%rip),%r12
lea    0xe92(%rip),%r15
jmp    119a <main+0x2a>
nop
mov    %r14,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
js     1190 <main+0x20>
je     11e5 <main+0x75>
mov    %rax,%rbx
mov    %rax,%rdi
xor    %esi,%esi
xor    %edx,%edx
call   1050 <waitpid@plt>
cmp    %rbx,%rax
jne    11db <main+0x6b>
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %r12,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x14,%edi
call   1080 <sleep@plt>
jmp    119a <main+0x2a>
mov    %r15,%rdi
call   1060 <perror@plt>
jmp    11bc <main+0x4c>
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe1c(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %edi,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [15 x i8] c"error in fork!\00"
@.str.1 = [16 x i8] c"Child pid = %d\0A\00"
@.str.2 = [14 x i8] c"waitpid error\00"
@.str.3 = [17 x i8] c"Parent PID = %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca i32
store i32 0, i32* %1
store i32 0, i32* %3
br label %4
4: ; preds = %0, %29
%5 = call i64 (...) @fork()
store i64 %5, i64* %2
%6 = load i64, i64* %2
%7 = icmp slt i64 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %4
%9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
br label %29
10: ; preds = %4
%11 = load i64, i64* %2
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %17
13: ; preds = %10
%14 = call i32 (...) @getpid()
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 %14)
%16 = call i32 @exit(i32 0)
unreachable
17: ; preds = %10
%18 = load i64, i64* %2
%19 = call i64 @waitpid(i64 %18, i32* null, i32 0)
%20 = load i64, i64* %2
%21 = icmp ne i64 %19, %20
br i1 %21, label %22, label %24
22: ; preds = %17
%23 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
br label %24
24: ; preds = %22, %17
%25 = call i32 (...) @getpid()
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i32 %25)
%27 = call i32 @sleep(i32 20)
br label %28
28: ; preds = %24
br label %29
29: ; preds = %28, %8
br label %4
}

LLVM IR (-O2):
@.str = [15 x i8] c"error in fork!\00"
@.str.1 = [16 x i8] c"Child pid = %d\0A\00"
@.str.2 = [14 x i8] c"waitpid error\00"
@.str.3 = [17 x i8] c"Parent PID = %d\0A\00"
define i32 @main() {
br label %1
1: ; preds = %21, %0
%2 = tail call i64 (...) @fork()
%3 = icmp slt i64 %2, 0
br i1 %3, label %4, label %6
4: ; preds = %1
%5 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
br label %21
6: ; preds = %1
%7 = icmp eq i64 %2, 0
br i1 %7, label %8, label %12
8: ; preds = %6
%9 = tail call i32 (...) @getpid()
%10 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 %9)
%11 = tail call i32 @exit(i32 0)
unreachable
12: ; preds = %6
%13 = tail call i64 @waitpid(i64 %2, i32* null, i32 0)
%14 = icmp eq i64 %13, %2
br i1 %14, label %17, label %15
15: ; preds = %12
%16 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
br label %17
17: ; preds = %15, %12
%18 = tail call i32 (...) @getpid()
%19 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i32 %18)
%20 = tail call i32 @sleep(i32 20)
br label %21
21: ; preds = %17, %4
br label %1
}