#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*/ knapsack(int *, int *, int, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ scanf(char *, int *, ...);
int main() {
  printf("Knapsack 01 Algorithm\n");
  int profit[100], weight[100], N, W;
  int i, j;
  printf("Enter number of objects\n");
  scanf("%d", &N);
  printf("Enter %d profit-weight pairs\n", N);
  for (i = 1; i <= N; i++)
    scanf("%d %d", &profit[i], &weight[i]);
  printf("Enter weight of bag \n");
  scanf("%d", &W);
  knapsack(profit, weight, N, W);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x340,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xeb7(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xec0(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xeec(%rip),%rdi
lea    -0x334(%rbp),%rsi
mov    $0x0,%al
call   1040 <scanf@plt>
mov    -0x334(%rbp),%esi
lea    0xeb0(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x1,-0x33c(%rbp)
mov    -0x33c(%rbp),%eax
cmp    -0x334(%rbp),%eax
jg     11ef <main+0xbf>
movslq -0x33c(%rbp),%rax
lea    -0x1a0(%rbp),%rsi
shl    $0x2,%rax
add    %rax,%rsi
movslq -0x33c(%rbp),%rax
lea    -0x330(%rbp),%rdx
shl    $0x2,%rax
add    %rax,%rdx
lea    0xe7a(%rip),%rdi
mov    $0x0,%al
call   1040 <scanf@plt>
mov    -0x33c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x33c(%rbp)
jmp    1191 <main+0x61>
lea    0xe5e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xe4d(%rip),%rdi
lea    -0x338(%rbp),%rsi
mov    $0x0,%al
call   1040 <scanf@plt>
lea    -0x1a0(%rbp),%rdi
lea    -0x330(%rbp),%rsi
mov    -0x334(%rbp),%edx
mov    -0x338(%rbp),%ecx
call   1050 <knapsack@plt>
xor    %eax,%eax
add    $0x340,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x338,%rsp
lea    0xecf(%rip),%rdi
call   1030 <puts@plt>
lea    0xed9(%rip),%rdi
call   1030 <puts@plt>
lea    0xeb4(%rip),%rdi
lea    0x8(%rsp),%rsi
xor    %eax,%eax
call   1050 <scanf@plt>
mov    0x8(%rsp),%esi
lea    0xe7c(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
cmpl   $0x0,0x8(%rsp)
jle    11d6 <main+0x96>
lea    0x14(%rsp),%r14
lea    0x1a4(%rsp),%r12
lea    0xe78(%rip),%r15
xor    %ebx,%ebx
nopl   0x0(%rax,%rax,1)
mov    %r15,%rdi
mov    %r12,%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1050 <scanf@plt>
movslq 0x8(%rsp),%rax
add    $0x1,%rbx
add    $0x4,%r14
add    $0x4,%r12
cmp    %rax,%rbx
jl     11b0 <main+0x70>
lea    0xe75(%rip),%rdi
call   1030 <puts@plt>
lea    0xe38(%rip),%rdi
lea    0xc(%rsp),%rsi
xor    %eax,%eax
call   1050 <scanf@plt>
mov    0x8(%rsp),%edx
mov    0xc(%rsp),%ecx
lea    0x1a0(%rsp),%rdi
lea    0x10(%rsp),%rsi
call   1060 <knapsack@plt>
xor    %eax,%eax
add    $0x338,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [23 x i8] c"Knapsack 01 Algorithm\0A\00"
@.str.1 = [25 x i8] c"Enter number of objects\0A\00"
@.str.2 = [3 x i8] c"%d\00"
@.str.3 = [30 x i8] c"Enter %d profit-weight pairs\0A\00"
@.str.4 = [6 x i8] c"%d %d\00"
@.str.5 = [22 x i8] c"Enter weight of bag \0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [100 x i32]
%3 = alloca [100 x i32]
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i32 0, i32* %1
%8 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
%9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
%10 = call i32 (i8*, i32*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %4)
%11 = load i32, i32* %4
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0), i32 %11)
store i32 1, i32* %6
br label %13
13: ; preds = %25, %0
%14 = load i32, i32* %6
%15 = load i32, i32* %4
%16 = icmp sle i32 %14, %15
br i1 %16, label %17, label %28
17: ; preds = %13
%18 = load i32, i32* %6
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds [100 x i32], [100 x i32]* %2, i64 0, i64 %19
%21 = load i32, i32* %6
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds [100 x i32], [100 x i32]* %3, i64 0, i64 %22
%24 = call i32 (i8*, i32*, ...) @scanf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i32* %20, i32* %23)
br label %25
25: ; preds = %17
%26 = load i32, i32* %6
%27 = add nsw i32 %26, 1
store i32 %27, i32* %6
br label %13
28: ; preds = %13
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0))
%30 = call i32 (i8*, i32*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %5)
%31 = getelementptr inbounds [100 x i32], [100 x i32]* %2, i64 0, i64 0
%32 = getelementptr inbounds [100 x i32], [100 x i32]* %3, i64 0, i64 0
%33 = load i32, i32* %4
%34 = load i32, i32* %5
%35 = call i32 @knapsack(i32* %31, i32* %32, i32 %33, i32 %34)
ret i32 0
}

LLVM IR (-O2):
@.str.2 = [3 x i8] c"%d\00"
@.str.3 = [30 x i8] c"Enter %d profit-weight pairs\0A\00"
@.str.4 = [6 x i8] c"%d %d\00"
@str = [22 x i8] c"Knapsack 01 Algorithm\00"
@str.6 = [24 x i8] c"Enter number of objects\00"
@str.7 = [21 x i8] c"Enter weight of bag \00"
define i32 @main() {
%1 = alloca [100 x i32]
%2 = alloca [100 x i32]
%3 = alloca i32
%4 = alloca i32
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str, i64 0, i64 0))
%6 = bitcast [100 x i32]* %1 to i8*
%7 = bitcast [100 x i32]* %2 to i8*
%8 = bitcast i32* %3 to i8*
%9 = bitcast i32* %4 to i8*
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str.6, i64 0, i64 0))
%11 = call i32 (i8*, i32*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %3)
%12 = load i32, i32* %3
%13 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0), i32 %12)
%14 = load i32, i32* %3
%15 = icmp slt i32 %14, 1
br i1 %15, label %25, label %16
16: ; preds = %0, %16
%17 = phi i64 [ %21, %16 ], [ 1, %0 ]
%18 = getelementptr inbounds [100 x i32], [100 x i32]* %1, i64 0, i64 %17
%19 = getelementptr inbounds [100 x i32], [100 x i32]* %2, i64 0, i64 %17
%20 = call i32 (i8*, i32*, ...) @scanf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i32* %18, i32* %19)
%21 = add nuw nsw i64 %17, 1
%22 = load i32, i32* %3
%23 = sext i32 %22 to i64
%24 = icmp slt i64 %17, %23
br i1 %24, label %16, label %25
25: ; preds = %16, %0
%26 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str.7, i64 0, i64 0))
%27 = call i32 (i8*, i32*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %4)
%28 = getelementptr inbounds [100 x i32], [100 x i32]* %1, i64 0, i64 0
%29 = getelementptr inbounds [100 x i32], [100 x i32]* %2, i64 0, i64 0
%30 = load i32, i32* %3
%31 = load i32, i32* %4
%32 = call i32 @knapsack(i32* %28, i32* %29, i32 %30, i32 %31)
ret i32 0
}