#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 */
double *copy_array(double *, int);
int /*<<< orphan*/ free(double *);
double *merge(double *, size_t, double *, size_t);
double *rec_merge_sort(double *array, size_t size) {
  if (size == 1) {
    return copy_array(array, 1);
  } else {
    size_t size2 = size / 2;
    double *s0 = rec_merge_sort(array, size2);
    double *s1 = rec_merge_sort(array + size2, size - size2);
    double *res = merge(s0, size2, s1, size - size2);
    free(s0);
    free(s1);
    return res;
  }
}


x86 assembly (clang, -O0):
<rec_merge_sort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x1,-0x18(%rbp)
jne    1172 <rec_merge_sort+0x32>
mov    -0x10(%rbp),%rdi
mov    $0x1,%esi
call   1050 <copy_array@plt>
mov    %rax,-0x8(%rbp)
jmp    11e6 <rec_merge_sort+0xa6>
mov    -0x18(%rbp),%rax
shr    $0x1,%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1060 <rec_merge_sort@plt>
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rax
shl    $0x3,%rax
add    %rax,%rdi
mov    -0x18(%rbp),%rsi
sub    -0x20(%rbp),%rsi
call   1060 <rec_merge_sort@plt>
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x30(%rbp),%rdx
mov    -0x18(%rbp),%rcx
sub    -0x20(%rbp),%rcx
call   1040 <merge@plt>
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rec_merge_sort>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
cmp    $0x1,%rsi
jne    1169 <rec_merge_sort+0x29>
mov    %r14,%rdi
mov    $0x1,%esi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1050 <copy_array@plt>
mov    %rsi,%rbx
mov    %rsi,%r15
shr    %r15
mov    %r14,%rdi
mov    %r15,%rsi
call   1060 <rec_merge_sort@plt>
mov    %rax,%r12
lea    (%r14,%r15,8),%rdi
sub    %r15,%rbx
mov    %rbx,%rsi
call   1060 <rec_merge_sort@plt>
mov    %rax,%r14
mov    %r12,%rdi
mov    %r15,%rsi
mov    %rax,%rdx
mov    %rbx,%rcx
call   1040 <merge@plt>
mov    %rax,%rbx
mov    %r12,%rdi
call   1030 <free@plt>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define double* @rec_merge_sort(double* %0, i64 %1) {
%3 = alloca double*
%4 = alloca double*
%5 = alloca i64
%6 = alloca i64
%7 = alloca double*
%8 = alloca double*
%9 = alloca double*
store double* %0, double** %4
store i64 %1, i64* %5
%10 = load i64, i64* %5
%11 = icmp eq i64 %10, 1
br i1 %11, label %12, label %15
12: ; preds = %2
%13 = load double*, double** %4
%14 = call double* @copy_array(double* %13, i32 1)
store double* %14, double** %3
br label %40
15: ; preds = %2
%16 = load i64, i64* %5
%17 = udiv i64 %16, 2
store i64 %17, i64* %6
%18 = load double*, double** %4
%19 = load i64, i64* %6
%20 = call double* @rec_merge_sort(double* %18, i64 %19)
store double* %20, double** %7
%21 = load double*, double** %4
%22 = load i64, i64* %6
%23 = getelementptr inbounds double, double* %21, i64 %22
%24 = load i64, i64* %5
%25 = load i64, i64* %6
%26 = sub i64 %24, %25
%27 = call double* @rec_merge_sort(double* %23, i64 %26)
store double* %27, double** %8
%28 = load double*, double** %7
%29 = load i64, i64* %6
%30 = load double*, double** %8
%31 = load i64, i64* %5
%32 = load i64, i64* %6
%33 = sub i64 %31, %32
%34 = call double* @merge(double* %28, i64 %29, double* %30, i64 %33)
store double* %34, double** %9
%35 = load double*, double** %7
%36 = call i32 @free(double* %35)
%37 = load double*, double** %8
%38 = call i32 @free(double* %37)
%39 = load double*, double** %9
store double* %39, double** %3
br label %40
40: ; preds = %15, %12
%41 = load double*, double** %3
ret double* %41
}

LLVM IR (-O2):
define double* @rec_merge_sort(double* %0, i64 %1) {
%3 = icmp eq i64 %1, 1
br i1 %3, label %4, label %8
4: ; preds = %2
%5 = tail call double* @copy_array(double* %0, i32 1)
br label %6
6: ; preds = %4, %8
%7 = phi double* [ %14, %8 ], [ %5, %4 ]
ret double* %7
8: ; preds = %2
%9 = lshr i64 %1, 1
%10 = tail call double* @rec_merge_sort(double* %0, i64 %9)
%11 = getelementptr inbounds double, double* %0, i64 %9
%12 = sub i64 %1, %9
%13 = tail call double* @rec_merge_sort(double* %11, i64 %12)
%14 = tail call double* @merge(double* %10, i64 %9, double* %13, i64 %12)
%15 = tail call i32 @free(double* %10)
%16 = tail call i32 @free(double* %13)
br label %6
}