#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*/ exchange(int *, int, int);
void insertion_sort(int *array, int length) {
  for (int i = 1; i < length; i++) {
    for (int j = i; j > 0 && (array[j] < array[j - 1]); j--) {
      exchange(array, j, j - 1);
    }
  }
}


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    11ab <insertion_sort+0x9b>
mov    -0x10(%rbp),%eax
mov    %eax,-0x14(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x14(%rbp)
mov    %al,-0x15(%rbp)
jle    1168 <insertion_sort+0x58>
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    1178 <insertion_sort+0x68>
jmp    1198 <insertion_sort+0x88>
mov    -0x8(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x14(%rbp),%edx
sub    $0x1,%edx
call   1030 <exchange@plt>
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
jmp    1138 <insertion_sort+0x28>
jmp    119d <insertion_sort+0x8d>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    1126 <insertion_sort+0x16>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<insertion_sort>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
cmp    $0x2,%esi
jl     1164 <insertion_sort+0x54>
mov    %esi,%r14d
mov    %rdi,%r15
mov    $0x1,%r12d
jmp    1139 <insertion_sort+0x29>
nopl   0x0(%rax,%rax,1)
add    $0x1,%r12d
cmp    %r14d,%r12d
je     1164 <insertion_sort+0x54>
mov    %r12d,%ebx
nopl   0x0(%rax)
mov    %ebx,%eax
mov    (%r15,%rax,4),%eax
lea    -0x1(%rbx),%ebp
cmp    (%r15,%rbp,4),%eax
jge    1130 <insertion_sort+0x20>
mov    %r15,%rdi
mov    %ebx,%esi
mov    %ebp,%edx
call   1030 <exchange@plt>
cmp    $0x1,%ebx
mov    %ebp,%ebx
jg     1140 <insertion_sort+0x30>
jmp    1130 <insertion_sort+0x20>
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
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
store i32 1, i32* %5
br label %7
7: ; preds = %41, %2
%8 = load i32, i32* %5
%9 = load i32, i32* %4
%10 = icmp slt i32 %8, %9
br i1 %10, label %11, label %44
11: ; preds = %7
%12 = load i32, i32* %5
store i32 %12, i32* %6
br label %13
13: ; preds = %37, %11
%14 = load i32, i32* %6
%15 = icmp sgt i32 %14, 0
br i1 %15, label %16, label %29
16: ; preds = %13
%17 = load i32*, i32** %3
%18 = load i32, i32* %6
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds i32, i32* %17, i64 %19
%21 = load i32, i32* %20
%22 = load i32*, i32** %3
%23 = load i32, i32* %6
%24 = sub nsw i32 %23, 1
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i32, i32* %22, i64 %25
%27 = load i32, i32* %26
%28 = icmp slt i32 %21, %27
br label %29
29: ; preds = %16, %13
%30 = phi i1 [ false, %13 ], [ %28, %16 ]
br i1 %30, label %31, label %40
31: ; preds = %29
%32 = load i32*, i32** %3
%33 = load i32, i32* %6
%34 = load i32, i32* %6
%35 = sub nsw i32 %34, 1
%36 = call i32 @exchange(i32* %32, i32 %33, i32 %35)
br label %37
37: ; preds = %31
%38 = load i32, i32* %6
%39 = add nsw i32 %38, -1
store i32 %39, i32* %6
br label %13
40: ; preds = %29
br label %41
41: ; preds = %40
%42 = load i32, i32* %5
%43 = add nsw i32 %42, 1
store i32 %43, i32* %5
br label %7
44: ; preds = %7
ret void
}

LLVM IR (-O2):
define void @insertion_sort(i32* %0, i32 %1) {
%3 = icmp sgt i32 %1, 1
br i1 %3, label %4, label %6
4: ; preds = %2, %17
%5 = phi i32 [ %18, %17 ], [ 1, %2 ]
br label %7
6: ; preds = %17, %2
ret void
7: ; preds = %4, %20
%8 = phi i32 [ %5, %4 ], [ %12, %20 ]
%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 %20, label %17
17: ; preds = %20, %7
%18 = add nuw nsw i32 %5, 1
%19 = icmp eq i32 %18, %1
br i1 %19, label %6, label %4
20: ; preds = %7
%21 = tail call i32 @exchange(i32* %0, i32 %8, i32 %12)
%22 = icmp sgt i32 %8, 1
br i1 %22, label %7, label %17
}