#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*/ free(int *);
int *malloc(int);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ quicksort_ascending(int *, int);
int /*<<< orphan*/ scanf(char *, int *, ...);
int markAndToys(void) {
  int numToys, moneyAvailable;
  scanf("%d %d", &numToys, &moneyAvailable);
  int *toyPrices = malloc(sizeof(int) * numToys);
  for (int i = 0; i < numToys; i++) {
    scanf("%d", &toyPrices[i]);
  }
  quicksort_ascending(toyPrices, numToys);
  int numToysBought = 0;
  for (int i = 0; i < numToys; i++) {
    if (moneyAvailable > toyPrices[i]) {
      moneyAvailable -= toyPrices[i];
      numToysBought++;
    } else {
      break;
    }
  }
  printf("%d", numToysBought);
  free(toyPrices);
  return 0;
}


x86 assembly (clang, -O0):
<markAndToys>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    0xea1(%rip),%rdi
lea    -0x4(%rbp),%rsi
lea    -0x8(%rbp),%rdx
mov    $0x0,%al
call   1070 <scanf@plt>
movslq -0x4(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x4(%rbp),%eax
jge    11bf <markAndToys+0x6f>
mov    -0x10(%rbp),%rsi
movslq -0x14(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rsi
lea    0xe59(%rip),%rdi
mov    $0x0,%al
call   1070 <scanf@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1188 <markAndToys+0x38>
mov    -0x10(%rbp),%rdi
mov    -0x4(%rbp),%esi
call   1040 <quicksort_ascending@plt>
movl   $0x0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x4(%rbp),%eax
jge    1232 <markAndToys+0xe2>
mov    -0x8(%rbp),%eax
mov    -0x10(%rbp),%rcx
movslq -0x1c(%rbp),%rdx
cmp    (%rcx,%rdx,4),%eax
jle    121a <markAndToys+0xca>
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,4),%ecx
mov    -0x8(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x8(%rbp)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    121f <markAndToys+0xcf>
jmp    1232 <markAndToys+0xe2>
jmp    1224 <markAndToys+0xd4>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11d9 <markAndToys+0x89>
mov    -0x18(%rbp),%esi
lea    0xdc7(%rip),%rdi
call   1050 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<markAndToys>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
lea    0xe9e(%rip),%rdi
xor    %r15d,%r15d
mov    %rsp,%rsi
lea    0x4(%rsp),%rdx
xor    %eax,%eax
call   1070 <scanf@plt>
mov    (%rsp),%r13d
lea    0x0(,%r13,4),%edi
call   1060 <malloc@plt>
mov    %rax,%r14
test   %r13d,%r13d
jle    11be <markAndToys+0x6e>
lea    0xe6f(%rip),%r12
mov    %r14,%rbx
xor    %ebp,%ebp
nopl   0x0(%rax)
mov    %r12,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1070 <scanf@plt>
add    $0x1,%rbp
movslq (%rsp),%r13
add    $0x4,%rbx
cmp    %r13,%rbp
jl     11a0 <markAndToys+0x50>
mov    %r14,%rdi
mov    %r13d,%esi
call   1040 <quicksort_ascending@plt>
mov    (%rsp),%eax
test   %eax,%eax
jle    11f6 <markAndToys+0xa6>
mov    0x4(%rsp),%ecx
xor    %r15d,%r15d
nopw   0x0(%rax,%rax,1)
sub    (%r14,%r15,4),%ecx
jle    11f6 <markAndToys+0xa6>
mov    %ecx,0x4(%rsp)
add    $0x1,%r15
cmp    %r15,%rax
jne    11e0 <markAndToys+0x90>
mov    %eax,%r15d
lea    0xe06(%rip),%rdi
mov    %r15d,%esi
call   1050 <printf@plt>
mov    %r14,%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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