#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*/ swap(int *, int *);
int insertion_sort(int *elements, int size) {
  int iter = 0;
  int prev = 0;
  for (iter = 0; iter < size; iter++) {
    prev = iter;
    while (prev) {
      if (elements[prev] < elements[prev - 1]) {
        swap(&elements[prev], &elements[prev - 1]);
        prev--;
      } else {
        break;
      }
    }
  }
}


x86 assembly (clang, -O0):
<insertion_sort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11c3 <insertion_sort+0xb3>
mov    -0x18(%rbp),%eax
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     11b0 <insertion_sort+0xa0>
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
mov    -0x10(%rbp),%rcx
mov    -0x1c(%rbp),%edx
sub    $0x1,%edx
movslq %edx,%rdx
cmp    (%rcx,%rdx,4),%eax
jge    11a6 <insertion_sort+0x96>
mov    -0x10(%rbp),%rdi
movslq -0x1c(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rdi
mov    -0x10(%rbp),%rsi
mov    -0x1c(%rbp),%eax
sub    $0x1,%eax
cltq
shl    $0x2,%rax
add    %rax,%rsi
call   1030 <swap@plt>
mov    -0x1c(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x1c(%rbp)
jmp    11ab <insertion_sort+0x9b>
jmp    11b0 <insertion_sort+0xa0>
jmp    1146 <insertion_sort+0x36>
jmp    11b5 <insertion_sort+0xa5>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1134 <insertion_sort+0x24>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<insertion_sort>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %esi,%esi
jle    116d <insertion_sort+0x5d>
mov    %rdi,%r14
mov    %esi,%r15d
xor    %r12d,%r12d
xor    %r13d,%r13d
jmp    1141 <insertion_sort+0x31>
nopl   (%rax)
add    $0x1,%r13
add    $0x4,%r14
add    $0xffffffffffffffff,%r12
cmp    %r15,%r13
je     116d <insertion_sort+0x5d>
test   %r13d,%r13d
je     1130 <insertion_sort+0x20>
mov    %r12,%rbp
mov    %r14,%rdi
nopl   0x0(%rax)
mov    (%rdi),%eax
cmp    -0x4(%rdi),%eax
jge    1130 <insertion_sort+0x20>
lea    -0x4(%rdi),%rbx
mov    %rbx,%rsi
call   1030 <swap@plt>
mov    %rbx,%rdi
inc    %rbp
jne    1150 <insertion_sort+0x40>
jmp    1130 <insertion_sort+0x20>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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