#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 */
scalar_t__ EOF;
scalar_t__ calloc(int, int);
int /*<<< orphan*/ knapsack(int *, int *, int, int);
scalar_t__ scanf(char *, int *, int *);
int main(void) {
  int w, n;
  while (scanf("%d%d", &w, &n) != EOF) {
    if (w == 0 && n == 0)
      break;
    int i;
    int *fun = (int *)calloc(n, sizeof(int));
    int *cost = (int *)calloc(n, sizeof(int));
    for (i = 0; i < n; i++)
      scanf("%d%d", cost + i, fun + i);
    knapsack(cost, fun, w, n);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xeba(%rip),%rdi
lea    -0x8(%rbp),%rsi
lea    -0xc(%rbp),%rdx
call   1040 <scanf@plt>
mov    0x2e8e(%rip),%rcx
cmp    (%rcx),%rax
je     1201 <main+0xd1>
cmpl   $0x0,-0x8(%rbp)
jne    117c <main+0x4c>
cmpl   $0x0,-0xc(%rbp)
jne    117c <main+0x4c>
jmp    1201 <main+0xd1>
mov    -0xc(%rbp),%edi
mov    $0x4,%esi
call   1030 <calloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0xc(%rbp),%edi
mov    $0x4,%esi
call   1030 <calloc@plt>
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    11e9 <main+0xb9>
mov    -0x20(%rbp),%rsi
movslq -0x10(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rsi
mov    -0x18(%rbp),%rdx
movslq -0x10(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rdx
lea    0xe2a(%rip),%rdi
call   1040 <scanf@plt>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    11a5 <main+0x75>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x8(%rbp),%edx
mov    -0xc(%rbp),%ecx
call   1050 <knapsack@plt>
jmp    113f <main+0xf>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
lea    0xebe(%rip),%rdi
lea    0x4(%rsp),%rsi
mov    %rsp,%rdx
call   1040 <scanf@plt>
mov    0x2e92(%rip),%rcx
cmp    (%rcx),%rax
jne    116c <main+0x3c>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe8d(%rip),%rbx
jmp    11ab <main+0x7b>
cs nopw 0x0(%rax,%rax,1)
nop
mov    0x4(%rsp),%edx
mov    %r13,%rdi
mov    %r14,%rsi
call   1050 <knapsack@plt>
mov    %rbx,%rdi
lea    0x4(%rsp),%rsi
mov    %rsp,%rdx
call   1040 <scanf@plt>
mov    0x2e42(%rip),%rcx
cmp    (%rcx),%rax
je     115b <main+0x2b>
mov    (%rsp),%edi
mov    0x4(%rsp),%eax
or     %edi,%eax
je     115b <main+0x2b>
mov    $0x4,%esi
call   1030 <calloc@plt>
mov    %rax,%r14
mov    (%rsp),%edi
mov    $0x4,%esi
call   1030 <calloc@plt>
mov    %rax,%r13
mov    (%rsp),%ecx
test   %ecx,%ecx
jle    1180 <main+0x50>
mov    %r13,%rbp
mov    %r14,%r12
xor    %r15d,%r15d
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %rbx,%rdi
mov    %rbp,%rsi
mov    %r12,%rdx
call   1040 <scanf@plt>
add    $0x1,%r15
movslq (%rsp),%rcx
add    $0x4,%r12
add    $0x4,%rbp
cmp    %rcx,%r15
jl     11f0 <main+0xc0>
jmp    1180 <main+0x50>

LLVM IR (-O0):
@.str = [5 x i8] c"%d%d\00"
@EOF = global i64 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i32*
store i32 0, i32* %1
br label %7
7: ; preds = %42, %0
%8 = call i64 @scanf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32* %2, i32* %3)
%9 = load i64, i64* @EOF
%10 = icmp ne i64 %8, %9
br i1 %10, label %11, label %48
11: ; preds = %7
%12 = load i32, i32* %2
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %18
14: ; preds = %11
%15 = load i32, i32* %3
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %14
br label %48
18: ; preds = %14, %11
%19 = load i32, i32* %3
%20 = call i64 @calloc(i32 %19, i32 4)
%21 = inttoptr i64 %20 to i32*
store i32* %21, i32** %5
%22 = load i32, i32* %3
%23 = call i64 @calloc(i32 %22, i32 4)
%24 = inttoptr i64 %23 to i32*
store i32* %24, i32** %6
store i32 0, i32* %4
br label %25
25: ; preds = %39, %18
%26 = load i32, i32* %4
%27 = load i32, i32* %3
%28 = icmp slt i32 %26, %27
br i1 %28, label %29, label %42
29: ; preds = %25
%30 = load i32*, i32** %6
%31 = load i32, i32* %4
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds i32, i32* %30, i64 %32
%34 = load i32*, i32** %5
%35 = load i32, i32* %4
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds i32, i32* %34, i64 %36
%38 = call i64 @scanf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32* %33, i32* %37)
br label %39
39: ; preds = %29
%40 = load i32, i32* %4
%41 = add nsw i32 %40, 1
store i32 %41, i32* %4
br label %25
42: ; preds = %25
%43 = load i32*, i32** %6
%44 = load i32*, i32** %5
%45 = load i32, i32* %2
%46 = load i32, i32* %3
%47 = call i32 @knapsack(i32* %43, i32* %44, i32 %45, i32 %46)
br label %7
48: ; preds = %17, %7
ret i32 0
}

LLVM IR (-O2):
@.str = [5 x i8] c"%d%d\00"
@EOF = global i64 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = bitcast i32* %1 to i8*
%4 = bitcast i32* %2 to i8*
%5 = call i64 @scanf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32* %1, i32* %2)
%6 = load i64, i64* @EOF
%7 = icmp eq i64 %5, %6
br i1 %7, label %38, label %8
8: ; preds = %0, %31
%9 = load i32, i32* %1
%10 = icmp eq i32 %9, 0
%11 = load i32, i32* %2
%12 = icmp eq i32 %11, 0
%13 = select i1 %10, i1 %12, i1 false
br i1 %13, label %38, label %14
14: ; preds = %8
%15 = call i64 @calloc(i32 %11, i32 4)
%16 = inttoptr i64 %15 to i32*
%17 = load i32, i32* %2
%18 = call i64 @calloc(i32 %17, i32 4)
%19 = inttoptr i64 %18 to i32*
%20 = load i32, i32* %2
%21 = icmp sgt i32 %20, 0
br i1 %21, label %22, label %31
22: ; preds = %14, %22
%23 = phi i64 [ %27, %22 ], [ 0, %14 ]
%24 = getelementptr inbounds i32, i32* %19, i64 %23
%25 = getelementptr inbounds i32, i32* %16, i64 %23
%26 = call i64 @scanf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32* %24, i32* %25)
%27 = add nuw nsw i64 %23, 1
%28 = load i32, i32* %2
%29 = sext i32 %28 to i64
%30 = icmp slt i64 %27, %29
br i1 %30, label %22, label %31
31: ; preds = %22, %14
%32 = phi i32 [ %20, %14 ], [ %28, %22 ]
%33 = load i32, i32* %1
%34 = call i32 @knapsack(i32* %19, i32* %16, i32 %33, i32 %32)
%35 = call i64 @scanf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32* %1, i32* %2)
%36 = load i64, i64* @EOF
%37 = icmp eq i64 %35, %36
br i1 %37, label %38, label %8
38: ; preds = %31, %8, %0
ret i32 0
}