#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 /*<<< orphan*/ decrement_stack_depth();
int /*<<< orphan*/ increment_stack_depth();
int partition(int *, int, int);
void tail_recursive_quicksort(int A[], int p, int r) {
  increment_stack_depth();
  while (p < r - 1) {
    int q = partition(A, p, r);
    if (q < (p + r) / 2) {
      tail_recursive_quicksort(A, p, q);
      p = q;
    } else {
      tail_recursive_quicksort(A, q + 1, r);
      r = q;
    }
  }
  decrement_stack_depth();
}


x86 assembly (clang, -O0):
<tail_recursive_quicksort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    $0x0,%al
call   1060 <increment_stack_depth@plt>
mov    -0xc(%rbp),%eax
mov    -0x10(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jge    11d4 <tail_recursive_quicksort+0x94>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%edx
call   1030 <partition@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0xc(%rbp),%eax
add    -0x10(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x18(%rbp),%eax
cmp    %ecx,%eax
jge    11b7 <tail_recursive_quicksort+0x77>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x14(%rbp),%edx
call   1040 <tail_recursive_quicksort@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0xc(%rbp)
jmp    11cf <tail_recursive_quicksort+0x8f>
mov    -0x8(%rbp),%rdi
mov    -0x14(%rbp),%esi
add    $0x1,%esi
mov    -0x10(%rbp),%edx
call   1040 <tail_recursive_quicksort@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x10(%rbp)
jmp    1159 <tail_recursive_quicksort+0x19>
mov    $0x0,%al
call   1050 <decrement_stack_depth@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tail_recursive_quicksort>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%r15d
mov    %esi,%ebx
mov    %rdi,%r14
xor    %eax,%eax
call   1060 <increment_stack_depth@plt>
lea    -0x1(%r15),%eax
cmp    %ebx,%eax
jg     118f <tail_recursive_quicksort+0x4f>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <decrement_stack_depth@plt>
nop
mov    %ebp,%esi
add    $0x1,%esi
mov    %r14,%rdi
mov    %r15d,%edx
call   1040 <tail_recursive_quicksort@plt>
mov    %ebp,%r15d
mov    %ebx,%ebp
lea    -0x1(%r15),%eax
mov    %ebp,%ebx
cmp    %eax,%ebp
jge    115e <tail_recursive_quicksort+0x1e>
mov    %r14,%rdi
mov    %ebx,%esi
mov    %r15d,%edx
call   1030 <partition@plt>
mov    %eax,%ebp
lea    (%rbx,%r15,1),%eax
mov    %eax,%ecx
shr    $0x1f,%ecx
add    %eax,%ecx
sar    %ecx
cmp    %ecx,%ebp
jge    1170 <tail_recursive_quicksort+0x30>
mov    %r14,%rdi
mov    %ebx,%esi
mov    %ebp,%edx
call   1040 <tail_recursive_quicksort@plt>
jmp    1185 <tail_recursive_quicksort+0x45>

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

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