int tribonacci(int n) {
  static int dp[38] = {0};
  dp[0] = 0;
  dp[1] = 1;
  dp[2] = 1;
  if (dp[n] == 0 && n > 2) {
    dp[n] = tribonacci(n - 1) + tribonacci(n - 2) + tribonacci(n - 3);
  }
  return dp[n];
}


x86 assembly (clang, -O0):
<tribonacci>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x0,0x2f1b(%rip)
movl   $0x1,0x2f15(%rip)
movl   $0x1,0x2f0f(%rip)
movslq -0x4(%rbp),%rcx
lea    0x2efc(%rip),%rax
cmpl   $0x0,(%rax,%rcx,4)
jne    1199 <tribonacci+0x89>
cmpl   $0x2,-0x4(%rbp)
jle    1199 <tribonacci+0x89>
mov    -0x4(%rbp),%edi
sub    $0x1,%edi
call   1030 <tribonacci@plt>
mov    %eax,-0xc(%rbp)
mov    -0x4(%rbp),%edi
sub    $0x2,%edi
call   1030 <tribonacci@plt>
mov    %eax,%ecx
mov    -0xc(%rbp),%eax
add    %ecx,%eax
mov    %eax,-0x8(%rbp)
mov    -0x4(%rbp),%edi
sub    $0x3,%edi
call   1030 <tribonacci@plt>
mov    -0x8(%rbp),%edx
add    %eax,%edx
movslq -0x4(%rbp),%rcx
lea    0x2eaa(%rip),%rax
mov    %edx,(%rax,%rcx,4)
movslq -0x4(%rbp),%rcx
lea    0x2e9c(%rip),%rax
mov    (%rax,%rcx,4),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tribonacci>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
movabs $0x100000000,%rax
mov    %rax,0x2f18(%rip)
lea    0x2f11(%rip),%r14
movl   $0x1,0x2f0f(%rip)
movslq %edi,%rbx
mov    (%r14,%rbx,4),%eax
cmp    $0x3,%ebx
jl     116f <tribonacci+0x5f>
test   %eax,%eax
jne    116f <tribonacci+0x5f>
lea    -0x1(%rbx),%edi
call   1030 <tribonacci@plt>
mov    %eax,%r15d
lea    -0x2(%rbx),%edi
call   1030 <tribonacci@plt>
mov    %eax,%ebp
add    %r15d,%ebp
lea    -0x3(%rbx),%edi
call   1030 <tribonacci@plt>
add    %ebp,%eax
mov    %eax,(%r14,%rbx,4)
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@tribonacci.dp = internal global [38 x i32] zeroinitializer
define i32 @tribonacci(i32 %0) {
%2 = alloca i32
store i32 %0, i32* %2
store i32 0, i32* getelementptr inbounds ([38 x i32], [38 x i32]* @tribonacci.dp, i64 0, i64 0)
store i32 1, i32* getelementptr inbounds ([38 x i32], [38 x i32]* @tribonacci.dp, i64 0, i64 1)
store i32 1, i32* getelementptr inbounds ([38 x i32], [38 x i32]* @tribonacci.dp, i64 0, i64 2)
%3 = load i32, i32* %2
%4 = sext i32 %3 to i64
%5 = getelementptr inbounds [38 x i32], [38 x i32]* @tribonacci.dp, i64 0, i64 %4
%6 = load i32, i32* %5
%7 = icmp eq i32 %6, 0
br i1 %7, label %8, label %26
8: ; preds = %1
%9 = load i32, i32* %2
%10 = icmp sgt i32 %9, 2
br i1 %10, label %11, label %26
11: ; preds = %8
%12 = load i32, i32* %2
%13 = sub nsw i32 %12, 1
%14 = call i32 @tribonacci(i32 %13)
%15 = load i32, i32* %2
%16 = sub nsw i32 %15, 2
%17 = call i32 @tribonacci(i32 %16)
%18 = add nsw i32 %14, %17
%19 = load i32, i32* %2
%20 = sub nsw i32 %19, 3
%21 = call i32 @tribonacci(i32 %20)
%22 = add nsw i32 %18, %21
%23 = load i32, i32* %2
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds [38 x i32], [38 x i32]* @tribonacci.dp, i64 0, i64 %24
store i32 %22, i32* %25
br label %26
26: ; preds = %11, %8, %1
%27 = load i32, i32* %2
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds [38 x i32], [38 x i32]* @tribonacci.dp, i64 0, i64 %28
%30 = load i32, i32* %29
ret i32 %30
}

LLVM IR (-O2):
@tribonacci.dp = internal unnamed_addr global [38 x i32] zeroinitializer
define i32 @tribonacci(i32 %0) {
store i32 0, i32* getelementptr inbounds ([38 x i32], [38 x i32]* @tribonacci.dp, i64 0, i64 0)
store i32 1, i32* getelementptr inbounds ([38 x i32], [38 x i32]* @tribonacci.dp, i64 0, i64 1)
store i32 1, i32* getelementptr inbounds ([38 x i32], [38 x i32]* @tribonacci.dp, i64 0, i64 2)
%2 = sext i32 %0 to i64
%3 = getelementptr inbounds [38 x i32], [38 x i32]* @tribonacci.dp, i64 0, i64 %2
%4 = load i32, i32* %3
%5 = icmp eq i32 %4, 0
%6 = icmp sgt i32 %0, 2
%7 = and i1 %6, %5
br i1 %7, label %8, label %17
8: ; preds = %1
%9 = add nsw i32 %0, -1
%10 = tail call i32 @tribonacci(i32 %9)
%11 = add nsw i32 %0, -2
%12 = tail call i32 @tribonacci(i32 %11)
%13 = add nsw i32 %12, %10
%14 = add nsw i32 %0, -3
%15 = tail call i32 @tribonacci(i32 %14)
%16 = add nsw i32 %13, %15
store i32 %16, i32* %3
br label %17
17: ; preds = %8, %1
%18 = phi i32 [ %16, %8 ], [ %4, %1 ]
ret i32 %18
}