#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 */
struct IndexEntry {
  int dummy;
};
typedef int int64_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ loadFileToBuffer(int /*<<< orphan*/ *, struct IndexEntry *,
                                    int, int, int);
int partitionFile(int /*<<< orphan*/ *, int, int);
int /*<<< orphan*/ quickSortMemory(struct IndexEntry *, int /*<<< orphan*/,
                                   int);
int /*<<< orphan*/ writeBufferToFile(int /*<<< orphan*/ *, struct IndexEntry *,
                                     int, int);
void quickSortFile(FILE *file, int64_t lowerIdx, int64_t upperIdx,
                   struct IndexEntry *sortBuffer, int64_t bufcount) {
  int64_t size = upperIdx - lowerIdx + 1;
  if (size >= 2) {
    if (size <= bufcount) {
      loadFileToBuffer(file, sortBuffer, lowerIdx, upperIdx, bufcount);
      quickSortMemory(sortBuffer, 0, size - 1);
      writeBufferToFile(file, sortBuffer, lowerIdx, size);
    } else {
      int64_t newPivot = partitionFile(file, lowerIdx, upperIdx);
      quickSortFile(file, lowerIdx, newPivot - 1, sortBuffer, bufcount);
      quickSortFile(file, newPivot + 1, upperIdx, sortBuffer, bufcount);
    }
  }
}


x86 assembly (clang, -O0):
<quickSortFile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %rcx,-0x18(%rbp)
mov    %r8d,-0x1c(%rbp)
mov    -0x10(%rbp),%eax
sub    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
cmpl   $0x2,-0x20(%rbp)
jl     1217 <quickSortFile+0xc7>
mov    -0x20(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jg     11cc <quickSortFile+0x7c>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0xc(%rbp),%edx
mov    -0x10(%rbp),%ecx
mov    -0x1c(%rbp),%r8d
call   1070 <loadFileToBuffer@plt>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%edx
sub    $0x1,%edx
xor    %esi,%esi
call   1050 <quickSortMemory@plt>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0xc(%rbp),%edx
mov    -0x20(%rbp),%ecx
call   1030 <writeBufferToFile@plt>
jmp    1212 <quickSortFile+0xc2>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%edx
call   1040 <partitionFile@plt>
mov    %eax,-0x24(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x24(%rbp),%edx
sub    $0x1,%edx
mov    -0x18(%rbp),%rcx
mov    -0x1c(%rbp),%r8d
call   1060 <quickSortFile@plt>
mov    -0x8(%rbp),%rdi
mov    -0x24(%rbp),%esi
add    $0x1,%esi
mov    -0x10(%rbp),%edx
mov    -0x18(%rbp),%rcx
mov    -0x1c(%rbp),%r8d
call   1060 <quickSortFile@plt>
jmp    1217 <quickSortFile+0xc7>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<quickSortFile>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rcx,0x10(%rsp)
mov    %edx,%ebp
sub    %esi,%ebp
jle    11ba <quickSortFile+0x6a>
mov    %r8d,%r12d
mov    %edx,%r13d
mov    %esi,%ebx
mov    %rdi,%r15
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
cmp    %r12d,%ebp
jl     11c9 <quickSortFile+0x79>
mov    %r15,%rdi
mov    %ebx,%esi
mov    %r13d,%edx
call   1040 <partitionFile@plt>
mov    %eax,%r14d
lea    -0x1(%r14),%edx
mov    %r15,%rdi
mov    %ebx,%esi
mov    0x10(%rsp),%rcx
mov    %r12d,%r8d
call   1060 <quickSortFile@plt>
add    $0x1,%r14d
mov    %r13d,%ebp
mov    %r14d,%ebx
sub    %r14d,%ebp
jg     1180 <quickSortFile+0x30>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0x1(%rbp),%eax
mov    %eax,0xc(%rsp)
mov    %r15,%rdi
mov    0x10(%rsp),%r14
mov    %r14,%rsi
mov    %ebx,%edx
mov    %r13d,%ecx
mov    %r12d,%r8d
call   1070 <loadFileToBuffer@plt>
mov    %r14,%rdi
xor    %esi,%esi
mov    %ebp,%edx
call   1050 <quickSortMemory@plt>
mov    %r15,%rdi
mov    %r14,%rsi
mov    %ebx,%edx
mov    0xc(%rsp),%ecx
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <writeBufferToFile@plt>

LLVM IR (-O0):
%struct.IndexEntry = type { i32 }
define void @quickSortFile(i32* %0, i32 %1, i32 %2, %struct.IndexEntry* %3, i32 %4) {
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32
%9 = alloca %struct.IndexEntry*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store i32* %0, i32** %6
store i32 %1, i32* %7
store i32 %2, i32* %8
store %struct.IndexEntry* %3, %struct.IndexEntry** %9
store i32 %4, i32* %10
%13 = load i32, i32* %8
%14 = load i32, i32* %7
%15 = sub nsw i32 %13, %14
%16 = add nsw i32 %15, 1
store i32 %16, i32* %11
%17 = load i32, i32* %11
%18 = icmp sge i32 %17, 2
br i1 %18, label %19, label %57
19: ; preds = %5
%20 = load i32, i32* %11
%21 = load i32, i32* %10
%22 = icmp sle i32 %20, %21
br i1 %22, label %23, label %39
23: ; preds = %19
%24 = load i32*, i32** %6
%25 = load %struct.IndexEntry*, %struct.IndexEntry** %9
%26 = load i32, i32* %7
%27 = load i32, i32* %8
%28 = load i32, i32* %10
%29 = call i32 @loadFileToBuffer(i32* %24, %struct.IndexEntry* %25, i32 %26, i32 %27, i32 %28)
%30 = load %struct.IndexEntry*, %struct.IndexEntry** %9
%31 = load i32, i32* %11
%32 = sub nsw i32 %31, 1
%33 = call i32 @quickSortMemory(%struct.IndexEntry* %30, i32 0, i32 %32)
%34 = load i32*, i32** %6
%35 = load %struct.IndexEntry*, %struct.IndexEntry** %9
%36 = load i32, i32* %7
%37 = load i32, i32* %11
%38 = call i32 @writeBufferToFile(i32* %34, %struct.IndexEntry* %35, i32 %36, i32 %37)
br label %56
39: ; preds = %19
%40 = load i32*, i32** %6
%41 = load i32, i32* %7
%42 = load i32, i32* %8
%43 = call i32 @partitionFile(i32* %40, i32 %41, i32 %42)
store i32 %43, i32* %12
%44 = load i32*, i32** %6
%45 = load i32, i32* %7
%46 = load i32, i32* %12
%47 = sub nsw i32 %46, 1
%48 = load %struct.IndexEntry*, %struct.IndexEntry** %9
%49 = load i32, i32* %10
%50 = load i32*, i32** %6
%51 = load i32, i32* %12
%52 = add nsw i32 %51, 1
%53 = load i32, i32* %8
%54 = load %struct.IndexEntry*, %struct.IndexEntry** %9
%55 = load i32, i32* %10
br label %56
56: ; preds = %39, %23
br label %57
57: ; preds = %56, %5
ret void
}

LLVM IR (-O2):
%struct.IndexEntry = type { i32 }
define void @quickSortFile(i32* %0, i32 %1, i32 %2, %struct.IndexEntry* %3, i32 %4) {
%6 = sub nsw i32 %2, %1
%7 = icmp sgt i32 %6, 0
br i1 %7, label %8, label %23
8: ; preds = %5, %17
%9 = phi i32 [ %21, %17 ], [ %6, %5 ]
%10 = phi i32 [ %20, %17 ], [ %1, %5 ]
%11 = icmp slt i32 %9, %4
br i1 %11, label %12, label %17
12: ; preds = %8
%13 = add nuw nsw i32 %9, 1
%14 = tail call i32 @loadFileToBuffer(i32* %0, %struct.IndexEntry* %3, i32 %10, i32 %2, i32 %4)
%15 = tail call i32 @quickSortMemory(%struct.IndexEntry* %3, i32 0, i32 %9)
%16 = tail call i32 @writeBufferToFile(i32* %0, %struct.IndexEntry* %3, i32 %10, i32 %13)
br label %23
17: ; preds = %8
%18 = tail call i32 @partitionFile(i32* %0, i32 %10, i32 %2)
%19 = add nsw i32 %18, -1
tail call void @quickSortFile(i32* %0, i32 %10, i32 %19, %struct.IndexEntry* %3, i32 %4)
%20 = add nsw i32 %18, 1
%21 = sub nsw i32 %2, %20
%22 = icmp sgt i32 %21, 0
br i1 %22, label %8, label %23
23: ; preds = %17, %5, %12
ret void
}