#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*/ errno;
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int getpid();
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int wait(int *);
int createProcess(int n) {
  if (n > 0) {
    int bIsOK = 1;
    pid_t pid = -1;
    while (bIsOK == 1) {
      if (n > 0) {
        pid = fork();
        if (pid > 0) {
          bIsOK = 1;
          n--;
        } else if (pid == 0) {
          bIsOK = 0;
          fprintf(stderr, "create process %d %d ok.\n", getpid(), n);
        } else {
          fprintf(stderr, "create process error.\n");
          return -1;
        }
      } else {
        int status;
        if (wait(&status) == -1) {
          fprintf(stderr, "wait error %s\n", strerror(errno));
          n++;
        }
      }
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<createProcess>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jle    1278 <createProcess+0x128>
movl   $0x1,-0xc(%rbp)
movq   $0xffffffffffffffff,-0x18(%rbp)
ff
cmpl   $0x1,-0xc(%rbp)
jne    1273 <createProcess+0x123>
cmpl   $0x0,-0x8(%rbp)
jle    1220 <createProcess+0xd0>
mov    $0x0,%al
call   1070 <fork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jle    11b3 <createProcess+0x63>
movl   $0x1,-0xc(%rbp)
mov    -0x8(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x8(%rbp)
jmp    121b <createProcess+0xcb>
cmpq   $0x0,-0x18(%rbp)
jne    11f3 <createProcess+0xa3>
movl   $0x0,-0xc(%rbp)
mov    0x2e14(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0x20(%rbp),%edi
mov    %eax,%edx
mov    -0x8(%rbp),%ecx
lea    0xe19(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
jmp    1216 <createProcess+0xc6>
mov    0x2de6(%rip),%rax
mov    (%rax),%edi
lea    0xe17(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    127f <createProcess+0x12f>
jmp    121b <createProcess+0xcb>
jmp    126e <createProcess+0x11e>
lea    -0x1c(%rbp),%rdi
call   1060 <wait@plt>
cmp    $0xffffffff,%eax
jne    1269 <createProcess+0x119>
mov    0x2da7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2d93(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
mov    -0x24(%rbp),%edi
mov    %rax,%rdx
lea    0xdd8(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    126e <createProcess+0x11e>
jmp    1174 <createProcess+0x24>
jmp    1278 <createProcess+0x128>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<createProcess>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
test   %edi,%edi
jle    11f3 <createProcess+0xa3>
mov    %edi,%ebp
lea    0xc(%rsp),%rbx
mov    0x2e6e(%rip),%r15
mov    0x2e5f(%rip),%r12
lea    0xeb1(%rip),%r14
jmp    119f <createProcess+0x4f>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %rbx,%rdi
call   1060 <wait@plt>
xor    %ebp,%ebp
cmp    $0xffffffff,%eax
je     11b4 <createProcess+0x64>
test   %ebp,%ebp
jle    1190 <createProcess+0x40>
xor    %eax,%eax
call   1070 <fork@plt>
test   %rax,%rax
jle    11d6 <createProcess+0x86>
add    $0xffffffff,%ebp
jmp    119f <createProcess+0x4f>
mov    (%r15),%ebp
mov    (%r12),%edi
call   1050 <strerror@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    $0x1,%ebp
jmp    119f <createProcess+0x4f>
mov    (%r15),%r14d
je     11f7 <createProcess+0xa7>
lea    0xe38(%rip),%rsi
mov    %r14d,%edi
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    $0xffffffff,%ebx
jmp    1215 <createProcess+0xc5>
xor    %ebx,%ebx
jmp    1215 <createProcess+0xc5>
xor    %ebx,%ebx
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xdf9(%rip),%rsi
mov    %r14d,%edi
mov    %eax,%edx
mov    %ebp,%ecx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [26 x i8] c"create process %d %d ok.\0A\00"
@.str.1 = [23 x i8] c"create process error.\0A\00"
@.str.2 = [15 x i8] c"wait error %s\0A\00"
@errno = global i32 0
define i32 @createProcess(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i64
%6 = alloca i32
store i32 %0, i32* %3
%7 = load i32, i32* %3
%8 = icmp sgt i32 %7, 0
br i1 %8, label %9, label %49
9: ; preds = %1
store i32 1, i32* %4
store i64 -1, i64* %5
br label %10
10: ; preds = %47, %9
%11 = load i32, i32* %4
%12 = icmp eq i32 %11, 1
br i1 %12, label %13, label %48
13: ; preds = %10
%14 = load i32, i32* %3
%15 = icmp sgt i32 %14, 0
br i1 %15, label %16, label %36
16: ; preds = %13
%17 = call i64 (...) @fork()
store i64 %17, i64* %5
%18 = load i64, i64* %5
%19 = icmp sgt i64 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %16
store i32 1, i32* %4
%21 = load i32, i32* %3
%22 = add nsw i32 %21, -1
store i32 %22, i32* %3
br label %35
23: ; preds = %16
%24 = load i64, i64* %5
%25 = icmp eq i64 %24, 0
br i1 %25, label %26, label %31
26: ; preds = %23
store i32 0, i32* %4
%27 = load i32, i32* @stderr
%28 = call i32 (...) @getpid()
%29 = load i32, i32* %3
%30 = call i32 (i32, i8*, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i32 %28, i32 %29)
br label %34
31: ; preds = %23
%32 = load i32, i32* @stderr
%33 = call i32 (i32, i8*, ...) @fprintf(i32 %32, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %2
br label %50
34: ; preds = %26
br label %35
35: ; preds = %34, %20
br label %47
36: ; preds = %13
%37 = call i32 @wait(i32* %6)
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %46
39: ; preds = %36
%40 = load i32, i32* @stderr
%41 = load i32, i32* @errno
%42 = call i8* @strerror(i32 %41)
%43 = call i32 (i32, i8*, ...) @fprintf(i32 %40, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i8* %42)
%44 = load i32, i32* %3
%45 = add nsw i32 %44, 1
store i32 %45, i32* %3
br label %46
46: ; preds = %39, %36
br label %47
47: ; preds = %46, %35
br label %10
48: ; preds = %10
br label %49
49: ; preds = %48, %1
store i32 0, i32* %2
br label %50
50: ; preds = %49, %31
%51 = load i32, i32* %2
ret i32 %51
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [26 x i8] c"create process %d %d ok.\0A\00"
@.str.1 = [23 x i8] c"create process error.\0A\00"
@.str.2 = [15 x i8] c"wait error %s\0A\00"
@errno = global i32 0
define i32 @createProcess(i32 %0) {
%2 = alloca i32
%3 = icmp sgt i32 %0, 0
br i1 %3, label %4, label %34
4: ; preds = %1
%5 = bitcast i32* %2 to i8*
br label %6
6: ; preds = %14, %4
%7 = phi i32 [ %0, %4 ], [ %15, %14 ]
%8 = icmp sgt i32 %7, 0
br i1 %8, label %9, label %19
9: ; preds = %6
%10 = call i64 (...) @fork()
%11 = icmp sgt i64 %10, 0
br i1 %11, label %12, label %16
12: ; preds = %9
%13 = add nsw i32 %7, -1
br label %14
14: ; preds = %12, %27
%15 = phi i32 [ %13, %12 ], [ %28, %27 ]
br label %6
16: ; preds = %9
%17 = icmp eq i64 %10, 0
%18 = load i32, i32* @stderr
br i1 %17, label %31, label %29
19: ; preds = %6
%20 = call i32 @wait(i32* %2)
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %27
22: ; preds = %19
%23 = load i32, i32* @stderr
%24 = load i32, i32* @errno
%25 = call i8* @strerror(i32 %24)
%26 = call i32 (i32, i8*, ...) @fprintf(i32 %23, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i8* %25)
br label %27
27: ; preds = %22, %19
%28 = phi i32 [ 1, %22 ], [ 0, %19 ]
br label %14
29: ; preds = %16
%30 = call i32 (i32, i8*, ...) @fprintf(i32 %18, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
br label %34
31: ; preds = %16
%32 = call i32 (...) @getpid()
%33 = call i32 (i32, i8*, ...) @fprintf(i32 %18, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i32 %32, i32 %7)
br label %34
34: ; preds = %31, %1, %29
%35 = phi i32 [ -1, %29 ], [ 0, %1 ], [ 0, %31 ]
ret i32 %35
}