void selection_sort(int arr[], int n) {
  printf("%s : %d\n", __FUNCTION__, n);
  int i, j;
  int min;
  for (i = 0; i < n; i++) {
    min = i;
    for (j = i + 1; j < n; j++)
      if (arr[j] < arr[min])
        min = j;
    swap(&arr[i], &arr[min]);
  }
}


x86 assembly (clang, -O0):
<selection_sort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0xc(%rbp),%edx
lea    0xec7(%rip),%rdi
lea    0xec9(%rip),%rsi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    11dd <selection_sort+0xbd>
mov    -0x10(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    11aa <selection_sort+0x8a>
mov    -0x8(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
mov    -0x8(%rbp),%rcx
movslq -0x18(%rbp),%rdx
cmp    (%rcx,%rdx,4),%eax
jge    1197 <selection_sort+0x77>
mov    -0x14(%rbp),%eax
mov    %eax,-0x18(%rbp)
jmp    119c <selection_sort+0x7c>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1169 <selection_sort+0x49>
mov    -0x8(%rbp),%rdi
movslq -0x10(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rdi
mov    -0x8(%rbp),%rsi
movslq -0x18(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rsi
mov    $0x0,%al
call   1040 <swap@plt>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    114e <selection_sort+0x2e>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<selection_sort>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %rdi,%rbx
lea    0xecd(%rip),%rdi
lea    0xecf(%rip),%rsi
mov    %ebp,%edx
xor    %eax,%eax
call   1030 <printf@plt>
test   %ebp,%ebp
jle    11bc <selection_sort+0x9c>
mov    %ebp,%ebp
mov    $0x1,%r14d
xor    %r8d,%r8d
jmp    1181 <selection_sort+0x61>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r8d,%edx
lea    (%rbx,%r8,4),%rdi
movslq %edx,%rax
lea    (%rbx,%rax,4),%rsi
xor    %eax,%eax
call   1040 <swap@plt>
add    $0x1,%r14
mov    %r15,%r8
cmp    %rbp,%r15
je     11bc <selection_sort+0x9c>
lea    0x1(%r8),%r15
cmp    %rbp,%r15
jae    1160 <selection_sort+0x40>
mov    %r14,%rcx
mov    %r8d,%esi
jmp    11ab <selection_sort+0x8b>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
add    $0x1,%rcx
mov    %edx,%esi
cmp    %rcx,%rbp
je     1163 <selection_sort+0x43>
mov    (%rbx,%rcx,4),%edi
movslq %esi,%rax
mov    %ecx,%edx
cmp    (%rbx,%rax,4),%edi
jl     11a0 <selection_sort+0x80>
mov    %esi,%edx
jmp    11a0 <selection_sort+0x80>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [9 x i8] c"%s : %d\0A\00"
@__FUNCTION__.selection_sort = [15 x i8] c"selection_sort\00"
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
%8 = load i32, i32* %4
%9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @__FUNCTION__.selection_sort, i64 0, i64 0), i32 %8)
store i32 0, i32* %5
br label %10
10: ; preds = %50, %2
%11 = load i32, i32* %5
%12 = load i32, i32* %4
%13 = icmp slt i32 %11, %12
br i1 %13, label %14, label %53
14: ; preds = %10
%15 = load i32, i32* %5
store i32 %15, i32* %7
%16 = load i32, i32* %5
%17 = add nsw i32 %16, 1
store i32 %17, i32* %6
br label %18
18: ; preds = %37, %14
%19 = load i32, i32* %6
%20 = load i32, i32* %4
%21 = icmp slt i32 %19, %20
br i1 %21, label %22, label %40
22: ; preds = %18
%23 = load i32*, i32** %3
%24 = load i32, i32* %6
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i32, i32* %23, i64 %25
%27 = load i32, i32* %26
%28 = load i32*, i32** %3
%29 = load i32, i32* %7
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i32, i32* %28, i64 %30
%32 = load i32, i32* %31
%33 = icmp slt i32 %27, %32
br i1 %33, label %34, label %36
34: ; preds = %22
%35 = load i32, i32* %6
store i32 %35, i32* %7
br label %36
36: ; preds = %34, %22
br label %37
37: ; preds = %36
%38 = load i32, i32* %6
%39 = add nsw i32 %38, 1
store i32 %39, i32* %6
br label %18
40: ; preds = %18
%41 = load i32*, i32** %3
%42 = load i32, i32* %5
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds i32, i32* %41, i64 %43
%45 = load i32*, i32** %3
%46 = load i32, i32* %7
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds i32, i32* %45, i64 %47
%49 = call i32 (i32*, i32*, ...) bitcast (i32 (...)* @swap to i32 (i32*, i32*, ...)*)(i32* %44, i32* %48)
br label %50
50: ; preds = %40
%51 = load i32, i32* %5
%52 = add nsw i32 %51, 1
store i32 %52, i32* %5
br label %10
53: ; preds = %10
ret void
}

LLVM IR (-O2):
@.str = [9 x i8] c"%s : %d\0A\00"
@__FUNCTION__.selection_sort = [15 x i8] c"selection_sort\00"
define void @selection_sort(i32* %0, i32 %1) {
%3 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @__FUNCTION__.selection_sort, i64 0, i64 0), i32 %1)
%4 = icmp sgt i32 %1, 0
br i1 %4, label %5, label %35
5: ; preds = %2
%6 = zext i32 %1 to i64
%7 = zext i32 %1 to i64
br label %8
8: ; preds = %5, %27
%9 = phi i64 [ 0, %5 ], [ %11, %27 ]
%10 = phi i64 [ 1, %5 ], [ %33, %27 ]
%11 = add nuw nsw i64 %9, 1
%12 = icmp ult i64 %11, %6
%13 = trunc i64 %9 to i32
br i1 %12, label %14, label %27
14: ; preds = %8, %14
%15 = phi i64 [ %25, %14 ], [ %10, %8 ]
%16 = phi i32 [ %24, %14 ], [ %13, %8 ]
%17 = getelementptr inbounds i32, i32* %0, i64 %15
%18 = load i32, i32* %17
%19 = sext i32 %16 to i64
%20 = getelementptr inbounds i32, i32* %0, i64 %19
%21 = load i32, i32* %20
%22 = icmp slt i32 %18, %21
%23 = trunc i64 %15 to i32
%24 = select i1 %22, i32 %23, i32 %16
%25 = add nuw nsw i64 %15, 1
%26 = icmp eq i64 %25, %7
br i1 %26, label %27, label %14
27: ; preds = %14, %8
%28 = phi i32 [ %13, %8 ], [ %24, %14 ]
%29 = getelementptr inbounds i32, i32* %0, i64 %9
%30 = sext i32 %28 to i64
%31 = getelementptr inbounds i32, i32* %0, i64 %30
%32 = tail call i32 (i32*, i32*, ...) bitcast (i32 (...)* @swap to i32 (i32*, i32*, ...)*)(i32* %29, i32* %31)
%33 = add nuw nsw i64 %10, 1
%34 = icmp eq i64 %11, %7
br i1 %34, label %35, label %8
35: ; preds = %27, %2
ret void
}