#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 a[], int length) {
  for (int i = 0; i < length; i++) {
    for (int k = i + 1; k < length; k++) {
      if (a[i] > a[k]) {
        swap(&a[i], &a[k]);
      }
    }
  }
  return;
}


x86 assembly (clang, -O0):
<selection_sort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    11ac <selection_sort+0x9c>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    1199 <selection_sort+0x89>
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
mov    -0x8(%rbp),%rcx
movslq -0x14(%rbp),%rdx
cmp    (%rcx,%rdx,4),%eax
jle    1186 <selection_sort+0x76>
mov    -0x8(%rbp),%rdi
movslq -0x10(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rdi
mov    -0x8(%rbp),%rsi
movslq -0x14(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rsi
call   1030 <swap@plt>
jmp    118b <selection_sort+0x7b>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    113b <selection_sort+0x2b>
jmp    119e <selection_sort+0x8e>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%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    %rdi,0x8(%rsp)
test   %esi,%esi
jle    11b1 <selection_sort+0xa1>
mov    %esi,%ecx
mov    0x8(%rsp),%rax
lea    0x4(%rax),%r13
mov    $0x1,%ebp
xor    %r14d,%r14d
mov    %rcx,0x10(%rsp)
jmp    1162 <selection_sort+0x52>
cs nopw 0x0(%rax,%rax,1)
nop
add    $0x1,%rbp
add    $0x4,%r13
mov    0x10(%rsp),%rcx
cmp    %rcx,%r14
je     11b1 <selection_sort+0xa1>
mov    %r14,%rax
add    $0x1,%r14
cmp    %rcx,%r14
jae    1150 <selection_sort+0x40>
mov    0x8(%rsp),%rcx
lea    (%rcx,%rax,4),%r15
mov    0x10(%rsp),%r12
mov    %r13,%rbx
jmp    119d <selection_sort+0x8d>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
add    $0x4,%rbx
add    $0xffffffffffffffff,%r12
cmp    %r12,%rbp
je     1150 <selection_sort+0x40>
mov    (%r15),%eax
cmp    (%rbx),%eax
jle    1190 <selection_sort+0x80>
mov    %r15,%rdi
mov    %rbx,%rsi
call   1030 <swap@plt>
jmp    1190 <selection_sort+0x80>
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
store i32* %0, i32** %3
store i32 %1, i32* %4
store i32 0, i32* %5
br label %7
7: ; preds = %45, %2
%8 = load i32, i32* %5
%9 = load i32, i32* %4
%10 = icmp slt i32 %8, %9
br i1 %10, label %11, label %48
11: ; preds = %7
%12 = load i32, i32* %5
%13 = add nsw i32 %12, 1
store i32 %13, i32* %6
br label %14
14: ; preds = %41, %11
%15 = load i32, i32* %6
%16 = load i32, i32* %4
%17 = icmp slt i32 %15, %16
br i1 %17, label %18, label %44
18: ; preds = %14
%19 = load i32*, i32** %3
%20 = load i32, i32* %5
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds i32, i32* %19, i64 %21
%23 = load i32, i32* %22
%24 = load i32*, i32** %3
%25 = load i32, i32* %6
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds i32, i32* %24, i64 %26
%28 = load i32, i32* %27
%29 = icmp sgt i32 %23, %28
br i1 %29, label %30, label %40
30: ; preds = %18
%31 = load i32*, i32** %3
%32 = load i32, i32* %5
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds i32, i32* %31, i64 %33
%35 = load i32*, i32** %3
%36 = load i32, i32* %6
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds i32, i32* %35, i64 %37
%39 = call i32 @swap(i32* %34, i32* %38)
br label %40
40: ; preds = %30, %18
br label %41
41: ; preds = %40
%42 = load i32, i32* %6
%43 = add nsw i32 %42, 1
store i32 %43, i32* %6
br label %14
44: ; preds = %14
br label %45
45: ; preds = %44
%46 = load i32, i32* %5
%47 = add nsw i32 %46, 1
store i32 %47, i32* %5
br label %7
48: ; preds = %7
ret void
}

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