#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 /*<<< orphan*/ merge(int *, int, int *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ waitpid(int, int *, int /*<<< orphan*/);
void mergesort(int array[], int length) {
  int middle;
  int *left, *right;
  int llength;
  int lchild = -1;
  int rchild = -1;
  int status;
  if (length <= 1)
    return;
  middle = length / 2;
  llength = length - middle;
  left = array;
  right = array + llength;
  lchild = fork();
  if (lchild < 0) {
    perror("fork");
    exit(1);
  }
  if (lchild == 0) {
    mergesort(left, llength);
    exit(0);
  } else {
    rchild = fork();
    if (rchild < 0) {
      perror("fork");
      exit(1);
    }
    if (rchild == 0) {
      mergesort(right, middle);
      exit(0);
    }
  }
  waitpid(lchild, &status, 0);
  waitpid(rchild, &status, 0);
  merge(left, llength, right, middle);
}


x86 assembly (clang, -O0):
<mergesort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0xffffffff,-0x28(%rbp)
movl   $0xffffffff,-0x2c(%rbp)
cmpl   $0x1,-0xc(%rbp)
jg     118c <mergesort+0x2c>
jmp    1280 <mergesort+0x120>
mov    -0xc(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x10(%rbp)
mov    -0xc(%rbp),%eax
sub    -0x10(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rax
movslq -0x24(%rbp),%rcx
shl    $0x2,%rcx
add    %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    $0x0,%al
call   1080 <fork@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jge    11e8 <mergesort+0x88>
lea    0xe27(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
cmpl   $0x0,-0x28(%rbp)
jne    1205 <mergesort+0xa5>
mov    -0x18(%rbp),%rdi
mov    -0x24(%rbp),%esi
call   1040 <mergesort@plt>
xor    %edi,%edi
call   1070 <exit@plt>
mov    $0x0,%al
call   1080 <fork@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    122f <mergesort+0xcf>
lea    0xde0(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
cmpl   $0x0,-0x2c(%rbp)
jne    124c <mergesort+0xec>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%esi
call   1040 <mergesort@plt>
xor    %edi,%edi
call   1070 <exit@plt>
jmp    1251 <mergesort+0xf1>
mov    -0x28(%rbp),%edi
lea    -0x30(%rbp),%rsi
xor    %edx,%edx
call   1050 <waitpid@plt>
mov    -0x2c(%rbp),%edi
lea    -0x30(%rbp),%rsi
xor    %edx,%edx
call   1050 <waitpid@plt>
mov    -0x18(%rbp),%rdi
mov    -0x24(%rbp),%esi
mov    -0x20(%rbp),%rdx
mov    -0x10(%rbp),%ecx
call   1030 <merge@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mergesort>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
cmp    $0x2,%esi
jl     11dc <mergesort+0x7c>
mov    %esi,%ebx
mov    %rdi,%r14
mov    %esi,%r13d
shr    %r13d
sub    %r13d,%ebx
xor    %eax,%eax
call   1080 <fork@plt>
test   %eax,%eax
js     11eb <mergesort+0x8b>
je     1201 <mergesort+0xa1>
mov    %eax,%r15d
xor    %eax,%eax
call   1080 <fork@plt>
test   %eax,%eax
js     11eb <mergesort+0x8b>
mov    %eax,%ebp
movslq %ebx,%rax
lea    (%r14,%rax,4),%rax
mov    %rax,0x8(%rsp)
je     1208 <mergesort+0xa8>
lea    0x14(%rsp),%r12
mov    %r15d,%edi
mov    %r12,%rsi
xor    %edx,%edx
call   1050 <waitpid@plt>
mov    %ebp,%edi
mov    %r12,%rsi
xor    %edx,%edx
call   1050 <waitpid@plt>
mov    %r14,%rdi
mov    %ebx,%esi
mov    0x8(%rsp),%rdx
mov    %r13d,%ecx
call   1030 <merge@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe0e(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    %r14,%rdi
mov    %ebx,%esi
jmp    1210 <mergesort+0xb0>
mov    0x8(%rsp),%rdi
mov    %r13d,%esi
call   1040 <mergesort@plt>
xor    %edi,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [5 x i8] c"fork\00"
define void @mergesort(i32* %0, i32 %1) {
%3 = alloca i32*
%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** %3
store i32 %1, i32* %4
store i32 -1, i32* %9
store i32 -1, i32* %10
%12 = load i32, i32* %4
%13 = icmp sle i32 %12, 1
br i1 %13, label %14, label %15
14: ; preds = %2
br label %64
15: ; preds = %2
%16 = load i32, i32* %4
%17 = sdiv i32 %16, 2
store i32 %17, i32* %5
%18 = load i32, i32* %4
%19 = load i32, i32* %5
%20 = sub nsw i32 %18, %19
store i32 %20, i32* %8
%21 = load i32*, i32** %3
store i32* %21, i32** %6
%22 = load i32*, i32** %3
%23 = load i32, i32* %8
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds i32, i32* %22, i64 %24
store i32* %25, i32** %7
%26 = call i32 (...) @fork()
store i32 %26, i32* %9
%27 = load i32, i32* %9
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %15
%30 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%31 = call i32 @exit(i32 1)
unreachable
32: ; preds = %15
%33 = load i32, i32* %9
%34 = icmp eq i32 %33, 0
br i1 %34, label %35, label %39
35: ; preds = %32
%36 = load i32*, i32** %6
%37 = load i32, i32* %8
%38 = call i32 @exit(i32 0)
unreachable
39: ; preds = %32
%40 = call i32 (...) @fork()
store i32 %40, i32* %10
%41 = load i32, i32* %10
%42 = icmp slt i32 %41, 0
br i1 %42, label %43, label %46
43: ; preds = %39
%44 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%45 = call i32 @exit(i32 1)
unreachable
46: ; preds = %39
%47 = load i32, i32* %10
%48 = icmp eq i32 %47, 0
br i1 %48, label %49, label %53
49: ; preds = %46
%50 = load i32*, i32** %7
%51 = load i32, i32* %5
%52 = call i32 @exit(i32 0)
unreachable
53: ; preds = %46
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)
%59 = load i32*, i32** %6
%60 = load i32, i32* %8
%61 = load i32*, i32** %7
%62 = load i32, i32* %5
%63 = call i32 @merge(i32* %59, i32 %60, i32* %61, i32 %62)
br label %64
64: ; preds = %54, %14
ret void
}

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