#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 */
typedef char uint32_t;

/* Variables and functions */
char *tb_ber128load_slowpath(char *, char *);
char *tb_ber128load(char *p, uint32_t *value) {
  if (!(p[0] & 0x80)) {
    *value = *(p++) & 0x7f;
  } else if (!(p[1] & 0x80)) {
    *value = (p[0] & 0x7f) << 7 | (p[1] & 0x7f);
    p += 2;
  } else {
    return tb_ber128load_slowpath(p, value);
  }
  return p;
}


x86 assembly (clang, -O0):
<tb_ber128load>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
and    $0x80,%eax
cmp    $0x0,%eax
jne    1157 <tb_ber128load+0x47>
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x10(%rbp)
movsbl (%rax),%eax
and    $0x7f,%eax
mov    %al,%cl
mov    -0x18(%rbp),%rax
mov    %cl,(%rax)
jmp    11bb <tb_ber128load+0xab>
mov    -0x10(%rbp),%rax
movsbl 0x1(%rax),%eax
and    $0x80,%eax
cmp    $0x0,%eax
jne    11a0 <tb_ber128load+0x90>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
and    $0x7f,%eax
shl    $0x7,%eax
mov    -0x10(%rbp),%rcx
movsbl 0x1(%rcx),%ecx
and    $0x7f,%ecx
or     %ecx,%eax
mov    %al,%cl
mov    -0x18(%rbp),%rax
mov    %cl,(%rax)
mov    -0x10(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x10(%rbp)
jmp    11b6 <tb_ber128load+0xa6>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1030 <tb_ber128load_slowpath@plt>
mov    %rax,-0x8(%rbp)
jmp    11c3 <tb_ber128load+0xb3>
jmp    11bb <tb_ber128load+0xab>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tb_ber128load>:
mov    (%rdi),%cl
lea    0x1(%rdi),%rax
test   %cl,%cl
js     111d <tb_ber128load+0xd>
mov    %cl,(%rsi)
ret
mov    (%rax),%al
test   %al,%al
js     1132 <tb_ber128load+0x22>
shl    $0x7,%cl
or     %cl,%al
mov    %al,(%rsi)
add    $0x2,%rdi
mov    %rdi,%rax
ret
jmp    1030 <tb_ber128load_slowpath@plt>

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

LLVM IR (-O2):
define i8* @tb_ber128load(i8* %0, i8* %1) {
%3 = load i8, i8* %0
%4 = icmp sgt i8 %3, -1
%5 = getelementptr inbounds i8, i8* %0, i64 1
br i1 %4, label %6, label %7
6: ; preds = %2
store i8 %3, i8* %1
br label %16
7: ; preds = %2
%8 = load i8, i8* %5
%9 = icmp sgt i8 %8, -1
br i1 %9, label %10, label %14
10: ; preds = %7
%11 = shl i8 %3, 7
%12 = or i8 %8, %11
store i8 %12, i8* %1
%13 = getelementptr inbounds i8, i8* %0, i64 2
br label %16
14: ; preds = %7
%15 = tail call i8* @tb_ber128load_slowpath(i8* %0, i8* %1)
br label %16
16: ; preds = %6, %10, %14
%17 = phi i8* [ %15, %14 ], [ %13, %10 ], [ %5, %6 ]
ret i8* %17
}