#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 */
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, int, int, int);
int get_int_line(int /*<<< orphan*/ *);
int /*<<< orphan*/ get_item_prices(int *, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ solve(int, int *, int, int *);
int main(int argc, char **argv) {
  FILE *in_file, *out_file;
  int case_count;
  int current_case;
  int credit_amount;
  int num_items;
  int items[2001];
  int solution[2];
  if (argc != 3) {
    printf("Usage: A-Store-Credit input_file output_file\n");
    return 1;
  }
  in_file = fopen(argv[1], "r");
  if (in_file == NULL) {
    printf("Could not read from %s\n", argv[1]);
    return 1;
  }
  out_file = fopen(argv[2], "w");
  if (out_file == NULL) {
    printf("Could not write to %s\n", argv[2]);
    fclose(in_file);
    return 1;
  }
  case_count = get_int_line(in_file);
  current_case = 1;
  while (current_case <= case_count) {
    credit_amount = get_int_line(in_file);
    num_items = get_int_line(in_file);
    get_item_prices(items, in_file);
    solve(credit_amount, items, num_items, solution);
    fprintf(out_file, "Case #%i: %i %i\n", current_case, solution[0],
            solution[1]);
    current_case++;
  }
  fclose(in_file);
  fclose(out_file);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1f90,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     11ad <main+0x3d>
lea    0xe66(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12e7 <main+0x177>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe72(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11f2 <main+0x82>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xe51(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12e7 <main+0x177>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
lea    0xe47(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1240 <main+0xd0>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rsi
lea    0xe26(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x1,-0x4(%rbp)
jmp    12e7 <main+0x177>
mov    -0x18(%rbp),%rdi
call   1080 <get_int_line@plt>
mov    %eax,-0x24(%rbp)
movl   $0x1,-0x28(%rbp)
mov    -0x28(%rbp),%eax
cmp    -0x24(%rbp),%eax
jg     12ce <main+0x15e>
mov    -0x18(%rbp),%rdi
call   1080 <get_int_line@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x18(%rbp),%rdi
call   1080 <get_int_line@plt>
mov    %eax,-0x30(%rbp)
lea    -0x1f80(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1070 <get_item_prices@plt>
mov    -0x2c(%rbp),%edi
lea    -0x1f80(%rbp),%rsi
mov    -0x30(%rbp),%edx
lea    -0x1f88(%rbp),%rcx
call   1030 <solve@plt>
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%edx
mov    -0x1f88(%rbp),%ecx
mov    -0x1f84(%rbp),%r8d
lea    0xda6(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    1253 <main+0xe3>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1f90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1f58,%rsp
cmp    $0x3,%edi
jne    127c <main+0xfc>
mov    %rsi,%rbx
mov    0x8(%rsi),%rdi
lea    0xe58(%rip),%rsi
call   10a0 <fopen@plt>
test   %rax,%rax
je     128a <main+0x10a>
mov    %rax,%r13
mov    0x10(%rbx),%rdi
lea    0xe56(%rip),%rsi
call   10a0 <fopen@plt>
test   %rax,%rax
je     129e <main+0x11e>
mov    %rax,%rbp
mov    %r13,%rdi
call   1090 <get_int_line@plt>
mov    %rax,(%rsp)
test   %eax,%eax
jle    1268 <main+0xe8>
mov    (%rsp),%rax
neg    %eax
mov    %rax,(%rsp)
mov    $0x1,%ebx
nopl   0x0(%rax,%rax,1)
mov    %r13,%rdi
call   1090 <get_int_line@plt>
mov    %eax,%r12d
mov    %r13,%rdi
call   1090 <get_int_line@plt>
mov    %eax,%r14d
lea    0x10(%rsp),%r15
mov    %r15,%rdi
mov    %r13,%rsi
call   1080 <get_item_prices@plt>
mov    %r12d,%edi
mov    %r15,%rsi
mov    %r14d,%edx
lea    0x8(%rsp),%rcx
call   1030 <solve@plt>
mov    0x8(%rsp),%ecx
mov    0xc(%rsp),%r8d
mov    %rbp,%rdi
lea    0xde7(%rip),%rsi
mov    %ebx,%edx
call   1070 <fprintf@plt>
mov    (%rsp),%rax
add    %ebx,%eax
add    $0x1,%eax
mov    %ebx,%ecx
add    $0x1,%ecx
mov    %ecx,%ebx
cmp    $0x1,%eax
jne    1200 <main+0x80>
mov    %r13,%rdi
call   1050 <fclose@plt>
mov    %rbp,%rdi
call   1050 <fclose@plt>
xor    %eax,%eax
jmp    12bd <main+0x13d>
lea    0xdc1(%rip),%rdi
call   1040 <puts@plt>
jmp    12b8 <main+0x138>
mov    0x8(%rbx),%rsi
lea    0xd6d(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
jmp    12b8 <main+0x138>
mov    0x10(%rbx),%rsi
lea    0xd73(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r13,%rdi
call   1050 <fclose@plt>
mov    $0x1,%eax
add    $0x1f58,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [46 x i8] c"Usage: A-Store-Credit input_file output_file\0A\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [24 x i8] c"Could not read from %s\0A\00"
@.str.3 = [2 x i8] c"w\00"
@.str.4 = [23 x i8] c"Could not write to %s\0A\00"
@.str.5 = [17 x i8] c"Case #%i: %i %i\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca [2001 x i32]
%13 = alloca [2 x i32]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%14 = load i32, i32* %4
%15 = icmp ne i32 %14, 3
br i1 %15, label %16, label %18
16: ; preds = %2
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %78
18: ; preds = %2
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 1
%21 = load i8*, i8** %20
%22 = call i32* @fopen(i8* %21, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %22, i32** %6
%23 = load i32*, i32** %6
%24 = icmp eq i32* %23, null
br i1 %24, label %25, label %30
25: ; preds = %18
%26 = load i8**, i8*** %5
%27 = getelementptr inbounds i8*, i8** %26, i64 1
%28 = load i8*, i8** %27
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i8* %28)
store i32 1, i32* %3
br label %78
30: ; preds = %18
%31 = load i8**, i8*** %5
%32 = getelementptr inbounds i8*, i8** %31, i64 2
%33 = load i8*, i8** %32
%34 = call i32* @fopen(i8* %33, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %34, i32** %7
%35 = load i32*, i32** %7
%36 = icmp eq i32* %35, null
br i1 %36, label %37, label %44
37: ; preds = %30
%38 = load i8**, i8*** %5
%39 = getelementptr inbounds i8*, i8** %38, i64 2
%40 = load i8*, i8** %39
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0), i8* %40)
%42 = load i32*, i32** %6
%43 = call i32 @fclose(i32* %42)
store i32 1, i32* %3
br label %78
44: ; preds = %30
%45 = load i32*, i32** %6
%46 = call i32 @get_int_line(i32* %45)
store i32 %46, i32* %8
store i32 1, i32* %9
br label %47
47: ; preds = %51, %44
%48 = load i32, i32* %9
%49 = load i32, i32* %8
%50 = icmp sle i32 %48, %49
br i1 %50, label %51, label %73
51: ; preds = %47
%52 = load i32*, i32** %6
%53 = call i32 @get_int_line(i32* %52)
store i32 %53, i32* %10
%54 = load i32*, i32** %6
%55 = call i32 @get_int_line(i32* %54)
store i32 %55, i32* %11
%56 = getelementptr inbounds [2001 x i32], [2001 x i32]* %12, i64 0, i64 0
%57 = load i32*, i32** %6
%58 = call i32 @get_item_prices(i32* %56, i32* %57)
%59 = load i32, i32* %10
%60 = getelementptr inbounds [2001 x i32], [2001 x i32]* %12, i64 0, i64 0
%61 = load i32, i32* %11
%62 = getelementptr inbounds [2 x i32], [2 x i32]* %13, i64 0, i64 0
%63 = call i32 @solve(i32 %59, i32* %60, i32 %61, i32* %62)
%64 = load i32*, i32** %7
%65 = load i32, i32* %9
%66 = getelementptr inbounds [2 x i32], [2 x i32]* %13, i64 0, i64 0
%67 = load i32, i32* %66
%68 = getelementptr inbounds [2 x i32], [2 x i32]* %13, i64 0, i64 1
%69 = load i32, i32* %68
%70 = call i32 @fprintf(i32* %64, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0), i32 %65, i32 %67, i32 %69)
%71 = load i32, i32* %9
%72 = add nsw i32 %71, 1
store i32 %72, i32* %9
br label %47
73: ; preds = %47
%74 = load i32*, i32** %6
%75 = call i32 @fclose(i32* %74)
%76 = load i32*, i32** %7
%77 = call i32 @fclose(i32* %76)
store i32 0, i32* %3
br label %78
78: ; preds = %73, %37, %25, %16
%79 = load i32, i32* %3
ret i32 %79
}

LLVM IR (-O2):
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [24 x i8] c"Could not read from %s\0A\00"
@.str.3 = [2 x i8] c"w\00"
@.str.4 = [23 x i8] c"Could not write to %s\0A\00"
@.str.5 = [17 x i8] c"Case #%i: %i %i\0A\00"
@str = [45 x i8] c"Usage: A-Store-Credit input_file output_file\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [2001 x i32]
%4 = alloca [2 x i32]
%5 = bitcast [2001 x i32]* %3 to i8*
%6 = bitcast [2 x i32]* %4 to i8*
%7 = icmp eq i32 %0, 3
br i1 %7, label %10, label %8
8: ; preds = %2
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([45 x i8], [45 x i8]* @str, i64 0, i64 0))
br label %48
10: ; preds = %2
%11 = getelementptr inbounds i8*, i8** %1, i64 1
%12 = load i8*, i8** %11
%13 = tail call i32* @fopen(i8* %12, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%14 = icmp eq i32* %13, null
br i1 %14, label %15, label %18
15: ; preds = %10
%16 = load i8*, i8** %11
%17 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i8* %16)
br label %48
18: ; preds = %10
%19 = getelementptr inbounds i8*, i8** %1, i64 2
%20 = load i8*, i8** %19
%21 = tail call i32* @fopen(i8* %20, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%22 = icmp eq i32* %21, null
br i1 %22, label %23, label %27
23: ; preds = %18
%24 = load i8*, i8** %19
%25 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0), i8* %24)
%26 = tail call i32 @fclose(i32* %13)
br label %48
27: ; preds = %18
%28 = tail call i32 @get_int_line(i32* %13)
%29 = icmp slt i32 %28, 1
br i1 %29, label %45, label %30
30: ; preds = %27
%31 = getelementptr inbounds [2001 x i32], [2001 x i32]* %3, i64 0, i64 0
%32 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%33 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
br label %34
34: ; preds = %30, %34
%35 = phi i32 [ 1, %30 ], [ %43, %34 ]
%36 = call i32 @get_int_line(i32* %13)
%37 = call i32 @get_int_line(i32* %13)
%38 = call i32 @get_item_prices(i32* %31, i32* %13)
%39 = call i32 @solve(i32 %36, i32* %31, i32 %37, i32* %32)
%40 = load i32, i32* %32
%41 = load i32, i32* %33
%42 = call i32 @fprintf(i32* %21, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0), i32 %35, i32 %40, i32 %41)
%43 = add nuw i32 %35, 1
%44 = icmp eq i32 %35, %28
br i1 %44, label %45, label %34
45: ; preds = %34, %27
%46 = call i32 @fclose(i32* %13)
%47 = call i32 @fclose(i32* %21)
br label %48
48: ; preds = %45, %23, %15, %8
%49 = phi i32 [ 1, %8 ], [ 1, %15 ], [ 1, %23 ], [ 0, %45 ]
ret i32 %49
}