void insertion_sort(int arr[], int n) {
  printf("%s : %d\n", __FUNCTION__, n);
  int i, j;
  for (i = 1; i < n; i++) {
    j = i;
    while ((j > 0) && (arr[j] < arr[j - 1])) {
      swap(&arr[j], &arr[j - 1]);
      j--;
    }
  }
}


x86 assembly (clang, -O0):
<insertion_sort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0xc(%rbp),%edx
lea    0xec7(%rip),%rdi
lea    0xec9(%rip),%rsi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x1,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    11ea <insertion_sort+0xca>
mov    -0x10(%rbp),%eax
mov    %eax,-0x14(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x14(%rbp)
mov    %al,-0x15(%rbp)
jle    1190 <insertion_sort+0x70>
mov    -0x8(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
mov    -0x8(%rbp),%rcx
mov    -0x14(%rbp),%edx
sub    $0x1,%edx
movslq %edx,%rdx
cmp    (%rcx,%rdx,4),%eax
setl   %al
mov    %al,-0x15(%rbp)
mov    -0x15(%rbp),%al
test   $0x1,%al
jne    11a0 <insertion_sort+0x80>
jmp    11d7 <insertion_sort+0xb7>
mov    -0x8(%rbp),%rdi
movslq -0x14(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rdi
mov    -0x8(%rbp),%rsi
mov    -0x14(%rbp),%eax
sub    $0x1,%eax
cltq
shl    $0x2,%rax
add    %rax,%rsi
mov    $0x0,%al
call   1040 <swap@plt>
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
jmp    1160 <insertion_sort+0x40>
jmp    11dc <insertion_sort+0xbc>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    114e <insertion_sort+0x2e>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<insertion_sort>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %esi,%r14d
mov    %rdi,%rbx
lea    0xecb(%rip),%rdi
lea    0xecd(%rip),%rsi
mov    %r14d,%edx
xor    %eax,%eax
call   1030 <printf@plt>
cmp    $0x2,%r14d
jl     1196 <insertion_sort+0x76>
mov    $0x1,%r15d
mov    $0x2,%r12d
jmp    116d <insertion_sort+0x4d>
nopw   0x0(%rax,%rax,1)
add    $0x1,%r15d
add    $0x1,%r12d
cmp    %r14d,%r15d
je     1196 <insertion_sort+0x76>
mov    %r12d,%ebp
mov    %ebp,%eax
lea    -0x1(%rax),%ebp
mov    (%rbx,%rbp,4),%ecx
add    $0xfffffffe,%eax
cmp    (%rbx,%rax,4),%ecx
jge    1160 <insertion_sort+0x40>
lea    (%rbx,%rbp,4),%rdi
lea    (%rbx,%rax,4),%rsi
xor    %eax,%eax
call   1040 <swap@plt>
cmp    $0x1,%ebp
jg     1170 <insertion_sort+0x50>
jmp    1160 <insertion_sort+0x40>
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [9 x i8] c"%s : %d\0A\00"
@__FUNCTION__.insertion_sort = [15 x i8] c"insertion_sort\00"
define void @insertion_sort(i32* %0, i32 %1) {
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32* %0, i32** %3
store i32 %1, i32* %4
%7 = load i32, i32* %4
%8 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @__FUNCTION__.insertion_sort, i64 0, i64 0), i32 %7)
store i32 1, i32* %5
br label %9
9: ; preds = %47, %2
%10 = load i32, i32* %5
%11 = load i32, i32* %4
%12 = icmp slt i32 %10, %11
br i1 %12, label %13, label %50
13: ; preds = %9
%14 = load i32, i32* %5
store i32 %14, i32* %6
br label %15
15: ; preds = %33, %13
%16 = load i32, i32* %6
%17 = icmp sgt i32 %16, 0
br i1 %17, label %18, label %31
18: ; preds = %15
%19 = load i32*, i32** %3
%20 = load i32, i32* %6
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds i32, i32* %19, i64 %21
%23 = load i32, i32* %22
%24 = load i32*, i32** %3
%25 = load i32, i32* %6
%26 = sub nsw i32 %25, 1
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds i32, i32* %24, i64 %27
%29 = load i32, i32* %28
%30 = icmp slt i32 %23, %29
br label %31
31: ; preds = %18, %15
%32 = phi i1 [ false, %15 ], [ %30, %18 ]
br i1 %32, label %33, label %46
33: ; preds = %31
%34 = load i32*, i32** %3
%35 = load i32, i32* %6
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds i32, i32* %34, i64 %36
%38 = load i32*, i32** %3
%39 = load i32, i32* %6
%40 = sub nsw i32 %39, 1
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds i32, i32* %38, i64 %41
%43 = call i32 (i32*, i32*, ...) bitcast (i32 (...)* @swap to i32 (i32*, i32*, ...)*)(i32* %37, i32* %42)
%44 = load i32, i32* %6
%45 = add nsw i32 %44, -1
store i32 %45, i32* %6
br label %15
46: ; preds = %31
br label %47
47: ; preds = %46
%48 = load i32, i32* %5
%49 = add nsw i32 %48, 1
store i32 %49, i32* %5
br label %9
50: ; preds = %9
ret void
}

LLVM IR (-O2):
@.str = [9 x i8] c"%s : %d\0A\00"
@__FUNCTION__.insertion_sort = [15 x i8] c"insertion_sort\00"
define void @insertion_sort(i32* %0, i32 %1) {
%3 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @__FUNCTION__.insertion_sort, i64 0, i64 0), i32 %1)
%4 = icmp sgt i32 %1, 1
br i1 %4, label %5, label %23
5: ; preds = %2, %20
%6 = phi i32 [ %21, %20 ], [ 1, %2 ]
br label %7
7: ; preds = %5, %17
%8 = phi i32 [ %6, %5 ], [ %12, %17 ]
%9 = zext i32 %8 to i64
%10 = getelementptr inbounds i32, i32* %0, i64 %9
%11 = load i32, i32* %10
%12 = add nsw i32 %8, -1
%13 = zext i32 %12 to i64
%14 = getelementptr inbounds i32, i32* %0, i64 %13
%15 = load i32, i32* %14
%16 = icmp slt i32 %11, %15
br i1 %16, label %17, label %20
17: ; preds = %7
%18 = tail call i32 (i32*, i32*, ...) bitcast (i32 (...)* @swap to i32 (i32*, i32*, ...)*)(i32* %10, i32* %14)
%19 = icmp sgt i32 %8, 1
br i1 %19, label %7, label %20
20: ; preds = %7, %17
%21 = add nuw nsw i32 %6, 1
%22 = icmp eq i32 %21, %1
br i1 %22, label %23, label %5
23: ; preds = %20, %2
ret void
}