#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 */
unsigned char *array_index(unsigned char *, size_t, size_t);
int /*<<< orphan*/ memswap(unsigned char *, unsigned char *, size_t);
size_t partition(unsigned char *base, size_t element_size, size_t num_elements,
                 size_t pivot_index,
                 int (*compare)(const void *, const void *, void *),
                 void *arg) {
  if (pivot_index != num_elements - 1) {
    unsigned char *pivot = array_index(base, element_size, pivot_index);
    unsigned char *other = array_index(base, element_size, num_elements - 1);
    memswap(pivot, other, element_size);
    pivot_index = num_elements - 1;
  }
  size_t store_index = 0;
  for (size_t i = 0; i < num_elements - 1; i++) {
    unsigned char *pivot = array_index(base, element_size, pivot_index);
    unsigned char *value = array_index(base, element_size, i);
    if (compare(value, pivot, arg) <= 0) {
      unsigned char *other = array_index(base, element_size, store_index);
      if (value != other)
        memswap(value, other, element_size);
      store_index++;
    }
  }
  unsigned char *pivot = array_index(base, element_size, pivot_index);
  unsigned char *value = array_index(base, element_size, store_index);
  memswap(pivot, value, element_size);
  return store_index;
}


x86 assembly (clang, -O0):
<partition>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    %r9,-0x30(%rbp)
mov    -0x20(%rbp),%rax
mov    -0x18(%rbp),%rcx
sub    $0x1,%rcx
cmp    %rcx,%rax
je     11a3 <partition+0x83>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <array_index@plt>
mov    %rax,-0x38(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
sub    $0x1,%rdx
call   1030 <array_index@plt>
mov    %rax,-0x40(%rbp)
mov    -0x38(%rbp),%rdi
mov    -0x40(%rbp),%rsi
mov    -0x10(%rbp),%rdx
call   1040 <memswap@plt>
mov    -0x18(%rbp),%rax
sub    $0x1,%rax
mov    %rax,-0x20(%rbp)
movq   $0x0,-0x48(%rbp)
movq   $0x0,-0x50(%rbp)
mov    -0x50(%rbp),%rax
mov    -0x18(%rbp),%rcx
sub    $0x1,%rcx
cmp    %rcx,%rax
jae    1263 <partition+0x143>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <array_index@plt>
mov    %rax,-0x58(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x50(%rbp),%rdx
call   1030 <array_index@plt>
mov    %rax,-0x60(%rbp)
mov    -0x28(%rbp),%rax
mov    -0x60(%rbp),%rdi
mov    -0x58(%rbp),%rsi
mov    -0x30(%rbp),%rdx
call   *%rax
cmp    $0x0,%eax
jg     124d <partition+0x12d>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x48(%rbp),%rdx
call   1030 <array_index@plt>
mov    %rax,-0x68(%rbp)
mov    -0x60(%rbp),%rax
cmp    -0x68(%rbp),%rax
je     1241 <partition+0x121>
mov    -0x60(%rbp),%rdi
mov    -0x68(%rbp),%rsi
mov    -0x10(%rbp),%rdx
call   1040 <memswap@plt>
mov    -0x48(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x48(%rbp)
jmp    1252 <partition+0x132>
mov    -0x50(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x50(%rbp)
jmp    11b3 <partition+0x93>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <array_index@plt>
mov    %rax,-0x70(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x48(%rbp),%rdx
call   1030 <array_index@plt>
mov    %rax,-0x78(%rbp)
mov    -0x70(%rbp),%rdi
mov    -0x78(%rbp),%rsi
mov    -0x10(%rbp),%rdx
call   1040 <memswap@plt>
mov    -0x48(%rbp),%rax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<partition>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %r9,0x30(%rsp)
mov    %r8,0x28(%rsp)
mov    %rdx,%rbp
mov    %rsi,%rbx
mov    %rdi,%r14
add    $0xffffffffffffffff,%rbp
cmp    %rcx,%rbp
je     117a <partition+0x5a>
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %rcx,%rdx
call   1030 <array_index@plt>
mov    %rax,%r15
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %rbp,%rdx
call   1030 <array_index@plt>
mov    %r15,%rdi
mov    %rax,%rsi
mov    %rbx,%rdx
call   1040 <memswap@plt>
mov    %rbp,%rcx
mov    %rbx,0x18(%rsp)
mov    %r14,0x20(%rsp)
xor    %eax,%eax
test   %rbp,%rbp
je     122f <partition+0x10f>
mov    %rax,0x8(%rsp)
xor    %r12d,%r12d
mov    %rbp,0x10(%rsp)
mov    0x18(%rsp),%r15
jmp    11ca <partition+0xaa>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
add    $0x1,%rbx
mov    %rbx,0x8(%rsp)
mov    0x10(%rsp),%rbp
mov    %r14,%rcx
add    $0x1,%r12
cmp    %r12,%rbp
je     1234 <partition+0x114>
mov    0x20(%rsp),%rbx
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %rcx,%r14
mov    %rcx,%rdx
call   1030 <array_index@plt>
mov    %rax,%r13
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
call   1030 <array_index@plt>
mov    %rax,%rbp
mov    %rax,%rdi
mov    %r13,%rsi
mov    0x30(%rsp),%rdx
call   *0x28(%rsp)
test   %eax,%eax
jg     11b9 <partition+0x99>
mov    %rbx,%rdi
mov    %r15,%rsi
mov    0x8(%rsp),%rbx
mov    %rbx,%rdx
call   1030 <array_index@plt>
cmp    %rax,%rbp
je     11b0 <partition+0x90>
mov    %rbp,%rdi
mov    %rax,%rsi
mov    %r15,%rdx
call   1040 <memswap@plt>
jmp    11b0 <partition+0x90>
mov    %rax,0x8(%rsp)
mov    0x20(%rsp),%rbx
mov    %rbx,%rdi
mov    0x18(%rsp),%rbp
mov    %rbp,%rsi
mov    %rcx,%rdx
call   1030 <array_index@plt>
mov    %rax,%r14
mov    %rbx,%rdi
mov    %rbp,%rsi
mov    0x8(%rsp),%rbx
mov    %rbx,%rdx
call   1030 <array_index@plt>
mov    %r14,%rdi
mov    %rax,%rsi
mov    %rbp,%rdx
call   1040 <memswap@plt>
mov    %rbx,%rax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i64 @partition(i8* %0, i64 %1, i64 %2, i64 %3, i32 (i8*, i8*, i8*)* %4, i8* %5) {
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i64
%10 = alloca i64
%11 = alloca i32 (i8*, i8*, i8*)*
%12 = alloca i8*
%13 = alloca i8*
%14 = alloca i8*
%15 = alloca i64
%16 = alloca i64
%17 = alloca i8*
%18 = alloca i8*
%19 = alloca i8*
%20 = alloca i8*
%21 = alloca i8*
store i8* %0, i8** %7
store i64 %1, i64* %8
store i64 %2, i64* %9
store i64 %3, i64* %10
store i32 (i8*, i8*, i8*)* %4, i32 (i8*, i8*, i8*)** %11
store i8* %5, i8** %12
%22 = load i64, i64* %10
%23 = load i64, i64* %9
%24 = sub i64 %23, 1
%25 = icmp ne i64 %22, %24
br i1 %25, label %26, label %42
26: ; preds = %6
%27 = load i8*, i8** %7
%28 = load i64, i64* %8
%29 = load i64, i64* %10
%30 = call i8* @array_index(i8* %27, i64 %28, i64 %29)
store i8* %30, i8** %13
%31 = load i8*, i8** %7
%32 = load i64, i64* %8
%33 = load i64, i64* %9
%34 = sub i64 %33, 1
%35 = call i8* @array_index(i8* %31, i64 %32, i64 %34)
store i8* %35, i8** %14
%36 = load i8*, i8** %13
%37 = load i8*, i8** %14
%38 = load i64, i64* %8
%39 = call i32 @memswap(i8* %36, i8* %37, i64 %38)
%40 = load i64, i64* %9
%41 = sub i64 %40, 1
store i64 %41, i64* %10
br label %42
42: ; preds = %26, %6
store i64 0, i64* %15
store i64 0, i64* %16
br label %43
43: ; preds = %80, %42
%44 = load i64, i64* %16
%45 = load i64, i64* %9
%46 = sub i64 %45, 1
%47 = icmp ult i64 %44, %46
br i1 %47, label %48, label %83
48: ; preds = %43
%49 = load i8*, i8** %7
%50 = load i64, i64* %8
%51 = load i64, i64* %10
%52 = call i8* @array_index(i8* %49, i64 %50, i64 %51)
store i8* %52, i8** %17
%53 = load i8*, i8** %7
%54 = load i64, i64* %8
%55 = load i64, i64* %16
%56 = call i8* @array_index(i8* %53, i64 %54, i64 %55)
store i8* %56, i8** %18
%57 = load i32 (i8*, i8*, i8*)*, i32 (i8*, i8*, i8*)** %11
%58 = load i8*, i8** %18
%59 = load i8*, i8** %17
%60 = load i8*, i8** %12
%61 = call i32 %57(i8* %58, i8* %59, i8* %60)
%62 = icmp sle i32 %61, 0
br i1 %62, label %63, label %79
63: ; preds = %48
%64 = load i8*, i8** %7
%65 = load i64, i64* %8
%66 = load i64, i64* %15
%67 = call i8* @array_index(i8* %64, i64 %65, i64 %66)
store i8* %67, i8** %19
%68 = load i8*, i8** %18
%69 = load i8*, i8** %19
%70 = icmp ne i8* %68, %69
br i1 %70, label %71, label %76
71: ; preds = %63
%72 = load i8*, i8** %18
%73 = load i8*, i8** %19
%74 = load i64, i64* %8
%75 = call i32 @memswap(i8* %72, i8* %73, i64 %74)
br label %76
76: ; preds = %71, %63
%77 = load i64, i64* %15
%78 = add i64 %77, 1
store i64 %78, i64* %15
br label %79
79: ; preds = %76, %48
br label %80
80: ; preds = %79
%81 = load i64, i64* %16
%82 = add i64 %81, 1
store i64 %82, i64* %16
br label %43
83: ; preds = %43
%84 = load i8*, i8** %7
%85 = load i64, i64* %8
%86 = load i64, i64* %10
%87 = call i8* @array_index(i8* %84, i64 %85, i64 %86)
store i8* %87, i8** %20
%88 = load i8*, i8** %7
%89 = load i64, i64* %8
%90 = load i64, i64* %15
%91 = call i8* @array_index(i8* %88, i64 %89, i64 %90)
store i8* %91, i8** %21
%92 = load i8*, i8** %20
%93 = load i8*, i8** %21
%94 = load i64, i64* %8
%95 = call i32 @memswap(i8* %92, i8* %93, i64 %94)
%96 = load i64, i64* %15
ret i64 %96
}

LLVM IR (-O2):
define i64 @partition(i8* %0, i64 %1, i64 %2, i64 %3, i32 (i8*, i8*, i8*)* nocapture %4, i8* %5) {
%7 = add i64 %2, -1
%8 = icmp eq i64 %7, %3
br i1 %8, label %13, label %9
9: ; preds = %6
%10 = tail call i8* @array_index(i8* %0, i64 %1, i64 %3)
%11 = tail call i8* @array_index(i8* %0, i64 %1, i64 %7)
%12 = tail call i32 @memswap(i8* %10, i8* %11, i64 %1)
br label %13
13: ; preds = %9, %6
%14 = phi i64 [ %7, %9 ], [ %3, %6 ]
%15 = icmp eq i64 %7, 0
br i1 %15, label %16, label %21
16: ; preds = %35, %13
%17 = phi i64 [ 0, %13 ], [ %36, %35 ]
%18 = tail call i8* @array_index(i8* %0, i64 %1, i64 %14)
%19 = tail call i8* @array_index(i8* %0, i64 %1, i64 %17)
%20 = tail call i32 @memswap(i8* %18, i8* %19, i64 %1)
ret i64 %17
21: ; preds = %13, %35
%22 = phi i64 [ %36, %35 ], [ 0, %13 ]
%23 = phi i64 [ %37, %35 ], [ 0, %13 ]
%24 = tail call i8* @array_index(i8* %0, i64 %1, i64 %14)
%25 = tail call i8* @array_index(i8* %0, i64 %1, i64 %23)
%26 = tail call i32 %4(i8* %25, i8* %24, i8* %5)
%27 = icmp slt i32 %26, 1
br i1 %27, label %28, label %35
28: ; preds = %21
%29 = tail call i8* @array_index(i8* %0, i64 %1, i64 %22)
%30 = icmp eq i8* %25, %29
br i1 %30, label %33, label %31
31: ; preds = %28
%32 = tail call i32 @memswap(i8* %25, i8* %29, i64 %1)
br label %33
33: ; preds = %31, %28
%34 = add i64 %22, 1
br label %35
35: ; preds = %33, %21
%36 = phi i64 [ %34, %33 ], [ %22, %21 ]
%37 = add nuw i64 %23, 1
%38 = icmp eq i64 %37, %7
br i1 %38, label %16, label %21
}