#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*/ quicksort(int *, int /*<<< orphan*/, int);
int purchased_toys(int num_items, int *toys, int money) {
  int i, purchased = 0;
  quicksort(toys, 0, num_items - 1);
  for (i = 0; i < num_items; i++) {
    if (money > 0 && toys[i] < money) {
      money -= toys[i];
      purchased++;
    } else
      break;
  }
  return purchased;
}


x86 assembly (clang, -O0):
<purchased_toys>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x4(%rbp),%edx
sub    $0x1,%edx
xor    %esi,%esi
call   1030 <quicksort@plt>
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x4(%rbp),%eax
jge    11a4 <purchased_toys+0x94>
cmpl   $0x0,-0x14(%rbp)
jle    118c <purchased_toys+0x7c>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
cmp    -0x14(%rbp),%eax
jge    118c <purchased_toys+0x7c>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
mov    (%rax,%rcx,4),%ecx
mov    -0x14(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x14(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1191 <purchased_toys+0x81>
jmp    11a4 <purchased_toys+0x94>
jmp    1196 <purchased_toys+0x86>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1141 <purchased_toys+0x31>
mov    -0x1c(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<purchased_toys>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%ebp
mov    %rsi,%r14
mov    %edi,%r15d
lea    -0x1(%r15),%edx
xor    %ebx,%ebx
mov    %rsi,%rdi
xor    %esi,%esi
call   1030 <quicksort@plt>
test   %r15d,%r15d
jle    1153 <purchased_toys+0x43>
test   %ebp,%ebp
jle    1153 <purchased_toys+0x43>
mov    %r15d,%eax
xor    %ebx,%ebx
nopl   (%rax)
sub    (%r14,%rbx,4),%ebp
jle    1153 <purchased_toys+0x43>
add    $0x1,%rbx
cmp    %rax,%rbx
jae    1153 <purchased_toys+0x43>
test   %ebp,%ebp
jg     1140 <purchased_toys+0x30>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @purchased_toys(i32 %0, i32* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %4
store i32* %1, i32** %5
store i32 %2, i32* %6
store i32 0, i32* %8
%9 = load i32*, i32** %5
%10 = load i32, i32* %4
%11 = sub nsw i32 %10, 1
%12 = call i32 @quicksort(i32* %9, i32 0, i32 %11)
store i32 0, i32* %7
br label %13
13: ; preds = %40, %3
%14 = load i32, i32* %7
%15 = load i32, i32* %4
%16 = icmp slt i32 %14, %15
br i1 %16, label %17, label %43
17: ; preds = %13
%18 = load i32, i32* %6
%19 = icmp sgt i32 %18, 0
br i1 %19, label %20, label %38
20: ; preds = %17
%21 = load i32*, i32** %5
%22 = load i32, i32* %7
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds i32, i32* %21, i64 %23
%25 = load i32, i32* %24
%26 = load i32, i32* %6
%27 = icmp slt i32 %25, %26
br i1 %27, label %28, label %38
28: ; preds = %20
%29 = load i32*, i32** %5
%30 = load i32, i32* %7
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i32, i32* %29, i64 %31
%33 = load i32, i32* %32
%34 = load i32, i32* %6
%35 = sub nsw i32 %34, %33
store i32 %35, i32* %6
%36 = load i32, i32* %8
%37 = add nsw i32 %36, 1
store i32 %37, i32* %8
br label %39
38: ; preds = %20, %17
br label %43
39: ; preds = %28
br label %40
40: ; preds = %39
%41 = load i32, i32* %7
%42 = add nsw i32 %41, 1
store i32 %42, i32* %7
br label %13
43: ; preds = %38, %13
%44 = load i32, i32* %8
ret i32 %44
}

LLVM IR (-O2):
define i32 @purchased_toys(i32 %0, i32* %1, i32 %2) {
%4 = add nsw i32 %0, -1
%5 = tail call i32 @quicksort(i32* %1, i32 0, i32 %4)
%6 = icmp sgt i32 %0, 0
%7 = icmp sgt i32 %2, 0
%8 = and i1 %6, %7
br i1 %8, label %9, label %26
9: ; preds = %3
%10 = zext i32 %0 to i64
br label %11
11: ; preds = %9, %17
%12 = phi i64 [ 0, %9 ], [ %19, %17 ]
%13 = phi i32 [ %2, %9 ], [ %18, %17 ]
%14 = getelementptr inbounds i32, i32* %1, i64 %12
%15 = load i32, i32* %14
%16 = icmp sgt i32 %13, %15
br i1 %16, label %17, label %23
17: ; preds = %11
%18 = sub nsw i32 %13, %15
%19 = add nuw nsw i64 %12, 1
%20 = icmp ult i64 %19, %10
%21 = icmp sgt i32 %18, 0
%22 = select i1 %20, i1 %21, i1 false
br i1 %22, label %11, label %23
23: ; preds = %11, %17
%24 = phi i64 [ %19, %17 ], [ %12, %11 ]
%25 = trunc i64 %24 to i32
br label %26
26: ; preds = %23, %3
%27 = phi i32 [ 0, %3 ], [ %25, %23 ]
ret i32 %27
}