void HeadSort(int *array, int length) {
  int i;
  for (i = (length - 2) / 2; i >= 0; i--) {
    ShiftAdjust(array, i, length - 1);
  }
  printf("\ninit the heap...\n");
  Print(array, length);
  for (int i = length - 1; i > 0; i--) {
    printf("\nafter swap (%d, %d)...\n", array[0], array[i]);
    Print(array, length);
    do {
      typeof(array[0]) __tmp = (array[0]);
      (array[0]) = (array[i]);
      (array[i]) = __tmp;
    } while (0);
    ShiftAdjust(array, 0, i - 1);
    Print(array, length);
  }
}


x86 assembly (clang, -O0):
<HeadSort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0xc(%rbp),%eax
sub    $0x2,%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jl     117c <HeadSort+0x4c>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%esi
mov    -0xc(%rbp),%edx
sub    $0x1,%edx
mov    $0x0,%al
call   1040 <ShiftAdjust@plt>
mov    -0x10(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x10(%rbp)
jmp    1150 <HeadSort+0x20>
lea    0xe7d(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    $0x0,%al
call   1050 <Print@plt>
mov    -0xc(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jle    122f <HeadSort+0xff>
mov    -0x8(%rbp),%rax
mov    (%rax),%esi
mov    -0x8(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,4),%edx
lea    0xe50(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    $0x0,%al
call   1050 <Print@plt>
mov    -0x8(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,4),%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x18(%rbp),%edx
mov    -0x8(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x8(%rbp),%rdi
mov    -0x14(%rbp),%edx
sub    $0x1,%edx
xor    %esi,%esi
mov    $0x0,%al
call   1040 <ShiftAdjust@plt>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    $0x0,%al
call   1050 <Print@plt>
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
jmp    11a1 <HeadSort+0x71>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<HeadSort>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%r14d
mov    %rdi,%r12
test   %esi,%esi
jle    1184 <HeadSort+0x44>
lea    -0x2(%r14),%eax
shr    $0x1f,%eax
lea    (%r14,%rax,1),%ebp
add    $0xfffffffe,%ebp
sar    %ebp
lea    -0x1(%r14),%r15d
nopl   0x0(%rax)
mov    %r12,%rdi
mov    %ebp,%esi
mov    %r15d,%edx
xor    %eax,%eax
call   1050 <ShiftAdjust@plt>
add    $0xffffffff,%ebp
jb     1170 <HeadSort+0x30>
lea    0xe8e(%rip),%rdi
call   1030 <puts@plt>
mov    %r12,%rdi
mov    %r14d,%esi
xor    %eax,%eax
call   1060 <Print@plt>
cmp    $0x2,%r14d
jl     121c <HeadSort+0xdc>
mov    %r14d,%ebx
lea    -0x2(%r14),%r13d
add    $0x1,%rbx
lea    0xe4b(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
nop
lea    -0x2(%rbx),%ebp
mov    (%r12),%esi
mov    (%r12,%rbp,4),%edx
mov    %r15,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %r12,%rdi
mov    %r14d,%esi
xor    %eax,%eax
call   1060 <Print@plt>
mov    (%r12),%eax
mov    (%r12,%rbp,4),%ecx
mov    %ecx,(%r12)
mov    %eax,(%r12,%rbp,4)
mov    %r12,%rdi
xor    %esi,%esi
mov    %r13d,%edx
xor    %eax,%eax
call   1050 <ShiftAdjust@plt>
mov    %r12,%rdi
mov    %r14d,%esi
xor    %eax,%eax
call   1060 <Print@plt>
add    $0xffffffff,%r13d
add    $0xffffffffffffffff,%rbx
cmp    $0x2,%rbx
ja     11c0 <HeadSort+0x80>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [19 x i8] c"\0Ainit the heap...\0A\00"
@.str.1 = [25 x i8] c"\0Aafter swap (%d, %d)...\0A\00"
define void @HeadSort(i32* %0, i32 %1) {
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i32* %0, i32** %3
store i32 %1, i32* %4
%8 = load i32, i32* %4
%9 = sub nsw i32 %8, 2
%10 = sdiv i32 %9, 2
store i32 %10, i32* %5
br label %11
11: ; preds = %20, %2
%12 = load i32, i32* %5
%13 = icmp sge i32 %12, 0
br i1 %13, label %14, label %23
14: ; preds = %11
%15 = load i32*, i32** %3
%16 = load i32, i32* %5
%17 = load i32, i32* %4
%18 = sub nsw i32 %17, 1
%19 = call i32 (i32*, i32, i32, ...) bitcast (i32 (...)* @ShiftAdjust to i32 (i32*, i32, i32, ...)*)(i32* %15, i32 %16, i32 %18)
br label %20
20: ; preds = %14
%21 = load i32, i32* %5
%22 = add nsw i32 %21, -1
store i32 %22, i32* %5
br label %11
23: ; preds = %11
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
%25 = load i32*, i32** %3
%26 = load i32, i32* %4
%27 = call i32 (i32*, i32, ...) bitcast (i32 (...)* @Print to i32 (i32*, i32, ...)*)(i32* %25, i32 %26)
%28 = load i32, i32* %4
%29 = sub nsw i32 %28, 1
store i32 %29, i32* %6
br label %30
30: ; preds = %70, %23
%31 = load i32, i32* %6
%32 = icmp sgt i32 %31, 0
br i1 %32, label %33, label %73
33: ; preds = %30
%34 = load i32*, i32** %3
%35 = getelementptr inbounds i32, i32* %34, i64 0
%36 = load i32, i32* %35
%37 = load i32*, i32** %3
%38 = load i32, i32* %6
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds i32, i32* %37, i64 %39
%41 = load i32, i32* %40
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i32 %36, i32 %41)
%43 = load i32*, i32** %3
%44 = load i32, i32* %4
%45 = call i32 (i32*, i32, ...) bitcast (i32 (...)* @Print to i32 (i32*, i32, ...)*)(i32* %43, i32 %44)
br label %46
46: ; preds = %33
%47 = load i32*, i32** %3
%48 = getelementptr inbounds i32, i32* %47, i64 0
%49 = load i32, i32* %48
store i32 %49, i32* %7
%50 = load i32*, i32** %3
%51 = load i32, i32* %6
%52 = sext i32 %51 to i64
%53 = getelementptr inbounds i32, i32* %50, i64 %52
%54 = load i32, i32* %53
%55 = load i32*, i32** %3
%56 = getelementptr inbounds i32, i32* %55, i64 0
store i32 %54, i32* %56
%57 = load i32, i32* %7
%58 = load i32*, i32** %3
%59 = load i32, i32* %6
%60 = sext i32 %59 to i64
%61 = getelementptr inbounds i32, i32* %58, i64 %60
store i32 %57, i32* %61
br label %62
62: ; preds = %46
%63 = load i32*, i32** %3
%64 = load i32, i32* %6
%65 = sub nsw i32 %64, 1
%66 = call i32 (i32*, i32, i32, ...) bitcast (i32 (...)* @ShiftAdjust to i32 (i32*, i32, i32, ...)*)(i32* %63, i32 0, i32 %65)
%67 = load i32*, i32** %3
%68 = load i32, i32* %4
%69 = call i32 (i32*, i32, ...) bitcast (i32 (...)* @Print to i32 (i32*, i32, ...)*)(i32* %67, i32 %68)
br label %70
70: ; preds = %62
%71 = load i32, i32* %6
%72 = add nsw i32 %71, -1
store i32 %72, i32* %6
br label %30
73: ; preds = %30
ret void
}

LLVM IR (-O2):
@.str.1 = [25 x i8] c"\0Aafter swap (%d, %d)...\0A\00"
@str = [18 x i8] c"\0Ainit the heap...\00"
define void @HeadSort(i32* %0, i32 %1) {
%3 = icmp sgt i32 %1, 0
br i1 %3, label %4, label %13
4: ; preds = %2
%5 = add nsw i32 %1, -2
%6 = sdiv i32 %5, 2
%7 = add nsw i32 %1, -1
br label %8
8: ; preds = %4, %8
%9 = phi i32 [ %6, %4 ], [ %11, %8 ]
%10 = tail call i32 (i32*, i32, i32, ...) bitcast (i32 (...)* @ShiftAdjust to i32 (i32*, i32, i32, ...)*)(i32* %0, i32 %9, i32 %7)
%11 = add nsw i32 %9, -1
%12 = icmp eq i32 %9, 0
br i1 %12, label %13, label %8
13: ; preds = %8, %2
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str, i64 0, i64 0))
%15 = tail call i32 (i32*, i32, ...) bitcast (i32 (...)* @Print to i32 (i32*, i32, ...)*)(i32* %0, i32 %1)
%16 = icmp sgt i32 %1, 1
br i1 %16, label %17, label %19
17: ; preds = %13
%18 = zext i32 %1 to i64
br label %20
19: ; preds = %20, %13
ret void
20: ; preds = %17, %20
%21 = phi i64 [ %18, %17 ], [ %23, %20 ]
%22 = trunc i64 %21 to i32
%23 = add nsw i64 %21, -1
%24 = load i32, i32* %0
%25 = and i64 %23, 4294967295
%26 = getelementptr inbounds i32, i32* %0, i64 %25
%27 = load i32, i32* %26
%28 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i32 %24, i32 %27)
%29 = tail call i32 (i32*, i32, ...) bitcast (i32 (...)* @Print to i32 (i32*, i32, ...)*)(i32* %0, i32 %1)
%30 = load i32, i32* %0
%31 = load i32, i32* %26
store i32 %31, i32* %0
store i32 %30, i32* %26
%32 = add nsw i32 %22, -2
%33 = tail call i32 (i32*, i32, i32, ...) bitcast (i32 (...)* @ShiftAdjust to i32 (i32*, i32, i32, ...)*)(i32* %0, i32 0, i32 %32)
%34 = tail call i32 (i32*, i32, ...) bitcast (i32 (...)* @Print to i32 (i32*, i32, ...)*)(i32* %0, i32 %1)
%35 = icmp ugt i64 %21, 2
br i1 %35, label %20, label %19
}