#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 max(int, int);
int cut_rod(int *p, int size_p, int rod_length) {
  if (rod_length == 0) {
    return 0;
  }
  int maxP = -1;
  for (int i = 1; i < rod_length + 1; i++) {
    maxP = max(p[i - 1] + cut_rod(p, size_p, rod_length - i), maxP);
  }
  return maxP;
}


x86 assembly (clang, -O0):
<cut_rod>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jne    1148 <cut_rod+0x28>
movl   $0x0,-0x4(%rbp)
jmp    11b0 <cut_rod+0x90>
movl   $0xffffffff,-0x1c(%rbp)
movl   $0x1,-0x20(%rbp)
mov    -0x20(%rbp),%eax
mov    -0x18(%rbp),%ecx
add    $0x1,%ecx
cmp    %ecx,%eax
jge    11aa <cut_rod+0x8a>
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,4),%eax
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x18(%rbp),%edx
sub    -0x20(%rbp),%edx
call   1040 <cut_rod@plt>
mov    -0x24(%rbp),%edi
add    %eax,%edi
mov    -0x1c(%rbp),%esi
call   1030 <max@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    1156 <cut_rod+0x36>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cut_rod>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %edx,%edx
je     117f <cut_rod+0x5f>
jle    1184 <cut_rod+0x64>
mov    %edx,%ebx
mov    %esi,%r13d
mov    %rdi,%r15
mov    %edx,%eax
mov    %rax,(%rsp)
add    $0xffffffff,%ebx
mov    $0xffffffff,%r12d
xor    %r14d,%r14d
nopl   0x0(%rax,%rax,1)
mov    (%r15,%r14,4),%ebp
mov    %r15,%rdi
mov    %r13d,%esi
mov    %ebx,%edx
call   1040 <cut_rod@plt>
add    %eax,%ebp
mov    %ebp,%edi
mov    %r12d,%esi
call   1030 <max@plt>
mov    %eax,%r12d
add    $0xffffffff,%ebx
add    $0x1,%r14
cmp    %r14,(%rsp)
jne    1150 <cut_rod+0x30>
jmp    118a <cut_rod+0x6a>
xor    %r12d,%r12d
jmp    118a <cut_rod+0x6a>
mov    $0xffffffff,%r12d
mov    %r12d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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