#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 find_pivot(void *, int, int, size_t);
int partition(void *, int, int, int, size_t);
void _qsort(void *base, int i, int j, size_t size) {
  int pivot_index, mid;
  pivot_index = find_pivot(base, i, j, size);
  if (pivot_index == -1)
    return;
  mid = partition(base, i, j, pivot_index, size);
  _qsort(base, i, mid - 1, size);
  _qsort(base, mid, j, size);
}


x86 assembly (clang, -O0):
<_qsort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %rcx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%edx
mov    -0x18(%rbp),%rcx
call   1040 <find_pivot@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    116b <_qsort+0x3b>
jmp    11ad <_qsort+0x7d>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%edx
mov    -0x1c(%rbp),%ecx
mov    -0x18(%rbp),%r8
call   1030 <partition@plt>
mov    %eax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x20(%rbp),%edx
sub    $0x1,%edx
mov    -0x18(%rbp),%rcx
call   1050 <_qsort@plt>
mov    -0x8(%rbp),%rdi
mov    -0x20(%rbp),%esi
mov    -0x10(%rbp),%edx
mov    -0x18(%rbp),%rcx
call   1050 <_qsort@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_qsort>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rcx,%r15
mov    %edx,%r14d
mov    %esi,%ebx
mov    %rdi,%r12
call   1040 <find_pivot@plt>
cmp    $0xffffffff,%eax
je     118b <_qsort+0x5b>
nopl   (%rax)
mov    %r12,%rdi
mov    %ebx,%esi
mov    %r14d,%edx
mov    %eax,%ecx
mov    %r15,%r8
call   1030 <partition@plt>
mov    %eax,%ebp
lea    -0x1(%rbp),%edx
mov    %r12,%rdi
mov    %ebx,%esi
mov    %r15,%rcx
call   1050 <_qsort@plt>
mov    %r12,%rdi
mov    %ebp,%esi
mov    %r14d,%edx
mov    %r15,%rcx
call   1040 <find_pivot@plt>
mov    %ebp,%ebx
cmp    $0xffffffff,%eax
jne    1150 <_qsort+0x20>
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

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