#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*/ tty_putc(char);
int /*<<< orphan*/ tty_puts(char *);
void selection_sort(unsigned int *base, unsigned int n) {
  int i, min, j, x;
  tty_puts("Selection Sort\n");
  for (i = 0; i < n - 1; i++) {
    tty_putc('-');
    min = i;
    for (j = i + 1; j < n; j++) {
      if (base[j] < base[min])
        min = j;
    }
    if (min != i) {
      x = base[i];
      base[i] = base[min];
      base[min] = x;
    }
    tty_putc('+');
  }
}


x86 assembly (clang, -O0):
<selection_sort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
lea    0xeca(%rip),%rdi
call   1040 <tty_puts@plt>
movl   $0x0,-0x10(%rbp)
mov    -0x10(%rbp),%eax
mov    -0xc(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jae    1203 <selection_sort+0xe3>
mov    $0x2d,%edi
call   1030 <tty_putc@plt>
mov    -0x10(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0xc(%rbp),%eax
jae    11ad <selection_sort+0x8d>
mov    -0x8(%rbp),%rax
movslq -0x18(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
mov    -0x8(%rbp),%rcx
movslq -0x14(%rbp),%rdx
cmp    (%rcx,%rdx,4),%eax
jae    119a <selection_sort+0x7a>
mov    -0x18(%rbp),%eax
mov    %eax,-0x14(%rbp)
jmp    119f <selection_sort+0x7f>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    116c <selection_sort+0x4c>
mov    -0x14(%rbp),%eax
cmp    -0x10(%rbp),%eax
je     11eb <selection_sort+0xcb>
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,4),%edx
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x1c(%rbp),%edx
mov    -0x8(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    $0x2b,%edi
call   1030 <tty_putc@plt>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    1142 <selection_sort+0x22>
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
mov    %esi,%ebp
mov    %rdi,%rbx
lea    0xec9(%rip),%rdi
call   1040 <tty_puts@plt>
mov    %ebp,%eax
add    $0xffffffff,%eax
je     11dc <selection_sort+0xbc>
mov    %ebp,%ebp
mov    %eax,%r14d
mov    $0x1,%r15d
xor    %r12d,%r12d
jmp    1176 <selection_sort+0x56>
nopw   0x0(%rax,%rax,1)
mov    $0x2b,%edi
call   1030 <tty_putc@plt>
add    $0x1,%r15
mov    %r13,%r12
cmp    %r14,%r13
je     11dc <selection_sort+0xbc>
mov    $0x2d,%edi
call   1030 <tty_putc@plt>
lea    0x1(%r12),%r13
cmp    %rbp,%r13
jae    11c0 <selection_sort+0xa0>
mov    %r15,%rcx
mov    %r12d,%edx
jmp    11ab <selection_sort+0x8b>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
add    $0x1,%rcx
mov    %eax,%edx
cmp    %rcx,%rbp
je     11c3 <selection_sort+0xa3>
mov    (%rbx,%rcx,4),%esi
movslq %edx,%rdi
mov    %ecx,%eax
cmp    (%rbx,%rdi,4),%esi
jb     11a0 <selection_sort+0x80>
mov    %edx,%eax
jmp    11a0 <selection_sort+0x80>
nopl   0x0(%rax)
mov    %r12d,%eax
mov    %eax,%ecx
cmp    %rcx,%r12
je     1160 <selection_sort+0x40>
mov    (%rbx,%r12,4),%ecx
cltq
mov    (%rbx,%rax,4),%edx
mov    %edx,(%rbx,%r12,4)
mov    %ecx,(%rbx,%rax,4)
jmp    1160 <selection_sort+0x40>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [16 x i8] c"Selection Sort\0A\00"
define void @selection_sort(i32* %0, i32 %1) {
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32* %0, i32** %3
store i32 %1, i32* %4
%9 = call i32 @tty_puts(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %5
br label %10
10: ; preds = %68, %2
%11 = load i32, i32* %5
%12 = load i32, i32* %4
%13 = sub i32 %12, 1
%14 = icmp ult i32 %11, %13
br i1 %14, label %15, label %71
15: ; preds = %10
%16 = call i32 @tty_putc(i8 signext 45)
%17 = load i32, i32* %5
store i32 %17, i32* %6
%18 = load i32, i32* %5
%19 = add nsw i32 %18, 1
store i32 %19, i32* %7
br label %20
20: ; preds = %39, %15
%21 = load i32, i32* %7
%22 = load i32, i32* %4
%23 = icmp ult i32 %21, %22
br i1 %23, label %24, label %42
24: ; preds = %20
%25 = load i32*, i32** %3
%26 = load i32, i32* %7
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds i32, i32* %25, i64 %27
%29 = load i32, i32* %28
%30 = load i32*, i32** %3
%31 = load i32, i32* %6
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds i32, i32* %30, i64 %32
%34 = load i32, i32* %33
%35 = icmp ult i32 %29, %34
br i1 %35, label %36, label %38
36: ; preds = %24
%37 = load i32, i32* %7
store i32 %37, i32* %6
br label %38
38: ; preds = %36, %24
br label %39
39: ; preds = %38
%40 = load i32, i32* %7
%41 = add nsw i32 %40, 1
store i32 %41, i32* %7
br label %20
42: ; preds = %20
%43 = load i32, i32* %6
%44 = load i32, i32* %5
%45 = icmp ne i32 %43, %44
br i1 %45, label %46, label %66
46: ; preds = %42
%47 = load i32*, i32** %3
%48 = load i32, i32* %5
%49 = sext i32 %48 to i64
%50 = getelementptr inbounds i32, i32* %47, i64 %49
%51 = load i32, i32* %50
store i32 %51, i32* %8
%52 = load i32*, i32** %3
%53 = load i32, i32* %6
%54 = sext i32 %53 to i64
%55 = getelementptr inbounds i32, i32* %52, i64 %54
%56 = load i32, i32* %55
%57 = load i32*, i32** %3
%58 = load i32, i32* %5
%59 = sext i32 %58 to i64
%60 = getelementptr inbounds i32, i32* %57, i64 %59
store i32 %56, i32* %60
%61 = load i32, i32* %8
%62 = load i32*, i32** %3
%63 = load i32, i32* %6
%64 = sext i32 %63 to i64
%65 = getelementptr inbounds i32, i32* %62, i64 %64
store i32 %61, i32* %65
br label %66
66: ; preds = %46, %42
%67 = call i32 @tty_putc(i8 signext 43)
br label %68
68: ; preds = %66
%69 = load i32, i32* %5
%70 = add nsw i32 %69, 1
store i32 %70, i32* %5
br label %10
71: ; preds = %10
ret void
}

LLVM IR (-O2):
@.str = [16 x i8] c"Selection Sort\0A\00"
define void @selection_sort(i32* nocapture %0, i32 %1) {
%3 = tail call i32 @tty_puts(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%4 = add i32 %1, -1
%5 = icmp eq i32 %4, 0
br i1 %5, label %43, label %6
6: ; preds = %2
%7 = zext i32 %1 to i64
%8 = zext i32 %4 to i64
br label %9
9: ; preds = %6, %39
%10 = phi i64 [ 0, %6 ], [ %13, %39 ]
%11 = phi i64 [ 1, %6 ], [ %41, %39 ]
%12 = tail call i32 @tty_putc(i8 signext 45)
%13 = add nuw nsw i64 %10, 1
%14 = icmp ult i64 %13, %7
%15 = trunc i64 %10 to i32
br i1 %14, label %16, label %29
16: ; preds = %9, %16
%17 = phi i64 [ %27, %16 ], [ %11, %9 ]
%18 = phi i32 [ %26, %16 ], [ %15, %9 ]
%19 = getelementptr inbounds i32, i32* %0, i64 %17
%20 = load i32, i32* %19
%21 = sext i32 %18 to i64
%22 = getelementptr inbounds i32, i32* %0, i64 %21
%23 = load i32, i32* %22
%24 = icmp ult i32 %20, %23
%25 = trunc i64 %17 to i32
%26 = select i1 %24, i32 %25, i32 %18
%27 = add nuw nsw i64 %17, 1
%28 = icmp eq i64 %27, %7
br i1 %28, label %29, label %16
29: ; preds = %16, %9
%30 = phi i32 [ %15, %9 ], [ %26, %16 ]
%31 = zext i32 %30 to i64
%32 = icmp eq i64 %10, %31
br i1 %32, label %39, label %33
33: ; preds = %29
%34 = getelementptr inbounds i32, i32* %0, i64 %10
%35 = load i32, i32* %34
%36 = sext i32 %30 to i64
%37 = getelementptr inbounds i32, i32* %0, i64 %36
%38 = load i32, i32* %37
store i32 %38, i32* %34
store i32 %35, i32* %37
br label %39
39: ; preds = %33, %29
%40 = tail call i32 @tty_putc(i8 signext 43)
%41 = add nuw nsw i64 %11, 1
%42 = icmp eq i64 %13, %8
br i1 %42, label %43, label %9
43: ; preds = %39, %2
ret void
}