#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 int n1;
typedef int n;

/* Variables and functions */
scalar_t__ calloc(int, int);
int /*<<< orphan*/ free(double *);
int /*<<< orphan*/ memcpy(double *, double *, int);
int /*<<< orphan*/ merge(double *, long, double *, long, double *);
void CSPL_Sort_mergesort(double *inval, long n) {
  double *A1, *A2;
  long n1, n2;
  if (n < 2)
    return;
  n1 = n / 2;
  n2 = n - n1;
  {
    A1 = (double *)calloc((n1) - (0) + 1, sizeof(double));
    memcpy((A1), &(inval)[(0)], sizeof(double) * ((n1) - (0) + 1));
  };
  {
    A2 = (double *)calloc((n) - (n1) + 1, sizeof(double));
    memcpy((A2), &(inval)[(n1)], sizeof(double) * ((n) - (n1) + 1));
  };
  CSPL_Sort_mergesort(A1, n1);
  CSPL_Sort_mergesort(A2, n2);
  merge(A1, n1, A2, n2, inval);
  free(A1);
  free(A2);
}


x86 assembly (clang, -O0):
<CSPL_Sort_mergesort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpq   $0x2,-0x10(%rbp)
jge    1170 <CSPL_Sort_mergesort+0x20>
jmp    1254 <CSPL_Sort_mergesort+0x104>
mov    -0x10(%rbp),%rax
mov    $0x2,%ecx
cqto
idiv   %rcx
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
sub    -0x28(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rax
sub    $0x0,%rax
add    $0x1,%rax
mov    %eax,%edi
mov    $0x8,%esi
call   1040 <calloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x28(%rbp),%rax
sub    $0x0,%rax
add    $0x1,%rax
shl    $0x3,%rax
mov    %eax,%edx
call   1060 <memcpy@plt>
mov    -0x10(%rbp),%rax
sub    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %eax,%edi
mov    $0x8,%esi
call   1040 <calloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x28(%rbp),%rax
shl    $0x3,%rax
add    %rax,%rsi
mov    -0x10(%rbp),%rax
sub    -0x28(%rbp),%rax
add    $0x1,%rax
shl    $0x3,%rax
mov    %eax,%edx
call   1060 <memcpy@plt>
mov    -0x18(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1050 <CSPL_Sort_mergesort@plt>
mov    -0x20(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   1050 <CSPL_Sort_mergesort@plt>
mov    -0x18(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    -0x30(%rbp),%rcx
mov    -0x8(%rbp),%r8
call   1070 <merge@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<CSPL_Sort_mergesort>:
cmp    $0x1,%rsi
jg     1157 <CSPL_Sort_mergesort+0x7>
ret
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rsi,%rbx
mov    %rdi,%r14
mov    %rsi,%r15
shr    %r15
sub    %r15,%rbx
lea    0x1(%r15),%edi
mov    $0x8,%esi
call   1040 <calloc@plt>
mov    %rax,%r12
lea    0x8(,%r15,8),%edx
mov    %rax,%rdi
mov    %r14,%rsi
call   1060 <memcpy@plt>
lea    0x1(%rbx),%edi
mov    $0x8,%esi
call   1040 <calloc@plt>
mov    %rax,%r13
lea    (%r14,%r15,8),%rsi
lea    0x8(,%rbx,8),%edx
mov    %rax,%rdi
call   1060 <memcpy@plt>
mov    %r12,%rdi
mov    %r15,%rsi
call   1050 <CSPL_Sort_mergesort@plt>
mov    %r13,%rdi
mov    %rbx,%rsi
call   1050 <CSPL_Sort_mergesort@plt>
mov    %r12,%rdi
mov    %r15,%rsi
mov    %r13,%rdx
mov    %rbx,%rcx
mov    %r14,%r8
call   1070 <merge@plt>
mov    %r12,%rdi
call   1030 <free@plt>
mov    %r13,%rdi
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
jmp    1030 <free@plt>

LLVM IR (-O0):
define void @CSPL_Sort_mergesort(double* %0, i64 %1) {
%3 = alloca double*
%4 = alloca i64
%5 = alloca double*
%6 = alloca double*
%7 = alloca i64
%8 = alloca i64
store double* %0, double** %3
store i64 %1, i64* %4
%9 = load i64, i64* %4
%10 = icmp slt i64 %9, 2
br i1 %10, label %11, label %12
11: ; preds = %2
br label %65
12: ; preds = %2
%13 = load i64, i64* %4
%14 = sdiv i64 %13, 2
store i64 %14, i64* %7
%15 = load i64, i64* %4
%16 = load i64, i64* %7
%17 = sub nsw i64 %15, %16
store i64 %17, i64* %8
%18 = load i64, i64* %7
%19 = sub nsw i64 %18, 0
%20 = add nsw i64 %19, 1
%21 = trunc i64 %20 to i32
%22 = call i64 @calloc(i32 %21, i32 8)
%23 = inttoptr i64 %22 to double*
store double* %23, double** %5
%24 = load double*, double** %5
%25 = load double*, double** %3
%26 = getelementptr inbounds double, double* %25, i64 0
%27 = load i64, i64* %7
%28 = sub nsw i64 %27, 0
%29 = add nsw i64 %28, 1
%30 = mul i64 8, %29
%31 = trunc i64 %30 to i32
%32 = call i32 @memcpy(double* %24, double* %26, i32 %31)
%33 = load i64, i64* %4
%34 = load i64, i64* %7
%35 = sub nsw i64 %33, %34
%36 = add nsw i64 %35, 1
%37 = trunc i64 %36 to i32
%38 = call i64 @calloc(i32 %37, i32 8)
%39 = inttoptr i64 %38 to double*
store double* %39, double** %6
%40 = load double*, double** %6
%41 = load double*, double** %3
%42 = load i64, i64* %7
%43 = getelementptr inbounds double, double* %41, i64 %42
%44 = load i64, i64* %4
%45 = load i64, i64* %7
%46 = sub nsw i64 %44, %45
%47 = add nsw i64 %46, 1
%48 = mul i64 8, %47
%49 = trunc i64 %48 to i32
%50 = call i32 @memcpy(double* %40, double* %43, i32 %49)
%51 = load double*, double** %5
%52 = load i64, i64* %7
%53 = load double*, double** %6
%54 = load i64, i64* %8
%55 = load double*, double** %5
%56 = load i64, i64* %7
%57 = load double*, double** %6
%58 = load i64, i64* %8
%59 = load double*, double** %3
%60 = call i32 @merge(double* %55, i64 %56, double* %57, i64 %58, double* %59)
%61 = load double*, double** %5
%62 = call i32 @free(double* %61)
%63 = load double*, double** %6
%64 = call i32 @free(double* %63)
br label %65
65: ; preds = %12, %11
ret void
}

LLVM IR (-O2):
define void @CSPL_Sort_mergesort(double* %0, i64 %1) {
%3 = icmp slt i64 %1, 2
br i1 %3, label %4, label %5
4: ; preds = %2, %5
ret void
5: ; preds = %2
%6 = lshr i64 %1, 1
%7 = sub nsw i64 %1, %6
%8 = trunc i64 %6 to i32
%9 = add i32 %8, 1
%10 = tail call i64 @calloc(i32 %9, i32 8)
%11 = inttoptr i64 %10 to double*
%12 = shl i32 %9, 3
%13 = tail call i32 @memcpy(double* %11, double* %0, i32 %12)
%14 = trunc i64 %7 to i32
%15 = add i32 %14, 1
%16 = tail call i64 @calloc(i32 %15, i32 8)
%17 = inttoptr i64 %16 to double*
%18 = getelementptr inbounds double, double* %0, i64 %6
%19 = shl i32 %15, 3
%20 = tail call i32 @memcpy(double* %17, double* %18, i32 %19)
tail call void @CSPL_Sort_mergesort(double* %11, i64 %6)
tail call void @CSPL_Sort_mergesort(double* %17, i64 %7)
%21 = tail call i32 @merge(double* %11, i64 %6, double* %17, i64 %7, double* %0)
%22 = tail call i32 @free(double* %11)
%23 = tail call i32 @free(double* %17)
br label %4
}