#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 _topnsel_quicksort(long *, int, int);
void topnsel_quicksort(long *data, int insize, int outsize) {
  int startidx = 0;
  int endidx = insize - 1;
  int pivotidx = -2;
  if (insize <= outsize)
    return;
  while (pivotidx < outsize - 1 || pivotidx > outsize + 1) {
    pivotidx = _topnsel_quicksort(data, startidx, endidx);
    if (pivotidx > outsize)
      endidx = pivotidx - 1;
    else
      startidx = pivotidx + 1;
  }
}


x86 assembly (clang, -O0):
<topnsel_quicksort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0xc(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x18(%rbp)
movl   $0xfffffffe,-0x1c(%rbp)
mov    -0xc(%rbp),%eax
cmp    -0x10(%rbp),%eax
jg     114a <topnsel_quicksort+0x3a>
jmp    11c0 <topnsel_quicksort+0xb0>
jmp    114f <topnsel_quicksort+0x3f>
mov    -0x1c(%rbp),%ecx
mov    -0x10(%rbp),%edx
sub    $0x1,%edx
mov    $0x1,%al
cmp    %edx,%ecx
mov    %al,-0x1d(%rbp)
jl     1176 <topnsel_quicksort+0x66>
mov    -0x1c(%rbp),%eax
mov    -0x10(%rbp),%ecx
add    $0x1,%ecx
cmp    %ecx,%eax
setg   %al
mov    %al,-0x1d(%rbp)
mov    -0x1d(%rbp),%al
test   $0x1,%al
jne    1186 <topnsel_quicksort+0x76>
jmp    11c0 <topnsel_quicksort+0xb0>
mov    -0x8(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x18(%rbp),%edx
call   1030 <_topnsel_quicksort@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x10(%rbp),%eax
jle    11b2 <topnsel_quicksort+0xa2>
mov    -0x1c(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    11bb <topnsel_quicksort+0xab>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    114f <topnsel_quicksort+0x3f>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<topnsel_quicksort>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
cmp    %edx,%esi
jle    1169 <topnsel_quicksort+0x59>
mov    %edx,%r15d
cmp    $0xfffffffc,%edx
ja     1169 <topnsel_quicksort+0x59>
mov    %rdi,%r14
lea    -0x1(%r15),%r12d
lea    0x1(%r15),%r13d
add    $0xffffffff,%esi
xor    %ecx,%ecx
nopw   0x0(%rax,%rax,1)
mov    %ecx,%ebp
mov    %esi,%ebx
mov    %r14,%rdi
mov    %ecx,%esi
mov    %ebx,%edx
call   1030 <_topnsel_quicksort@plt>
lea    0x1(%rax),%ecx
cmp    %r15d,%eax
cmovg  %ebp,%ecx
lea    -0x1(%rax),%esi
cmovle %ebx,%esi
cmp    %r12d,%eax
jl     1140 <topnsel_quicksort+0x30>
cmp    %r13d,%eax
jg     1140 <topnsel_quicksort+0x30>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @topnsel_quicksort(i64* %0, i32 %1, i32 %2) {
%4 = alloca i64*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i64* %0, i64** %4
store i32 %1, i32* %5
store i32 %2, i32* %6
store i32 0, i32* %7
%10 = load i32, i32* %5
%11 = sub nsw i32 %10, 1
store i32 %11, i32* %8
store i32 -2, i32* %9
%12 = load i32, i32* %5
%13 = load i32, i32* %6
%14 = icmp sle i32 %12, %13
br i1 %14, label %15, label %16
15: ; preds = %3
br label %44
16: ; preds = %3
br label %17
17: ; preds = %43, %16
%18 = load i32, i32* %9
%19 = load i32, i32* %6
%20 = sub nsw i32 %19, 1
%21 = icmp slt i32 %18, %20
br i1 %21, label %27, label %22
22: ; preds = %17
%23 = load i32, i32* %9
%24 = load i32, i32* %6
%25 = add nsw i32 %24, 1
%26 = icmp sgt i32 %23, %25
br label %27
27: ; preds = %22, %17
%28 = phi i1 [ true, %17 ], [ %26, %22 ]
br i1 %28, label %29, label %44
29: ; preds = %27
%30 = load i64*, i64** %4
%31 = load i32, i32* %7
%32 = load i32, i32* %8
%33 = call i32 @_topnsel_quicksort(i64* %30, i32 %31, i32 %32)
store i32 %33, i32* %9
%34 = load i32, i32* %9
%35 = load i32, i32* %6
%36 = icmp sgt i32 %34, %35
br i1 %36, label %37, label %40
37: ; preds = %29
%38 = load i32, i32* %9
%39 = sub nsw i32 %38, 1
store i32 %39, i32* %8
br label %43
40: ; preds = %29
%41 = load i32, i32* %9
%42 = add nsw i32 %41, 1
store i32 %42, i32* %7
br label %43
43: ; preds = %40, %37
br label %17
44: ; preds = %15, %27
ret void
}

LLVM IR (-O2):
define void @topnsel_quicksort(i64* %0, i32 %1, i32 %2) {
%4 = icmp sgt i32 %1, %2
br i1 %4, label %5, label %23
5: ; preds = %3
%6 = add nsw i32 %2, -1
%7 = add nsw i32 %2, 1
%8 = icmp ult i32 %2, -3
br i1 %8, label %9, label %23
9: ; preds = %5
%10 = add nsw i32 %1, -1
br label %11
11: ; preds = %9, %11
%12 = phi i32 [ %19, %11 ], [ %10, %9 ]
%13 = phi i32 [ %18, %11 ], [ 0, %9 ]
%14 = tail call i32 @_topnsel_quicksort(i64* %0, i32 %13, i32 %12)
%15 = icmp sgt i32 %14, %2
%16 = add nsw i32 %14, -1
%17 = add nsw i32 %14, 1
%18 = select i1 %15, i32 %13, i32 %17
%19 = select i1 %15, i32 %16, i32 %12
%20 = icmp slt i32 %14, %6
%21 = icmp sgt i32 %14, %7
%22 = select i1 %20, i1 true, i1 %21
br i1 %22, label %11, label %23
23: ; preds = %11, %5, %3
ret void
}