#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 insertSort(int arr[]) {
  int i, j, curr;
  int pass = 0, comp = 0, swap = 0;
  for (i = 1; i < 40; i++) {
    pass++;
    curr = arr[i];
    j = i;
    comp++;
    while ((j > 0) && (arr[j - 1] > curr)) {
      swap++;
      arr[j] = arr[j - 1];
      j = j - 1;
    }
    arr[j] = curr;
  }
  printf("\n\nInsertion Sort:\n");
  printf("Passes: %d\nComparisons: %d\nSwaps: %d\n", pass, comp, swap);
}


x86 assembly (clang, -O0):
<insertSort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
movl   $0x1,-0xc(%rbp)
cmpl   $0x28,-0xc(%rbp)
jge    11ee <insertSort+0xde>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
mov    %eax,-0x14(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x10(%rbp)
mov    %al,-0x21(%rbp)
jle    1190 <insertSort+0x80>
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,4),%eax
cmp    -0x14(%rbp),%eax
setg   %al
mov    %al,-0x21(%rbp)
mov    -0x21(%rbp),%al
test   $0x1,%al
jne    11a0 <insertSort+0x90>
jmp    11d2 <insertSort+0xc2>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,4),%edx
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x10(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    1168 <insertSort+0x58>
mov    -0x14(%rbp),%edx
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    1138 <insertSort+0x28>
lea    0xe0b(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x18(%rbp),%esi
mov    -0x1c(%rbp),%edx
mov    -0x20(%rbp),%ecx
lea    0xe07(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<insertSort>:
push   %r14
push   %rbx
push   %rax
xor    %r14d,%r14d
mov    $0x1,%r9d
xor    %r8d,%r8d
jmp    1153 <insertSort+0x33>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %esi,%r14d
movslq %ebx,%rax
mov    %edx,(%rdi,%rax,4)
add    $0x1,%r9
cmp    $0x27,%r8d
je     118e <insertSort+0x6e>
mov    %r14d,%esi
add    $0x1,%r8d
mov    (%rdi,%r9,4),%edx
lea    (%r9,%rsi,1),%r14d
mov    %r9,%rbx
cs nopw 0x0(%rax,%rax,1)
nop
lea    -0x1(%rbx),%rax
mov    %eax,%ecx
mov    (%rdi,%rcx,4),%ecx
cmp    %edx,%ecx
jle    1140 <insertSort+0x20>
add    $0x1,%esi
mov    %ecx,(%rdi,%rbx,4)
mov    %rax,%rbx
test   %eax,%eax
jne    1170 <insertSort+0x50>
xor    %ebx,%ebx
jmp    1143 <insertSort+0x23>
lea    0xe91(%rip),%rdi
call   1030 <puts@plt>
lea    0xe5f(%rip),%rdi
mov    $0x27,%esi
mov    $0x27,%edx
mov    %r14d,%ecx
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1040 <printf@plt>

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

LLVM IR (-O2):
@.str.1 = [38 x i8] c"Passes: %d\0AComparisons: %d\0ASwaps: %d\0A\00"
@str = [18 x i8] c"\0A\0AInsertion Sort:\00"
define void @insertSort(i32* nocapture %0) {
br label %2
2: ; preds = %1, %23
%3 = phi i64 [ 1, %1 ], [ %29, %23 ]
%4 = phi i32 [ 0, %1 ], [ %25, %23 ]
%5 = phi i32 [ 0, %1 ], [ %6, %23 ]
%6 = add nuw nsw i32 %5, 1
%7 = getelementptr inbounds i32, i32* %0, i64 %3
%8 = load i32, i32* %7
%9 = trunc i64 %3 to i32
%10 = add i32 %4, %9
br label %11
11: ; preds = %2, %19
%12 = phi i64 [ %3, %2 ], [ %14, %19 ]
%13 = phi i32 [ %4, %2 ], [ %20, %19 ]
%14 = add nsw i64 %12, -1
%15 = and i64 %14, 4294967295
%16 = getelementptr inbounds i32, i32* %0, i64 %15
%17 = load i32, i32* %16
%18 = icmp sgt i32 %17, %8
br i1 %18, label %19, label %23
19: ; preds = %11
%20 = add i32 %13, 1
%21 = getelementptr inbounds i32, i32* %0, i64 %12
store i32 %17, i32* %21
%22 = icmp eq i32 %20, %10
br i1 %22, label %23, label %11
23: ; preds = %19, %11
%24 = phi i64 [ 0, %19 ], [ %12, %11 ]
%25 = phi i32 [ %10, %19 ], [ %13, %11 ]
%26 = shl i64 %24, 32
%27 = ashr exact i64 %26, 32
%28 = getelementptr inbounds i32, i32* %0, i64 %27
store i32 %8, i32* %28
%29 = add nuw nsw i64 %3, 1
%30 = icmp eq i32 %6, 39
br i1 %30, label %31, label %2
31: ; preds = %23
%32 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str, i64 0, i64 0))
%33 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0), i32 39, i32 39, i32 %25)
ret void
}