#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 */

/* Variables and functions */
int WEXITSTATUS(int);
int /*<<< orphan*/ exit(int);
int fork();
int getpid();
int getppid();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int, int, ...);
int /*<<< orphan*/ sleep(int);
int wait(int *);
void create_for_process(int process_count) {
  int i, pid, status;
  for (i = 0; i < process_count; i++) {
    switch (pid = fork()) {
    case -1:
      perror("fork error");
      exit(1);
    case 0:
      printf("^start process [id=%d, pid = %d, ppid = %d]\n", i + 1, getpid(),
             getppid());
      sleep(1);
      exit(i + 1);
    }
  }
  for (i = 0; i < process_count; i++) {
    pid = wait(&status);
    printf("$close process [pid = %d, status = %d]\n", pid,
           WEXITSTATUS(status));
  }
}


x86 assembly (clang, -O0):
<create_for_process>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%eax
cmp    -0x4(%rbp),%eax
jge    1249 <create_for_process+0xb9>
xor    %eax,%eax
call   10b0 <fork@plt>
mov    %eax,-0xc(%rbp)
mov    %eax,%ecx
mov    %ecx,-0x14(%rbp)
sub    $0xffffffff,%eax
je     11db <create_for_process+0x4b>
jmp    11cb <create_for_process+0x3b>
mov    -0x14(%rbp),%eax
test   %eax,%eax
je     11f1 <create_for_process+0x61>
jmp    1236 <create_for_process+0xa6>
lea    0xe1e(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x18(%rbp)
mov    $0x0,%al
call   1070 <getppid@plt>
mov    -0x1c(%rbp),%esi
mov    -0x18(%rbp),%edx
mov    %eax,%ecx
lea    0xdf1(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   1090 <sleep@plt>
mov    -0x8(%rbp),%edi
add    $0x1,%edi
call   1080 <exit@plt>
jmp    123b <create_for_process+0xab>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    11a2 <create_for_process+0x12>
movl   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%eax
cmp    -0x4(%rbp),%eax
jge    1297 <create_for_process+0x107>
lea    -0x10(%rbp),%rdi
call   10a0 <wait@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%edi
call   1050 <WEXITSTATUS@plt>
mov    -0x20(%rbp),%esi
mov    %eax,%edx
lea    0xdb6(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    1250 <create_for_process+0xc0>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_for_process>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
test   %edi,%edi
jle    1216 <create_for_process+0x86>
mov    %edi,%ebp
mov    %edi,%r14d
neg    %r14d
mov    $0x1,%ebx
nopl   0x0(%rax,%rax,1)
xor    %eax,%eax
call   10b0 <fork@plt>
test   %eax,%eax
je     1221 <create_for_process+0x91>
cmp    $0xffffffff,%eax
je     1256 <create_for_process+0xc6>
lea    (%r14,%rbx,1),%eax
add    $0x1,%eax
mov    %ebx,%ecx
add    $0x1,%ecx
mov    %ecx,%ebx
cmp    $0x1,%eax
jne    11b0 <create_for_process+0x20>
test   %ebp,%ebp
jle    1216 <create_for_process+0x86>
lea    0x4(%rsp),%r14
lea    0xe51(%rip),%r15
nopw   0x0(%rax,%rax,1)
mov    %r14,%rdi
call   10a0 <wait@plt>
mov    %eax,%ebx
mov    0x4(%rsp),%edi
call   1050 <WEXITSTATUS@plt>
mov    %r15,%rdi
mov    %ebx,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <printf@plt>
add    $0xffffffff,%ebp
jne    11f0 <create_for_process+0x60>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   1070 <getppid@plt>
lea    0xdd3(%rip),%rdi
mov    %ebx,%esi
mov    %ebp,%edx
mov    %eax,%ecx
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x1,%edi
call   1090 <sleep@plt>
mov    %ebx,%edi
call   1080 <exit@plt>
lea    0xda3(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [11 x i8] c"fork error\00"
@.str.1 = [45 x i8] c"^start process [id=%d, pid = %d, ppid = %d]\0A\00"
@.str.2 = [40 x i8] c"$close process [pid = %d, status = %d]\0A\00"
define void @create_for_process(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 %0, i32* %2
store i32 0, i32* %3
br label %6
6: ; preds = %26, %1
%7 = load i32, i32* %3
%8 = load i32, i32* %2
%9 = icmp slt i32 %7, %8
br i1 %9, label %10, label %29
10: ; preds = %6
%11 = call i32 (...) @fork()
store i32 %11, i32* %4
switch i32 %11, label %25 [
i32 -1, label %12
i32 0, label %15
]
12: ; preds = %10
%13 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%14 = call i32 @exit(i32 1)
unreachable
15: ; preds = %10
%16 = load i32, i32* %3
%17 = add nsw i32 %16, 1
%18 = call i32 (...) @getpid()
%19 = call i32 (...) @getppid()
%20 = call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0), i32 %17, i32 %18, i32 %19)
%21 = call i32 @sleep(i32 1)
%22 = load i32, i32* %3
%23 = add nsw i32 %22, 1
%24 = call i32 @exit(i32 %23)
unreachable
25: ; preds = %10
br label %26
26: ; preds = %25
%27 = load i32, i32* %3
%28 = add nsw i32 %27, 1
store i32 %28, i32* %3
br label %6
29: ; preds = %6
store i32 0, i32* %3
br label %30
30: ; preds = %40, %29
%31 = load i32, i32* %3
%32 = load i32, i32* %2
%33 = icmp slt i32 %31, %32
br i1 %33, label %34, label %43
34: ; preds = %30
%35 = call i32 @wait(i32* %5)
store i32 %35, i32* %4
%36 = load i32, i32* %4
%37 = load i32, i32* %5
%38 = call i32 @WEXITSTATUS(i32 %37)
%39 = call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0), i32 %36, i32 %38)
br label %40
40: ; preds = %34
%41 = load i32, i32* %3
%42 = add nsw i32 %41, 1
store i32 %42, i32* %3
br label %30
43: ; preds = %30
ret void
}

LLVM IR (-O2):
@.str = [11 x i8] c"fork error\00"
@.str.1 = [45 x i8] c"^start process [id=%d, pid = %d, ppid = %d]\0A\00"
@.str.2 = [40 x i8] c"$close process [pid = %d, status = %d]\0A\00"
define void @create_for_process(i32 %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
%4 = icmp sgt i32 %0, 0
br i1 %4, label %6, label %30
5: ; preds = %19
br i1 %4, label %22, label %30
6: ; preds = %1, %19
%7 = phi i32 [ %20, %19 ], [ 0, %1 ]
%8 = tail call i32 (...) @fork()
switch i32 %8, label %19 [
i32 -1, label %9
i32 0, label %12
]
9: ; preds = %6
%10 = tail call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%11 = tail call i32 @exit(i32 1)
unreachable
12: ; preds = %6
%13 = add nuw nsw i32 %7, 1
%14 = tail call i32 (...) @getpid()
%15 = tail call i32 (...) @getppid()
%16 = tail call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0), i32 %13, i32 %14, i32 %15)
%17 = tail call i32 @sleep(i32 1)
%18 = tail call i32 @exit(i32 %13)
unreachable
19: ; preds = %6
%20 = add nuw nsw i32 %7, 1
%21 = icmp eq i32 %20, %0
br i1 %21, label %5, label %6
22: ; preds = %5, %22
%23 = phi i32 [ %28, %22 ], [ 0, %5 ]
%24 = call i32 @wait(i32* %2)
%25 = load i32, i32* %2
%26 = call i32 @WEXITSTATUS(i32 %25)
%27 = call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0), i32 %24, i32 %26)
%28 = add nuw nsw i32 %23, 1
%29 = icmp eq i32 %28, %0
br i1 %29, label %30, label %22
30: ; preds = %22, %1, %5
ret void
}