#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_SUCCESS;
int /*<<< orphan*/ ForkedFunc(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ free(scalar_t__ *);
int /*<<< orphan*/ getSemId();
scalar_t__ getpid();
scalar_t__ *malloc(int);
int /*<<< orphan*/ wait(scalar_t__ *);
int main(int argc, char **argv) {
  pid_t *children = malloc(10 * sizeof(pid_t));
  pid_t mainPID = getpid();
  int i;
  getSemId();
  for (i = 0; i < 10; ++i) {
    children[i] = fork();
    if (children[i] == 0) {
      free(children);
      ForkedFunc(i + 1);
      break;
    }
  }
  if (getpid() == mainPID) {
    for (i = 0; i < 10; ++i) {
      wait(&children[i]);
    }
    free(children);
  }
  exit(EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x50,%edi
call   1060 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %rax,-0x20(%rbp)
mov    $0x0,%al
call   1050 <getSemId@plt>
movl   $0x0,-0x24(%rbp)
cmpl   $0xa,-0x24(%rbp)
jge    121c <main+0x9c>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %rax,%rdx
mov    -0x18(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x18(%rbp),%rax
movslq -0x24(%rbp),%rcx
cmpq   $0x0,(%rax,%rcx,8)
jne    1209 <main+0x89>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x24(%rbp),%edi
add    $0x1,%edi
call   1070 <ForkedFunc@plt>
jmp    121c <main+0x9c>
jmp    120e <main+0x8e>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    11bd <main+0x3d>
mov    $0x0,%al
call   1040 <getpid@plt>
cmp    -0x20(%rbp),%rax
jne    1269 <main+0xe9>
movl   $0x0,-0x24(%rbp)
cmpl   $0xa,-0x24(%rbp)
jge    1260 <main+0xe0>
mov    -0x18(%rbp),%rdi
movslq -0x24(%rbp),%rax
shl    $0x3,%rax
add    %rax,%rdi
call   1090 <wait@plt>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1234 <main+0xb4>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    0x2d70(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
mov    $0x50,%edi
call   1060 <malloc@plt>
mov    %rax,%rbx
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %rax,%r14
xor    %eax,%eax
call   1050 <getSemId@plt>
xor    %eax,%eax
call   10a0 <fork@plt>
mov    %rax,(%rbx)
mov    $0x1,%ebp
test   %rax,%rax
je     127f <main+0xff>
xor    %eax,%eax
call   10a0 <fork@plt>
mov    %rax,0x8(%rbx)
mov    $0x2,%ebp
test   %rax,%rax
je     127f <main+0xff>
xor    %eax,%eax
call   10a0 <fork@plt>
mov    %rax,0x10(%rbx)
mov    $0x3,%ebp
test   %rax,%rax
je     127f <main+0xff>
xor    %eax,%eax
call   10a0 <fork@plt>
mov    %rax,0x18(%rbx)
mov    $0x4,%ebp
test   %rax,%rax
je     127f <main+0xff>
xor    %eax,%eax
call   10a0 <fork@plt>
mov    %rax,0x20(%rbx)
mov    $0x5,%ebp
test   %rax,%rax
je     127f <main+0xff>
xor    %eax,%eax
call   10a0 <fork@plt>
mov    %rax,0x28(%rbx)
mov    $0x6,%ebp
test   %rax,%rax
je     127f <main+0xff>
xor    %eax,%eax
call   10a0 <fork@plt>
mov    %rax,0x30(%rbx)
mov    $0x7,%ebp
test   %rax,%rax
je     127f <main+0xff>
xor    %eax,%eax
call   10a0 <fork@plt>
mov    %rax,0x38(%rbx)
mov    $0x8,%ebp
test   %rax,%rax
je     127f <main+0xff>
xor    %eax,%eax
call   10a0 <fork@plt>
mov    %rax,0x40(%rbx)
mov    $0x9,%ebp
test   %rax,%rax
je     127f <main+0xff>
xor    %eax,%eax
call   10a0 <fork@plt>
mov    %rax,0x48(%rbx)
mov    $0xa,%ebp
test   %rax,%rax
jne    128e <main+0x10e>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %ebp,%edi
call   1070 <ForkedFunc@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
cmp    %r14,%rax
jne    12fe <main+0x17e>
mov    %rbx,%rdi
call   1090 <wait@plt>
lea    0x8(%rbx),%rdi
call   1090 <wait@plt>
lea    0x10(%rbx),%rdi
call   1090 <wait@plt>
lea    0x18(%rbx),%rdi
call   1090 <wait@plt>
lea    0x20(%rbx),%rdi
call   1090 <wait@plt>
lea    0x28(%rbx),%rdi
call   1090 <wait@plt>
lea    0x30(%rbx),%rdi
call   1090 <wait@plt>
lea    0x38(%rbx),%rdi
call   1090 <wait@plt>
lea    0x40(%rbx),%rdi
call   1090 <wait@plt>
mov    %rbx,%rdi
add    $0x48,%rdi
call   1090 <wait@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    0x2cdb(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64*
%7 = alloca i64
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = call i64* @malloc(i32 80)
store i64* %9, i64** %6
%10 = call i64 (...) @getpid()
store i64 %10, i64* %7
%11 = call i32 (...) @getSemId()
store i32 0, i32* %8
br label %12
12: ; preds = %34, %2
%13 = load i32, i32* %8
%14 = icmp slt i32 %13, 10
br i1 %14, label %15, label %37
15: ; preds = %12
%16 = call i64 (...) @fork()
%17 = load i64*, i64** %6
%18 = load i32, i32* %8
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds i64, i64* %17, i64 %19
store i64 %16, i64* %20
%21 = load i64*, i64** %6
%22 = load i32, i32* %8
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds i64, i64* %21, i64 %23
%25 = load i64, i64* %24
%26 = icmp eq i64 %25, 0
br i1 %26, label %27, label %33
27: ; preds = %15
%28 = load i64*, i64** %6
%29 = call i32 @free(i64* %28)
%30 = load i32, i32* %8
%31 = add nsw i32 %30, 1
%32 = call i32 @ForkedFunc(i32 %31)
br label %37
33: ; preds = %15
br label %34
34: ; preds = %33
%35 = load i32, i32* %8
%36 = add nsw i32 %35, 1
store i32 %36, i32* %8
br label %12
37: ; preds = %27, %12
%38 = call i64 (...) @getpid()
%39 = load i64, i64* %7
%40 = icmp eq i64 %38, %39
br i1 %40, label %41, label %57
41: ; preds = %37
store i32 0, i32* %8
br label %42
42: ; preds = %51, %41
%43 = load i32, i32* %8
%44 = icmp slt i32 %43, 10
br i1 %44, label %45, label %54
45: ; preds = %42
%46 = load i64*, i64** %6
%47 = load i32, i32* %8
%48 = sext i32 %47 to i64
%49 = getelementptr inbounds i64, i64* %46, i64 %48
%50 = call i32 @wait(i64* %49)
br label %51
51: ; preds = %45
%52 = load i32, i32* %8
%53 = add nsw i32 %52, 1
store i32 %53, i32* %8
br label %42
54: ; preds = %42
%55 = load i64*, i64** %6
%56 = call i32 @free(i64* %55)
br label %57
57: ; preds = %54, %37
%58 = load i32, i32* @EXIT_SUCCESS
%59 = call i32 @exit(i32 %58)
unreachable
}

LLVM IR (-O2):
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i64* @malloc(i32 80)
%4 = tail call i64 (...) @getpid()
%5 = tail call i32 (...) @getSemId()
%6 = tail call i64 (...) @fork()
store i64 %6, i64* %3
%7 = icmp eq i64 %6, 0
br i1 %7, label %8, label %12
8: ; preds = %44, %40, %36, %32, %28, %24, %20, %16, %12, %2
%9 = phi i32 [ 1, %2 ], [ 2, %12 ], [ 3, %16 ], [ 4, %20 ], [ 5, %24 ], [ 6, %28 ], [ 7, %32 ], [ 8, %36 ], [ 9, %40 ], [ 10, %44 ]
%10 = tail call i32 @free(i64* %3)
%11 = tail call i32 @ForkedFunc(i32 %9)
br label %48
12: ; preds = %2
%13 = tail call i64 (...) @fork()
%14 = getelementptr inbounds i64, i64* %3, i64 1
store i64 %13, i64* %14
%15 = icmp eq i64 %13, 0
br i1 %15, label %8, label %16
16: ; preds = %12
%17 = tail call i64 (...) @fork()
%18 = getelementptr inbounds i64, i64* %3, i64 2
store i64 %17, i64* %18
%19 = icmp eq i64 %17, 0
br i1 %19, label %8, label %20
20: ; preds = %16
%21 = tail call i64 (...) @fork()
%22 = getelementptr inbounds i64, i64* %3, i64 3
store i64 %21, i64* %22
%23 = icmp eq i64 %21, 0
br i1 %23, label %8, label %24
24: ; preds = %20
%25 = tail call i64 (...) @fork()
%26 = getelementptr inbounds i64, i64* %3, i64 4
store i64 %25, i64* %26
%27 = icmp eq i64 %25, 0
br i1 %27, label %8, label %28
28: ; preds = %24
%29 = tail call i64 (...) @fork()
%30 = getelementptr inbounds i64, i64* %3, i64 5
store i64 %29, i64* %30
%31 = icmp eq i64 %29, 0
br i1 %31, label %8, label %32
32: ; preds = %28
%33 = tail call i64 (...) @fork()
%34 = getelementptr inbounds i64, i64* %3, i64 6
store i64 %33, i64* %34
%35 = icmp eq i64 %33, 0
br i1 %35, label %8, label %36
36: ; preds = %32
%37 = tail call i64 (...) @fork()
%38 = getelementptr inbounds i64, i64* %3, i64 7
store i64 %37, i64* %38
%39 = icmp eq i64 %37, 0
br i1 %39, label %8, label %40
40: ; preds = %36
%41 = tail call i64 (...) @fork()
%42 = getelementptr inbounds i64, i64* %3, i64 8
store i64 %41, i64* %42
%43 = icmp eq i64 %41, 0
br i1 %43, label %8, label %44
44: ; preds = %40
%45 = tail call i64 (...) @fork()
%46 = getelementptr inbounds i64, i64* %3, i64 9
store i64 %45, i64* %46
%47 = icmp eq i64 %45, 0
br i1 %47, label %8, label %48
48: ; preds = %44, %8
%49 = tail call i64 (...) @getpid()
%50 = icmp eq i64 %49, %4
br i1 %50, label %51, label %72
51: ; preds = %48
%52 = tail call i32 @wait(i64* %3)
%53 = getelementptr inbounds i64, i64* %3, i64 1
%54 = tail call i32 @wait(i64* %53)
%55 = getelementptr inbounds i64, i64* %3, i64 2
%56 = tail call i32 @wait(i64* %55)
%57 = getelementptr inbounds i64, i64* %3, i64 3
%58 = tail call i32 @wait(i64* %57)
%59 = getelementptr inbounds i64, i64* %3, i64 4
%60 = tail call i32 @wait(i64* %59)
%61 = getelementptr inbounds i64, i64* %3, i64 5
%62 = tail call i32 @wait(i64* %61)
%63 = getelementptr inbounds i64, i64* %3, i64 6
%64 = tail call i32 @wait(i64* %63)
%65 = getelementptr inbounds i64, i64* %3, i64 7
%66 = tail call i32 @wait(i64* %65)
%67 = getelementptr inbounds i64, i64* %3, i64 8
%68 = tail call i32 @wait(i64* %67)
%69 = getelementptr inbounds i64, i64* %3, i64 9
%70 = tail call i32 @wait(i64* %69)
%71 = tail call i32 @free(i64* %3)
br label %72
72: ; preds = %51, %48
%73 = load i32, i32* @EXIT_SUCCESS
%74 = tail call i32 @exit(i32 %73)
unreachable
}