#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(int *, int *);
void selection_sort(int *v, int len) {
  if (len > 1) {
    int i;
    int max = len - 1;
    for (i = len - 2; i >= 0; i--) {
      if (v[i] > v[max]) {
        max = i;
      }
    }
    _swap(&v[len - 1], &v[max]);
    selection_sort(v, len - 1);
  }
}


x86 assembly (clang, -O0):
<selection_sort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
cmpl   $0x1,-0xc(%rbp)
jle    11c0 <selection_sort+0xa0>
mov    -0xc(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0xc(%rbp),%eax
sub    $0x2,%eax
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jl     118a <selection_sort+0x6a>
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    1177 <selection_sort+0x57>
mov    -0x10(%rbp),%eax
mov    %eax,-0x14(%rbp)
jmp    117c <selection_sort+0x5c>
mov    -0x10(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x10(%rbp)
jmp    114b <selection_sort+0x2b>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%eax
sub    $0x1,%eax
cltq
shl    $0x2,%rax
add    %rax,%rdi
mov    -0x8(%rbp),%rsi
movslq -0x14(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rsi
call   1040 <_swap@plt>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
sub    $0x1,%esi
call   1030 <selection_sort@plt>
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     11ae <selection_sort+0x9e>
mov    %esi,%r14d
mov    %rdi,%rbp
lea    -0x2(%r14),%eax
mov    %esi,%r15d
add    $0xffffffff,%r14d
lea    (%rdi,%rax,4),%r12
jmp    1172 <selection_sort+0x62>
nopl   0x0(%rax,%rax,1)
mov    %r13d,%eax
lea    0x0(,%rax,4),%rdi
add    %rbp,%rdi
movslq %edx,%rax
lea    0x0(,%rax,4),%rsi
add    %rbp,%rsi
call   1030 <_swap@plt>
add    $0xffffffff,%r14d
add    $0xfffffffffffffffc,%r12
cmp    $0x2,%r15
mov    %r13,%r15
jle    11ae <selection_sort+0x9e>
lea    -0x1(%r15),%r13
mov    %r12,%rax
mov    %r14d,%ecx
mov    %r13d,%esi
jmp    119a <selection_sort+0x8a>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
add    $0xfffffffffffffffc,%rax
mov    %edx,%esi
test   %ecx,%ecx
jle    1140 <selection_sort+0x30>
mov    (%rax),%edi
movslq %esi,%rbx
add    $0xffffffff,%ecx
mov    %ecx,%edx
cmp    0x0(%rbp,%rbx,4),%edi
jg     1190 <selection_sort+0x80>
mov    %esi,%edx
jmp    1190 <selection_sort+0x80>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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