#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 atoi(char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ free(int *);
int *msort(int *, int *);
int /*<<< orphan*/ print_array(int *, int);
int *random_array(int);
int /*<<< orphan*/ stderr;
int test_msort(int argc, char *argv[]) {
  int len;
  if (argc > 1) {
    len = atoi(argv[1]);
  } else {
    len = 1000;
  }
  if (len > 0) {
    int *arr = random_array(len);
    int *sorted = msort(arr, arr + len - 1);
    print_array(sorted, len);
    free(sorted);
  } else {
    fprintf(stderr, "Can't sort 0 length array.\n");
  }
}


x86 assembly (clang, -O0):
<test_msort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x1,-0x8(%rbp)
jle    118e <test_msort+0x2e>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1080 <atoi@plt>
mov    %eax,-0x14(%rbp)
jmp    1195 <test_msort+0x35>
movl   $0x3e8,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jle    11e5 <test_msort+0x85>
mov    -0x14(%rbp),%edi
call   1070 <random_array@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x20(%rbp),%rsi
movslq -0x14(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rsi
add    $0xfffffffffffffffc,%rsi
call   1040 <msort@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1050 <print_array@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
jmp    11fa <test_msort+0x9a>
mov    0x2df4(%rip),%rax
mov    (%rax),%edi
lea    0xe0b(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_msort>:
push   %r14
push   %rbx
push   %rax
mov    $0x3e8,%ebx
cmp    $0x2,%edi
jl     117d <test_msort+0x1d>
mov    0x8(%rsi),%rdi
call   1080 <atoi@plt>
mov    %eax,%ebx
test   %eax,%eax
jle    11ad <test_msort+0x4d>
mov    %ebx,%edi
call   1070 <random_array@plt>
mov    %ebx,%ecx
lea    (%rax,%rcx,4),%rsi
add    $0xfffffffffffffffc,%rsi
mov    %rax,%rdi
call   1040 <msort@plt>
mov    %rax,%r14
mov    %rax,%rdi
mov    %ebx,%esi
call   1050 <print_array@plt>
mov    %r14,%rdi
call   1030 <free@plt>
jmp    11c2 <test_msort+0x62>
mov    0x2e2c(%rip),%rax
mov    (%rax),%edi
lea    0xe43(%rip),%rsi
call   1060 <fprintf@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [28 x i8] c"Can't sort 0 length array.\0A\00"
define i32 @test_msort(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i32*
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* %4
%10 = icmp sgt i32 %9, 1
br i1 %10, label %11, label %16
11: ; preds = %2
%12 = load i8**, i8*** %5
%13 = getelementptr inbounds i8*, i8** %12, i64 1
%14 = load i8*, i8** %13
%15 = call i32 @atoi(i8* %14)
store i32 %15, i32* %6
br label %17
16: ; preds = %2
store i32 1000, i32* %6
br label %17
17: ; preds = %16, %11
%18 = load i32, i32* %6
%19 = icmp sgt i32 %18, 0
br i1 %19, label %20, label %35
20: ; preds = %17
%21 = load i32, i32* %6
%22 = call i32* @random_array(i32 %21)
store i32* %22, i32** %7
%23 = load i32*, i32** %7
%24 = load i32*, i32** %7
%25 = load i32, i32* %6
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds i32, i32* %24, i64 %26
%28 = getelementptr inbounds i32, i32* %27, i64 -1
%29 = call i32* @msort(i32* %23, i32* %28)
store i32* %29, i32** %8
%30 = load i32*, i32** %8
%31 = load i32, i32* %6
%32 = call i32 @print_array(i32* %30, i32 %31)
%33 = load i32*, i32** %8
%34 = call i32 @free(i32* %33)
br label %38
35: ; preds = %17
%36 = load i32, i32* @stderr
%37 = call i32 @fprintf(i32 %36, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
br label %38
38: ; preds = %35, %20
%39 = load i32, i32* %3
ret i32 %39
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [28 x i8] c"Can't sort 0 length array.\0A\00"
define i32 @test_msort(i32 %0, i8** nocapture %1) {
%3 = icmp sgt i32 %0, 1
br i1 %3, label %4, label %9
4: ; preds = %2
%5 = getelementptr inbounds i8*, i8** %1, i64 1
%6 = load i8*, i8** %5
%7 = tail call i32 @atoi(i8* %6)
%8 = icmp sgt i32 %7, 0
br i1 %8, label %9, label %18
9: ; preds = %2, %4
%10 = phi i32 [ %7, %4 ], [ 1000, %2 ]
%11 = tail call i32* @random_array(i32 %10)
%12 = zext i32 %10 to i64
%13 = getelementptr inbounds i32, i32* %11, i64 %12
%14 = getelementptr inbounds i32, i32* %13, i64 -1
%15 = tail call i32* @msort(i32* %11, i32* %14)
%16 = tail call i32 @print_array(i32* %15, i32 %10)
%17 = tail call i32 @free(i32* %15)
br label %21
18: ; preds = %4
%19 = load i32, i32* @stderr
%20 = tail call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
br label %21
21: ; preds = %18, %9
ret i32 undef
}