#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(double *, int, int);
void iterative_quicksort(double array[], int array_size) {
  double stack[1000];
  int top, start, end, pivot;
  top = -1;
  stack[++top] = 0;
  stack[++top] = array_size - 1;
  while (top >= 0) {
    end = stack[top--];
    start = stack[top--];
    pivot = partition(array, start, end);
    if (pivot - 1 > start) {
      stack[++top] = start;
      stack[++top] = pivot - 1;
    }
    if (pivot + 1 < end) {
      stack[++top] = pivot + 1;
      stack[++top] = end;
    }
  }
}


x86 assembly (clang, -O0):
<iterative_quicksort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1f60,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0xffffffff,-0x1f54(%rbp)
ff ff ff
mov    -0x1f54(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1f54(%rbp)
cltq
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x1f50(%rbp,%rax,8)
ff ff
mov    -0xc(%rbp),%eax
sub    $0x1,%eax
cvtsi2sd %eax,%xmm0
mov    -0x1f54(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1f54(%rbp)
cltq
movsd  %xmm0,-0x1f50(%rbp,%rax,8)
ff ff
cmpl   $0x0,-0x1f54(%rbp)
jl     129a <iterative_quicksort+0x18a>
mov    -0x1f54(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x1f54(%rbp)
cltq
cvttsd2si -0x1f50(%rbp,%rax,8),%eax
ff ff
mov    %eax,-0x1f5c(%rbp)
mov    -0x1f54(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x1f54(%rbp)
cltq
cvttsd2si -0x1f50(%rbp,%rax,8),%eax
ff ff
mov    %eax,-0x1f58(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x1f58(%rbp),%esi
mov    -0x1f5c(%rbp),%edx
call   1030 <partition@plt>
mov    %eax,-0x1f60(%rbp)
mov    -0x1f60(%rbp),%eax
sub    $0x1,%eax
cmp    -0x1f58(%rbp),%eax
jle    1237 <iterative_quicksort+0x127>
cvtsi2sdl -0x1f58(%rbp),%xmm0
ff
mov    -0x1f54(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1f54(%rbp)
cltq
movsd  %xmm0,-0x1f50(%rbp,%rax,8)
ff ff
mov    -0x1f60(%rbp),%eax
sub    $0x1,%eax
cvtsi2sd %eax,%xmm0
mov    -0x1f54(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1f54(%rbp)
cltq
movsd  %xmm0,-0x1f50(%rbp,%rax,8)
ff ff
mov    -0x1f60(%rbp),%eax
add    $0x1,%eax
cmp    -0x1f5c(%rbp),%eax
jge    1295 <iterative_quicksort+0x185>
mov    -0x1f60(%rbp),%eax
add    $0x1,%eax
cvtsi2sd %eax,%xmm0
mov    -0x1f54(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1f54(%rbp)
cltq
movsd  %xmm0,-0x1f50(%rbp,%rax,8)
ff ff
cvtsi2sdl -0x1f5c(%rbp),%xmm0
ff
mov    -0x1f54(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1f54(%rbp)
cltq
movsd  %xmm0,-0x1f50(%rbp,%rax,8)
ff ff
jmp    116d <iterative_quicksort+0x5d>
add    $0x1f60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<iterative_quicksort>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x1f40,%rsp
mov    %rdi,%r14
movq   $0x0,(%rsp)
add    $0xffffffff,%esi
cvtsi2sd %esi,%xmm0
movsd  %xmm0,0x8(%rsp)
mov    $0x1,%r12d
jmp    116a <iterative_quicksort+0x5a>
nop
xorps  %xmm0,%xmm0
cvtsi2sd %eax,%xmm0
movslq %r12d,%rax
movsd  %xmm0,0x8(%rsp,%rax,8)
xorps  %xmm0,%xmm0
cvtsi2sd %r15d,%xmm0
add    $0x2,%r12d
movsd  %xmm0,(%rsp,%r12,8)
mov    %r12d,%eax
movsd  (%rsp,%rax,8),%xmm0
cvttsd2si %xmm0,%r15d
movslq %r12d,%rbp
cvttsd2si -0x8(%rsp,%rbp,8),%ebx
mov    %r14,%rdi
mov    %ebx,%esi
mov    %r15d,%edx
call   1030 <partition@plt>
lea    -0x1(%rax),%ecx
cmp    %ebx,%ecx
jle    11b0 <iterative_quicksort+0xa0>
add    $0xffffffffffffffff,%rbp
mov    %r12d,%edx
xorps  %xmm0,%xmm0
cvtsi2sd %ebx,%xmm0
movsd  %xmm0,(%rsp,%rbp,8)
xorps  %xmm0,%xmm0
cvtsi2sd %ecx,%xmm0
movsd  %xmm0,(%rsp,%rdx,8)
jmp    11b4 <iterative_quicksort+0xa4>
nopl   (%rax)
add    $0xfffffffe,%r12d
add    $0x1,%eax
cmp    %r15d,%eax
jl     1140 <iterative_quicksort+0x30>
test   %r12d,%r12d
jns    1162 <iterative_quicksort+0x52>
add    $0x1f40,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @iterative_quicksort(double* %0, i32 %1) {
%3 = alloca double*
%4 = alloca i32
%5 = alloca [1000 x double]
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store double* %0, double** %3
store i32 %1, i32* %4
store i32 -1, i32* %6
%10 = load i32, i32* %6
%11 = add nsw i32 %10, 1
store i32 %11, i32* %6
%12 = sext i32 %11 to i64
%13 = getelementptr inbounds [1000 x double], [1000 x double]* %5, i64 0, i64 %12
store double 0.000000e+00, double* %13
%14 = load i32, i32* %4
%15 = sub nsw i32 %14, 1
%16 = sitofp i32 %15 to double
%17 = load i32, i32* %6
%18 = add nsw i32 %17, 1
store i32 %18, i32* %6
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds [1000 x double], [1000 x double]* %5, i64 0, i64 %19
store double %16, double* %20
br label %21
21: ; preds = %78, %2
%22 = load i32, i32* %6
%23 = icmp sge i32 %22, 0
br i1 %23, label %24, label %79
24: ; preds = %21
%25 = load i32, i32* %6
%26 = add nsw i32 %25, -1
store i32 %26, i32* %6
%27 = sext i32 %25 to i64
%28 = getelementptr inbounds [1000 x double], [1000 x double]* %5, i64 0, i64 %27
%29 = load double, double* %28
%30 = fptosi double %29 to i32
store i32 %30, i32* %8
%31 = load i32, i32* %6
%32 = add nsw i32 %31, -1
store i32 %32, i32* %6
%33 = sext i32 %31 to i64
%34 = getelementptr inbounds [1000 x double], [1000 x double]* %5, i64 0, i64 %33
%35 = load double, double* %34
%36 = fptosi double %35 to i32
store i32 %36, i32* %7
%37 = load double*, double** %3
%38 = load i32, i32* %7
%39 = load i32, i32* %8
%40 = call i32 @partition(double* %37, i32 %38, i32 %39)
store i32 %40, i32* %9
%41 = load i32, i32* %9
%42 = sub nsw i32 %41, 1
%43 = load i32, i32* %7
%44 = icmp sgt i32 %42, %43
br i1 %44, label %45, label %59
45: ; preds = %24
%46 = load i32, i32* %7
%47 = sitofp i32 %46 to double
%48 = load i32, i32* %6
%49 = add nsw i32 %48, 1
store i32 %49, i32* %6
%50 = sext i32 %49 to i64
%51 = getelementptr inbounds [1000 x double], [1000 x double]* %5, i64 0, i64 %50
store double %47, double* %51
%52 = load i32, i32* %9
%53 = sub nsw i32 %52, 1
%54 = sitofp i32 %53 to double
%55 = load i32, i32* %6
%56 = add nsw i32 %55, 1
store i32 %56, i32* %6
%57 = sext i32 %56 to i64
%58 = getelementptr inbounds [1000 x double], [1000 x double]* %5, i64 0, i64 %57
store double %54, double* %58
br label %59
59: ; preds = %45, %24
%60 = load i32, i32* %9
%61 = add nsw i32 %60, 1
%62 = load i32, i32* %8
%63 = icmp slt i32 %61, %62
br i1 %63, label %64, label %78
64: ; preds = %59
%65 = load i32, i32* %9
%66 = add nsw i32 %65, 1
%67 = sitofp i32 %66 to double
%68 = load i32, i32* %6
%69 = add nsw i32 %68, 1
store i32 %69, i32* %6
%70 = sext i32 %69 to i64
%71 = getelementptr inbounds [1000 x double], [1000 x double]* %5, i64 0, i64 %70
store double %67, double* %71
%72 = load i32, i32* %8
%73 = sitofp i32 %72 to double
%74 = load i32, i32* %6
%75 = add nsw i32 %74, 1
store i32 %75, i32* %6
%76 = sext i32 %75 to i64
%77 = getelementptr inbounds [1000 x double], [1000 x double]* %5, i64 0, i64 %76
store double %73, double* %77
br label %78
78: ; preds = %64, %59
br label %21
79: ; preds = %21
ret void
}

LLVM IR (-O2):
define void @iterative_quicksort(double* %0, i32 %1) {
%3 = alloca [1000 x double]
%4 = bitcast [1000 x double]* %3 to i8*
%5 = getelementptr inbounds [1000 x double], [1000 x double]* %3, i64 0, i64 0
store double 0.000000e+00, double* %5
%6 = add nsw i32 %1, -1
%7 = sitofp i32 %6 to double
%8 = getelementptr inbounds [1000 x double], [1000 x double]* %3, i64 0, i64 1
store double %7, double* %8
br label %9
9: ; preds = %42, %2
%10 = phi double [ %7, %2 ], [ %46, %42 ]
%11 = phi i32 [ 1, %2 ], [ %43, %42 ]
%12 = add nsw i32 %11, -1
%13 = fptosi double %10 to i32
%14 = add nsw i32 %11, -2
%15 = sext i32 %12 to i64
%16 = getelementptr inbounds [1000 x double], [1000 x double]* %3, i64 0, i64 %15
%17 = load double, double* %16
%18 = fptosi double %17 to i32
%19 = tail call i32 @partition(double* %0, i32 %18, i32 %13)
%20 = add nsw i32 %19, -1
%21 = icmp sgt i32 %20, %18
br i1 %21, label %22, label %27
22: ; preds = %9
%23 = zext i32 %11 to i64
%24 = getelementptr inbounds [1000 x double], [1000 x double]* %3, i64 0, i64 %23
%25 = sitofp i32 %18 to double
store double %25, double* %16
%26 = sitofp i32 %20 to double
store double %26, double* %24
br label %27
27: ; preds = %22, %9
%28 = phi i32 [ %11, %22 ], [ %14, %9 ]
%29 = add nsw i32 %19, 1
%30 = icmp slt i32 %29, %13
br i1 %30, label %31, label %40
31: ; preds = %27
%32 = sitofp i32 %29 to double
%33 = add nsw i32 %28, 1
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds [1000 x double], [1000 x double]* %3, i64 0, i64 %34
store double %32, double* %35
%36 = sitofp i32 %13 to double
%37 = add nsw i32 %28, 2
%38 = zext i32 %37 to i64
%39 = getelementptr inbounds [1000 x double], [1000 x double]* %3, i64 0, i64 %38
store double %36, double* %39
br label %42
40: ; preds = %27
%41 = icmp sgt i32 %28, -1
br i1 %41, label %42, label %47
42: ; preds = %31, %40
%43 = phi i32 [ %37, %31 ], [ %28, %40 ]
%44 = zext i32 %43 to i64
%45 = getelementptr inbounds [1000 x double], [1000 x double]* %3, i64 0, i64 %44
%46 = load double, double* %45
br label %9
47: ; preds = %40
ret void
}