#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 *);
void selection_sort(int *n, int length) {
  int key, j;
  for (int i = 1; i < length; i++) {
    key = n[i];
    j = i - 1;
    while (key < n[j] && j >= 0) {
      swap(&n[j], &n[j + 1]);
      j--;
    }
  }
}


x86 assembly (clang, -O0):
<selection_sort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x1,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    11c4 <selection_sort+0xb4>
mov    -0x8(%rbp),%rax
movslq -0x18(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
mov    %eax,-0x10(%rbp)
mov    -0x18(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%ecx
mov    -0x8(%rbp),%rdx
movslq -0x14(%rbp),%rsi
xor    %eax,%eax
cmp    (%rdx,%rsi,4),%ecx
mov    %al,-0x19(%rbp)
jge    116c <selection_sort+0x5c>
cmpl   $0x0,-0x14(%rbp)
setge  %al
mov    %al,-0x19(%rbp)
mov    -0x19(%rbp),%al
test   $0x1,%al
jne    117c <selection_sort+0x6c>
jmp    11b1 <selection_sort+0xa1>
mov    -0x8(%rbp),%rdi
movslq -0x14(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rdi
mov    -0x8(%rbp),%rsi
mov    -0x14(%rbp),%eax
add    $0x1,%eax
cltq
shl    $0x2,%rax
add    %rax,%rsi
call   1030 <swap@plt>
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
jmp    1149 <selection_sort+0x39>
jmp    11b6 <selection_sort+0xa6>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1126 <selection_sort+0x16>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<selection_sort>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,0x8(%rsp)
cmp    $0x2,%esi
jl     11a4 <selection_sort+0x94>
mov    %rdi,%r15
mov    0x8(%rsp),%rax
add    $0xffffffff,%eax
mov    %rax,0x8(%rsp)
mov    $0x1,%ebp
xor    %r13d,%r13d
mov    %rdi,0x10(%rsp)
jmp    1165 <selection_sort+0x55>
nopw   0x0(%rax,%rax,1)
add    $0x1,%rbp
add    $0x1,%r13
addq   $0x4,0x10(%rsp)
cmp    0x8(%rsp),%r13
je     11a4 <selection_sort+0x94>
mov    (%r15,%rbp,4),%r14d
cmp    -0x4(%r15,%rbp,4),%r14d
jge    1150 <selection_sort+0x40>
mov    0x10(%rsp),%rbx
mov    %rbp,%r12
nopl   0x0(%rax,%rax,1)
mov    %r12d,%eax
lea    (%r15,%rax,4),%rsi
mov    %rbx,%rdi
call   1030 <swap@plt>
cmp    -0x4(%rbx),%r14d
jge    1150 <selection_sort+0x40>
add    $0xffffffffffffffff,%r12
add    $0xfffffffffffffffc,%rbx
test   %r12,%r12
jg     1180 <selection_sort+0x70>
jmp    1150 <selection_sort+0x40>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define void @selection_sort(i32* %0, i32 %1) {
%3 = icmp sgt i32 %1, 1
br i1 %3, label %4, label %7
4: ; preds = %2
%5 = add nsw i32 %1, -1
%6 = zext i32 %5 to i64
br label %8
7: ; preds = %30, %2
ret void
8: ; preds = %4, %30
%9 = phi i64 [ 1, %4 ], [ %31, %30 ]
%10 = phi i64 [ 0, %4 ], [ %32, %30 ]
%11 = getelementptr inbounds i32, i32* %0, i64 %9
%12 = load i32, i32* %11
%13 = add nsw i64 %9, -1
%14 = getelementptr inbounds i32, i32* %0, i64 %13
%15 = load i32, i32* %14
%16 = icmp slt i32 %12, %15
br i1 %16, label %17, label %30
17: ; preds = %8, %17
%18 = phi i64 [ %24, %17 ], [ %10, %8 ]
%19 = phi i32* [ %25, %17 ], [ %14, %8 ]
%20 = phi i64 [ %18, %17 ], [ %9, %8 ]
%21 = and i64 %20, 4294967295
%22 = getelementptr inbounds i32, i32* %0, i64 %21
%23 = tail call i32 @swap(i32* %19, i32* %22)
%24 = add nsw i64 %18, -1
%25 = getelementptr inbounds i32, i32* %0, i64 %24
%26 = load i32, i32* %25
%27 = icmp slt i32 %12, %26
%28 = icmp sgt i64 %18, 0
%29 = select i1 %27, i1 %28, i1 false
br i1 %29, label %17, label %30
30: ; preds = %17, %8
%31 = add nuw nsw i64 %9, 1
%32 = add nuw nsw i64 %10, 1
%33 = icmp eq i64 %32, %6
br i1 %33, label %7, label %8
}