#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 memoized_cut_rod_aux(int *, int, int, int *);
int memoized_cut_rod(int *p, int size_p, int rod_length) {
  int *result = malloc((rod_length + 1) * sizeof(int));
  result[0] = 0;
  for (int i = 1; i < rod_length + 1; i++) {
    result[i] = -1;
  }
  int maxP = memoized_cut_rod_aux(p, size_p, rod_length, result);
  free(result);
  return maxP;
}


x86 assembly (clang, -O0):
<memoized_cut_rod>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    -0x10(%rbp),%eax
add    $0x1,%eax
cltq
shl    $0x2,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
movl   $0x0,(%rax)
movl   $0x1,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    -0x10(%rbp),%ecx
add    $0x1,%ecx
cmp    %ecx,%eax
jge    1198 <memoized_cut_rod+0x68>
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movl   $0xffffffff,(%rax,%rcx,4)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    116a <memoized_cut_rod+0x3a>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%edx
mov    -0x18(%rbp),%rcx
call   1040 <memoized_cut_rod_aux@plt>
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<memoized_cut_rod>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%ebx
mov    %esi,%r14d
mov    %rdi,%r15
lea    0x4(,%rbx,4),%edi
call   1060 <malloc@plt>
mov    %rax,%rbp
movl   $0x0,(%rax)
test   %ebx,%ebx
jle    117f <memoized_cut_rod+0x3f>
mov    %rbp,%rdi
add    $0x4,%rdi
mov    %ebx,%edx
shl    $0x2,%rdx
mov    $0xff,%esi
call   1040 <memset@plt>
mov    %r15,%rdi
mov    %r14d,%esi
mov    %ebx,%edx
mov    %rbp,%rcx
call   1050 <memoized_cut_rod_aux@plt>
mov    %eax,%ebx
mov    %rbp,%rdi
call   1030 <free@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i32 @memoized_cut_rod(i32* %0, i32 %1, i32 %2) {
%4 = shl i32 %2, 2
%5 = add i32 %4, 4
%6 = tail call i32* @malloc(i32 %5)
store i32 0, i32* %6
%7 = icmp slt i32 %2, 1
br i1 %7, label %13, label %8
8: ; preds = %3
%9 = getelementptr i32, i32* %6, i64 1
%10 = bitcast i32* %9 to i8*
%11 = zext i32 %2 to i64
%12 = shl nuw nsw i64 %11, 2
br label %13
13: ; preds = %8, %3
%14 = tail call i32 @memoized_cut_rod_aux(i32* %0, i32 %1, i32 %2, i32* %6)
%15 = tail call i32 @free(i32* %6)
ret i32 %14
}