#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 */
scalar_t__ comp(void *, size_t, size_t,
                int (*)(void const *, void const *, void *), void *, size_t,
                size_t);
int /*<<< orphan*/ exch(void *, size_t, size_t, size_t, size_t);
void selection_sort(void *base, size_t nmemb, size_t size,
                    int (*compare)(const void *a, const void *b, void *context),
                    void *context) {
  size_t i;
  size_t j;
  size_t min;
  if (base == NULL) {
    return;
  }
  if (compare == NULL) {
    return;
  }
  for (i = 0; i < nmemb; i++) {
    min = i;
    for (j = i + 1; j < nmemb; j++) {
      if (comp(base, nmemb, size, compare, context, j, min) < 0) {
        min = j;
      }
    }
    exch(base, nmemb, size, i, min);
  }
}


x86 assembly (clang, -O0):
<selection_sort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    114c <selection_sort+0x2c>
jmp    120b <selection_sort+0xeb>
cmpq   $0x0,-0x20(%rbp)
jne    115c <selection_sort+0x3c>
jmp    120b <selection_sort+0xeb>
movq   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%rax
cmp    -0x10(%rbp),%rax
jae    120b <selection_sort+0xeb>
mov    -0x30(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
cmp    -0x10(%rbp),%rax
jae    11e1 <selection_sort+0xc1>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
mov    -0x28(%rbp),%r8
mov    -0x38(%rbp),%r9
mov    -0x40(%rbp),%rax
mov    %rax,(%rsp)
call   1040 <comp@plt>
cmp    $0x0,%rax
jge    11cb <selection_sort+0xab>
mov    -0x38(%rbp),%rax
mov    %rax,-0x40(%rbp)
jmp    11d0 <selection_sort+0xb0>
mov    -0x38(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x38(%rbp)
jmp    1186 <selection_sort+0x66>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x30(%rbp),%rcx
mov    -0x40(%rbp),%r8
call   1030 <exch@plt>
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
jmp    1164 <selection_sort+0x44>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<selection_sort>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %r8,0x30(%rsp)
mov    %rcx,0x18(%rsp)
test   %rdi,%rdi
je     11f4 <selection_sort+0xd4>
cmpq   $0x0,0x18(%rsp)
je     11f4 <selection_sort+0xd4>
test   %rsi,%rsi
je     11f4 <selection_sort+0xd4>
mov    %rdx,%r15
mov    %rdi,%r13
xor    %ebp,%ebp
mov    %rsi,0x10(%rsp)
mov    0x30(%rsp),%r12
mov    0x18(%rsp),%rbx
jmp    119a <selection_sort+0x7a>
nop
mov    %r13,%rdi
mov    0x10(%rsp),%rsi
mov    %r15,%rdx
mov    0x28(%rsp),%rcx
mov    %rbp,%r8
call   1030 <exch@plt>
mov    0x10(%rsp),%rsi
mov    0x20(%rsp),%rax
mov    %rax,%rbp
cmp    %rsi,%rax
je     11f4 <selection_sort+0xd4>
lea    0x1(%rbp),%rax
mov    %rbp,0x28(%rsp)
mov    %rax,0x20(%rsp)
cmp    %rsi,%rax
jae    1170 <selection_sort+0x50>
mov    0x28(%rsp),%rbp
mov    0x20(%rsp),%r14
nopw   0x0(%rax,%rax,1)
mov    %rbp,(%rsp)
mov    %r13,%rdi
mov    0x10(%rsp),%rsi
mov    %r15,%rdx
mov    %rbx,%rcx
mov    %r12,%r8
mov    %r14,%r9
call   1040 <comp@plt>
test   %rax,%rax
cmovs  %r14,%rbp
add    $0x1,%r14
cmp    0x10(%rsp),%r14
jb     11c0 <selection_sort+0xa0>
jmp    1170 <selection_sort+0x50>
add    $0x38,%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, i64 %2, i32 (i8*, i8*, i8*)* %3, i8* %4) {
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i32 (i8*, i8*, i8*)*
%10 = alloca i8*
%11 = alloca i64
%12 = alloca i64
%13 = alloca i64
store i8* %0, i8** %6
store i64 %1, i64* %7
store i64 %2, i64* %8
store i32 (i8*, i8*, i8*)* %3, i32 (i8*, i8*, i8*)** %9
store i8* %4, i8** %10
%14 = load i8*, i8** %6
%15 = icmp eq i8* %14, null
br i1 %15, label %16, label %17
16: ; preds = %5
br label %60
17: ; preds = %5
%18 = load i32 (i8*, i8*, i8*)*, i32 (i8*, i8*, i8*)** %9
%19 = icmp eq i32 (i8*, i8*, i8*)* %18, null
br i1 %19, label %20, label %21
20: ; preds = %17
br label %60
21: ; preds = %17
store i64 0, i64* %11
br label %22
22: ; preds = %57, %21
%23 = load i64, i64* %11
%24 = load i64, i64* %7
%25 = icmp ult i64 %23, %24
br i1 %25, label %26, label %60
26: ; preds = %22
%27 = load i64, i64* %11
store i64 %27, i64* %13
%28 = load i64, i64* %11
%29 = add i64 %28, 1
store i64 %29, i64* %12
br label %30
30: ; preds = %47, %26
%31 = load i64, i64* %12
%32 = load i64, i64* %7
%33 = icmp ult i64 %31, %32
br i1 %33, label %34, label %50
34: ; preds = %30
%35 = load i8*, i8** %6
%36 = load i64, i64* %7
%37 = load i64, i64* %8
%38 = load i32 (i8*, i8*, i8*)*, i32 (i8*, i8*, i8*)** %9
%39 = load i8*, i8** %10
%40 = load i64, i64* %12
%41 = load i64, i64* %13
%42 = call i64 @comp(i8* %35, i64 %36, i64 %37, i32 (i8*, i8*, i8*)* %38, i8* %39, i64 %40, i64 %41)
%43 = icmp slt i64 %42, 0
br i1 %43, label %44, label %46
44: ; preds = %34
%45 = load i64, i64* %12
store i64 %45, i64* %13
br label %46
46: ; preds = %44, %34
br label %47
47: ; preds = %46
%48 = load i64, i64* %12
%49 = add i64 %48, 1
store i64 %49, i64* %12
br label %30
50: ; preds = %30
%51 = load i8*, i8** %6
%52 = load i64, i64* %7
%53 = load i64, i64* %8
%54 = load i64, i64* %11
%55 = load i64, i64* %13
%56 = call i32 @exch(i8* %51, i64 %52, i64 %53, i64 %54, i64 %55)
br label %57
57: ; preds = %50
%58 = load i64, i64* %11
%59 = add i64 %58, 1
store i64 %59, i64* %11
br label %22
60: ; preds = %16, %20, %22
ret void
}

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