#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 */
scalar_t__ stack_effect_for(unsigned char, int *, unsigned char *, int);
int calculate_max_stack_depth(unsigned char *code, int len) {
  int max, current;
  max = current = 0;
  int i;
  for (i = 0; i < len; i++) {
    int skip;
    current += stack_effect_for(code[i], &skip, code, i);
    i += skip;
    max = current > max ? current : max;
  }
  return max;
}


x86 assembly (clang, -O0):
<calculate_max_stack_depth>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x10(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    11a5 <calculate_max_stack_depth+0x95>
mov    -0x8(%rbp),%rax
movslq -0x18(%rbp),%rcx
mov    (%rax,%rcx,1),%al
mov    -0x8(%rbp),%rdx
mov    -0x18(%rbp),%ecx
lea    -0x1c(%rbp),%rsi
movzbl %al,%edi
call   1030 <stack_effect_for@plt>
mov    %rax,%rcx
movslq -0x14(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x14(%rbp)
mov    -0x1c(%rbp),%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x10(%rbp),%eax
jle    118b <calculate_max_stack_depth+0x7b>
mov    -0x14(%rbp),%eax
mov    %eax,-0x20(%rbp)
jmp    1191 <calculate_max_stack_depth+0x81>
mov    -0x10(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1134 <calculate_max_stack_depth+0x24>
mov    -0x10(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<calculate_max_stack_depth>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %esi,%esi
jle    1162 <calculate_max_stack_depth+0x52>
mov    %esi,%r14d
mov    %rdi,%r12
xor    %eax,%eax
lea    0x4(%rsp),%r15
xor    %ebx,%ebx
xor    %ebp,%ebp
movslq %eax,%r13
movzbl (%r12,%r13,1),%edi
mov    %r15,%rsi
mov    %r12,%rdx
mov    %r13d,%ecx
call   1030 <stack_effect_for@plt>
movslq %ebx,%rbx
add    %rax,%rbx
mov    0x4(%rsp),%eax
cmp    %ebx,%ebp
cmovl  %ebx,%ebp
add    %r13d,%eax
add    $0x1,%eax
cmp    %r14d,%eax
jl     1130 <calculate_max_stack_depth+0x20>
jmp    1164 <calculate_max_stack_depth+0x54>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i32 @calculate_max_stack_depth(i8* %0, i32 %1) {
%3 = alloca i32
%4 = icmp sgt i32 %1, 0
br i1 %4, label %5, label %25
5: ; preds = %2
%6 = bitcast i32* %3 to i8*
br label %7
7: ; preds = %5, %7
%8 = phi i32 [ 0, %5 ], [ %23, %7 ]
%9 = phi i64 [ 0, %5 ], [ %17, %7 ]
%10 = phi i32 [ 0, %5 ], [ %21, %7 ]
%11 = sext i32 %8 to i64
%12 = getelementptr inbounds i8, i8* %0, i64 %11
%13 = load i8, i8* %12
%14 = call i64 @stack_effect_for(i8 zeroext %13, i32* %3, i8* %0, i32 %8)
%15 = shl i64 %9, 32
%16 = ashr exact i64 %15, 32
%17 = add nsw i64 %14, %16
%18 = trunc i64 %17 to i32
%19 = load i32, i32* %3
%20 = icmp slt i32 %10, %18
%21 = select i1 %20, i32 %18, i32 %10
%22 = add nsw i32 %8, 1
%23 = add i32 %22, %19
%24 = icmp slt i32 %23, %1
br i1 %24, label %7, label %25
25: ; preds = %7, %2
%26 = phi i32 [ 0, %2 ], [ %21, %7 ]
ret i32 %26
}