#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*/ PrintArray(int *, int, int);
int /*<<< orphan*/ SwapValue(int *, int, int);
int /*<<< orphan*/ printf(char *);
void SelectionSort(int a[], int n) {
  int i, j;
  printf("\n-----------START----------");
  PrintArray(a, n, -1);
  printf("\n-----------START----------\n");
  for (i = 0; i < n; i++) {
    for (j = i + 1; j < n; j++) {
      if (a[i] > a[j])
        SwapValue(a, i, j);
    }
    PrintArray(a, n, i);
  }
  printf("\n-----------END----------");
  PrintArray(a, n, n - 1);
  printf("\n-----------END----------\n");
}


x86 assembly (clang, -O0):
<SelectionSort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
lea    0xeba(%rip),%rdi
call   1040 <printf@plt>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    $0xffffffff,%edx
call   1050 <PrintArray@plt>
lea    0xeb9(%rip),%rdi
call   1040 <printf@plt>
movl   $0x0,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    11eb <SelectionSort+0xbb>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    11ce <SelectionSort+0x9e>
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
mov    -0x8(%rbp),%rcx
movslq -0x14(%rbp),%rdx
cmp    (%rcx,%rdx,4),%eax
jle    11bb <SelectionSort+0x8b>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%esi
mov    -0x14(%rbp),%edx
call   1030 <SwapValue@plt>
jmp    11c0 <SelectionSort+0x90>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1184 <SelectionSort+0x54>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%edx
call   1050 <PrintArray@plt>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    116f <SelectionSort+0x3f>
lea    0xe47(%rip),%rdi
call   1040 <printf@plt>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0xc(%rbp),%edx
sub    $0x1,%edx
call   1050 <PrintArray@plt>
lea    0xe43(%rip),%rdi
call   1040 <printf@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<SelectionSort>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %rdi,%r15
lea    0xea9(%rip),%rbx
mov    %rbx,%rdi
call   1050 <printf@plt>
mov    %r15,%rdi
mov    %ebp,%esi
mov    $0xffffffff,%edx
call   1060 <PrintArray@plt>
mov    %rbx,%rdi
call   1030 <puts@plt>
mov    %rbp,(%rsp)
test   %ebp,%ebp
jle    11e2 <SelectionSort+0xa2>
mov    (%rsp),%r13d
mov    $0x1,%ebp
xor    %r12d,%r12d
jmp    11ab <SelectionSort+0x6b>
nopl   0x0(%rax)
mov    %r15,%rdi
mov    (%rsp),%rsi
mov    %r12d,%edx
call   1060 <PrintArray@plt>
add    $0x1,%rbp
mov    %r14,%r12
cmp    %r13,%r14
je     11e2 <SelectionSort+0xa2>
lea    0x1(%r12),%r14
cmp    %r13,%r14
jae    1190 <SelectionSort+0x50>
mov    %rbp,%rbx
jmp    11c9 <SelectionSort+0x89>
nopw   0x0(%rax,%rax,1)
add    $0x1,%rbx
cmp    %rbx,%r13
je     1190 <SelectionSort+0x50>
mov    (%r15,%r12,4),%eax
cmp    (%r15,%rbx,4),%eax
jle    11c0 <SelectionSort+0x80>
mov    %r15,%rdi
mov    %r12d,%esi
mov    %ebx,%edx
call   1040 <SwapValue@plt>
jmp    11c0 <SelectionSort+0x80>
lea    0xe33(%rip),%rbx
mov    %rbx,%rdi
call   1050 <printf@plt>
mov    (%rsp),%rsi
lea    -0x1(%rsi),%edx
mov    %r15,%rdi
call   1060 <PrintArray@plt>
mov    %rbx,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <puts@plt>

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

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