#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 /*<<< orphan*/ clock_t;

/* Variables and functions */
int /*<<< orphan*/ clock();
int /*<<< orphan*/ free(int *);
int /*<<< orphan*/ get_time(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ heap_sort(int *, int const);
scalar_t__ malloc(int);
int /*<<< orphan*/ memcpy(int *, int *, int);
int /*<<< orphan*/ memset(int *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ quick_sort(int *, int /*<<< orphan*/, int const);
int /*<<< orphan*/ rand_array(int *, int const);
int /*<<< orphan*/ shell_sort(int *, int const);
int test_sort(const int count) {
  clock_t start, end;
  double timeUsed;
  int *p, *sort_data;
  p = (int *)malloc(sizeof(int) * count);
  if (p == NULL) {
    printf("memory alloc error.\n");
    return -1;
  }
  sort_data = (int *)malloc(sizeof(int) * count);
  if (sort_data == NULL) {
    printf("memory alloc error.\n");
    return -1;
  }
  memset(p, 0, sizeof(int) * count);
  rand_array(p, count);
  memset(sort_data, 0, sizeof(int) * count);
  memcpy(sort_data, p, sizeof(int) * count);
  start = clock();
  shell_sort(sort_data, count);
  end = clock();
  printf("shell  sort:");
  get_time(start, end);
  memset(sort_data, 0, sizeof(int) * count);
  memcpy(sort_data, p, sizeof(int) * count);
  start = clock();
  quick_sort(sort_data, 0, count - 1);
  end = clock();
  printf("quick  sort:");
  get_time(start, end);
  memset(sort_data, 0, sizeof(int) * count);
  memcpy(sort_data, p, sizeof(int) * count);
  start = clock();
  heap_sort(sort_data, count);
  end = clock();
  printf("heap   sort:");
  get_time(start, end);
  if (p != NULL) {
    free(p);
    p = NULL;
  }
  if (sort_data != NULL) {
    free(sort_data);
    sort_data = NULL;
  }
  return 0;
}


x86 assembly (clang, -O0):
<test_sort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
movslq -0x8(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   10a0 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11f1 <test_sort+0x41>
lea    0xe20(%rip),%rdi
call   1060 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13b5 <test_sort+0x205>
movslq -0x8(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   10a0 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1227 <test_sort+0x77>
lea    0xdea(%rip),%rdi
call   1060 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13b5 <test_sort+0x205>
mov    -0x20(%rbp),%rdi
movslq -0x8(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edx
xor    %esi,%esi
call   1070 <memset@plt>
mov    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%esi
call   10b0 <rand_array@plt>
mov    -0x28(%rbp),%rdi
movslq -0x8(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edx
xor    %esi,%esi
call   1070 <memset@plt>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rsi
movslq -0x8(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edx
call   1090 <memcpy@plt>
mov    $0x0,%al
call   1050 <clock@plt>
mov    %eax,-0xc(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x8(%rbp),%esi
call   10d0 <shell_sort@plt>
mov    $0x0,%al
call   1050 <clock@plt>
mov    %eax,-0x10(%rbp)
lea    0xd7a(%rip),%rdi
call   1060 <printf@plt>
mov    -0xc(%rbp),%edi
mov    -0x10(%rbp),%esi
call   1080 <get_time@plt>
mov    -0x28(%rbp),%rdi
movslq -0x8(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edx
xor    %esi,%esi
call   1070 <memset@plt>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rsi
movslq -0x8(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edx
call   1090 <memcpy@plt>
mov    $0x0,%al
call   1050 <clock@plt>
mov    %eax,-0xc(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x8(%rbp),%edx
sub    $0x1,%edx
xor    %esi,%esi
call   10c0 <quick_sort@plt>
mov    $0x0,%al
call   1050 <clock@plt>
mov    %eax,-0x10(%rbp)
lea    0xd1f(%rip),%rdi
call   1060 <printf@plt>
mov    -0xc(%rbp),%edi
mov    -0x10(%rbp),%esi
call   1080 <get_time@plt>
mov    -0x28(%rbp),%rdi
movslq -0x8(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edx
xor    %esi,%esi
call   1070 <memset@plt>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rsi
movslq -0x8(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edx
call   1090 <memcpy@plt>
mov    $0x0,%al
call   1050 <clock@plt>
mov    %eax,-0xc(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x8(%rbp),%esi
call   1040 <heap_sort@plt>
mov    $0x0,%al
call   1050 <clock@plt>
mov    %eax,-0x10(%rbp)
lea    0xcc9(%rip),%rdi
call   1060 <printf@plt>
mov    -0xc(%rbp),%edi
mov    -0x10(%rbp),%esi
call   1080 <get_time@plt>
cmpq   $0x0,-0x20(%rbp)
je     1392 <test_sort+0x1e2>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x20(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     13ae <test_sort+0x1fe>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x28(%rbp)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_sort>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edi,%r15d
lea    0x0(,%r15,4),%ebx
mov    %ebx,%edi
call   10b0 <malloc@plt>
test   %rax,%rax
je     1315 <test_sort+0x155>
mov    %rax,%r12
mov    %ebx,%edi
call   10b0 <malloc@plt>
test   %rax,%rax
je     1315 <test_sort+0x155>
mov    %rax,%rbp
mov    %r12,%rdi
xor    %esi,%esi
mov    %ebx,%edx
call   1080 <memset@plt>
mov    %r12,%rdi
mov    %r15d,%esi
call   10c0 <rand_array@plt>
mov    %rbp,%rdi
xor    %esi,%esi
mov    %ebx,%edx
call   1080 <memset@plt>
mov    %rbp,%rdi
mov    %r12,%rsi
mov    %ebx,%edx
call   10a0 <memcpy@plt>
xor    %eax,%eax
call   1060 <clock@plt>
mov    %eax,%r13d
mov    %rbp,%rdi
mov    %r15d,%esi
call   10e0 <shell_sort@plt>
xor    %eax,%eax
call   1060 <clock@plt>
mov    %eax,%r14d
lea    0xdae(%rip),%rdi
call   1070 <printf@plt>
mov    %r13d,%edi
mov    %r14d,%esi
call   1090 <get_time@plt>
mov    %rbp,%rdi
xor    %esi,%esi
mov    %ebx,%edx
call   1080 <memset@plt>
mov    %rbp,%rdi
mov    %r12,%rsi
mov    %ebx,%edx
call   10a0 <memcpy@plt>
xor    %eax,%eax
call   1060 <clock@plt>
mov    %eax,%r14d
lea    -0x1(%r15),%edx
mov    %rbp,%rdi
xor    %esi,%esi
call   10d0 <quick_sort@plt>
xor    %eax,%eax
call   1060 <clock@plt>
mov    %eax,%r13d
lea    0xd69(%rip),%rdi
call   1070 <printf@plt>
mov    %r14d,%edi
mov    %r13d,%esi
call   1090 <get_time@plt>
mov    %rbp,%rdi
xor    %esi,%esi
mov    %ebx,%edx
call   1080 <memset@plt>
mov    %rbp,%rdi
mov    %r12,%rsi
mov    %ebx,%edx
call   10a0 <memcpy@plt>
xor    %eax,%eax
call   1060 <clock@plt>
mov    %eax,%r14d
mov    %rbp,%rdi
mov    %r15d,%esi
call   1040 <heap_sort@plt>
xor    %eax,%eax
call   1060 <clock@plt>
mov    %eax,%ebx
lea    0xd28(%rip),%rdi
call   1070 <printf@plt>
mov    %r14d,%edi
mov    %ebx,%esi
call   1090 <get_time@plt>
mov    %r12,%rdi
call   1030 <free@plt>
mov    %rbp,%rdi
call   1030 <free@plt>
xor    %eax,%eax
jmp    1326 <test_sort+0x166>
lea    0xd0b(%rip),%rdi
call   1050 <puts@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [21 x i8] c"memory alloc error.\0A\00"
@.str.1 = [13 x i8] c"shell sort:\00"
@.str.2 = [13 x i8] c"quick sort:\00"
@.str.3 = [13 x i8] c"heap sort:\00"
define i32 @test_sort(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca double
%7 = alloca i32*
%8 = alloca i32*
store i32 %0, i32* %3
%9 = load i32, i32* %3
%10 = sext i32 %9 to i64
%11 = mul i64 4, %10
%12 = trunc i64 %11 to i32
%13 = call i64 @malloc(i32 %12)
%14 = inttoptr i64 %13 to i32*
store i32* %14, i32** %7
%15 = load i32*, i32** %7
%16 = icmp eq i32* %15, null
br i1 %16, label %17, label %19
17: ; preds = %1
%18 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
br label %119
19: ; preds = %1
%20 = load i32, i32* %3
%21 = sext i32 %20 to i64
%22 = mul i64 4, %21
%23 = trunc i64 %22 to i32
%24 = call i64 @malloc(i32 %23)
%25 = inttoptr i64 %24 to i32*
store i32* %25, i32** %8
%26 = load i32*, i32** %8
%27 = icmp eq i32* %26, null
br i1 %27, label %28, label %30
28: ; preds = %19
%29 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
br label %119
30: ; preds = %19
%31 = load i32*, i32** %7
%32 = load i32, i32* %3
%33 = sext i32 %32 to i64
%34 = mul i64 4, %33
%35 = trunc i64 %34 to i32
%36 = call i32 @memset(i32* %31, i32 0, i32 %35)
%37 = load i32*, i32** %7
%38 = load i32, i32* %3
%39 = call i32 @rand_array(i32* %37, i32 %38)
%40 = load i32*, i32** %8
%41 = load i32, i32* %3
%42 = sext i32 %41 to i64
%43 = mul i64 4, %42
%44 = trunc i64 %43 to i32
%45 = call i32 @memset(i32* %40, i32 0, i32 %44)
%46 = load i32*, i32** %8
%47 = load i32*, i32** %7
%48 = load i32, i32* %3
%49 = sext i32 %48 to i64
%50 = mul i64 4, %49
%51 = trunc i64 %50 to i32
%52 = call i32 @memcpy(i32* %46, i32* %47, i32 %51)
%53 = call i32 (...) @clock()
store i32 %53, i32* %4
%54 = load i32*, i32** %8
%55 = load i32, i32* %3
%56 = call i32 @shell_sort(i32* %54, i32 %55)
%57 = call i32 (...) @clock()
store i32 %57, i32* %5
%58 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%59 = load i32, i32* %4
%60 = load i32, i32* %5
%61 = call i32 @get_time(i32 %59, i32 %60)
%62 = load i32*, i32** %8
%63 = load i32, i32* %3
%64 = sext i32 %63 to i64
%65 = mul i64 4, %64
%66 = trunc i64 %65 to i32
%67 = call i32 @memset(i32* %62, i32 0, i32 %66)
%68 = load i32*, i32** %8
%69 = load i32*, i32** %7
%70 = load i32, i32* %3
%71 = sext i32 %70 to i64
%72 = mul i64 4, %71
%73 = trunc i64 %72 to i32
%74 = call i32 @memcpy(i32* %68, i32* %69, i32 %73)
%75 = call i32 (...) @clock()
store i32 %75, i32* %4
%76 = load i32*, i32** %8
%77 = load i32, i32* %3
%78 = sub nsw i32 %77, 1
%79 = call i32 @quick_sort(i32* %76, i32 0, i32 %78)
%80 = call i32 (...) @clock()
store i32 %80, i32* %5
%81 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%82 = load i32, i32* %4
%83 = load i32, i32* %5
%84 = call i32 @get_time(i32 %82, i32 %83)
%85 = load i32*, i32** %8
%86 = load i32, i32* %3
%87 = sext i32 %86 to i64
%88 = mul i64 4, %87
%89 = trunc i64 %88 to i32
%90 = call i32 @memset(i32* %85, i32 0, i32 %89)
%91 = load i32*, i32** %8
%92 = load i32*, i32** %7
%93 = load i32, i32* %3
%94 = sext i32 %93 to i64
%95 = mul i64 4, %94
%96 = trunc i64 %95 to i32
%97 = call i32 @memcpy(i32* %91, i32* %92, i32 %96)
%98 = call i32 (...) @clock()
store i32 %98, i32* %4
%99 = load i32*, i32** %8
%100 = load i32, i32* %3
%101 = call i32 @heap_sort(i32* %99, i32 %100)
%102 = call i32 (...) @clock()
store i32 %102, i32* %5
%103 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
%104 = load i32, i32* %4
%105 = load i32, i32* %5
%106 = call i32 @get_time(i32 %104, i32 %105)
%107 = load i32*, i32** %7
%108 = icmp ne i32* %107, null
br i1 %108, label %109, label %112
109: ; preds = %30
%110 = load i32*, i32** %7
%111 = call i32 @free(i32* %110)
store i32* null, i32** %7
br label %112
112: ; preds = %109, %30
%113 = load i32*, i32** %8
%114 = icmp ne i32* %113, null
br i1 %114, label %115, label %118
115: ; preds = %112
%116 = load i32*, i32** %8
%117 = call i32 @free(i32* %116)
store i32* null, i32** %8
br label %118
118: ; preds = %115, %112
store i32 0, i32* %2
br label %119
119: ; preds = %118, %28, %17
%120 = load i32, i32* %2
ret i32 %120
}

LLVM IR (-O2):
@.str.1 = [13 x i8] c"shell sort:\00"
@.str.2 = [13 x i8] c"quick sort:\00"
@.str.3 = [13 x i8] c"heap sort:\00"
@str.4 = [20 x i8] c"memory alloc error.\00"
define i32 @test_sort(i32 %0) {
%2 = shl i32 %0, 2
%3 = tail call i64 @malloc(i32 %2)
%4 = inttoptr i64 %3 to i32*
%5 = icmp eq i64 %3, 0
br i1 %5, label %6, label %8
6: ; preds = %1
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.4, i64 0, i64 0))
br label %41
8: ; preds = %1
%9 = tail call i64 @malloc(i32 %2)
%10 = inttoptr i64 %9 to i32*
%11 = icmp eq i64 %9, 0
br i1 %11, label %12, label %14
12: ; preds = %8
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.4, i64 0, i64 0))
br label %41
14: ; preds = %8
%15 = tail call i32 @memset(i32* %4, i32 0, i32 %2)
%16 = tail call i32 @rand_array(i32* %4, i32 %0)
%17 = tail call i32 @memset(i32* %10, i32 0, i32 %2)
%18 = tail call i32 @memcpy(i32* %10, i32* %4, i32 %2)
%19 = tail call i32 (...) @clock()
%20 = tail call i32 @shell_sort(i32* %10, i32 %0)
%21 = tail call i32 (...) @clock()
%22 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%23 = tail call i32 @get_time(i32 %19, i32 %21)
%24 = tail call i32 @memset(i32* %10, i32 0, i32 %2)
%25 = tail call i32 @memcpy(i32* %10, i32* %4, i32 %2)
%26 = tail call i32 (...) @clock()
%27 = add nsw i32 %0, -1
%28 = tail call i32 @quick_sort(i32* %10, i32 0, i32 %27)
%29 = tail call i32 (...) @clock()
%30 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%31 = tail call i32 @get_time(i32 %26, i32 %29)
%32 = tail call i32 @memset(i32* %10, i32 0, i32 %2)
%33 = tail call i32 @memcpy(i32* %10, i32* %4, i32 %2)
%34 = tail call i32 (...) @clock()
%35 = tail call i32 @heap_sort(i32* %10, i32 %0)
%36 = tail call i32 (...) @clock()
%37 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
%38 = tail call i32 @get_time(i32 %34, i32 %36)
%39 = tail call i32 @free(i32* %4)
%40 = tail call i32 @free(i32* %10)
br label %41
41: ; preds = %14, %12, %6
%42 = phi i32 [ -1, %6 ], [ -1, %12 ], [ 0, %14 ]
ret i32 %42
}