#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 select_min_key(int *, int, int);
void select_sort(int a[], int n) {
  int key, tmp;
  int i;
  for (i = 0; i < n; i++) {
    key = select_min_key(a, n, i);
    if (key != i) {
      tmp = a[i];
      a[i] = a[key];
      a[key] = tmp;
    }
  }
}


x86 assembly (clang, -O0):
<select_sort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    1195 <select_sort+0x85>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x18(%rbp),%edx
call   1030 <select_min_key@plt>
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0x18(%rbp),%eax
je     1182 <select_sort+0x72>
mov    -0x8(%rbp),%rax
movslq -0x18(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
mov    (%rax,%rcx,4),%edx
mov    -0x8(%rbp),%rax
movslq -0x18(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x14(%rbp),%edx
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
jmp    1187 <select_sort+0x77>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1126 <select_sort+0x16>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<select_sort>:
push   %r15
push   %r14
push   %rbx
test   %esi,%esi
jle    1161 <select_sort+0x51>
mov    %rdi,%r15
mov    %esi,%r14d
xor    %ebx,%ebx
jmp    1139 <select_sort+0x29>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
add    $0x1,%rbx
cmp    %rbx,%r14
je     1161 <select_sort+0x51>
mov    %r15,%rdi
mov    %r14d,%esi
mov    %ebx,%edx
call   1030 <select_min_key@plt>
mov    %eax,%ecx
cmp    %rcx,%rbx
je     1130 <select_sort+0x20>
mov    (%r15,%rbx,4),%ecx
cltq
mov    (%r15,%rax,4),%edx
mov    %edx,(%r15,%rbx,4)
mov    %ecx,(%r15,%rax,4)
jmp    1130 <select_sort+0x20>
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define void @select_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* %7
br label %8
8: ; preds = %41, %2
%9 = load i32, i32* %7
%10 = load i32, i32* %4
%11 = icmp slt i32 %9, %10
br i1 %11, label %12, label %44
12: ; preds = %8
%13 = load i32*, i32** %3
%14 = load i32, i32* %4
%15 = load i32, i32* %7
%16 = call i32 @select_min_key(i32* %13, i32 %14, i32 %15)
store i32 %16, i32* %5
%17 = load i32, i32* %5
%18 = load i32, i32* %7
%19 = icmp ne i32 %17, %18
br i1 %19, label %20, label %40
20: ; preds = %12
%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
store i32 %25, i32* %6
%26 = load i32*, i32** %3
%27 = load i32, i32* %5
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds i32, i32* %26, i64 %28
%30 = load i32, i32* %29
%31 = load i32*, i32** %3
%32 = load i32, i32* %7
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds i32, i32* %31, i64 %33
store i32 %30, i32* %34
%35 = load i32, i32* %6
%36 = load i32*, i32** %3
%37 = load i32, i32* %5
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds i32, i32* %36, i64 %38
store i32 %35, i32* %39
br label %40
40: ; preds = %20, %12
br label %41
41: ; preds = %40
%42 = load i32, i32* %7
%43 = add nsw i32 %42, 1
store i32 %43, i32* %7
br label %8
44: ; preds = %8
ret void
}

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