#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(int);
int fork();
int getpid();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sleep(int);
int main() {
  int i;
  pid_t pid;
  printf("begin.........................\n");
  for (i = 0; i < 5; i++) {
    pid = fork();
    if (pid == -1) {
      perror("fork error:");
      exit(1);
    } else if (pid == 0) {
      break;
    }
  }
  if (i < 5) {
    sleep(i);
    printf("%d child ,pid = %u\n", i + 1, getpid());
  } else {
    sleep(i);
    printf("parent\n");
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe8a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x8(%rbp)
cmpl   $0x5,-0x8(%rbp)
jge    11df <main+0x7f>
mov    $0x0,%al
call   1080 <fork@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    11b8 <main+0x58>
lea    0xe77(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
cmpl   $0x0,-0xc(%rbp)
jne    11c7 <main+0x67>
jmp    11df <main+0x7f>
jmp    11cc <main+0x6c>
jmp    11d1 <main+0x71>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    1184 <main+0x24>
cmpl   $0x5,-0x8(%rbp)
jge    1219 <main+0xb9>
mov    -0x8(%rbp),%edi
call   1070 <sleep@plt>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0x10(%rbp),%esi
mov    %eax,%edx
lea    0xe1f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    122f <main+0xcf>
mov    -0x8(%rbp),%edi
call   1070 <sleep@plt>
lea    0xe18(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
lea    0xea8(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   1090 <fork@plt>
mov    %eax,%ebx
test   %eax,%eax
je     11ff <main+0x8f>
cmp    $0xffffffff,%ebx
je     1226 <main+0xb6>
xor    %eax,%eax
call   1090 <fork@plt>
mov    $0x1,%ebx
test   %eax,%eax
je     11ff <main+0x8f>
cmp    $0xffffffff,%eax
je     1226 <main+0xb6>
xor    %eax,%eax
call   1090 <fork@plt>
mov    $0x2,%ebx
test   %eax,%eax
je     11ff <main+0x8f>
cmp    $0xffffffff,%eax
je     1226 <main+0xb6>
xor    %eax,%eax
call   1090 <fork@plt>
mov    $0x3,%ebx
test   %eax,%eax
je     11ff <main+0x8f>
cmp    $0xffffffff,%eax
je     1226 <main+0xb6>
xor    %eax,%eax
call   1090 <fork@plt>
mov    $0x4,%ebx
test   %eax,%eax
je     11ff <main+0x8f>
cmp    $0xffffffff,%eax
je     1226 <main+0xb6>
mov    $0x5,%edi
call   1080 <sleep@plt>
lea    0xe47(%rip),%rdi
call   1030 <puts@plt>
jmp    1222 <main+0xb2>
mov    %ebx,%edi
call   1080 <sleep@plt>
add    $0x1,%ebx
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xdf5(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
pop    %rbx
ret
lea    0xdd3(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [32 x i8] c"begin.........................\0A\00"
@.str.1 = [12 x i8] c"fork error:\00"
@.str.2 = [20 x i8] c"%d child ,pid = %u\0A\00"
@.str.3 = [8 x i8] c"parent\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
store i32 0, i32* %1
%4 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %2
br label %5
5: ; preds = %21, %0
%6 = load i32, i32* %2
%7 = icmp slt i32 %6, 5
br i1 %7, label %8, label %24
8: ; preds = %5
%9 = call i32 (...) @fork()
store i32 %9, i32* %3
%10 = load i32, i32* %3
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %15
12: ; preds = %8
%13 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%14 = call i32 @exit(i32 1)
unreachable
15: ; preds = %8
%16 = load i32, i32* %3
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %19
18: ; preds = %15
br label %24
19: ; preds = %15
br label %20
20: ; preds = %19
br label %21
21: ; preds = %20
%22 = load i32, i32* %2
%23 = add nsw i32 %22, 1
store i32 %23, i32* %2
br label %5
24: ; preds = %18, %5
%25 = load i32, i32* %2
%26 = icmp slt i32 %25, 5
br i1 %26, label %27, label %34
27: ; preds = %24
%28 = load i32, i32* %2
%29 = call i32 @sleep(i32 %28)
%30 = load i32, i32* %2
%31 = add nsw i32 %30, 1
%32 = call i32 (...) @getpid()
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i32 %31, i32 %32)
br label %38
34: ; preds = %24
%35 = load i32, i32* %2
%36 = call i32 @sleep(i32 %35)
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
br label %38
38: ; preds = %34, %27
ret i32 0
}

LLVM IR (-O2):
@.str.1 = [12 x i8] c"fork error:\00"
@.str.2 = [20 x i8] c"%d child ,pid = %u\0A\00"
@str = [31 x i8] c"begin.........................\00"
@str.4 = [7 x i8] c"parent\00"
define i32 @main() {
%1 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str, i64 0, i64 0))
%2 = tail call i32 (...) @fork()
switch i32 %2, label %6 [
i32 -1, label %3
i32 0, label %17
]
3: ; preds = %12, %10, %8, %6, %0
%4 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%5 = tail call i32 @exit(i32 1)
unreachable
6: ; preds = %0
%7 = tail call i32 (...) @fork()
switch i32 %7, label %8 [
i32 -1, label %3
i32 0, label %17
]
8: ; preds = %6
%9 = tail call i32 (...) @fork()
switch i32 %9, label %10 [
i32 -1, label %3
i32 0, label %17
]
10: ; preds = %8
%11 = tail call i32 (...) @fork()
switch i32 %11, label %12 [
i32 -1, label %3
i32 0, label %17
]
12: ; preds = %10
%13 = tail call i32 (...) @fork()
switch i32 %13, label %14 [
i32 -1, label %3
i32 0, label %17
]
14: ; preds = %12
%15 = tail call i32 @sleep(i32 5)
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @str.4, i64 0, i64 0))
br label %23
17: ; preds = %12, %10, %8, %6, %0
%18 = phi i32 [ %2, %0 ], [ 1, %6 ], [ 2, %8 ], [ 3, %10 ], [ 4, %12 ]
%19 = tail call i32 @sleep(i32 %18)
%20 = add nuw nsw i32 %18, 1
%21 = tail call i32 (...) @getpid()
%22 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i32 %20, i32 %21)
br label %23
23: ; preds = %14, %17
ret i32 0
}