#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 unsigned char uint64_t;

/* Variables and functions */
int /*<<< orphan*/ assert(int);
uint64_t read_leb128(unsigned char **data, int *b) {
  uint64_t uleb = 0;
  unsigned int bit = 0;
  unsigned char digit = 0;
  do {
    assert(bit < sizeof(uint64_t) * 8);
    digit = (**data) & 0x7f;
    uleb += digit << bit;
    bit += 7;
    (*data)++;
  } while ((*(*data - 1)) != digit);
  *b = bit;
  return uleb;
}


x86 assembly (clang, -O0):
<read_leb128>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movb   $0x0,-0x11(%rbp)
movl   $0x0,-0x18(%rbp)
movb   $0x0,-0x19(%rbp)
mov    -0x18(%rbp),%eax
cmp    $0x8,%rax
setb   %al
and    $0x1,%al
movzbl %al,%edi
call   1030 <assert@plt>
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
movzbl (%rax),%eax
and    $0x7f,%eax
mov    %al,-0x19(%rbp)
movzbl -0x19(%rbp),%eax
mov    -0x18(%rbp),%ecx
shl    %cl,%eax
mov    %eax,%ecx
movzbl -0x11(%rbp),%eax
add    %ecx,%eax
mov    %al,-0x11(%rbp)
mov    -0x18(%rbp),%eax
add    $0x7,%eax
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rcx
add    $0x1,%rcx
mov    %rcx,(%rax)
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
movzbl -0x1(%rax),%eax
movzbl -0x19(%rbp),%ecx
cmp    %ecx,%eax
jne    112f <read_leb128+0x1f>
mov    -0x18(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
movzbl -0x11(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_leb128>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r15
xor    %ebp,%ebp
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
xor    %edi,%edi
cmp    $0x8,%ebx
setb   %dil
call   1030 <assert@plt>
mov    (%r15),%rax
movzbl (%rax),%edx
and    $0x7f,%edx
mov    %edx,%esi
mov    %ebx,%ecx
shl    %cl,%esi
movzbl %bpl,%ebp
add    %esi,%ebp
add    $0x7,%ebx
lea    0x1(%rax),%rcx
mov    %rcx,(%r15)
cmp    %dl,(%rax)
jne    1130 <read_leb128+0x20>
mov    %ebx,(%r14)
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define zeroext i8 @read_leb128(i8** nocapture %0, i32* nocapture writeonly %1) {
br label %3
3: ; preds = %3, %2
%4 = phi i32 [ 0, %2 ], [ %15, %3 ]
%5 = phi i32 [ 0, %2 ], [ %16, %3 ]
%6 = icmp ult i32 %5, 8
%7 = zext i1 %6 to i32
%8 = tail call i32 @assert(i32 %7)
%9 = load i8*, i8** %0
%10 = load i8, i8* %9
%11 = and i8 %10, 127
%12 = zext i8 %11 to i32
%13 = shl i32 %12, %5
%14 = and i32 %4, 255
%15 = add nsw i32 %13, %14
%16 = add i32 %5, 7
%17 = getelementptr inbounds i8, i8* %9, i64 1
store i8* %17, i8** %0
%18 = load i8, i8* %9
%19 = icmp eq i8 %18, %11
br i1 %19, label %20, label %3
20: ; preds = %3
%21 = trunc i32 %15 to i8
store i32 %16, i32* %1
ret i8 %21
}