#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 /*<<< orphan*/ exit(int);
int fork();
int partition(int *, int, int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ waitpid(int, int *, int /*<<< orphan*/);
void quicksort(int array[], int left, int right) {
  int pivot_index = left;
  int pivot_new_index;
  int lchild = -1;
  int rchild = -1;
  if (right > left) {
    int status;
    pivot_new_index = partition(array, left, right, pivot_index);
    lchild = fork();
    if (lchild < 0) {
      perror("fork");
      exit(1);
    }
    if (lchild == 0) {
      quicksort(array, left, pivot_new_index - 1);
      exit(0);
    } else {
      rchild = fork();
      if (rchild < 0) {
        perror("fork");
        exit(1);
      }
      if (rchild == 0) {
        quicksort(array, pivot_new_index + 1, right);
        exit(0);
      }
    }
    waitpid(lchild, &status, 0);
    waitpid(rchild, &status, 0);
  }
}


x86 assembly (clang, -O0):
<quicksort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x14(%rbp)
movl   $0xffffffff,-0x1c(%rbp)
movl   $0xffffffff,-0x20(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0xc(%rbp),%eax
jle    1262 <quicksort+0x102>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%edx
mov    -0x14(%rbp),%ecx
call   1030 <partition@plt>
mov    %eax,-0x18(%rbp)
mov    $0x0,%al
call   1080 <fork@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11d1 <quicksort+0x71>
lea    0xe3e(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
cmpl   $0x0,-0x1c(%rbp)
jne    11f4 <quicksort+0x94>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x18(%rbp),%edx
sub    $0x1,%edx
call   1040 <quicksort@plt>
xor    %edi,%edi
call   1070 <exit@plt>
mov    $0x0,%al
call   1080 <fork@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    121e <quicksort+0xbe>
lea    0xdf1(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
cmpl   $0x0,-0x20(%rbp)
jne    1241 <quicksort+0xe1>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%esi
add    $0x1,%esi
mov    -0x10(%rbp),%edx
call   1040 <quicksort@plt>
xor    %edi,%edi
call   1070 <exit@plt>
jmp    1246 <quicksort+0xe6>
mov    -0x1c(%rbp),%edi
lea    -0x24(%rbp),%rsi
xor    %edx,%edx
call   1050 <waitpid@plt>
mov    -0x20(%rbp),%edi
lea    -0x24(%rbp),%rsi
xor    %edx,%edx
call   1050 <waitpid@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<quicksort>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
cmp    %esi,%edx
jle    11be <quicksort+0x5e>
mov    %edx,%ebp
mov    %esi,%ebx
mov    %rdi,%r15
mov    %esi,%ecx
call   1030 <partition@plt>
mov    %eax,%r14d
xor    %eax,%eax
call   1080 <fork@plt>
test   %eax,%eax
js     11cb <quicksort+0x6b>
je     11e1 <quicksort+0x81>
mov    %eax,%r12d
xor    %eax,%eax
call   1080 <fork@plt>
test   %eax,%eax
js     11cb <quicksort+0x6b>
je     11ef <quicksort+0x8f>
mov    %eax,%ebx
lea    0xc(%rsp),%rbp
mov    %r12d,%edi
mov    %rbp,%rsi
xor    %edx,%edx
call   1050 <waitpid@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
xor    %edx,%edx
call   1050 <waitpid@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe2e(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
add    $0xffffffff,%r14d
mov    %r15,%rdi
mov    %ebx,%esi
mov    %r14d,%edx
jmp    11fb <quicksort+0x9b>
add    $0x1,%r14d
mov    %r15,%rdi
mov    %r14d,%esi
mov    %ebp,%edx
call   1040 <quicksort@plt>
xor    %edi,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [5 x i8] c"fork\00"
define void @quicksort(i32* %0, i32 %1, i32 %2) {
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i32* %0, i32** %4
store i32 %1, i32* %5
store i32 %2, i32* %6
%12 = load i32, i32* %5
store i32 %12, i32* %7
store i32 -1, i32* %9
store i32 -1, i32* %10
%13 = load i32, i32* %6
%14 = load i32, i32* %5
%15 = icmp sgt i32 %13, %14
br i1 %15, label %16, label %59
16: ; preds = %3
%17 = load i32*, i32** %4
%18 = load i32, i32* %5
%19 = load i32, i32* %6
%20 = load i32, i32* %7
%21 = call i32 @partition(i32* %17, i32 %18, i32 %19, i32 %20)
store i32 %21, i32* %8
%22 = call i32 (...) @fork()
store i32 %22, i32* %9
%23 = load i32, i32* %9
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %16
%26 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%27 = call i32 @exit(i32 1)
unreachable
28: ; preds = %16
%29 = load i32, i32* %9
%30 = icmp eq i32 %29, 0
br i1 %30, label %31, label %37
31: ; preds = %28
%32 = load i32*, i32** %4
%33 = load i32, i32* %5
%34 = load i32, i32* %8
%35 = sub nsw i32 %34, 1
%36 = call i32 @exit(i32 0)
unreachable
37: ; preds = %28
%38 = call i32 (...) @fork()
store i32 %38, i32* %10
%39 = load i32, i32* %10
%40 = icmp slt i32 %39, 0
br i1 %40, label %41, label %44
41: ; preds = %37
%42 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%43 = call i32 @exit(i32 1)
unreachable
44: ; preds = %37
%45 = load i32, i32* %10
%46 = icmp eq i32 %45, 0
br i1 %46, label %47, label %53
47: ; preds = %44
%48 = load i32*, i32** %4
%49 = load i32, i32* %8
%50 = add nsw i32 %49, 1
%51 = load i32, i32* %6
%52 = call i32 @exit(i32 0)
unreachable
53: ; preds = %44
br label %54
54: ; preds = %53
%55 = load i32, i32* %9
%56 = call i32 @waitpid(i32 %55, i32* %11, i32 0)
%57 = load i32, i32* %10
%58 = call i32 @waitpid(i32 %57, i32* %11, i32 0)
br label %59
59: ; preds = %54, %3
ret void
}

LLVM IR (-O2):
@.str = [5 x i8] c"fork\00"
define void @quicksort(i32* %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = icmp sgt i32 %2, %1
br i1 %5, label %6, label %33
6: ; preds = %3
%7 = bitcast i32* %4 to i8*
%8 = tail call i32 @partition(i32* %0, i32 %1, i32 %2, i32 %1)
%9 = tail call i32 (...) @fork()
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %6
%12 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%13 = tail call i32 @exit(i32 1)
unreachable
14: ; preds = %6
%15 = icmp eq i32 %9, 0
br i1 %15, label %16, label %19
16: ; preds = %14
%17 = add nsw i32 %8, -1
tail call void @quicksort(i32* %0, i32 %1, i32 %17)
%18 = tail call i32 @exit(i32 0)
unreachable
19: ; preds = %14
%20 = tail call i32 (...) @fork()
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %19
%23 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%24 = tail call i32 @exit(i32 1)
unreachable
25: ; preds = %19
%26 = icmp eq i32 %20, 0
br i1 %26, label %27, label %30
27: ; preds = %25
%28 = add nsw i32 %8, 1
tail call void @quicksort(i32* %0, i32 %28, i32 %2)
%29 = tail call i32 @exit(i32 0)
unreachable
30: ; preds = %25
%31 = call i32 @waitpid(i32 %9, i32* %4, i32 0)
%32 = call i32 @waitpid(i32 %20, i32* %4, i32 0)
br label %33
33: ; preds = %30, %3
ret void
}