#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(double *, int, int);
void selection_sort(double array[], int n) {
  int i, j;
  for (i = 0; i < n - 1; i++) {
    int min = i;
    for (j = i + 1; j < n; j++) {
      if (array[j] < array[min])
        min = j;
    }
    swap(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
mov    -0xc(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jge    11ac <selection_sort+0x9c>
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    118f <selection_sort+0x7f>
mov    -0x8(%rbp),%rax
movslq -0x14(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm1
mov    -0x8(%rbp),%rax
movslq -0x18(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
ucomisd %xmm1,%xmm0
jbe    117c <selection_sort+0x6c>
mov    -0x14(%rbp),%eax
mov    %eax,-0x18(%rbp)
jmp    1181 <selection_sort+0x71>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1146 <selection_sort+0x36>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%esi
mov    -0x18(%rbp),%edx
call   1030 <swap@plt>
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
cmp    $0x2,%esi
jl     1190 <selection_sort+0x80>
mov    %esi,%r14d
mov    %rdi,%rbx
lea    -0x1(%r14),%r15d
mov    %esi,%ebp
xor    %esi,%esi
mov    $0x1,%r12d
jmp    1154 <selection_sort+0x44>
cs nopw 0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1030 <swap@plt>
add    $0x1,%r12
mov    %r13d,%esi
cmp    %r15d,%r13d
je     1190 <selection_sort+0x80>
lea    0x1(%rsi),%r13d
mov    %esi,%edx
cmp    %r14d,%r13d
jge    1140 <selection_sort+0x30>
mov    %r12,%rax
mov    %esi,%ecx
jmp    117b <selection_sort+0x6b>
cs nopw 0x0(%rax,%rax,1)
add    $0x1,%rax
mov    %edx,%ecx
cmp    %rax,%rbp
je     1140 <selection_sort+0x30>
movslq %ecx,%rdx
movsd  (%rbx,%rdx,8),%xmm0
ucomisd (%rbx,%rax,8),%xmm0
mov    %eax,%edx
ja     1170 <selection_sort+0x60>
mov    %ecx,%edx
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(double* %0, i32 %1) {
%3 = alloca double*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store double* %0, double** %3
store i32 %1, i32* %4
store i32 0, i32* %5
br label %8
8: ; preds = %44, %2
%9 = load i32, i32* %5
%10 = load i32, i32* %4
%11 = sub nsw i32 %10, 1
%12 = icmp slt i32 %9, %11
br i1 %12, label %13, label %47
13: ; preds = %8
%14 = load i32, i32* %5
store i32 %14, i32* %7
%15 = load i32, i32* %5
%16 = add nsw i32 %15, 1
store i32 %16, i32* %6
br label %17
17: ; preds = %36, %13
%18 = load i32, i32* %6
%19 = load i32, i32* %4
%20 = icmp slt i32 %18, %19
br i1 %20, label %21, label %39
21: ; preds = %17
%22 = load double*, double** %3
%23 = load i32, i32* %6
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds double, double* %22, i64 %24
%26 = load double, double* %25
%27 = load double*, double** %3
%28 = load i32, i32* %7
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds double, double* %27, i64 %29
%31 = load double, double* %30
%32 = fcmp olt double %26, %31
br i1 %32, label %33, label %35
33: ; preds = %21
%34 = load i32, i32* %6
store i32 %34, i32* %7
br label %35
35: ; preds = %33, %21
br label %36
36: ; preds = %35
%37 = load i32, i32* %6
%38 = add nsw i32 %37, 1
store i32 %38, i32* %6
br label %17
39: ; preds = %17
%40 = load double*, double** %3
%41 = load i32, i32* %5
%42 = load i32, i32* %7
%43 = call i32 @swap(double* %40, i32 %41, i32 %42)
br label %44
44: ; preds = %39
%45 = load i32, i32* %5
%46 = add nsw i32 %45, 1
store i32 %46, i32* %5
br label %8
47: ; preds = %8
ret void
}

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