#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 get_min_index(int *, int, int);
int /*<<< orphan*/ swap(int *, int *);
void selection_sort(int *elements, int size) {
  int curr = 0;
  int min_elem_index = 0;
  for (curr = 0; curr < size - 1; curr++) {
    min_elem_index = get_min_index(elements, curr, size);
    if (curr != min_elem_index) {
      swap(&elements[curr], &elements[min_elem_index]);
    }
  }
}


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)
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x10(%rbp)
mov    -0x10(%rbp),%eax
mov    -0xc(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jge    11a9 <selection_sort+0x89>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%esi
mov    -0xc(%rbp),%edx
call   1030 <get_min_index@plt>
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0x14(%rbp),%eax
je     1196 <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   1040 <swap@plt>
jmp    119b <selection_sort+0x7b>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    1144 <selection_sort+0x24>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<selection_sort>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
cmp    $0x2,%esi
jl     1171 <selection_sort+0x51>
mov    %esi,%r14d
mov    %rdi,%r15
lea    -0x1(%r14),%r13d
xor    %ebx,%ebx
mov    %rdi,%r12
jmp    114d <selection_sort+0x2d>
nop
add    $0x1,%rbx
add    $0x4,%r12
cmp    %rbx,%r13
je     1171 <selection_sort+0x51>
mov    %r15,%rdi
mov    %ebx,%esi
mov    %r14d,%edx
call   1030 <get_min_index@plt>
mov    %eax,%ecx
cmp    %rcx,%rbx
je     1140 <selection_sort+0x20>
cltq
lea    (%r15,%rax,4),%rsi
mov    %r12,%rdi
call   1040 <swap@plt>
jmp    1140 <selection_sort+0x20>
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
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
store i32 0, i32* %6
store i32 0, i32* %5
br label %7
7: ; preds = %31, %2
%8 = load i32, i32* %5
%9 = load i32, i32* %4
%10 = sub nsw i32 %9, 1
%11 = icmp slt i32 %8, %10
br i1 %11, label %12, label %34
12: ; preds = %7
%13 = load i32*, i32** %3
%14 = load i32, i32* %5
%15 = load i32, i32* %4
%16 = call i32 @get_min_index(i32* %13, i32 %14, i32 %15)
store i32 %16, i32* %6
%17 = load i32, i32* %5
%18 = load i32, i32* %6
%19 = icmp ne i32 %17, %18
br i1 %19, label %20, label %30
20: ; preds = %12
%21 = load i32*, i32** %3
%22 = load i32, i32* %5
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds i32, i32* %21, i64 %23
%25 = load i32*, i32** %3
%26 = load i32, i32* %6
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds i32, i32* %25, i64 %27
%29 = call i32 @swap(i32* %24, i32* %28)
br label %30
30: ; preds = %20, %12
br label %31
31: ; preds = %30
%32 = load i32, i32* %5
%33 = add nsw i32 %32, 1
store i32 %33, i32* %5
br label %7
34: ; preds = %7
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 %21
4: ; preds = %2
%5 = add nsw i32 %1, -1
%6 = zext i32 %5 to i64
br label %7
7: ; preds = %4, %18
%8 = phi i64 [ 0, %4 ], [ %19, %18 ]
%9 = trunc i64 %8 to i32
%10 = tail call i32 @get_min_index(i32* %0, i32 %9, i32 %1)
%11 = zext i32 %10 to i64
%12 = icmp eq i64 %8, %11
br i1 %12, label %18, label %13
13: ; preds = %7
%14 = getelementptr inbounds i32, i32* %0, i64 %8
%15 = sext i32 %10 to i64
%16 = getelementptr inbounds i32, i32* %0, i64 %15
%17 = tail call i32 @swap(i32* %14, i32* %16)
br label %18
18: ; preds = %7, %13
%19 = add nuw nsw i64 %8, 1
%20 = icmp eq i64 %19, %6
br i1 %20, label %21, label %7
21: ; preds = %18, %2
ret void
}