#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 /*<<< orphan*/ printf(char *, ...);
void quicksort(int array[], int left, int right) {
  int pivot_index = left;
  int pivot_new_index;
loop:
  printf("sorting %d to %d\n", left, right);
  if (right > left) {
    pivot_new_index = partition(array, left, right, pivot_index);
    printf("L\n");
    quicksort(array, left, pivot_new_index - 1);
    printf("R\n");
    pivot_index = left = pivot_new_index + 1;
    goto loop;
  }
}


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
mov    %eax,-0x14(%rbp)
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%edx
lea    0xeab(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x10(%rbp),%eax
cmp    -0xc(%rbp),%eax
jle    11bc <quicksort+0x8c>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%edx
mov    -0x14(%rbp),%ecx
call   1030 <partition@plt>
mov    %eax,-0x18(%rbp)
lea    0xe8e(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x18(%rbp),%edx
sub    $0x1,%edx
call   1040 <quicksort@plt>
lea    0xe71(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
mov    %eax,-0x14(%rbp)
jmp    1148 <quicksort+0x18>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<quicksort>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%ebx
mov    %esi,%ebp
mov    %rdi,%r14
lea    0xea7(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
cmp    %ebx,%ebp
jge    11ce <quicksort+0x8e>
lea    0xea9(%rip),%r12
lea    0xe8e(%rip),%r13
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r14,%rdi
mov    %ebp,%esi
mov    %ebx,%edx
mov    %ebp,%ecx
call   1040 <partition@plt>
mov    %eax,%r15d
lea    0xe7a(%rip),%rdi
call   1030 <puts@plt>
lea    -0x1(%r15),%edx
mov    %r14,%rdi
mov    %ebp,%esi
call   1050 <quicksort@plt>
mov    %r12,%rdi
call   1030 <puts@plt>
add    $0x1,%r15d
mov    %r13,%rdi
mov    %r15d,%esi
mov    %ebx,%edx
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r15d,%ebp
cmp    %ebx,%r15d
jl     1180 <quicksort+0x40>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [18 x i8] c"sorting %d to %d\0A\00"
@.str.1 = [3 x i8] c"L\0A\00"
@.str.2 = [3 x i8] c"R\0A\00"
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
store i32 %9, i32* %7
br label %10
10: ; preds = %17, %3
%11 = load i32, i32* %5
%12 = load i32, i32* %6
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %11, i32 %12)
%14 = load i32, i32* %6
%15 = load i32, i32* %5
%16 = icmp sgt i32 %14, %15
br i1 %16, label %17, label %31
17: ; preds = %10
%18 = load i32*, i32** %4
%19 = load i32, i32* %5
%20 = load i32, i32* %6
%21 = load i32, i32* %7
%22 = call i32 @partition(i32* %18, i32 %19, i32 %20, i32 %21)
store i32 %22, i32* %8
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%24 = load i32*, i32** %4
%25 = load i32, i32* %5
%26 = load i32, i32* %8
%27 = sub nsw i32 %26, 1
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%29 = load i32, i32* %8
%30 = add nsw i32 %29, 1
store i32 %30, i32* %5
store i32 %30, i32* %7
br label %10
31: ; preds = %10
ret void
}

LLVM IR (-O2):
@.str = [18 x i8] c"sorting %d to %d\0A\00"
@str = [2 x i8] c"L\00"
@str.3 = [2 x i8] c"R\00"
define void @quicksort(i32* %0, i32 %1, i32 %2) {
%4 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %1, i32 %2)
%5 = icmp slt i32 %1, %2
br i1 %5, label %6, label %15
6: ; preds = %3, %6
%7 = phi i32 [ %12, %6 ], [ %1, %3 ]
%8 = tail call i32 @partition(i32* %0, i32 %7, i32 %2, i32 %7)
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([2 x i8], [2 x i8]* @str, i64 0, i64 0))
%10 = add nsw i32 %8, -1
tail call void @quicksort(i32* %0, i32 %7, i32 %10)
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([2 x i8], [2 x i8]* @str.3, i64 0, i64 0))
%12 = add nsw i32 %8, 1
%13 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %12, i32 %2)
%14 = icmp slt i32 %12, %2
br i1 %14, label %6, label %15
15: ; preds = %6, %3
ret void
}