#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 uint;

/* Variables and functions */
int /*<<< orphan*/ giveCandy(int *, int *, int, int);
int *malloc(int);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ scanf(char *, int *);
int candies(void) {
  int numRecipients;
  int *priorities;
  int *candyAmounts;
  uint totalCandy = 0;
  scanf("%u", &numRecipients);
  priorities = malloc(sizeof(uint) * numRecipients);
  candyAmounts = malloc(sizeof(uint) * numRecipients);
  for (int i = 0; i < numRecipients; i++) {
    scanf("%d", priorities + i);
    candyAmounts[i] = 0;
  }
  for (int i = 0; i < numRecipients; i++) {
    giveCandy(priorities, candyAmounts, i, numRecipients - 1);
    totalCandy += candyAmounts[i];
  }
  printf("%u\n", totalCandy);
  return 0;
}


x86 assembly (clang, -O0):
<candies>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x1c(%rbp)
lea    0xeaa(%rip),%rdi
lea    -0x4(%rbp),%rsi
call   1060 <scanf@plt>
movslq -0x4(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x10(%rbp)
movslq -0x4(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x4(%rbp),%eax
jge    11d0 <candies+0x90>
mov    -0x10(%rbp),%rsi
movslq -0x20(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rsi
lea    0xe55(%rip),%rdi
call   1060 <scanf@plt>
mov    -0x18(%rbp),%rax
movslq -0x20(%rbp),%rcx
movl   $0x0,(%rax,%rcx,4)
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    118c <candies+0x4c>
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x4(%rbp),%eax
jge    1218 <candies+0xd8>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x24(%rbp),%edx
mov    -0x4(%rbp),%ecx
sub    $0x1,%ecx
call   1030 <giveCandy@plt>
mov    -0x18(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    11d7 <candies+0x97>
mov    -0x1c(%rbp),%esi
lea    0xde4(%rip),%rdi
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<candies>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
lea    0xead(%rip),%rdi
lea    0xc(%rsp),%rsi
call   1060 <scanf@plt>
mov    0xc(%rsp),%ebp
lea    0x0(,%rbp,4),%ebx
mov    %ebx,%edi
call   1050 <malloc@plt>
mov    %rax,%r14
mov    %ebx,%edi
call   1050 <malloc@plt>
test   %ebp,%ebp
jle    11e4 <candies+0xa4>
mov    %rax,%r15
xor    %ebx,%ebx
lea    0xe7a(%rip),%r12
mov    %r14,%rbp
nopl   0x0(%rax)
mov    %r12,%rdi
mov    %rbp,%rsi
call   1060 <scanf@plt>
movl   $0x0,(%r15,%rbx,4)
add    $0x1,%rbx
movslq 0xc(%rsp),%rcx
add    $0x4,%rbp
cmp    %rcx,%rbx
jl     1190 <candies+0x50>
test   %ecx,%ecx
jle    11e4 <candies+0xa4>
xor    %ebx,%ebx
xor    %ebp,%ebp
nopl   (%rax)
add    $0xffffffff,%ecx
mov    %r14,%rdi
mov    %r15,%rsi
mov    %ebx,%edx
call   1030 <giveCandy@plt>
add    (%r15,%rbx,4),%ebp
add    $0x1,%rbx
movslq 0xc(%rsp),%rcx
cmp    %rcx,%rbx
jl     11c0 <candies+0x80>
jmp    11e6 <candies+0xa6>
xor    %ebp,%ebp
lea    0xe19(%rip),%rdi
mov    %ebp,%esi
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

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