#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*/ binarySearch(int, int, short *, int, int);
int /*<<< orphan*/ freopen(char *, char *, int /*<<< orphan*/);
scalar_t__ malloc(int);
int /*<<< orphan*/ printf(char *, int /*<<< orphan*/);
int /*<<< orphan*/ scanf(char *, ...);
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ stdout;
int main() {
  short *pages;
  int books, scribes, i, sum;
  freopen("books.in", "r", stdin);
  freopen("books.out", "w", stdout);
  scanf("%d %d", &books, &scribes);
  pages = (short *)malloc(books * sizeof(short));
  for (i = sum = 0; i < books; ++i) {
    scanf("%hd", &pages[i]);
    sum += pages[i];
  }
  printf("%hd\n", binarySearch(books, scribes, pages, 1, sum));
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e82(%rip),%rax
mov    (%rax),%edx
lea    0xe91(%rip),%rdi
lea    0xe93(%rip),%rsi
call   1040 <freopen@plt>
mov    0x2e56(%rip),%rax
mov    (%rax),%edx
lea    0xe80(%rip),%rdi
lea    0xe83(%rip),%rsi
call   1040 <freopen@plt>
lea    0xe79(%rip),%rdi
lea    -0x14(%rbp),%rsi
lea    -0x18(%rbp),%rdx
mov    $0x0,%al
call   1070 <scanf@plt>
movslq -0x14(%rbp),%rax
shl    $0x1,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x10(%rbp)
movl   $0x0,-0x20(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    1217 <main+0xc7>
mov    -0x10(%rbp),%rsi
movslq -0x1c(%rbp),%rax
shl    $0x1,%rax
add    %rax,%rsi
lea    0xe2d(%rip),%rdi
mov    $0x0,%al
call   1070 <scanf@plt>
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movswl (%rax,%rcx,2),%eax
add    -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11ce <main+0x7e>
mov    -0x14(%rbp),%edi
mov    -0x18(%rbp),%esi
mov    -0x10(%rbp),%rdx
mov    -0x20(%rbp),%r8d
mov    $0x1,%ecx
call   1050 <binarySearch@plt>
mov    %eax,%esi
lea    0xde9(%rip),%rdi
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    0x2e85(%rip),%rax
mov    (%rax),%edx
lea    0xe94(%rip),%rdi
lea    0xe96(%rip),%rsi
call   1040 <freopen@plt>
mov    0x2e59(%rip),%rax
mov    (%rax),%edx
lea    0xe83(%rip),%rdi
lea    0xe86(%rip),%rsi
call   1040 <freopen@plt>
lea    0xe7c(%rip),%rdi
xor    %r12d,%r12d
lea    0x8(%rsp),%rsi
lea    0xc(%rsp),%rdx
xor    %eax,%eax
call   1070 <scanf@plt>
mov    0x8(%rsp),%edi
add    %edi,%edi
call   1060 <malloc@plt>
mov    %rax,%r14
mov    0x8(%rsp),%edi
test   %edi,%edi
jle    1205 <main+0xb5>
lea    0xe51(%rip),%r15
mov    %r14,%rbx
xor    %ebp,%ebp
xor    %r12d,%r12d
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r15,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1070 <scanf@plt>
movswl (%rbx),%eax
add    %eax,%r12d
add    $0x1,%rbp
movslq 0x8(%rsp),%rdi
add    $0x2,%rbx
cmp    %rdi,%rbp
jl     11e0 <main+0x90>
mov    0xc(%rsp),%esi
mov    %r14,%rdx
mov    $0x1,%ecx
mov    %r12d,%r8d
call   1050 <binarySearch@plt>
lea    0xe01(%rip),%rdi
mov    %eax,%esi
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [9 x i8] c"books.in\00"
@.str.1 = [2 x i8] c"r\00"
@stdin = global i32 0
@.str.2 = [10 x i8] c"books.out\00"
@.str.3 = [2 x i8] c"w\00"
@stdout = global i32 0
@.str.4 = [6 x i8] c"%d %d\00"
@.str.5 = [4 x i8] c"%hd\00"
@.str.6 = [5 x i8] c"%hd\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = bitcast i32* %1 to i8*
%4 = bitcast i32* %2 to i8*
%5 = load i32, i32* @stdin
%6 = tail call i32 @freopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 %5)
%7 = load i32, i32* @stdout
%8 = tail call i32 @freopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i32 %7)
%9 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i32* %1, i32* %2)
%10 = load i32, i32* %1
%11 = shl i32 %10, 1
%12 = call i64 @malloc(i32 %11)
%13 = inttoptr i64 %12 to i16*
%14 = load i32, i32* %1
%15 = icmp sgt i32 %14, 0
br i1 %15, label %16, label %28
16: ; preds = %0, %16
%17 = phi i64 [ %24, %16 ], [ 0, %0 ]
%18 = phi i32 [ %23, %16 ], [ 0, %0 ]
%19 = getelementptr inbounds i16, i16* %13, i64 %17
%20 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i16* %19)
%21 = load i16, i16* %19
%22 = sext i16 %21 to i32
%23 = add nsw i32 %18, %22
%24 = add nuw nsw i64 %17, 1
%25 = load i32, i32* %1
%26 = sext i32 %25 to i64
%27 = icmp slt i64 %24, %26
br i1 %27, label %16, label %28
28: ; preds = %16, %0
%29 = phi i32 [ 0, %0 ], [ %23, %16 ]
%30 = phi i32 [ %14, %0 ], [ %25, %16 ]
%31 = load i32, i32* %2
%32 = call i32 @binarySearch(i32 %30, i32 %31, i16* %13, i32 1, i32 %29)
%33 = call i32 @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0), i32 %32)
ret i32 0
}