#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*/ exchange(int *, int, int);
void selection_sort(int *array, int length) {
  for (int i = 0; i < length; i++) {
    int min = i;
    for (int j = i + 1; j < length; j++) {
      if (array[j] < array[min]) {
        min = j;
      }
      exchange(array, i, min);
    }
  }
}


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    119f <selection_sort+0x8f>
mov    -0x10(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    118c <selection_sort+0x7c>
mov    -0x8(%rbp),%rax
movslq -0x18(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
mov    -0x8(%rbp),%rcx
movslq -0x14(%rbp),%rdx
cmp    (%rcx,%rdx,4),%eax
jge    116f <selection_sort+0x5f>
mov    -0x18(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%esi
mov    -0x14(%rbp),%edx
call   1030 <exchange@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1141 <selection_sort+0x31>
jmp    1191 <selection_sort+0x81>
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
push   %rax
test   %esi,%esi
jle    119b <selection_sort+0x8b>
mov    %rdi,%rbx
mov    %esi,%r12d
xor    %eax,%eax
mov    $0x1,%r14d
mov    %esi,(%rsp)
jmp    114f <selection_sort+0x3f>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
add    $0x1,%r14
mov    (%rsp),%esi
mov    0x4(%rsp),%eax
cmp    %esi,%eax
je     119b <selection_sort+0x8b>
mov    %eax,%r15d
lea    0x1(%r15),%eax
mov    %eax,0x4(%rsp)
cmp    %esi,%eax
jge    1140 <selection_sort+0x30>
mov    %r14,%r13
mov    %r15d,%eax
jmp    1188 <selection_sort+0x78>
cs nopw 0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %r15d,%esi
mov    %ebp,%edx
call   1030 <exchange@plt>
add    $0x1,%r13
mov    %ebp,%eax
cmp    %r13,%r12
je     1140 <selection_sort+0x30>
mov    (%rbx,%r13,4),%ecx
movslq %eax,%rdx
mov    %r13d,%ebp
cmp    (%rbx,%rdx,4),%ecx
jl     1170 <selection_sort+0x60>
mov    %eax,%ebp
jmp    1170 <selection_sort+0x60>
add    $0x8,%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 0, i32* %5
br label %8
8: ; preds = %43, %2
%9 = load i32, i32* %5
%10 = load i32, i32* %4
%11 = icmp slt i32 %9, %10
br i1 %11, label %12, label %46
12: ; preds = %8
%13 = load i32, i32* %5
store i32 %13, i32* %6
%14 = load i32, i32* %5
%15 = add nsw i32 %14, 1
store i32 %15, i32* %7
br label %16
16: ; preds = %39, %12
%17 = load i32, i32* %7
%18 = load i32, i32* %4
%19 = icmp slt i32 %17, %18
br i1 %19, label %20, label %42
20: ; preds = %16
%21 = load i32*, i32** %3
%22 = load i32, i32* %7
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds i32, i32* %21, i64 %23
%25 = load i32, i32* %24
%26 = load i32*, i32** %3
%27 = load i32, i32* %6
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds i32, i32* %26, i64 %28
%30 = load i32, i32* %29
%31 = icmp slt i32 %25, %30
br i1 %31, label %32, label %34
32: ; preds = %20
%33 = load i32, i32* %7
store i32 %33, i32* %6
br label %34
34: ; preds = %32, %20
%35 = load i32*, i32** %3
%36 = load i32, i32* %5
%37 = load i32, i32* %6
%38 = call i32 @exchange(i32* %35, i32 %36, i32 %37)
br label %39
39: ; preds = %34
%40 = load i32, i32* %7
%41 = add nsw i32 %40, 1
store i32 %41, i32* %7
br label %16
42: ; preds = %16
br label %43
43: ; preds = %42
%44 = load i32, i32* %5
%45 = add nsw i32 %44, 1
store i32 %45, i32* %5
br label %8
46: ; preds = %8
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 %9
4: ; preds = %2
%5 = zext i32 %1 to i64
br label %10
6: ; preds = %15, %10
%7 = add nuw nsw i64 %11, 1
%8 = icmp eq i32 %13, %1
br i1 %8, label %9, label %10
9: ; preds = %6, %2
ret void
10: ; preds = %4, %6
%11 = phi i64 [ 1, %4 ], [ %7, %6 ]
%12 = phi i32 [ 0, %4 ], [ %13, %6 ]
%13 = add nuw nsw i32 %12, 1
%14 = icmp slt i32 %13, %1
br i1 %14, label %15, label %6
15: ; preds = %10, %15
%16 = phi i64 [ %27, %15 ], [ %11, %10 ]
%17 = phi i32 [ %25, %15 ], [ %12, %10 ]
%18 = getelementptr inbounds i32, i32* %0, i64 %16
%19 = load i32, i32* %18
%20 = sext i32 %17 to i64
%21 = getelementptr inbounds i32, i32* %0, i64 %20
%22 = load i32, i32* %21
%23 = icmp slt i32 %19, %22
%24 = trunc i64 %16 to i32
%25 = select i1 %23, i32 %24, i32 %17
%26 = tail call i32 @exchange(i32* %0, i32 %12, i32 %25)
%27 = add nuw nsw i64 %16, 1
%28 = icmp eq i64 %27, %5
br i1 %28, label %6, label %15
}