#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 */
void *elem_select(void *, void *, size_t, int (*)(void *, void *));
int /*<<< orphan*/ swap(void *, void *, size_t);
void selection_sort(void *first, void *last, size_t elem_size,
                    int (*bin_pred)(void *, void *)) {
  void *elem;
  while (first != last) {
    elem = elem_select(first, last, elem_size, bin_pred);
    if (elem != first)
      swap(elem, first, elem_size);
    first = (char *)first + elem_size;
  }
}


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)
mov    %rcx,-0x20(%rbp)
mov    -0x8(%rbp),%rax
cmp    -0x10(%rbp),%rax
je     118f <selection_sort+0x6f>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
call   1030 <elem_select@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x8(%rbp),%rax
je     117e <selection_sort+0x5e>
mov    -0x28(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1040 <swap@plt>
mov    -0x8(%rbp),%rax
add    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1138 <selection_sort+0x18>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<selection_sort>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
cmp    %rsi,%rdi
je     116e <selection_sort+0x4e>
mov    %rcx,%r14
mov    %rdx,%r12
mov    %rsi,%r15
mov    %rdi,%rbx
jmp    1148 <selection_sort+0x28>
nopl   0x0(%rax,%rax,1)
add    %r12,%rbx
cmp    %r15,%rbx
je     116e <selection_sort+0x4e>
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
mov    %r14,%rcx
call   1030 <elem_select@plt>
cmp    %rax,%rbx
je     1140 <selection_sort+0x20>
mov    %rax,%rdi
mov    %rbx,%rsi
mov    %r12,%rdx
call   1040 <swap@plt>
jmp    1140 <selection_sort+0x20>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define void @selection_sort(i8* %0, i8* %1, i64 %2, i32 (i8*, i8*)* %3) {
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32 (i8*, i8*)*
%9 = alloca i8*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i64 %2, i64* %7
store i32 (i8*, i8*)* %3, i32 (i8*, i8*)** %8
br label %10
10: ; preds = %28, %4
%11 = load i8*, i8** %5
%12 = load i8*, i8** %6
%13 = icmp ne i8* %11, %12
br i1 %13, label %14, label %32
14: ; preds = %10
%15 = load i8*, i8** %5
%16 = load i8*, i8** %6
%17 = load i64, i64* %7
%18 = load i32 (i8*, i8*)*, i32 (i8*, i8*)** %8
%19 = call i8* @elem_select(i8* %15, i8* %16, i64 %17, i32 (i8*, i8*)* %18)
store i8* %19, i8** %9
%20 = load i8*, i8** %9
%21 = load i8*, i8** %5
%22 = icmp ne i8* %20, %21
br i1 %22, label %23, label %28
23: ; preds = %14
%24 = load i8*, i8** %9
%25 = load i8*, i8** %5
%26 = load i64, i64* %7
%27 = call i32 @swap(i8* %24, i8* %25, i64 %26)
br label %28
28: ; preds = %23, %14
%29 = load i8*, i8** %5
%30 = load i64, i64* %7
%31 = getelementptr inbounds i8, i8* %29, i64 %30
store i8* %31, i8** %5
br label %10
32: ; preds = %10
ret void
}

LLVM IR (-O2):
define void @selection_sort(i8* %0, i8* %1, i64 %2, i32 (i8*, i8*)* %3) {
%5 = icmp eq i8* %0, %1
br i1 %5, label %15, label %6
6: ; preds = %4, %12
%7 = phi i8* [ %13, %12 ], [ %0, %4 ]
%8 = tail call i8* @elem_select(i8* %7, i8* %1, i64 %2, i32 (i8*, i8*)* %3)
%9 = icmp eq i8* %8, %7
br i1 %9, label %12, label %10
10: ; preds = %6
%11 = tail call i32 @swap(i8* %8, i8* %7, i64 %2)
br label %12
12: ; preds = %10, %6
%13 = getelementptr inbounds i8, i8* %7, i64 %2
%14 = icmp eq i8* %13, %1
br i1 %14, label %15, label %6
15: ; preds = %12, %4
ret void
}