#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 */
struct timeval {
  double tv_usec;
  int /*<<< orphan*/ tv_sec;
};

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int EXIT_SUCCESS;
unsigned long atoi(char *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ free(int *);
int /*<<< orphan*/ gettimeofday(struct timeval *, int /*<<< orphan*/ *);
int /*<<< orphan*/ heap_sort(int *, unsigned long);
scalar_t__ malloc(int);
int /*<<< orphan*/ printf(char *, ...);
int random();
int /*<<< orphan*/ srand(int /*<<< orphan*/);
int /*<<< orphan*/ time(int /*<<< orphan*/ *);
int main(int argc, char *argv[]) {
  struct timeval timevalA;
  struct timeval timevalB;
  int *vetor = NULL;
  unsigned long tam, i = 0;
  if (argc != 2) {
    printf("%s elementos\n", argv[0]);
    exit(EXIT_FAILURE);
  }
  tam = atoi(argv[1]);
  if (!(vetor = (int *)malloc(sizeof(int) * tam))) {
    printf("Erro ao alocar memória\n");
    exit(EXIT_FAILURE);
  }
  srand(time(NULL));
  for (i = 0; i < tam; i++) {
    *(vetor + i) = random() % 10000;
  }
  gettimeofday(&timevalA, NULL);
  heap_sort(vetor, tam);
  gettimeofday(&timevalB, NULL);
  printf("%lf\n", timevalB.tv_sec - timevalA.tv_sec +
                      (timevalB.tv_usec - timevalA.tv_usec) / (double)1000000);
  free(vetor);
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x38(%rbp)
movq   $0x0,-0x48(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11f3 <main+0x53>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe2a(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2df4(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10b0 <atoi@plt>
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   10a0 <malloc@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
jne    123d <main+0x9d>
lea    0xdee(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2daa(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1080 <time@plt>
mov    %eax,%edi
call   1070 <srand@plt>
movq   $0x0,-0x48(%rbp)
mov    -0x48(%rbp),%rax
cmp    -0x40(%rbp),%rax
jae    128e <main+0xee>
mov    $0x0,%al
call   1090 <random@plt>
mov    $0x2710,%ecx
cltd
idiv   %ecx
mov    -0x38(%rbp),%rax
mov    -0x48(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x48(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x48(%rbp)
jmp    1255 <main+0xb5>
lea    -0x20(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1060 <gettimeofday@plt>
mov    -0x38(%rbp),%rdi
mov    -0x40(%rbp),%rsi
call   1040 <heap_sort@plt>
lea    -0x30(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1060 <gettimeofday@plt>
mov    -0x28(%rbp),%eax
sub    -0x18(%rbp),%eax
cvtsi2sd %eax,%xmm0
movsd  -0x30(%rbp),%xmm1
subsd  -0x20(%rbp),%xmm1
movsd  0xd2f(%rip),%xmm2
divsd  %xmm2,%xmm1
addsd  %xmm1,%xmm0
lea    0xd4f(%rip),%rdi
mov    $0x1,%al
call   1050 <printf@plt>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
mov    0x2ce1(%rip),%rax
mov    (%rax),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x20,%rsp
cmp    $0x2,%edi
jne    129f <main+0xef>
mov    0x8(%rsi),%rdi
call   10c0 <atoi@plt>
mov    %rax,%r15
lea    0x0(,%rax,4),%edi
call   10b0 <malloc@plt>
test   %rax,%rax
je     12b2 <main+0x102>
mov    %rax,%r14
xor    %edi,%edi
call   1090 <time@plt>
mov    %eax,%edi
call   1080 <srand@plt>
test   %r15,%r15
je     1232 <main+0x82>
xor    %ebx,%ebx
nopl   0x0(%rax,%rax,1)
xor    %eax,%eax
call   10a0 <random@plt>
cltq
imul   $0x68db8bad,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x2c,%rcx
add    %edx,%ecx
imul   $0x2710,%ecx,%ecx
sub    %ecx,%eax
mov    %eax,(%r14,%rbx,4)
add    $0x1,%rbx
cmp    %rbx,%r15
jne    1200 <main+0x50>
lea    0x10(%rsp),%rdi
xor    %esi,%esi
call   1070 <gettimeofday@plt>
mov    %r14,%rdi
mov    %r15,%rsi
call   1040 <heap_sort@plt>
mov    %rsp,%rdi
xor    %esi,%esi
call   1070 <gettimeofday@plt>
mov    0x8(%rsp),%eax
sub    0x18(%rsp),%eax
cvtsi2sd %eax,%xmm1
movsd  (%rsp),%xmm0
subsd  0x10(%rsp),%xmm0
divsd  0xd8e(%rip),%xmm0
addsd  %xmm1,%xmm0
lea    0xd99(%rip),%rdi
mov    $0x1,%al
call   1060 <printf@plt>
mov    %r14,%rdi
call   1030 <free@plt>
mov    0x2d45(%rip),%rax
mov    (%rax),%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
mov    (%rsi),%rsi
lea    0xd5f(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
jmp    12be <main+0x10e>
lea    0xd62(%rip),%rdi
call   1050 <puts@plt>
mov    0x2d1b(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
%struct.timeval = type { double, i32 }
@.str = [14 x i8] c"%s elementos\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [25 x i8] c"Erro ao alocar mem\C3\B3ria\0A\00"
@.str.2 = [5 x i8] c"%lf\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.timeval
%7 = alloca %struct.timeval
%8 = alloca i32*
%9 = alloca i64
%10 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32* null, i32** %8
store i64 0, i64* %10
%11 = load i32, i32* %4
%12 = icmp ne i32 %11, 2
br i1 %12, label %13, label %20
13: ; preds = %2
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 0
%16 = load i8*, i8** %15
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* %16)
%18 = load i32, i32* @EXIT_FAILURE
%19 = call i32 @exit(i32 %18)
unreachable
20: ; preds = %2
%21 = load i8**, i8*** %5
%22 = getelementptr inbounds i8*, i8** %21, i64 1
%23 = load i8*, i8** %22
%24 = call i64 @atoi(i8* %23)
store i64 %24, i64* %9
%25 = load i64, i64* %9
%26 = mul i64 4, %25
%27 = trunc i64 %26 to i32
%28 = call i64 @malloc(i32 %27)
%29 = inttoptr i64 %28 to i32*
store i32* %29, i32** %8
%30 = icmp ne i32* %29, null
br i1 %30, label %35, label %31
31: ; preds = %20
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
%33 = load i32, i32* @EXIT_FAILURE
%34 = call i32 @exit(i32 %33)
unreachable
35: ; preds = %20
%36 = call i32 @time(i32* null)
%37 = call i32 @srand(i32 %36)
store i64 0, i64* %10
br label %38
38: ; preds = %48, %35
%39 = load i64, i64* %10
%40 = load i64, i64* %9
%41 = icmp ult i64 %39, %40
br i1 %41, label %42, label %51
42: ; preds = %38
%43 = call i32 (...) @random()
%44 = srem i32 %43, 10000
%45 = load i32*, i32** %8
%46 = load i64, i64* %10
%47 = getelementptr inbounds i32, i32* %45, i64 %46
store i32 %44, i32* %47
br label %48
48: ; preds = %42
%49 = load i64, i64* %10
%50 = add i64 %49, 1
store i64 %50, i64* %10
br label %38
51: ; preds = %38
%52 = call i32 @gettimeofday(%struct.timeval* %6, i32* null)
%53 = load i32*, i32** %8
%54 = load i64, i64* %9
%55 = call i32 @heap_sort(i32* %53, i64 %54)
%56 = call i32 @gettimeofday(%struct.timeval* %7, i32* null)
%57 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i32 0, i32 1
%58 = load i32, i32* %57
%59 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i32 0, i32 1
%60 = load i32, i32* %59
%61 = sub nsw i32 %58, %60
%62 = sitofp i32 %61 to double
%63 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i32 0, i32 0
%64 = load double, double* %63
%65 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i32 0, i32 0
%66 = load double, double* %65
%67 = fsub double %64, %66
%68 = fdiv double %67, 1.000000e+06
%69 = fadd double %62, %68
%70 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), double %69)
%71 = load i32*, i32** %8
%72 = call i32 @free(i32* %71)
%73 = load i32, i32* @EXIT_SUCCESS
ret i32 %73
}

LLVM IR (-O2):
%struct.timeval = type { double, i32 }
@.str = [14 x i8] c"%s elementos\0A\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [5 x i8] c"%lf\0A\00"
@EXIT_SUCCESS = global i32 0
@str = [24 x i8] c"Erro ao alocar mem\C3\B3ria\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.timeval
%4 = alloca %struct.timeval
%5 = bitcast %struct.timeval* %3 to i8*
%6 = bitcast %struct.timeval* %4 to i8*
%7 = icmp eq i32 %0, 2
br i1 %7, label %13, label %8
8: ; preds = %2
%9 = load i8*, i8** %1
%10 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* %9)
%11 = load i32, i32* @EXIT_FAILURE
%12 = tail call i32 @exit(i32 %11)
unreachable
13: ; preds = %2
%14 = getelementptr inbounds i8*, i8** %1, i64 1
%15 = load i8*, i8** %14
%16 = tail call i64 @atoi(i8* %15)
%17 = trunc i64 %16 to i32
%18 = shl i32 %17, 2
%19 = tail call i64 @malloc(i32 %18)
%20 = inttoptr i64 %19 to i32*
%21 = icmp eq i64 %19, 0
br i1 %21, label %22, label %26
22: ; preds = %13
%23 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str, i64 0, i64 0))
%24 = load i32, i32* @EXIT_FAILURE
%25 = tail call i32 @exit(i32 %24)
unreachable
26: ; preds = %13
%27 = tail call i32 @time(i32* null)
%28 = tail call i32 @srand(i32 %27)
%29 = icmp eq i64 %16, 0
br i1 %29, label %37, label %30
30: ; preds = %26, %30
%31 = phi i64 [ %35, %30 ], [ 0, %26 ]
%32 = tail call i32 (...) @random()
%33 = srem i32 %32, 10000
%34 = getelementptr inbounds i32, i32* %20, i64 %31
store i32 %33, i32* %34
%35 = add nuw i64 %31, 1
%36 = icmp eq i64 %35, %16
br i1 %36, label %37, label %30
37: ; preds = %30, %26
%38 = call i32 @gettimeofday(%struct.timeval* %3, i32* null)
%39 = tail call i32 @heap_sort(i32* %20, i64 %16)
%40 = call i32 @gettimeofday(%struct.timeval* %4, i32* null)
%41 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 1
%42 = load i32, i32* %41
%43 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 1
%44 = load i32, i32* %43
%45 = sub nsw i32 %42, %44
%46 = sitofp i32 %45 to double
%47 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 0
%48 = load double, double* %47
%49 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 0
%50 = load double, double* %49
%51 = fsub double %48, %50
%52 = fdiv double %51, 1.000000e+06
%53 = fadd double %52, %46
%54 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), double %53)
%55 = tail call i32 @free(i32* %20)
%56 = load i32, i32* @EXIT_SUCCESS
ret i32 %56
}