#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 /*<<< orphan*/ printf(char *, ...);
void insertion_sort(int *a, int len) {
  int i, input;
  for (i = 1; i < len; i++) {
    int j = i - 1;
    printf("input %i\n", i);
    input = a[i];
    while (j >= 0 && a[j] > input) {
      printf("shift: %i -> %i\n", j, j + 1);
      a[j + 1] = a[j];
      j--;
    }
    if (j + 1 != i) {
      printf("move input to %i\n", j + 1);
      a[j + 1] = input;
    }
  }
  printf("done\n");
}


x86 assembly (clang, -O0):
<insertion_sort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x1,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    1216 <insertion_sort+0x106>
mov    -0x10(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%esi
lea    0xebb(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
mov    %eax,-0x14(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x18(%rbp)
mov    %al,-0x19(%rbp)
jl     117d <insertion_sort+0x6d>
mov    -0x8(%rbp),%rax
movslq -0x18(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
cmp    -0x14(%rbp),%eax
setg   %al
mov    %al,-0x19(%rbp)
mov    -0x19(%rbp),%al
test   $0x1,%al
jne    118d <insertion_sort+0x7d>
jmp    11cd <insertion_sort+0xbd>
mov    -0x18(%rbp),%esi
mov    -0x18(%rbp),%edx
add    $0x1,%edx
lea    0xe6d(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x8(%rbp),%rax
movslq -0x18(%rbp),%rcx
mov    (%rax,%rcx,4),%edx
mov    -0x8(%rbp),%rax
mov    -0x18(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x18(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x18(%rbp)
jmp    115a <insertion_sort+0x4a>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
cmp    -0x10(%rbp),%eax
je     1203 <insertion_sort+0xf3>
mov    -0x18(%rbp),%esi
add    $0x1,%esi
lea    0xe32(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x14(%rbp),%edx
mov    -0x8(%rbp),%rax
mov    -0x18(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
mov    %edx,(%rax,%rcx,4)
jmp    1208 <insertion_sort+0xf8>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    1126 <insertion_sort+0x16>
lea    0xe10(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<insertion_sort>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
cmp    $0x2,%esi
jl     1204 <insertion_sort+0xe4>
mov    %rdi,%rbx
mov    %esi,%eax
mov    %rax,(%rsp)
mov    $0x1,%r14d
xor    %ebp,%ebp
jmp    1167 <insertion_sort+0x47>
nopw   0x0(%rax,%rax,1)
add    $0x1,%r14
mov    0x8(%rsp),%rbp
add    $0x1,%rbp
cmp    (%rsp),%r14
je     1204 <insertion_sort+0xe4>
lea    0xe92(%rip),%rdi
mov    %r14d,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %r14,0x10(%rsp)
mov    (%rbx,%r14,4),%r12d
mov    %rbp,0x8(%rsp)
cs nopw 0x0(%rax,%rax,1)
mov    %ebp,%r14d
cmp    %r12d,(%rbx,%r14,4)
jle    11d0 <insertion_sort+0xb0>
lea    0x1(%rbp),%r15
xor    %r13d,%r13d
lea    0xe63(%rip),%rdi
mov    %ebp,%esi
mov    %r15d,%edx
xor    %eax,%eax
call   1040 <printf@plt>
mov    (%rbx,%r14,4),%eax
mov    %eax,0x4(%rbx,%rbp,4)
add    $0xffffffffffffffff,%rbp
cmp    $0x1,%r15
jg     1190 <insertion_sort+0x70>
jmp    11d6 <insertion_sort+0xb6>
nopw   0x0(%rax,%rax,1)
add    $0x1,%ebp
mov    %ebp,%r13d
mov    %r13d,%eax
mov    0x10(%rsp),%r14
cmp    %rax,%r14
je     1150 <insertion_sort+0x30>
lea    0xe2d(%rip),%rdi
mov    %r13d,%esi
xor    %eax,%eax
call   1040 <printf@plt>
movslq %r13d,%rax
mov    %r12d,(%rbx,%rax,4)
jmp    1150 <insertion_sort+0x30>
lea    0xe22(%rip),%rdi
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <puts@plt>

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

LLVM IR (-O2):
@.str = [10 x i8] c"input %i\0A\00"
@.str.1 = [17 x i8] c"shift: %i -> %i\0A\00"
@.str.2 = [18 x i8] c"move input to %i\0A\00"
@str = [5 x i8] c"done\00"
define void @insertion_sort(i32* nocapture %0, i32 %1) {
%3 = icmp sgt i32 %1, 1
br i1 %3, label %4, label %39
4: ; preds = %2
%5 = zext i32 %1 to i64
br label %6
6: ; preds = %4, %36
%7 = phi i64 [ 1, %4 ], [ %37, %36 ]
%8 = trunc i64 %7 to i32
%9 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %8)
%10 = getelementptr inbounds i32, i32* %0, i64 %7
%11 = load i32, i32* %10
br label %12
12: ; preds = %6, %20
%13 = phi i64 [ %7, %6 ], [ %14, %20 ]
%14 = add nsw i64 %13, -1
%15 = and i64 %14, 4294967295
%16 = getelementptr inbounds i32, i32* %0, i64 %15
%17 = load i32, i32* %16
%18 = icmp sgt i32 %17, %11
%19 = trunc i64 %13 to i32
br i1 %18, label %20, label %26
20: ; preds = %12
%21 = trunc i64 %14 to i32
%22 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i32 %21, i32 %19)
%23 = load i32, i32* %16
%24 = getelementptr inbounds i32, i32* %0, i64 %13
store i32 %23, i32* %24
%25 = icmp sgt i64 %13, 1
br i1 %25, label %12, label %28
26: ; preds = %12
%27 = trunc i64 %13 to i32
br label %28
28: ; preds = %20, %26
%29 = phi i32 [ %27, %26 ], [ 0, %20 ]
%30 = zext i32 %29 to i64
%31 = icmp eq i64 %7, %30
br i1 %31, label %36, label %32
32: ; preds = %28
%33 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i32 %29)
%34 = sext i32 %29 to i64
%35 = getelementptr inbounds i32, i32* %0, i64 %34
store i32 %11, i32* %35
br label %36
36: ; preds = %32, %28
%37 = add nuw nsw i64 %7, 1
%38 = icmp eq i64 %37, %5
br i1 %38, label %39, label %6
39: ; preds = %36, %2
%40 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @str, i64 0, i64 0))
ret void
}