#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 /*<<< orphan*/ get_all_max(int *, int *, int, int);
scalar_t__ malloc(int);
scalar_t__ volume_at_point(int *, int *, int *, int *, int, int);
int array_volume(int *elevations, int *volumes, int size) {
  int i;
  int sum = 0;
  int *max_left = (int *)malloc(size * sizeof(int));
  int *max_right = (int *)malloc(size * sizeof(int));
  get_all_max(elevations, max_left, size, 1);
  get_all_max(elevations, max_right, size, 0);
  for (i = 0; i < size; i++) {
    sum += volume_at_point(elevations, volumes, max_left, max_right, size, i);
  }
  free(max_left);
  free(max_right);
  return sum;
}


x86 assembly (clang, -O0):
<array_volume>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x1c(%rbp)
movslq -0x14(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x28(%rbp)
movslq -0x14(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x14(%rbp),%edx
mov    $0x1,%ecx
call   1040 <get_all_max@plt>
mov    -0x8(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x14(%rbp),%edx
xor    %ecx,%ecx
call   1040 <get_all_max@plt>
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11f2 <array_volume+0xb2>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x28(%rbp),%rdx
mov    -0x30(%rbp),%rcx
mov    -0x14(%rbp),%r8d
mov    -0x18(%rbp),%r9d
call   1060 <volume_at_point@plt>
mov    %rax,%rcx
movslq -0x1c(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    11ae <array_volume+0x6e>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x1c(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<array_volume>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%ebx
mov    %rsi,(%rsp)
mov    %rdi,%r15
lea    0x0(,%rbx,4),%ebp
mov    %ebp,%edi
call   1050 <malloc@plt>
mov    %rax,%r12
mov    %ebp,%edi
call   1050 <malloc@plt>
mov    %rax,%r13
mov    %r15,%rdi
mov    %r12,%rsi
mov    %ebx,%edx
mov    $0x1,%ecx
call   1040 <get_all_max@plt>
xor    %ebp,%ebp
mov    %r15,%rdi
mov    %r13,%rsi
mov    %ebx,%edx
xor    %ecx,%ecx
call   1040 <get_all_max@plt>
test   %ebx,%ebx
jle    11c3 <array_volume+0x83>
xor    %r14d,%r14d
xor    %ebp,%ebp
nopl   0x0(%rax,%rax,1)
mov    %r15,%rdi
mov    (%rsp),%rsi
mov    %r12,%rdx
mov    %r13,%rcx
mov    %ebx,%r8d
mov    %r14d,%r9d
call   1060 <volume_at_point@plt>
add    %eax,%ebp
add    $0x1,%r14d
cmp    %r14d,%ebx
jne    11a0 <array_volume+0x60>
mov    %r12,%rdi
call   1030 <free@plt>
mov    %r13,%rdi
call   1030 <free@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @array_volume(i32* %0, i32* %1, i32 %2) {
%4 = alloca i32*
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32*
%10 = alloca i32*
store i32* %0, i32** %4
store i32* %1, i32** %5
store i32 %2, i32* %6
store i32 0, i32* %8
%11 = load i32, i32* %6
%12 = sext i32 %11 to i64
%13 = mul i64 %12, 4
%14 = trunc i64 %13 to i32
%15 = call i64 @malloc(i32 %14)
%16 = inttoptr i64 %15 to i32*
store i32* %16, i32** %9
%17 = load i32, i32* %6
%18 = sext i32 %17 to i64
%19 = mul i64 %18, 4
%20 = trunc i64 %19 to i32
%21 = call i64 @malloc(i32 %20)
%22 = inttoptr i64 %21 to i32*
store i32* %22, i32** %10
%23 = load i32*, i32** %4
%24 = load i32*, i32** %9
%25 = load i32, i32* %6
%26 = call i32 @get_all_max(i32* %23, i32* %24, i32 %25, i32 1)
%27 = load i32*, i32** %4
%28 = load i32*, i32** %10
%29 = load i32, i32* %6
%30 = call i32 @get_all_max(i32* %27, i32* %28, i32 %29, i32 0)
store i32 0, i32* %7
br label %31
31: ; preds = %47, %3
%32 = load i32, i32* %7
%33 = load i32, i32* %6
%34 = icmp slt i32 %32, %33
br i1 %34, label %35, label %50
35: ; preds = %31
%36 = load i32*, i32** %4
%37 = load i32*, i32** %5
%38 = load i32*, i32** %9
%39 = load i32*, i32** %10
%40 = load i32, i32* %6
%41 = load i32, i32* %7
%42 = call i64 @volume_at_point(i32* %36, i32* %37, i32* %38, i32* %39, i32 %40, i32 %41)
%43 = load i32, i32* %8
%44 = sext i32 %43 to i64
%45 = add nsw i64 %44, %42
%46 = trunc i64 %45 to i32
store i32 %46, i32* %8
br label %47
47: ; preds = %35
%48 = load i32, i32* %7
%49 = add nsw i32 %48, 1
store i32 %49, i32* %7
br label %31
50: ; preds = %31
%51 = load i32*, i32** %9
%52 = call i32 @free(i32* %51)
%53 = load i32*, i32** %10
%54 = call i32 @free(i32* %53)
%55 = load i32, i32* %8
ret i32 %55
}

LLVM IR (-O2):
define i32 @array_volume(i32* %0, i32* %1, i32 %2) {
%4 = shl i32 %2, 2
%5 = tail call i64 @malloc(i32 %4)
%6 = inttoptr i64 %5 to i32*
%7 = tail call i64 @malloc(i32 %4)
%8 = inttoptr i64 %7 to i32*
%9 = tail call i32 @get_all_max(i32* %0, i32* %6, i32 %2, i32 1)
%10 = tail call i32 @get_all_max(i32* %0, i32* %8, i32 %2, i32 0)
%11 = icmp sgt i32 %2, 0
br i1 %11, label %12, label %20
12: ; preds = %3, %12
%13 = phi i32 [ %18, %12 ], [ 0, %3 ]
%14 = phi i32 [ %17, %12 ], [ 0, %3 ]
%15 = tail call i64 @volume_at_point(i32* %0, i32* %1, i32* %6, i32* %8, i32 %2, i32 %13)
%16 = trunc i64 %15 to i32
%17 = add i32 %14, %16
%18 = add nuw nsw i32 %13, 1
%19 = icmp eq i32 %18, %2
br i1 %19, label %20, label %12
20: ; preds = %12, %3
%21 = phi i32 [ 0, %3 ], [ %17, %12 ]
%22 = tail call i32 @free(i32* %6)
%23 = tail call i32 @free(i32* %8)
ret i32 %21
}