#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);
int rand();
void _qsort(int *arr, int first, int last) {
  int pivot;
  if (first >= last)
    return;
  pivot = first + (rand() % (last + 1 - first));
  pivot = _partition(arr, first, last, pivot);
  _qsort(arr, first, pivot - 1);
  _qsort(arr, pivot + 1, last);
}


x86 assembly (clang, -O0):
<_qsort>:
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
jl     1153 <_qsort+0x23>
jmp    11ad <_qsort+0x7d>
mov    -0xc(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    $0x0,%al
call   1050 <rand@plt>
mov    -0x10(%rbp),%ecx
add    $0x1,%ecx
sub    -0xc(%rbp),%ecx
cltd
idiv   %ecx
mov    -0x18(%rbp),%eax
add    %edx,%eax
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,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x14(%rbp),%edx
sub    $0x1,%edx
call   1040 <_qsort@plt>
mov    -0x8(%rbp),%rdi
mov    -0x14(%rbp),%esi
add    $0x1,%esi
mov    -0x10(%rbp),%edx
call   1040 <_qsort@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_qsort>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
cmp    %edx,%esi
jge    118c <_qsort+0x5c>
mov    %edx,%r15d
mov    %esi,%r12d
mov    %rdi,%r14
lea    0x1(%r15),%ebp
nopl   0x0(%rax)
xor    %eax,%eax
call   1050 <rand@plt>
mov    %ebp,%ecx
sub    %r12d,%ecx
cltd
idiv   %ecx
lea    (%rdx,%r12,1),%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 <_qsort@plt>
add    $0x1,%ebx
mov    %ebx,%r12d
cmp    %r15d,%ebx
jl     1150 <_qsort+0x20>
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @_qsort(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 = load i32, i32* %5
%9 = load i32, i32* %6
%10 = icmp sge i32 %8, %9
br i1 %10, label %11, label %12
11: ; preds = %3
br label %34
12: ; preds = %3
%13 = load i32, i32* %5
%14 = call i32 (...) @rand()
%15 = load i32, i32* %6
%16 = add nsw i32 %15, 1
%17 = load i32, i32* %5
%18 = sub nsw i32 %16, %17
%19 = srem i32 %14, %18
%20 = add nsw i32 %13, %19
store i32 %20, i32* %7
%21 = load i32*, i32** %4
%22 = load i32, i32* %5
%23 = load i32, i32* %6
%24 = load i32, i32* %7
%25 = call i32 @_partition(i32* %21, i32 %22, i32 %23, i32 %24)
store i32 %25, i32* %7
%26 = load i32*, i32** %4
%27 = load i32, i32* %5
%28 = load i32, i32* %7
%29 = sub nsw i32 %28, 1
%30 = load i32*, i32** %4
%31 = load i32, i32* %7
%32 = add nsw i32 %31, 1
%33 = load i32, i32* %6
br label %34
34: ; preds = %12, %11
ret void
}

LLVM IR (-O2):
define void @_qsort(i32* %0, i32 %1, i32 %2) {
%4 = icmp slt i32 %1, %2
br i1 %4, label %5, label %17
5: ; preds = %3
%6 = add nsw i32 %2, 1
br label %7
7: ; preds = %5, %7
%8 = phi i32 [ %1, %5 ], [ %15, %7 ]
%9 = tail call i32 (...) @rand()
%10 = sub i32 %6, %8
%11 = srem i32 %9, %10
%12 = add nsw i32 %11, %8
%13 = tail call i32 @_partition(i32* %0, i32 %8, i32 %2, i32 %12)
%14 = add nsw i32 %13, -1
tail call void @_qsort(i32* %0, i32 %8, i32 %14)
%15 = add nsw i32 %13, 1
%16 = icmp slt i32 %15, %2
br i1 %16, label %7, label %17
17: ; preds = %7, %3
ret void
}