#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 stub1(void *, void *);
int /*<<< orphan*/ swap(void **, size_t, size_t);
void insertion_sort(void **base, size_t n, int (*comp)(void *, void *)) {
  size_t i, j;
  for (i = 1; i < n; i++)
    for (j = i; j > 0; j--)
      if ((*comp)(base[j - 1], base[j]) > 0)
        swap(base, j - 1, j);
}


x86 assembly (clang, -O0):
<insertion_sort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x1,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x10(%rbp),%rax
jae    11b9 <insertion_sort+0xa9>
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jbe    11a3 <insertion_sort+0x93>
mov    -0x18(%rbp),%rax
mov    -0x8(%rbp),%rcx
mov    -0x28(%rbp),%rdx
sub    $0x1,%rdx
mov    (%rcx,%rdx,8),%rdi
mov    -0x8(%rbp),%rcx
mov    -0x28(%rbp),%rdx
mov    (%rcx,%rdx,8),%rsi
call   *%rax
cmp    $0x0,%eax
jle    118d <insertion_sort+0x7d>
mov    -0x8(%rbp),%rdi
mov    -0x28(%rbp),%rsi
sub    $0x1,%rsi
mov    -0x28(%rbp),%rdx
call   1030 <swap@plt>
jmp    1192 <insertion_sort+0x82>
mov    -0x28(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x28(%rbp)
jmp    1142 <insertion_sort+0x32>
jmp    11a8 <insertion_sort+0x98>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    112c <insertion_sort+0x1c>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<insertion_sort>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
cmp    $0x2,%rsi
jb     117c <insertion_sort+0x6c>
mov    %rdx,%r15
mov    %rsi,%r14
mov    %rdi,%r12
mov    $0x1,%ebp
jmp    1149 <insertion_sort+0x39>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
add    $0x1,%rbp
cmp    %r14,%rbp
je     117c <insertion_sort+0x6c>
mov    %rbp,%rbx
jmp    1158 <insertion_sort+0x48>
xchg   %ax,%ax
mov    %r13,%rbx
test   %r13,%r13
je     1140 <insertion_sort+0x30>
lea    -0x1(%rbx),%r13
mov    -0x8(%r12,%rbx,8),%rdi
mov    (%r12,%rbx,8),%rsi
call   *%r15
test   %eax,%eax
jle    1150 <insertion_sort+0x40>
mov    %r12,%rdi
mov    %r13,%rsi
mov    %rbx,%rdx
call   1030 <swap@plt>
jmp    1150 <insertion_sort+0x40>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @insertion_sort(i8** %0, i64 %1, i32 (i8*, i8*)* %2) {
%4 = alloca i8**
%5 = alloca i64
%6 = alloca i32 (i8*, i8*)*
%7 = alloca i64
%8 = alloca i64
store i8** %0, i8*** %4
store i64 %1, i64* %5
store i32 (i8*, i8*)* %2, i32 (i8*, i8*)** %6
store i64 1, i64* %7
br label %9
9: ; preds = %42, %3
%10 = load i64, i64* %7
%11 = load i64, i64* %5
%12 = icmp ult i64 %10, %11
br i1 %12, label %13, label %45
13: ; preds = %9
%14 = load i64, i64* %7
store i64 %14, i64* %8
br label %15
15: ; preds = %38, %13
%16 = load i64, i64* %8
%17 = icmp ugt i64 %16, 0
br i1 %17, label %18, label %41
18: ; preds = %15
%19 = load i32 (i8*, i8*)*, i32 (i8*, i8*)** %6
%20 = load i8**, i8*** %4
%21 = load i64, i64* %8
%22 = sub i64 %21, 1
%23 = getelementptr inbounds i8*, i8** %20, i64 %22
%24 = load i8*, i8** %23
%25 = load i8**, i8*** %4
%26 = load i64, i64* %8
%27 = getelementptr inbounds i8*, i8** %25, i64 %26
%28 = load i8*, i8** %27
%29 = call i32 %19(i8* %24, i8* %28)
%30 = icmp sgt i32 %29, 0
br i1 %30, label %31, label %37
31: ; preds = %18
%32 = load i8**, i8*** %4
%33 = load i64, i64* %8
%34 = sub i64 %33, 1
%35 = load i64, i64* %8
%36 = call i32 @swap(i8** %32, i64 %34, i64 %35)
br label %37
37: ; preds = %31, %18
br label %38
38: ; preds = %37
%39 = load i64, i64* %8
%40 = add i64 %39, -1
store i64 %40, i64* %8
br label %15
41: ; preds = %15
br label %42
42: ; preds = %41
%43 = load i64, i64* %7
%44 = add i64 %43, 1
store i64 %44, i64* %7
br label %9
45: ; preds = %9
ret void
}

LLVM IR (-O2):
define void @insertion_sort(i8** %0, i64 %1, i32 (i8*, i8*)* nocapture %2) {
%4 = icmp ugt i64 %1, 1
br i1 %4, label %5, label %23
5: ; preds = %3, %20
%6 = phi i64 [ %21, %20 ], [ 1, %3 ]
br label %7
7: ; preds = %5, %18
%8 = phi i64 [ %9, %18 ], [ %6, %5 ]
%9 = add i64 %8, -1
%10 = getelementptr inbounds i8*, i8** %0, i64 %9
%11 = load i8*, i8** %10
%12 = getelementptr inbounds i8*, i8** %0, i64 %8
%13 = load i8*, i8** %12
%14 = tail call i32 %2(i8* %11, i8* %13)
%15 = icmp sgt i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %7
%17 = tail call i32 @swap(i8** %0, i64 %9, i64 %8)
br label %18
18: ; preds = %7, %16
%19 = icmp eq i64 %9, 0
br i1 %19, label %20, label %7
20: ; preds = %18
%21 = add nuw i64 %6, 1
%22 = icmp eq i64 %21, %1
br i1 %22, label %23, label %5
23: ; preds = %20, %3
ret void
}