#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 max(int, int);
int knapsackRecursive(int weight[], int profit[], int n, int knapSackWeight) {
  if (n == 0 || knapSackWeight == 0)
    return 0;
  if (weight[n - 1] > knapSackWeight)
    return knapsackRecursive(weight, profit, n - 1, knapSackWeight);
  return max(profit[n - 1] + knapsackRecursive(weight, profit, n - 1,
                                               knapSackWeight - weight[n - 1]),
             knapsackRecursive(weight, profit, n - 1, knapSackWeight));
}


x86 assembly (clang, -O0):
<knapsackRecursive>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %ecx,-0x20(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     114a <knapsackRecursive+0x2a>
cmpl   $0x0,-0x20(%rbp)
jne    1156 <knapsackRecursive+0x36>
movl   $0x0,-0x4(%rbp)
jmp    11f6 <knapsackRecursive+0xd6>
mov    -0x10(%rbp),%rax
mov    -0x1c(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,4),%eax
cmp    -0x20(%rbp),%eax
jle    118d <knapsackRecursive+0x6d>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
sub    $0x1,%edx
mov    -0x20(%rbp),%ecx
call   1040 <knapsackRecursive@plt>
mov    %eax,-0x4(%rbp)
jmp    11f6 <knapsackRecursive+0xd6>
mov    -0x18(%rbp),%rax
mov    -0x1c(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,4),%eax
mov    %eax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
sub    $0x1,%edx
mov    -0x20(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    -0x1c(%rbp),%r8d
sub    $0x1,%r8d
movslq %r8d,%r8
sub    (%rax,%r8,4),%ecx
call   1040 <knapsackRecursive@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%eax
add    %ecx,%eax
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
sub    $0x1,%edx
mov    -0x20(%rbp),%ecx
call   1040 <knapsackRecursive@plt>
mov    -0x24(%rbp),%edi
mov    %eax,%esi
call   1030 <max@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<knapsackRecursive>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
test   %ecx,%ecx
je     1189 <knapsackRecursive+0x69>
mov    %ecx,%r15d
mov    %rsi,%r14
mov    %rdi,%rbx
movslq %edx,%rbp
nopl   0x0(%rax,%rax,1)
sub    $0x1,%rbp
jb     1189 <knapsackRecursive+0x69>
mov    (%rbx,%rbp,4),%eax
cmp    %r15d,%eax
jg     1140 <knapsackRecursive+0x20>
mov    (%r14,%rbp,4),%r12d
mov    %r15d,%ecx
sub    %eax,%ecx
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %ebp,%edx
call   1040 <knapsackRecursive@plt>
add    %eax,%r12d
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %ebp,%edx
mov    %r15d,%ecx
call   1040 <knapsackRecursive@plt>
mov    %r12d,%edi
mov    %eax,%esi
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <max@plt>
xor    %eax,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @knapsackRecursive(i32* %0, i32* %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i32
store i32* %0, i32** %6
store i32* %1, i32** %7
store i32 %2, i32* %8
store i32 %3, i32* %9
%10 = load i32, i32* %8
%11 = icmp eq i32 %10, 0
br i1 %11, label %15, label %12
12: ; preds = %4
%13 = load i32, i32* %9
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %12, %4
store i32 0, i32* %5
br label %60
16: ; preds = %12
%17 = load i32*, i32** %6
%18 = load i32, i32* %8
%19 = sub nsw i32 %18, 1
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds i32, i32* %17, i64 %20
%22 = load i32, i32* %21
%23 = load i32, i32* %9
%24 = icmp sgt i32 %22, %23
br i1 %24, label %25, label %32
25: ; preds = %16
%26 = load i32*, i32** %6
%27 = load i32*, i32** %7
%28 = load i32, i32* %8
%29 = sub nsw i32 %28, 1
%30 = load i32, i32* %9
%31 = call i32 @knapsackRecursive(i32* %26, i32* %27, i32 %29, i32 %30)
store i32 %31, i32* %5
br label %60
32: ; preds = %16
%33 = load i32*, i32** %7
%34 = load i32, i32* %8
%35 = sub nsw i32 %34, 1
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds i32, i32* %33, i64 %36
%38 = load i32, i32* %37
%39 = load i32*, i32** %6
%40 = load i32*, i32** %7
%41 = load i32, i32* %8
%42 = sub nsw i32 %41, 1
%43 = load i32, i32* %9
%44 = load i32*, i32** %6
%45 = load i32, i32* %8
%46 = sub nsw i32 %45, 1
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds i32, i32* %44, i64 %47
%49 = load i32, i32* %48
%50 = sub nsw i32 %43, %49
%51 = call i32 @knapsackRecursive(i32* %39, i32* %40, i32 %42, i32 %50)
%52 = add nsw i32 %38, %51
%53 = load i32*, i32** %6
%54 = load i32*, i32** %7
%55 = load i32, i32* %8
%56 = sub nsw i32 %55, 1
%57 = load i32, i32* %9
%58 = call i32 @knapsackRecursive(i32* %53, i32* %54, i32 %56, i32 %57)
%59 = call i32 @max(i32 %52, i32 %58)
store i32 %59, i32* %5
br label %60
60: ; preds = %32, %25, %15
%61 = load i32, i32* %5
ret i32 %61
}

LLVM IR (-O2):
define i32 @knapsackRecursive(i32* %0, i32* %1, i32 %2, i32 %3) {
%5 = icmp eq i32 %3, 0
br i1 %5, label %16, label %6
6: ; preds = %4
%7 = sext i32 %2 to i64
br label %8
8: ; preds = %6, %11
%9 = phi i64 [ %7, %6 ], [ %12, %11 ]
%10 = icmp eq i64 %9, 0
br i1 %10, label %16, label %11
11: ; preds = %8
%12 = add nsw i64 %9, -1
%13 = getelementptr inbounds i32, i32* %0, i64 %12
%14 = load i32, i32* %13
%15 = icmp sgt i32 %14, %3
br i1 %15, label %8, label %18
16: ; preds = %8, %4, %18
%17 = phi i32 [ %26, %18 ], [ 0, %4 ], [ 0, %8 ]
ret i32 %17
18: ; preds = %11
%19 = trunc i64 %12 to i32
%20 = getelementptr inbounds i32, i32* %1, i64 %12
%21 = load i32, i32* %20
%22 = sub nsw i32 %3, %14
%23 = tail call i32 @knapsackRecursive(i32* %0, i32* %1, i32 %19, i32 %22)
%24 = add nsw i32 %23, %21
%25 = tail call i32 @knapsackRecursive(i32* %0, i32* %1, i32 %19, i32 %3)
%26 = tail call i32 @max(i32 %24, i32 %25)
br label %16
}