#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 stub1(void *, void *);
int /*<<< orphan*/ swap(void **, size_t, size_t);
void selection_sort(void **base, size_t n, int (*comp)(void *, void *)) {
  size_t i, j, k;
  for (i = 0; i < n - 1; i++) {
    k = i;
    for (j = i + 1; j < n; j++) {
      if ((*comp)(base[k], base[j]) > 0) {
        k = j;
      }
    }
    swap(base, k, i);
  }
}


x86 assembly (clang, -O0):
<selection_sort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    $0x1,%rcx
cmp    %rcx,%rax
jae    11ca <selection_sort+0xba>
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x10(%rbp),%rax
jae    11a8 <selection_sort+0x98>
mov    -0x18(%rbp),%rax
mov    -0x8(%rbp),%rcx
mov    -0x30(%rbp),%rdx
mov    (%rcx,%rdx,8),%rdi
mov    -0x8(%rbp),%rcx
mov    -0x28(%rbp),%rdx
mov    (%rcx,%rdx,8),%rsi
call   *%rax
cmp    $0x0,%eax
jle    1192 <selection_sort+0x82>
mov    -0x28(%rbp),%rax
mov    %rax,-0x30(%rbp)
jmp    1197 <selection_sort+0x87>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    1155 <selection_sort+0x45>
mov    -0x8(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <swap@plt>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    112c <selection_sort+0x1c>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<selection_sort>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%rax
add    $0xffffffffffffffff,%rax
mov    %rax,0x10(%rsp)
je     11a3 <selection_sort+0x93>
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%rbx
xor    %r13d,%r13d
jmp    115a <selection_sort+0x4a>
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %r13,%rdx
call   1030 <swap@plt>
mov    0x8(%rsp),%rax
mov    %rax,%r13
cmp    0x10(%rsp),%rax
je     11a3 <selection_sort+0x93>
lea    0x1(%r13),%rax
mov    %r13,%rsi
mov    %rax,0x8(%rsp)
cmp    %r15,%rax
jae    1140 <selection_sort+0x30>
mov    %r13,%r12
mov    0x8(%rsp),%rbp
jmp    118c <selection_sort+0x7c>
cs nopw 0x0(%rax,%rax,1)
nop
add    $0x1,%rbp
mov    %rsi,%r12
cmp    %r15,%rbp
jae    1140 <selection_sort+0x30>
mov    (%rbx,%r12,8),%rdi
mov    (%rbx,%rbp,8),%rsi
call   *%r14
mov    %rbp,%rsi
test   %eax,%eax
jg     1180 <selection_sort+0x70>
mov    %r12,%rsi
jmp    1180 <selection_sort+0x70>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @selection_sort(i8** %0, i64 %1, i32 (i8*, i8*)* %2) {
%4 = alloca i8**
%5 = alloca i64
%6 = alloca i32 (i8*, i8*)*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i64
store i8** %0, i8*** %4
store i64 %1, i64* %5
store i32 (i8*, i8*)* %2, i32 (i8*, i8*)** %6
store i64 0, i64* %7
br label %10
10: ; preds = %46, %3
%11 = load i64, i64* %7
%12 = load i64, i64* %5
%13 = sub i64 %12, 1
%14 = icmp ult i64 %11, %13
br i1 %14, label %15, label %49
15: ; preds = %10
%16 = load i64, i64* %7
store i64 %16, i64* %9
%17 = load i64, i64* %7
%18 = add i64 %17, 1
store i64 %18, i64* %8
br label %19
19: ; preds = %38, %15
%20 = load i64, i64* %8
%21 = load i64, i64* %5
%22 = icmp ult i64 %20, %21
br i1 %22, label %23, label %41
23: ; preds = %19
%24 = load i32 (i8*, i8*)*, i32 (i8*, i8*)** %6
%25 = load i8**, i8*** %4
%26 = load i64, i64* %9
%27 = getelementptr inbounds i8*, i8** %25, i64 %26
%28 = load i8*, i8** %27
%29 = load i8**, i8*** %4
%30 = load i64, i64* %8
%31 = getelementptr inbounds i8*, i8** %29, i64 %30
%32 = load i8*, i8** %31
%33 = call i32 %24(i8* %28, i8* %32)
%34 = icmp sgt i32 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %23
%36 = load i64, i64* %8
store i64 %36, i64* %9
br label %37
37: ; preds = %35, %23
br label %38
38: ; preds = %37
%39 = load i64, i64* %8
%40 = add i64 %39, 1
store i64 %40, i64* %8
br label %19
41: ; preds = %19
%42 = load i8**, i8*** %4
%43 = load i64, i64* %9
%44 = load i64, i64* %7
%45 = call i32 @swap(i8** %42, i64 %43, i64 %44)
br label %46
46: ; preds = %41
%47 = load i64, i64* %7
%48 = add i64 %47, 1
store i64 %48, i64* %7
br label %10
49: ; preds = %10
ret void
}

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