#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 /*<<< orphan*/ quickSort(double *, double *, int);
double fractionalKnapsack(double value[], double weight[],
                          double knapsackCapacity, int numberItems) {
  quickSort(value, weight, numberItems);
  double currentWeight = 0.0f;
  double currentValue = 0.0f;
  int item;
  for (item = 0; item < numberItems; item++) {
    if (currentWeight + weight[item] <= knapsackCapacity) {
      currentWeight += weight[item];
      currentValue += value[item];
    } else {
      currentValue +=
          (knapsackCapacity - currentWeight) * value[item] / weight[item];
      break;
    }
  }
  return currentValue;
}


x86 assembly (clang, -O0):
<fractionalKnapsack>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movsd  %xmm0,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1030 <quickSort@plt>
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x28(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
mov    -0x34(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    11f5 <fractionalKnapsack+0xe5>
movsd  -0x28(%rbp),%xmm1
mov    -0x10(%rbp),%rax
movslq -0x34(%rbp),%rcx
addsd  (%rax,%rcx,8),%xmm1
movsd  -0x18(%rbp),%xmm0
ucomisd %xmm1,%xmm0
jb     11af <fractionalKnapsack+0x9f>
mov    -0x10(%rbp),%rax
movslq -0x34(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
addsd  -0x28(%rbp),%xmm0
movsd  %xmm0,-0x28(%rbp)
mov    -0x8(%rbp),%rax
movslq -0x34(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
addsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x30(%rbp)
jmp    11e2 <fractionalKnapsack+0xd2>
movsd  -0x18(%rbp),%xmm0
subsd  -0x28(%rbp),%xmm0
mov    -0x8(%rbp),%rax
movslq -0x34(%rbp),%rcx
mulsd  (%rax,%rcx,8),%xmm0
mov    -0x10(%rbp),%rax
movslq -0x34(%rbp),%rcx
divsd  (%rax,%rcx,8),%xmm0
addsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x30(%rbp)
jmp    11f5 <fractionalKnapsack+0xe5>
jmp    11e7 <fractionalKnapsack+0xd7>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    114f <fractionalKnapsack+0x3f>
movsd  -0x30(%rbp),%xmm0
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fractionalKnapsack>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edx,%ebp
movsd  %xmm0,0x8(%rsp)
mov    %rsi,%rbx
mov    %rdi,%r14
call   1030 <quickSort@plt>
test   %ebp,%ebp
jle    1174 <fractionalKnapsack+0x64>
mov    %ebp,%ecx
xorpd  %xmm0,%xmm0
xor    %eax,%eax
xorpd  %xmm2,%xmm2
xorpd  %xmm1,%xmm1
movsd  0x8(%rsp),%xmm4
cs nopw 0x0(%rax,%rax,1)
nop
movsd  (%rbx,%rax,8),%xmm3
addsd  %xmm3,%xmm1
ucomisd %xmm1,%xmm4
jb     117a <fractionalKnapsack+0x6a>
addsd  (%r14,%rax,8),%xmm0
add    $0x1,%rax
movapd %xmm1,%xmm2
cmp    %rax,%rcx
jne    1150 <fractionalKnapsack+0x40>
jmp    118c <fractionalKnapsack+0x7c>
xorpd  %xmm0,%xmm0
jmp    118c <fractionalKnapsack+0x7c>
subsd  %xmm2,%xmm4
mulsd  (%r14,%rax,8),%xmm4
divsd  %xmm3,%xmm4
addsd  %xmm4,%xmm0
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define double @fractionalKnapsack(double* %0, double* %1, double %2, i32 %3) {
%5 = alloca double*
%6 = alloca double*
%7 = alloca double
%8 = alloca i32
%9 = alloca double
%10 = alloca double
%11 = alloca i32
store double* %0, double** %5
store double* %1, double** %6
store double %2, double* %7
store i32 %3, i32* %8
%12 = load double*, double** %5
%13 = load double*, double** %6
%14 = load i32, i32* %8
%15 = call i32 @quickSort(double* %12, double* %13, i32 %14)
store double 0.000000e+00, double* %9
store double 0.000000e+00, double* %10
store i32 0, i32* %11
br label %16
16: ; preds = %64, %4
%17 = load i32, i32* %11
%18 = load i32, i32* %8
%19 = icmp slt i32 %17, %18
br i1 %19, label %20, label %67
20: ; preds = %16
%21 = load double, double* %9
%22 = load double*, double** %6
%23 = load i32, i32* %11
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds double, double* %22, i64 %24
%26 = load double, double* %25
%27 = fadd double %21, %26
%28 = load double, double* %7
%29 = fcmp ole double %27, %28
br i1 %29, label %30, label %45
30: ; preds = %20
%31 = load double*, double** %6
%32 = load i32, i32* %11
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds double, double* %31, i64 %33
%35 = load double, double* %34
%36 = load double, double* %9
%37 = fadd double %36, %35
store double %37, double* %9
%38 = load double*, double** %5
%39 = load i32, i32* %11
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds double, double* %38, i64 %40
%42 = load double, double* %41
%43 = load double, double* %10
%44 = fadd double %43, %42
store double %44, double* %10
br label %63
45: ; preds = %20
%46 = load double, double* %7
%47 = load double, double* %9
%48 = fsub double %46, %47
%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 = fmul double %48, %53
%55 = load double*, double** %6
%56 = load i32, i32* %11
%57 = sext i32 %56 to i64
%58 = getelementptr inbounds double, double* %55, i64 %57
%59 = load double, double* %58
%60 = fdiv double %54, %59
%61 = load double, double* %10
%62 = fadd double %61, %60
store double %62, double* %10
br label %67
63: ; preds = %30
br label %64
64: ; preds = %63
%65 = load i32, i32* %11
%66 = add nsw i32 %65, 1
store i32 %66, i32* %11
br label %16
67: ; preds = %45, %16
%68 = load double, double* %10
ret double %68
}

LLVM IR (-O2):
define double @fractionalKnapsack(double* %0, double* %1, double %2, i32 %3) {
%5 = tail call i32 @quickSort(double* %0, double* %1, i32 %3)
%6 = icmp sgt i32 %3, 0
br i1 %6, label %7, label %31
7: ; preds = %4
%8 = zext i32 %3 to i64
br label %9
9: ; preds = %7, %17
%10 = phi i64 [ 0, %7 ], [ %21, %17 ]
%11 = phi double [ 0.000000e+00, %7 ], [ %20, %17 ]
%12 = phi double [ 0.000000e+00, %7 ], [ %15, %17 ]
%13 = getelementptr inbounds double, double* %1, i64 %10
%14 = load double, double* %13
%15 = fadd double %12, %14
%16 = fcmp ugt double %15, %2
br i1 %16, label %23, label %17
17: ; preds = %9
%18 = getelementptr inbounds double, double* %0, i64 %10
%19 = load double, double* %18
%20 = fadd double %11, %19
%21 = add nuw nsw i64 %10, 1
%22 = icmp eq i64 %21, %8
br i1 %22, label %31, label %9
23: ; preds = %9
%24 = and i64 %10, 4294967295
%25 = fsub double %2, %12
%26 = getelementptr inbounds double, double* %0, i64 %24
%27 = load double, double* %26
%28 = fmul double %25, %27
%29 = fdiv double %28, %14
%30 = fadd double %11, %29
br label %31
31: ; preds = %17, %4, %23
%32 = phi double [ %30, %23 ], [ 0.000000e+00, %4 ], [ %20, %17 ]
ret double %32
}