#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);
      if ((newPivot - 1) - lowerIdx > upperIdx - (newPivot + 1)) {
        quickSortFile(file, newPivot + 1, upperIdx, sortBuffer, bufcount);
        quickSortFile(file, lowerIdx, newPivot - 1, sortBuffer, bufcount);
      } else {
        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     1271 <quickSortFile+0x121>
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    126c <quickSortFile+0x11c>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%edx
call   1040 <partitionFile@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
sub    $0x1,%eax
sub    -0xc(%rbp),%eax
mov    -0x10(%rbp),%ecx
mov    -0x24(%rbp),%edx
add    $0x1,%edx
sub    %edx,%ecx
cmp    %ecx,%eax
jle    1233 <quickSortFile+0xe3>
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>
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>
jmp    1267 <quickSortFile+0x117>
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    126c <quickSortFile+0x11c>
jmp    1271 <quickSortFile+0x121>
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,0x8(%rsp)
mov    %rdi,0x10(%rsp)
mov    %edx,%r15d
mov    %esi,(%rsp)
sub    %esi,%r15d
jle    121d <quickSortFile+0xcd>
mov    %r8d,%r13d
mov    %edx,%ecx
mov    %r8d,0x4(%rsp)
mov    %r15d,%ebx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
cmp    %r13d,%ebx
jl     122c <quickSortFile+0xdc>
mov    0x10(%rsp),%r13
mov    %r13,%rdi
mov    (%rsp),%r15d
mov    %r15d,%esi
mov    %ecx,%edx
mov    %ecx,%r12d
call   1040 <partitionFile@plt>
mov    %eax,%r14d
lea    -0x1(%r14),%ebp
mov    %ebp,%ebx
sub    %r15d,%ebx
add    $0x1,%r14d
mov    %r12d,%r15d
sub    %r14d,%r15d
cmp    %r15d,%ebx
jle    11f0 <quickSortFile+0xa0>
mov    %r12d,%edx
mov    %r13,%rdi
mov    %r14d,%esi
mov    0x8(%rsp),%rcx
mov    0x4(%rsp),%r13d
mov    %r13d,%r8d
call   1060 <quickSortFile@plt>
mov    %ebp,%ecx
test   %ebx,%ebx
jg     1190 <quickSortFile+0x40>
jmp    121d <quickSortFile+0xcd>
mov    %r13,%rdi
mov    (%rsp),%esi
mov    %ebp,%edx
mov    0x8(%rsp),%rcx
mov    0x4(%rsp),%r13d
mov    %r13d,%r8d
call   1060 <quickSortFile@plt>
mov    %r12d,%ecx
mov    %r14d,%eax
mov    %r14d,(%rsp)
test   %r15d,%r15d
jg     1181 <quickSortFile+0x31>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0x1(%rbx),%r14d
mov    0x10(%rsp),%r15
mov    %r15,%rdi
mov    0x8(%rsp),%rbp
mov    %rbp,%rsi
mov    (%rsp),%r12d
mov    %r12d,%edx
mov    %r13d,%r8d
call   1070 <loadFileToBuffer@plt>
mov    %rbp,%rdi
xor    %esi,%esi
mov    %ebx,%edx
call   1050 <quickSortMemory@plt>
mov    %r15,%rdi
mov    %rbp,%rsi
mov    %r12d,%edx
mov    %r14d,%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 %81
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 %80
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* %12
%45 = sub nsw i32 %44, 1
%46 = load i32, i32* %7
%47 = sub nsw i32 %45, %46
%48 = load i32, i32* %8
%49 = load i32, i32* %12
%50 = add nsw i32 %49, 1
%51 = sub nsw i32 %48, %50
%52 = icmp sgt i32 %47, %51
br i1 %52, label %53, label %66
53: ; preds = %39
%54 = load i32*, i32** %6
%55 = load i32, i32* %12
%56 = add nsw i32 %55, 1
%57 = load i32, i32* %8
%58 = load %struct.IndexEntry*, %struct.IndexEntry** %9
%59 = load i32, i32* %10
%60 = load i32*, i32** %6
%61 = load i32, i32* %7
%62 = load i32, i32* %12
%63 = sub nsw i32 %62, 1
%64 = load %struct.IndexEntry*, %struct.IndexEntry** %9
%65 = load i32, i32* %10
br label %79
66: ; preds = %39
%67 = load i32*, i32** %6
%68 = load i32, i32* %7
%69 = load i32, i32* %12
%70 = sub nsw i32 %69, 1
%71 = load %struct.IndexEntry*, %struct.IndexEntry** %9
%72 = load i32, i32* %10
%73 = load i32*, i32** %6
%74 = load i32, i32* %12
%75 = add nsw i32 %74, 1
%76 = load i32, i32* %8
%77 = load %struct.IndexEntry*, %struct.IndexEntry** %9
%78 = load i32, i32* %10
br label %79
79: ; preds = %66, %53
br label %80
80: ; preds = %79, %23
br label %81
81: ; preds = %80, %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 %32
8: ; preds = %5, %30
%9 = phi i32 [ %26, %30 ], [ %6, %5 ]
%10 = phi i32 [ %14, %30 ], [ %2, %5 ]
%11 = phi i32 [ %25, %30 ], [ %1, %5 ]
br label %12
12: ; preds = %8, %28
%13 = phi i32 [ %9, %8 ], [ %24, %28 ]
%14 = phi i32 [ %10, %8 ], [ %23, %28 ]
%15 = icmp slt i32 %13, %4
br i1 %15, label %16, label %21
16: ; preds = %12
%17 = add nuw nsw i32 %13, 1
%18 = tail call i32 @loadFileToBuffer(i32* %0, %struct.IndexEntry* %3, i32 %11, i32 %14, i32 %4)
%19 = tail call i32 @quickSortMemory(%struct.IndexEntry* %3, i32 0, i32 %13)
%20 = tail call i32 @writeBufferToFile(i32* %0, %struct.IndexEntry* %3, i32 %11, i32 %17)
br label %32
21: ; preds = %12
%22 = tail call i32 @partitionFile(i32* %0, i32 %11, i32 %14)
%23 = add nsw i32 %22, -1
%24 = sub nsw i32 %23, %11
%25 = add nsw i32 %22, 1
%26 = sub nsw i32 %14, %25
%27 = icmp sgt i32 %24, %26
br i1 %27, label %28, label %30
28: ; preds = %21
tail call void @quickSortFile(i32* %0, i32 %25, i32 %14, %struct.IndexEntry* %3, i32 %4)
%29 = icmp sgt i32 %24, 0
br i1 %29, label %12, label %32
30: ; preds = %21
tail call void @quickSortFile(i32* %0, i32 %11, i32 %23, %struct.IndexEntry* %3, i32 %4)
%31 = icmp sgt i32 %26, 0
br i1 %31, label %8, label %32
32: ; preds = %30, %28, %5, %16
ret void
}