#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 */
double RAND_MAX;
int /*<<< orphan*/ printf(char *, int, int, int);
double rand();
int /*<<< orphan*/ swap(double *, int, int);
int partition(double *srcdata, int p, int r, int partitionType) {
  double x;
  int i, j;
  if (partitionType > 0) {
    i = (int)((rand() * 1.0 / RAND_MAX) * (r - p) + p);
    printf("partition at %d,%d,%d\n", p, r, i);
    swap(srcdata, i, r);
  }
  x = srcdata[r];
  i = p - 1;
  for (j = p; j < r; j++) {
    if (srcdata[j] <= x) {
      i++;
      swap(srcdata, i, j);
    }
  }
  swap(srcdata, i + 1, r);
  return i + 1;
}


x86 assembly (clang, -O0):
<partition>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %ecx,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jle    11af <partition+0x7f>
mov    $0x0,%al
call   1050 <rand@plt>
movsd  0xea2(%rip),%xmm1
mulsd  %xmm1,%xmm0
mov    0x2e7f(%rip),%rax
divsd  (%rax),%xmm0
mov    -0x10(%rbp),%eax
sub    -0xc(%rbp),%eax
cvtsi2sd %eax,%xmm2
cvtsi2sdl -0xc(%rbp),%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
cvttsd2si %xmm0,%eax
mov    %eax,-0x24(%rbp)
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%edx
mov    -0x24(%rbp),%ecx
lea    0xe6d(%rip),%rdi
call   1030 <printf@plt>
mov    -0x8(%rbp),%rdi
mov    -0x24(%rbp),%esi
mov    -0x10(%rbp),%edx
call   1040 <swap@plt>
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
movsd  %xmm0,-0x20(%rbp)
mov    -0xc(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x24(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%eax
cmp    -0x10(%rbp),%eax
jge    1223 <partition+0xf3>
mov    -0x8(%rbp),%rax
movslq -0x28(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm1
movsd  -0x20(%rbp),%xmm0
ucomisd %xmm1,%xmm0
jb     1210 <partition+0xe0>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x24(%rbp),%esi
mov    -0x28(%rbp),%edx
call   1040 <swap@plt>
jmp    1215 <partition+0xe5>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    11d0 <partition+0xa0>
mov    -0x8(%rbp),%rdi
mov    -0x24(%rbp),%esi
add    $0x1,%esi
mov    -0x10(%rbp),%edx
call   1040 <swap@plt>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<partition>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %edx,%r15d
mov    %esi,%ebx
mov    %rdi,%r14
test   %ecx,%ecx
jle    1193 <partition+0x63>
xor    %eax,%eax
call   1050 <rand@plt>
mov    0x2e92(%rip),%rax
divsd  (%rax),%xmm0
mov    %r15d,%eax
sub    %ebx,%eax
cvtsi2sd %eax,%xmm1
cvtsi2sd %ebx,%xmm2
mulsd  %xmm0,%xmm1
addsd  %xmm1,%xmm2
cvttsd2si %xmm2,%ebp
lea    0xe86(%rip),%rdi
mov    %ebx,%esi
mov    %r15d,%edx
mov    %ebp,%ecx
call   1030 <printf@plt>
mov    %r14,%rdi
mov    %ebp,%esi
mov    %r15d,%edx
call   1040 <swap@plt>
lea    -0x1(%rbx),%r12d
cmp    %ebx,%r15d
jle    11ec <partition+0xbc>
movslq %r15d,%rax
movsd  (%r14,%rax,8),%xmm0
movslq %ebx,%rax
lea    (%r14,%rax,8),%rbp
movsd  %xmm0,0x8(%rsp)
jmp    11cc <partition+0x9c>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
add    $0x1,%ebx
add    $0x8,%rbp
cmp    %ebx,%r15d
je     11ec <partition+0xbc>
ucomisd 0x0(%rbp),%xmm0
jb     11c0 <partition+0x90>
add    $0x1,%r12d
mov    %r14,%rdi
mov    %r12d,%esi
mov    %ebx,%edx
call   1040 <swap@plt>
movsd  0x8(%rsp),%xmm0
jmp    11c0 <partition+0x90>
add    $0x1,%r12d
mov    %r14,%rdi
mov    %r12d,%esi
mov    %r15d,%edx
call   1040 <swap@plt>
mov    %r12d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@RAND_MAX = global double 0.000000e+00
@.str = [23 x i8] c"partition at %d,%d,%d\0A\00"
define i32 @partition(double* %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca double*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca double
%10 = alloca i32
%11 = alloca i32
store double* %0, double** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i32 %3, i32* %8
%12 = load i32, i32* %8
%13 = icmp sgt i32 %12, 0
br i1 %13, label %14, label %35
14: ; preds = %4
%15 = call double (...) @rand()
%16 = fmul double %15, 1.000000e+00
%17 = load double, double* @RAND_MAX
%18 = fdiv double %16, %17
%19 = load i32, i32* %7
%20 = load i32, i32* %6
%21 = sub nsw i32 %19, %20
%22 = sitofp i32 %21 to double
%23 = load i32, i32* %6
%24 = sitofp i32 %23 to double
%25 = call double @llvm.fmuladd.f64(double %18, double %22, double %24)
%26 = fptosi double %25 to i32
store i32 %26, i32* %10
%27 = load i32, i32* %6
%28 = load i32, i32* %7
%29 = load i32, i32* %10
%30 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 %27, i32 %28, i32 %29)
%31 = load double*, double** %5
%32 = load i32, i32* %10
%33 = load i32, i32* %7
%34 = call i32 @swap(double* %31, i32 %32, i32 %33)
br label %35
35: ; preds = %14, %4
%36 = load double*, double** %5
%37 = load i32, i32* %7
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds double, double* %36, i64 %38
%40 = load double, double* %39
store double %40, double* %9
%41 = load i32, i32* %6
%42 = sub nsw i32 %41, 1
store i32 %42, i32* %10
%43 = load i32, i32* %6
store i32 %43, i32* %11
br label %44
44: ; preds = %64, %35
%45 = load i32, i32* %11
%46 = load i32, i32* %7
%47 = icmp slt i32 %45, %46
br i1 %47, label %48, label %67
48: ; preds = %44
%49 = load double*, double** %5
%50 = load i32, i32* %11
%51 = sext i32 %50 to i64
%52 = getelementptr inbounds double, double* %49, i64 %51
%53 = load double, double* %52
%54 = load double, double* %9
%55 = fcmp ole double %53, %54
br i1 %55, label %56, label %63
56: ; preds = %48
%57 = load i32, i32* %10
%58 = add nsw i32 %57, 1
store i32 %58, i32* %10
%59 = load double*, double** %5
%60 = load i32, i32* %10
%61 = load i32, i32* %11
%62 = call i32 @swap(double* %59, i32 %60, i32 %61)
br label %63
63: ; preds = %56, %48
br label %64
64: ; preds = %63
%65 = load i32, i32* %11
%66 = add nsw i32 %65, 1
store i32 %66, i32* %11
br label %44
67: ; preds = %44
%68 = load double*, double** %5
%69 = load i32, i32* %10
%70 = add nsw i32 %69, 1
%71 = load i32, i32* %7
%72 = call i32 @swap(double* %68, i32 %70, i32 %71)
%73 = load i32, i32* %10
%74 = add nsw i32 %73, 1
ret i32 %74
}

LLVM IR (-O2):
@RAND_MAX = global double 0.000000e+00
@.str = [23 x i8] c"partition at %d,%d,%d\0A\00"
define i32 @partition(double* %0, i32 %1, i32 %2, i32 %3) {
%5 = icmp sgt i32 %3, 0
br i1 %5, label %6, label %17
6: ; preds = %4
%7 = tail call double (...) @rand()
%8 = load double, double* @RAND_MAX
%9 = fdiv double %7, %8
%10 = sub nsw i32 %2, %1
%11 = sitofp i32 %10 to double
%12 = sitofp i32 %1 to double
%13 = tail call double @llvm.fmuladd.f64(double %9, double %11, double %12)
%14 = fptosi double %13 to i32
%15 = tail call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 %1, i32 %2, i32 %14)
%16 = tail call i32 @swap(double* %0, i32 %14, i32 %2)
br label %17
17: ; preds = %6, %4
%18 = sext i32 %2 to i64
%19 = getelementptr inbounds double, double* %0, i64 %18
%20 = load double, double* %19
%21 = add nsw i32 %1, -1
%22 = icmp sgt i32 %2, %1
br i1 %22, label %23, label %40
23: ; preds = %17
%24 = sext i32 %1 to i64
br label %25
25: ; preds = %23, %35
%26 = phi i64 [ %24, %23 ], [ %37, %35 ]
%27 = phi i32 [ %21, %23 ], [ %36, %35 ]
%28 = getelementptr inbounds double, double* %0, i64 %26
%29 = load double, double* %28
%30 = fcmp ugt double %29, %20
br i1 %30, label %35, label %31
31: ; preds = %25
%32 = add nsw i32 %27, 1
%33 = trunc i64 %26 to i32
%34 = tail call i32 @swap(double* %0, i32 %32, i32 %33)
br label %35
35: ; preds = %25, %31
%36 = phi i32 [ %32, %31 ], [ %27, %25 ]
%37 = add nsw i64 %26, 1
%38 = trunc i64 %37 to i32
%39 = icmp eq i32 %38, %2
br i1 %39, label %40, label %25
40: ; preds = %35, %17
%41 = phi i32 [ %21, %17 ], [ %36, %35 ]
%42 = add nsw i32 %41, 1
%43 = tail call i32 @swap(double* %0, i32 %42, i32 %2)
ret i32 %42
}