#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_FAILURE;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
int getpid();
int /*<<< orphan*/ handle_termination();
int /*<<< orphan*/ perror(char *);
size_t split_process(size_t times, pid_t *pids) {
  if (pids != NULL) {
    pids[0] = getpid();
  }
  for (size_t i = 0; i < times - 1; i++) {
    pid_t child = fork();
    switch (child) {
    case -1: {
      perror("Failed to fork");
      exit(EXIT_FAILURE);
    }
    case 0: {
      handle_termination();
      return i + 1;
    }
    default:
      if (pids != NULL) {
        pids[i + 1] = child;
      }
      break;
    }
  }
  if (times > 1) {
    handle_termination();
  }
  return 0;
}


x86 assembly (clang, -O0):
<split_process>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     117a <split_process+0x2a>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
movq   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    $0x1,%rcx
cmp    %rcx,%rax
jae    122c <split_process+0xdc>
xor    %eax,%eax
call   1070 <fork@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x28(%rbp)
sub    $0xffffffff,%eax
je     11c5 <split_process+0x75>
jmp    11b5 <split_process+0x65>
mov    -0x28(%rbp),%eax
test   %eax,%eax
je     11df <split_process+0x8f>
jmp    11f7 <split_process+0xa7>
lea    0xe34(%rip),%rdi
call   1040 <perror@plt>
mov    0x2e08(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>
mov    $0x0,%al
call   1060 <handle_termination@plt>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
jmp    1246 <split_process+0xf6>
cmpq   $0x0,-0x18(%rbp)
je     1211 <split_process+0xc1>
mov    -0x24(%rbp),%edx
mov    -0x18(%rbp),%rax
mov    -0x20(%rbp),%rcx
mov    %edx,0x4(%rax,%rcx,4)
jmp    1216 <split_process+0xc6>
jmp    121b <split_process+0xcb>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    1182 <split_process+0x32>
cmpq   $0x1,-0x10(%rbp)
jbe    123e <split_process+0xee>
mov    $0x0,%al
call   1060 <handle_termination@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<split_process>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r15
mov    %rdi,%r14
test   %rsi,%rsi
je     116a <split_process+0x1a>
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %eax,(%r15)
cmp    $0x1,%r14
je     11a4 <split_process+0x54>
mov    $0x1,%ebx
jmp    1189 <split_process+0x39>
nopw   0x0(%rax,%rax,1)
add    $0x1,%rbx
cmp    %rbx,%r14
je     11a8 <split_process+0x58>
xor    %eax,%eax
call   1070 <fork@plt>
test   %eax,%eax
je     11b0 <split_process+0x60>
cmp    $0xffffffff,%eax
je     11c0 <split_process+0x70>
test   %r15,%r15
je     1180 <split_process+0x30>
mov    %eax,(%r15,%rbx,4)
jmp    1180 <split_process+0x30>
xor    %ebx,%ebx
jmp    11b7 <split_process+0x67>
xor    %ebx,%ebx
cmp    $0x2,%r14
jb     11b7 <split_process+0x67>
xor    %eax,%eax
call   1060 <handle_termination@plt>
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xe39(%rip),%rdi
call   1040 <perror@plt>
mov    0x2e0d(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@.str = [15 x i8] c"Failed to fork\00"
@EXIT_FAILURE = global i32 0
define i64 @split_process(i64 %0, i32* %1) {
%3 = alloca i64
%4 = alloca i64
%5 = alloca i32*
%6 = alloca i64
%7 = alloca i32
store i64 %0, i64* %4
store i32* %1, i32** %5
%8 = load i32*, i32** %5
%9 = icmp ne i32* %8, null
br i1 %9, label %10, label %14
10: ; preds = %2
%11 = call i32 (...) @getpid()
%12 = load i32*, i32** %5
%13 = getelementptr inbounds i32, i32* %12, i64 0
store i32 %11, i32* %13
br label %14
14: ; preds = %10, %2
store i64 0, i64* %6
br label %15
15: ; preds = %42, %14
%16 = load i64, i64* %6
%17 = load i64, i64* %4
%18 = sub i64 %17, 1
%19 = icmp ult i64 %16, %18
br i1 %19, label %20, label %45
20: ; preds = %15
%21 = call i32 (...) @fork()
store i32 %21, i32* %7
%22 = load i32, i32* %7
switch i32 %22, label %31 [
i32 -1, label %23
i32 0, label %27
]
23: ; preds = %20
%24 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%25 = load i32, i32* @EXIT_FAILURE
%26 = call i32 @exit(i32 %25)
unreachable
27: ; preds = %20
%28 = call i32 (...) @handle_termination()
%29 = load i64, i64* %6
%30 = add i64 %29, 1
store i64 %30, i64* %3
br label %51
31: ; preds = %20
%32 = load i32*, i32** %5
%33 = icmp ne i32* %32, null
br i1 %33, label %34, label %40
34: ; preds = %31
%35 = load i32, i32* %7
%36 = load i32*, i32** %5
%37 = load i64, i64* %6
%38 = add i64 %37, 1
%39 = getelementptr inbounds i32, i32* %36, i64 %38
store i32 %35, i32* %39
br label %40
40: ; preds = %34, %31
br label %41
41: ; preds = %40
br label %42
42: ; preds = %41
%43 = load i64, i64* %6
%44 = add i64 %43, 1
store i64 %44, i64* %6
br label %15
45: ; preds = %15
%46 = load i64, i64* %4
%47 = icmp ugt i64 %46, 1
br i1 %47, label %48, label %50
48: ; preds = %45
%49 = call i32 (...) @handle_termination()
br label %50
50: ; preds = %48, %45
store i64 0, i64* %3
br label %51
51: ; preds = %50, %27
%52 = load i64, i64* %3
ret i64 %52
}

LLVM IR (-O2):
@.str = [15 x i8] c"Failed to fork\00"
@EXIT_FAILURE = global i32 0
define i64 @split_process(i64 %0, i32* writeonly %1) {
%3 = icmp eq i32* %1, null
br i1 %3, label %6, label %4
4: ; preds = %2
%5 = tail call i32 (...) @getpid()
store i32 %5, i32* %1
br label %6
6: ; preds = %4, %2
%7 = add i64 %0, -1
%8 = icmp eq i64 %7, 0
br i1 %8, label %29, label %9
9: ; preds = %6, %20
%10 = phi i64 [ %17, %20 ], [ 0, %6 ]
%11 = tail call i32 (...) @fork()
switch i32 %11, label %16 [
i32 -1, label %12
i32 0, label %22
]
12: ; preds = %9
%13 = tail call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%14 = load i32, i32* @EXIT_FAILURE
%15 = tail call i32 @exit(i32 %14)
unreachable
16: ; preds = %9
%17 = add nuw i64 %10, 1
br i1 %3, label %20, label %18
18: ; preds = %16
%19 = getelementptr inbounds i32, i32* %1, i64 %17
store i32 %11, i32* %19
br label %20
20: ; preds = %16, %18
%21 = icmp eq i64 %17, %7
br i1 %21, label %25, label %9
22: ; preds = %9
%23 = tail call i32 (...) @handle_termination()
%24 = add nuw i64 %10, 1
br label %29
25: ; preds = %20
%26 = icmp ugt i64 %0, 1
br i1 %26, label %27, label %29
27: ; preds = %25
%28 = tail call i32 (...) @handle_termination()
br label %29
29: ; preds = %6, %22, %25, %27
%30 = phi i64 [ %24, %22 ], [ 0, %27 ], [ 0, %25 ], [ 0, %6 ]
ret i64 %30
}