#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 */
scalar_t__ calloc(int, int);
int /*<<< orphan*/ free(int *);
int /*<<< orphan*/ memmove(int *, int *, int);
int *three_way_merge(int *, int *, int *, int, int, int);
int *three_way_merge_sort(int *data_set, int size) {
  if (size == 1)
    return data_set;
  if (size == 2) {
    if (data_set[0] > data_set[1]) {
      int temp;
      temp = data_set[0];
      data_set[0] = data_set[1];
      data_set[1] = temp;
    }
    return data_set;
  }
  int size_a = size / 3;
  int size_b = (size - size_a) / 2;
  int size_c = size - (size_a + size_b);
  int *part_a = (int *)calloc(size_a, sizeof(int));
  int *part_b = (int *)calloc(size_b, sizeof(int));
  int *part_c = (int *)calloc(size_c, sizeof(int));
  memmove(part_a, data_set, sizeof(int) * size_a);
  memmove(part_b, data_set + size_a, sizeof(int) * size_b);
  memmove(part_c, data_set + size_a + size_b, sizeof(int) * size_c);
  free(data_set);
  return three_way_merge(three_way_merge_sort(part_a, size_a),
                         three_way_merge_sort(part_b, size_b),
                         three_way_merge_sort(part_c, size_c), size_a, size_b,
                         size_c);
}


x86 assembly (clang, -O0):
<three_way_merge_sort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
cmpl   $0x1,-0x14(%rbp)
jne    1176 <three_way_merge_sort+0x26>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12da <three_way_merge_sort+0x18a>
cmpl   $0x2,-0x14(%rbp)
jne    11c0 <three_way_merge_sort+0x70>
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    -0x10(%rbp),%rcx
cmp    0x4(%rcx),%eax
jle    11b3 <three_way_merge_sort+0x63>
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x4(%rax),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x18(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12da <three_way_merge_sort+0x18a>
mov    -0x14(%rbp),%eax
mov    $0x3,%ecx
cltd
idiv   %ecx
mov    %eax,-0x1c(%rbp)
mov    -0x14(%rbp),%eax
sub    -0x1c(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x20(%rbp)
mov    -0x14(%rbp),%eax
mov    -0x1c(%rbp),%ecx
add    -0x20(%rbp),%ecx
sub    %ecx,%eax
mov    %eax,-0x24(%rbp)
mov    -0x1c(%rbp),%edi
mov    $0x4,%esi
call   1040 <calloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%edi
mov    $0x4,%esi
call   1040 <calloc@plt>
mov    %rax,-0x38(%rbp)
mov    -0x24(%rbp),%edi
mov    $0x4,%esi
call   1040 <calloc@plt>
mov    %rax,-0x40(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rsi
movslq -0x1c(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edx
call   1060 <memmove@plt>
mov    -0x38(%rbp),%rdi
mov    -0x10(%rbp),%rsi
movslq -0x1c(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rsi
movslq -0x20(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edx
call   1060 <memmove@plt>
mov    -0x40(%rbp),%rdi
mov    -0x10(%rbp),%rsi
movslq -0x1c(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rsi
movslq -0x20(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rsi
movslq -0x24(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edx
call   1060 <memmove@plt>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
mov    -0x30(%rbp),%rdi
mov    -0x1c(%rbp),%esi
call   1070 <three_way_merge_sort@plt>
mov    %rax,-0x50(%rbp)
mov    -0x38(%rbp),%rdi
mov    -0x20(%rbp),%esi
call   1070 <three_way_merge_sort@plt>
mov    %rax,-0x48(%rbp)
mov    -0x40(%rbp),%rdi
mov    -0x24(%rbp),%esi
call   1070 <three_way_merge_sort@plt>
mov    -0x50(%rbp),%rdi
mov    -0x48(%rbp),%rsi
mov    %rax,%rdx
mov    -0x1c(%rbp),%ecx
mov    -0x20(%rbp),%r8d
mov    -0x24(%rbp),%r9d
call   1050 <three_way_merge@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<three_way_merge_sort>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%r14
cmp    $0x1,%esi
je     1180 <three_way_merge_sort+0x30>
mov    %esi,%r13d
cmp    $0x2,%esi
jne    1192 <three_way_merge_sort+0x42>
mov    (%r14),%eax
mov    0x4(%r14),%ecx
cmp    %ecx,%eax
jle    1180 <three_way_merge_sort+0x30>
mov    %ecx,(%r14)
mov    %eax,0x4(%r14)
mov    %r14,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
movslq %r13d,%rax
imul   $0x55555556,%rax,%rbx
mov    %rbx,%rax
shr    $0x3f,%rax
shr    $0x20,%rbx
add    %eax,%ebx
mov    %r13d,%eax
sub    %ebx,%eax
mov    %eax,%ebp
shr    $0x1f,%ebp
add    %eax,%ebp
sar    %ebp
sub    %ebx,%r13d
sub    %ebp,%r13d
mov    %ebx,%edi
mov    $0x4,%esi
call   1040 <calloc@plt>
mov    %rax,%r12
mov    %rax,0x10(%rsp)
mov    %ebp,%edi
mov    $0x4,%esi
call   1040 <calloc@plt>
mov    %rax,(%rsp)
mov    %r13d,%edi
mov    $0x4,%esi
call   1040 <calloc@plt>
mov    %rax,0x8(%rsp)
movslq %ebx,%r15
lea    0x0(,%r15,4),%edx
mov    %r12,%rdi
mov    %r14,%rsi
call   1060 <memmove@plt>
lea    (%r14,%r15,4),%rbx
movslq %ebp,%r12
lea    0x0(,%r12,4),%edx
mov    (%rsp),%rbp
mov    %rbp,%rdi
mov    %rbx,%rsi
call   1060 <memmove@plt>
lea    (%rbx,%r12,4),%rsi
lea    0x0(,%r13,4),%edx
mov    0x8(%rsp),%rbx
mov    %rbx,%rdi
call   1060 <memmove@plt>
mov    %r14,%rdi
call   1030 <free@plt>
mov    0x10(%rsp),%rdi
mov    %r15d,%esi
call   1070 <three_way_merge_sort@plt>
mov    %rax,%r14
mov    %rbp,%rdi
mov    %r12d,%esi
call   1070 <three_way_merge_sort@plt>
mov    %rax,%rbp
mov    %rbx,%rdi
mov    %r13d,%esi
call   1070 <three_way_merge_sort@plt>
mov    %r14,%rdi
mov    %rbp,%rsi
mov    %rax,%rdx
mov    %r15d,%ecx
mov    %r12d,%r8d
mov    %r13d,%r9d
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <three_way_merge@plt>

LLVM IR (-O0):
define i32* @three_way_merge_sort(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*
%12 = alloca i32*
store i32* %0, i32** %4
store i32 %1, i32* %5
%13 = load i32, i32* %5
%14 = icmp eq i32 %13, 1
br i1 %14, label %15, label %17
15: ; preds = %2
%16 = load i32*, i32** %4
store i32* %16, i32** %3
br label %108
17: ; preds = %2
%18 = load i32, i32* %5
%19 = icmp eq i32 %18, 2
br i1 %19, label %20, label %42
20: ; preds = %17
%21 = load i32*, i32** %4
%22 = getelementptr inbounds i32, i32* %21, i64 0
%23 = load i32, i32* %22
%24 = load i32*, i32** %4
%25 = getelementptr inbounds i32, i32* %24, i64 1
%26 = load i32, i32* %25
%27 = icmp sgt i32 %23, %26
br i1 %27, label %28, label %40
28: ; preds = %20
%29 = load i32*, i32** %4
%30 = getelementptr inbounds i32, i32* %29, i64 0
%31 = load i32, i32* %30
store i32 %31, i32* %6
%32 = load i32*, i32** %4
%33 = getelementptr inbounds i32, i32* %32, i64 1
%34 = load i32, i32* %33
%35 = load i32*, i32** %4
%36 = getelementptr inbounds i32, i32* %35, i64 0
store i32 %34, i32* %36
%37 = load i32, i32* %6
%38 = load i32*, i32** %4
%39 = getelementptr inbounds i32, i32* %38, i64 1
store i32 %37, i32* %39
br label %40
40: ; preds = %28, %20
%41 = load i32*, i32** %4
store i32* %41, i32** %3
br label %108
42: ; preds = %17
%43 = load i32, i32* %5
%44 = sdiv i32 %43, 3
store i32 %44, i32* %7
%45 = load i32, i32* %5
%46 = load i32, i32* %7
%47 = sub nsw i32 %45, %46
%48 = sdiv i32 %47, 2
store i32 %48, i32* %8
%49 = load i32, i32* %5
%50 = load i32, i32* %7
%51 = load i32, i32* %8
%52 = add nsw i32 %50, %51
%53 = sub nsw i32 %49, %52
store i32 %53, i32* %9
%54 = load i32, i32* %7
%55 = call i64 @calloc(i32 %54, i32 4)
%56 = inttoptr i64 %55 to i32*
store i32* %56, i32** %10
%57 = load i32, i32* %8
%58 = call i64 @calloc(i32 %57, i32 4)
%59 = inttoptr i64 %58 to i32*
store i32* %59, i32** %11
%60 = load i32, i32* %9
%61 = call i64 @calloc(i32 %60, i32 4)
%62 = inttoptr i64 %61 to i32*
store i32* %62, i32** %12
%63 = load i32*, i32** %10
%64 = load i32*, i32** %4
%65 = load i32, i32* %7
%66 = sext i32 %65 to i64
%67 = mul i64 4, %66
%68 = trunc i64 %67 to i32
%69 = call i32 @memmove(i32* %63, i32* %64, i32 %68)
%70 = load i32*, i32** %11
%71 = load i32*, i32** %4
%72 = load i32, i32* %7
%73 = sext i32 %72 to i64
%74 = getelementptr inbounds i32, i32* %71, i64 %73
%75 = load i32, i32* %8
%76 = sext i32 %75 to i64
%77 = mul i64 4, %76
%78 = trunc i64 %77 to i32
%79 = call i32 @memmove(i32* %70, i32* %74, i32 %78)
%80 = load i32*, i32** %12
%81 = load i32*, i32** %4
%82 = load i32, i32* %7
%83 = sext i32 %82 to i64
%84 = getelementptr inbounds i32, i32* %81, i64 %83
%85 = load i32, i32* %8
%86 = sext i32 %85 to i64
%87 = getelementptr inbounds i32, i32* %84, i64 %86
%88 = load i32, i32* %9
%89 = sext i32 %88 to i64
%90 = mul i64 4, %89
%91 = trunc i64 %90 to i32
%92 = call i32 @memmove(i32* %80, i32* %87, i32 %91)
%93 = load i32*, i32** %4
%94 = call i32 @free(i32* %93)
%95 = load i32*, i32** %10
%96 = load i32, i32* %7
%97 = call i32* @three_way_merge_sort(i32* %95, i32 %96)
%98 = load i32*, i32** %11
%99 = load i32, i32* %8
%100 = call i32* @three_way_merge_sort(i32* %98, i32 %99)
%101 = load i32*, i32** %12
%102 = load i32, i32* %9
%103 = call i32* @three_way_merge_sort(i32* %101, i32 %102)
%104 = load i32, i32* %7
%105 = load i32, i32* %8
%106 = load i32, i32* %9
%107 = call i32* @three_way_merge(i32* %97, i32* %100, i32* %103, i32 %104, i32 %105, i32 %106)
store i32* %107, i32** %3
br label %108
108: ; preds = %42, %40, %15
%109 = load i32*, i32** %3
ret i32* %109
}

LLVM IR (-O2):
define i32* @three_way_merge_sort(i32* %0, i32 %1) {
switch i32 %1, label %11 [
i32 1, label %9
i32 2, label %3
]
3: ; preds = %2
%4 = load i32, i32* %0
%5 = getelementptr inbounds i32, i32* %0, i64 1
%6 = load i32, i32* %5
%7 = icmp sgt i32 %4, %6
br i1 %7, label %8, label %9
8: ; preds = %3
store i32 %6, i32* %0
store i32 %4, i32* %5
br label %9
9: ; preds = %2, %8, %3, %11
%10 = phi i32* [ %37, %11 ], [ %0, %3 ], [ %0, %8 ], [ %0, %2 ]
ret i32* %10
11: ; preds = %2
%12 = sdiv i32 %1, 3
%13 = sub nsw i32 %1, %12
%14 = sdiv i32 %13, 2
%15 = add nsw i32 %12, %14
%16 = sub i32 %1, %15
%17 = tail call i64 @calloc(i32 %12, i32 4)
%18 = inttoptr i64 %17 to i32*
%19 = tail call i64 @calloc(i32 %14, i32 4)
%20 = inttoptr i64 %19 to i32*
%21 = tail call i64 @calloc(i32 %16, i32 4)
%22 = inttoptr i64 %21 to i32*
%23 = sext i32 %12 to i64
%24 = shl i32 %12, 2
%25 = tail call i32 @memmove(i32* %18, i32* %0, i32 %24)
%26 = getelementptr inbounds i32, i32* %0, i64 %23
%27 = sext i32 %14 to i64
%28 = shl i32 %14, 2
%29 = tail call i32 @memmove(i32* %20, i32* %26, i32 %28)
%30 = getelementptr inbounds i32, i32* %26, i64 %27
%31 = shl i32 %16, 2
%32 = tail call i32 @memmove(i32* %22, i32* %30, i32 %31)
%33 = tail call i32 @free(i32* %0)
%34 = tail call i32* @three_way_merge_sort(i32* %18, i32 %12)
%35 = tail call i32* @three_way_merge_sort(i32* %20, i32 %14)
%36 = tail call i32* @three_way_merge_sort(i32* %22, i32 %16)
%37 = tail call i32* @three_way_merge(i32* %34, i32* %35, i32* %36, i32 %12, i32 %14, i32 %16)
br label %9
}