#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 *array_index(unsigned char *, size_t, size_t);
size_t partition(unsigned char *, size_t, size_t, size_t,
                 int (*)(void const *, void const *, void *), void *);
void qsort_r(void *base_ptr, size_t num_elements, size_t element_size,
             int (*compare)(const void *, const void *, void *), void *arg) {
  unsigned char *base = (unsigned char *)base_ptr;
  if (!element_size || num_elements < 2)
    return;
  size_t pivot_index = num_elements / 2;
  pivot_index =
      partition(base, element_size, num_elements, pivot_index, compare, arg);
  if (2 <= pivot_index)
    qsort_r(base, pivot_index, element_size, compare, arg);
  if (2 <= num_elements - (pivot_index + 1))
    qsort_r(array_index(base, element_size, pivot_index + 1),
            num_elements - (pivot_index + 1), element_size, compare, arg);
}


x86 assembly (clang, -O0):
<qsort_r>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     116a <qsort_r+0x3a>
cmpq   $0x2,-0x10(%rbp)
jae    116f <qsort_r+0x3f>
jmp    1219 <qsort_r+0xe9>
mov    -0x10(%rbp),%rax
shr    $0x1,%rax
mov    %rax,-0x38(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x10(%rbp),%rdx
mov    -0x38(%rbp),%rcx
mov    -0x20(%rbp),%r8
mov    -0x28(%rbp),%r9
call   1030 <partition@plt>
mov    %rax,-0x38(%rbp)
mov    $0x2,%eax
cmp    -0x38(%rbp),%rax
ja     11c4 <qsort_r+0x94>
mov    -0x30(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
mov    -0x28(%rbp),%r8
call   1050 <qsort_r@plt>
mov    -0x10(%rbp),%rcx
mov    -0x38(%rbp),%rax
add    $0x1,%rax
sub    %rax,%rcx
mov    $0x2,%eax
cmp    %rcx,%rax
ja     1219 <qsort_r+0xe9>
mov    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x38(%rbp),%rdx
add    $0x1,%rdx
call   1040 <array_index@plt>
mov    %rax,%rdi
mov    -0x10(%rbp),%rsi
mov    -0x38(%rbp),%rax
add    $0x1,%rax
sub    %rax,%rsi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
mov    -0x28(%rbp),%r8
call   1050 <qsort_r@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<qsort_r>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %rdx,%rdx
je     11b7 <qsort_r+0x87>
mov    %rsi,%rbx
cmp    $0x2,%rsi
jb     11b7 <qsort_r+0x87>
mov    %r8,%r14
mov    %rcx,%r15
mov    %rdx,%r12
mov    %rdi,%r13
cs nopw 0x0(%rax,%rax,1)
nop
mov    %rbx,%rcx
shr    %rcx
mov    %r13,%rdi
mov    %r12,%rsi
mov    %rbx,%rdx
mov    %r15,%r8
mov    %r14,%r9
call   1030 <partition@plt>
mov    %rax,%rbp
cmp    $0x2,%rax
jb     1197 <qsort_r+0x67>
mov    %r13,%rdi
mov    %rbp,%rsi
mov    %r12,%rdx
mov    %r15,%rcx
mov    %r14,%r8
call   1050 <qsort_r@plt>
add    $0x1,%rbp
sub    %rbp,%rbx
cmp    $0x2,%rbx
jb     11b7 <qsort_r+0x87>
mov    %r13,%rdi
mov    %r12,%rsi
mov    %rbp,%rdx
call   1040 <array_index@plt>
mov    %rax,%r13
jmp    1160 <qsort_r+0x30>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @qsort_r(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 i8*
%12 = 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
%13 = load i8*, i8** %6
store i8* %13, i8** %11
%14 = load i64, i64* %8
%15 = icmp ne i64 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %5
%17 = load i64, i64* %7
%18 = icmp ult i64 %17, 2
br i1 %18, label %19, label %20
19: ; preds = %16, %5
br label %57
20: ; preds = %16
%21 = load i64, i64* %7
%22 = udiv i64 %21, 2
store i64 %22, i64* %12
%23 = load i8*, i8** %11
%24 = load i64, i64* %8
%25 = load i64, i64* %7
%26 = load i64, i64* %12
%27 = load i32 (i8*, i8*, i8*)*, i32 (i8*, i8*, i8*)** %9
%28 = load i8*, i8** %10
%29 = call i64 @partition(i8* %23, i64 %24, i64 %25, i64 %26, i32 (i8*, i8*, i8*)* %27, i8* %28)
store i64 %29, i64* %12
%30 = load i64, i64* %12
%31 = icmp ule i64 2, %30
br i1 %31, label %32, label %38
32: ; preds = %20
%33 = load i8*, i8** %11
%34 = load i64, i64* %12
%35 = load i64, i64* %8
%36 = load i32 (i8*, i8*, i8*)*, i32 (i8*, i8*, i8*)** %9
%37 = load i8*, i8** %10
br label %38
38: ; preds = %32, %20
%39 = load i64, i64* %7
%40 = load i64, i64* %12
%41 = add i64 %40, 1
%42 = sub i64 %39, %41
%43 = icmp ule i64 2, %42
br i1 %43, label %44, label %57
44: ; preds = %38
%45 = load i8*, i8** %11
%46 = load i64, i64* %8
%47 = load i64, i64* %12
%48 = add i64 %47, 1
%49 = call i8* @array_index(i8* %45, i64 %46, i64 %48)
%50 = load i64, i64* %7
%51 = load i64, i64* %12
%52 = add i64 %51, 1
%53 = sub i64 %50, %52
%54 = load i64, i64* %8
%55 = load i32 (i8*, i8*, i8*)*, i32 (i8*, i8*, i8*)** %9
%56 = load i8*, i8** %10
br label %57
57: ; preds = %19, %44, %38
ret void
}

LLVM IR (-O2):
define void @qsort_r(i8* %0, i64 %1, i64 %2, i32 (i8*, i8*, i8*)* %3, i8* %4) {
%6 = icmp eq i64 %2, 0
%7 = icmp ult i64 %1, 2
%8 = or i1 %6, %7
br i1 %8, label %22, label %9
9: ; preds = %5, %20
%10 = phi i64 [ %18, %20 ], [ %1, %5 ]
%11 = phi i8* [ %21, %20 ], [ %0, %5 ]
%12 = lshr i64 %10, 1
%13 = tail call i64 @partition(i8* %11, i64 %2, i64 %10, i64 %12, i32 (i8*, i8*, i8*)* %3, i8* %4)
%14 = icmp ugt i64 %13, 1
br i1 %14, label %15, label %16
15: ; preds = %9
tail call void @qsort_r(i8* %11, i64 %13, i64 %2, i32 (i8*, i8*, i8*)* %3, i8* %4)
br label %16
16: ; preds = %15, %9
%17 = add i64 %13, 1
%18 = sub i64 %10, %17
%19 = icmp ugt i64 %18, 1
br i1 %19, label %20, label %22
20: ; preds = %16
%21 = tail call i8* @array_index(i8* %11, i64 %2, i64 %17)
br label %9
22: ; preds = %16, %5
ret void
}