#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 get_length(const unsigned char *buffer, size_t *len) {
  if (buffer[0] < 0x81) {
    *len = buffer[0];
    return 1;
  } else if ((*buffer & 0x7f) == 1) {
    *len = buffer[1];
    return 2;
  } else if ((*buffer & 0x7f) == 2) {
    size_t tmp = buffer[1];
    *len = (tmp << 8) + buffer[2];
    return 3;
  }
  return 0;
}


x86 assembly (clang, -O0):
<get_length>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rax
movzbl (%rax),%eax
cmp    $0x81,%eax
jge    113a <get_length+0x3a>
mov    -0x10(%rbp),%rax
movzbl (%rax),%eax
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x1,-0x4(%rbp)
jmp    11c0 <get_length+0xc0>
mov    -0x10(%rbp),%rax
movzbl (%rax),%eax
and    $0x7f,%eax
cmp    $0x1,%eax
jne    116a <get_length+0x6a>
mov    -0x10(%rbp),%rax
movzbl 0x1(%rax),%eax
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x2,-0x4(%rbp)
jmp    11c0 <get_length+0xc0>
mov    -0x10(%rbp),%rax
movzbl (%rax),%eax
and    $0x7f,%eax
cmp    $0x2,%eax
jne    11af <get_length+0xaf>
mov    -0x10(%rbp),%rax
movzbl 0x1(%rax),%eax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rcx
shl    $0x8,%rcx
mov    -0x10(%rbp),%rax
movzbl 0x2(%rax),%eax
add    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x3,-0x4(%rbp)
jmp    11c0 <get_length+0xc0>
jmp    11b4 <get_length+0xb4>
jmp    11b9 <get_length+0xb9>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_length>:
movzbl (%rdi),%ecx
cmp    $0x80,%rcx
ja     1113 <get_length+0x13>
mov    $0x1,%eax
jmp    113d <get_length+0x3d>
and    $0x7f,%cl
cmp    $0x2,%cl
je     112d <get_length+0x2d>
xor    %eax,%eax
cmp    $0x1,%cl
jne    1140 <get_length+0x40>
movzbl 0x1(%rdi),%ecx
mov    $0x2,%eax
jmp    113d <get_length+0x3d>
movzwl 0x1(%rdi),%ecx
shl    $0x30,%rcx
bswap  %rcx
mov    $0x3,%eax
mov    %rcx,(%rsi)
ret

LLVM IR (-O0):
define i32 @get_length(i8* %0, i64* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64*
%6 = alloca i64
store i8* %0, i8** %4
store i64* %1, i64** %5
%7 = load i8*, i8** %4
%8 = getelementptr inbounds i8, i8* %7, i64 0
%9 = load i8, i8* %8
%10 = zext i8 %9 to i32
%11 = icmp slt i32 %10, 129
br i1 %11, label %12, label %18
12: ; preds = %2
%13 = load i8*, i8** %4
%14 = getelementptr inbounds i8, i8* %13, i64 0
%15 = load i8, i8* %14
%16 = zext i8 %15 to i64
%17 = load i64*, i64** %5
store i64 %16, i64* %17
store i32 1, i32* %3
br label %52
18: ; preds = %2
%19 = load i8*, i8** %4
%20 = load i8, i8* %19
%21 = zext i8 %20 to i32
%22 = and i32 %21, 127
%23 = icmp eq i32 %22, 1
br i1 %23, label %24, label %30
24: ; preds = %18
%25 = load i8*, i8** %4
%26 = getelementptr inbounds i8, i8* %25, i64 1
%27 = load i8, i8* %26
%28 = zext i8 %27 to i64
%29 = load i64*, i64** %5
store i64 %28, i64* %29
store i32 2, i32* %3
br label %52
30: ; preds = %18
%31 = load i8*, i8** %4
%32 = load i8, i8* %31
%33 = zext i8 %32 to i32
%34 = and i32 %33, 127
%35 = icmp eq i32 %34, 2
br i1 %35, label %36, label %49
36: ; preds = %30
%37 = load i8*, i8** %4
%38 = getelementptr inbounds i8, i8* %37, i64 1
%39 = load i8, i8* %38
%40 = zext i8 %39 to i64
store i64 %40, i64* %6
%41 = load i64, i64* %6
%42 = shl i64 %41, 8
%43 = load i8*, i8** %4
%44 = getelementptr inbounds i8, i8* %43, i64 2
%45 = load i8, i8* %44
%46 = zext i8 %45 to i64
%47 = add i64 %42, %46
%48 = load i64*, i64** %5
store i64 %47, i64* %48
store i32 3, i32* %3
br label %52
49: ; preds = %30
br label %50
50: ; preds = %49
br label %51
51: ; preds = %50
store i32 0, i32* %3
br label %52
52: ; preds = %51, %36, %24, %12
%53 = load i32, i32* %3
ret i32 %53
}

LLVM IR (-O2):
define i32 @get_length(i8* nocapture %0, i64* nocapture writeonly %1) {
%3 = load i8, i8* %0
%4 = icmp ult i8 %3, -127
br i1 %4, label %5, label %7
5: ; preds = %2
%6 = zext i8 %3 to i64
br label %22
7: ; preds = %2
%8 = and i8 %3, 127
switch i8 %8, label %25 [
i8 1, label %9
i8 2, label %13
]
9: ; preds = %7
%10 = getelementptr inbounds i8, i8* %0, i64 1
%11 = load i8, i8* %10
%12 = zext i8 %11 to i64
br label %22
13: ; preds = %7
%14 = getelementptr inbounds i8, i8* %0, i64 1
%15 = load i8, i8* %14
%16 = zext i8 %15 to i64
%17 = shl nuw nsw i64 %16, 8
%18 = getelementptr inbounds i8, i8* %0, i64 2
%19 = load i8, i8* %18
%20 = zext i8 %19 to i64
%21 = or i64 %17, %20
br label %22
22: ; preds = %5, %9, %13
%23 = phi i64 [ %21, %13 ], [ %12, %9 ], [ %6, %5 ]
%24 = phi i32 [ 3, %13 ], [ 2, %9 ], [ 1, %5 ]
store i64 %23, i64* %1
br label %25
25: ; preds = %22, %7
%26 = phi i32 [ 0, %7 ], [ %24, %22 ]
ret i32 %26
}