#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*/ inchi_swap(char *, char *, size_t);
int stub1(void const *, void const *);
int insertions_sort(void *base, size_t num, size_t width,
                    int (*compare)(const void *e1, const void *e2)) {
  char *i, *j, *pk = (char *)base;
  int num_trans = 0;
  size_t k;
  for (k = 1; k < num; k++, pk += width) {
    for (i = j = pk + width;
         j > (char *)base && (i -= width, (*compare)(i, j)) > 0; j = i) {
      inchi_swap(i, j, width);
      num_trans++;
    }
  }
  return num_trans;
}


x86 assembly (clang, -O0):
<insertions_sort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x38(%rbp)
movl   $0x0,-0x3c(%rbp)
movq   $0x1,-0x48(%rbp)
mov    -0x48(%rbp),%rax
cmp    -0x10(%rbp),%rax
jae    11f4 <insertions_sort+0xe4>
mov    -0x38(%rbp),%rax
add    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    %rax,-0x28(%rbp)
mov    -0x30(%rbp),%rcx
xor    %eax,%eax
cmp    -0x8(%rbp),%rcx
mov    %al,-0x49(%rbp)
jbe    119b <insertions_sort+0x8b>
mov    -0x18(%rbp),%rdx
mov    -0x28(%rbp),%rax
xor    %ecx,%ecx
sub    %rdx,%rcx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   *%rax
cmp    $0x0,%eax
setg   %al
mov    %al,-0x49(%rbp)
mov    -0x49(%rbp),%al
test   $0x1,%al
jne    11ab <insertions_sort+0x9b>
jmp    11d2 <insertions_sort+0xc2>
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <inchi_swap@plt>
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x30(%rbp)
jmp    115d <insertions_sort+0x4d>
jmp    11d7 <insertions_sort+0xc7>
mov    -0x48(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x48(%rbp)
mov    -0x18(%rbp),%rax
add    -0x38(%rbp),%rax
mov    %rax,-0x38(%rbp)
jmp    113f <insertions_sort+0x2f>
mov    -0x3c(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<insertions_sort>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rcx,0x20(%rsp)
xor    %ebp,%ebp
mov    %rsi,0x8(%rsp)
cmp    $0x2,%rsi
jb     11b1 <insertions_sort+0xa1>
mov    %rdx,%r13
mov    %rdi,%r12
mov    %rdx,%r14
neg    %r14
xor    %ebp,%ebp
mov    $0x1,%eax
mov    %rdi,%rbx
jmp    1165 <insertions_sort+0x55>
nopl   0x0(%rax)
mov    0x18(%rsp),%rax
add    $0x1,%rax
cmp    0x8(%rsp),%rax
mov    0x10(%rsp),%rbx
je     11b1 <insertions_sort+0xa1>
lea    (%rbx,%r13,1),%rcx
cmp    %r12,%rcx
mov    %rax,0x18(%rsp)
mov    %rcx,0x10(%rsp)
jbe    1150 <insertions_sort+0x40>
nopl   0x0(%rax,%rax,1)
lea    (%rbx,%r13,1),%r15
mov    %rbx,%rdi
mov    %r15,%rsi
call   *0x20(%rsp)
test   %eax,%eax
jle    1150 <insertions_sort+0x40>
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r13,%rdx
call   1030 <inchi_swap@plt>
add    $0x1,%ebp
add    %r14,%rbx
lea    (%rbx,%r13,1),%rax
cmp    %r12,%rax
ja     1180 <insertions_sort+0x70>
jmp    1150 <insertions_sort+0x40>
mov    %ebp,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @insertions_sort(i8* %0, i64 %1, i64 %2, i32 (i8*, i8*)* %3) {
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
%8 = alloca i32 (i8*, i8*)*
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca i32
%13 = alloca i64
store i8* %0, i8** %5
store i64 %1, i64* %6
store i64 %2, i64* %7
store i32 (i8*, i8*)* %3, i32 (i8*, i8*)** %8
%14 = load i8*, i8** %5
store i8* %14, i8** %11
store i32 0, i32* %12
store i64 1, i64* %13
br label %15
15: ; preds = %49, %4
%16 = load i64, i64* %13
%17 = load i64, i64* %6
%18 = icmp ult i64 %16, %17
br i1 %18, label %19, label %55
19: ; preds = %15
%20 = load i8*, i8** %11
%21 = load i64, i64* %7
%22 = getelementptr inbounds i8, i8* %20, i64 %21
store i8* %22, i8** %10
store i8* %22, i8** %9
br label %23
23: ; preds = %46, %19
%24 = load i8*, i8** %10
%25 = load i8*, i8** %5
%26 = icmp ugt i8* %24, %25
br i1 %26, label %27, label %37
27: ; preds = %23
%28 = load i64, i64* %7
%29 = load i8*, i8** %9
%30 = sub i64 0, %28
%31 = getelementptr inbounds i8, i8* %29, i64 %30
store i8* %31, i8** %9
%32 = load i32 (i8*, i8*)*, i32 (i8*, i8*)** %8
%33 = load i8*, i8** %9
%34 = load i8*, i8** %10
%35 = call i32 %32(i8* %33, i8* %34)
%36 = icmp sgt i32 %35, 0
br label %37
37: ; preds = %27, %23
%38 = phi i1 [ false, %23 ], [ %36, %27 ]
br i1 %38, label %39, label %48
39: ; preds = %37
%40 = load i8*, i8** %9
%41 = load i8*, i8** %10
%42 = load i64, i64* %7
%43 = call i32 @inchi_swap(i8* %40, i8* %41, i64 %42)
%44 = load i32, i32* %12
%45 = add nsw i32 %44, 1
store i32 %45, i32* %12
br label %46
46: ; preds = %39
%47 = load i8*, i8** %9
store i8* %47, i8** %10
br label %23
48: ; preds = %37
br label %49
49: ; preds = %48
%50 = load i64, i64* %13
%51 = add i64 %50, 1
store i64 %51, i64* %13
%52 = load i64, i64* %7
%53 = load i8*, i8** %11
%54 = getelementptr inbounds i8, i8* %53, i64 %52
store i8* %54, i8** %11
br label %15
55: ; preds = %15
%56 = load i32, i32* %12
ret i32 %56
}

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