#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 partition(int *, int, int, int);
void quicksort(int vec[], int left, int right) {
  if (left < right) {
    int middle = (left + right) / 2;
    int newp = partition(vec, left, right, middle);
    quicksort(vec, left, newp - 1);
    quicksort(vec, newp + 1, right);
  }
}


x86 assembly (clang, -O0):
<quicksort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    -0xc(%rbp),%eax
cmp    -0x10(%rbp),%eax
jge    1188 <quicksort+0x68>
mov    -0xc(%rbp),%eax
add    -0x10(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%edx
mov    -0x14(%rbp),%ecx
call   1030 <partition@plt>
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x18(%rbp),%edx
sub    $0x1,%edx
call   1040 <quicksort@plt>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%esi
add    $0x1,%esi
mov    -0x10(%rbp),%edx
call   1040 <quicksort@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<quicksort>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
cmp    %edx,%esi
jge    1176 <quicksort+0x56>
mov    %edx,%r15d
mov    %esi,%r12d
mov    %rdi,%r14
cs nopw 0x0(%rax,%rax,1)
nop
lea    (%r12,%r15,1),%eax
mov    %eax,%ecx
shr    $0x1f,%ecx
add    %eax,%ecx
sar    %ecx
mov    %r14,%rdi
mov    %r12d,%esi
mov    %r15d,%edx
call   1030 <partition@plt>
mov    %eax,%ebx
lea    -0x1(%rbx),%edx
mov    %r14,%rdi
mov    %r12d,%esi
call   1040 <quicksort@plt>
add    $0x1,%ebx
mov    %ebx,%r12d
cmp    %r15d,%ebx
jl     1140 <quicksort+0x20>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

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