#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 read_raw_byte(char *, void *, int);
int read_raw_varint64(unsigned long long *tag, void *_buffer, int offset) {
  short shift = 0;
  signed char b;
  *tag = 0;
  while (shift < 64) {
    offset = read_raw_byte((char *)&b, _buffer, offset);
    *tag |= (unsigned long long)(b & 0x7F) << shift;
    if ((b & 0x80) == 0) {
      return offset;
    }
    shift += 7;
  }
  return -1;
}


x86 assembly (clang, -O0):
<read_raw_varint64>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
movw   $0x0,-0x1e(%rbp)
mov    -0x10(%rbp),%rax
movq   $0x0,(%rax)
movswl -0x1e(%rbp),%eax
cmp    $0x40,%eax
jge    11a0 <read_raw_varint64+0x90>
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
lea    -0x1f(%rbp),%rdi
call   1030 <read_raw_byte@plt>
mov    %eax,-0x1c(%rbp)
movsbl -0x1f(%rbp),%eax
and    $0x7f,%eax
cltq
movswl -0x1e(%rbp),%ecx
mov    %ecx,%ecx
shl    %cl,%rax
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
or     (%rax),%rcx
mov    %rcx,(%rax)
movsbl -0x1f(%rbp),%eax
and    $0x80,%eax
cmp    $0x0,%eax
jne    1190 <read_raw_varint64+0x80>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11a7 <read_raw_varint64+0x97>
movswl -0x1e(%rbp),%eax
add    $0x7,%eax
mov    %ax,-0x1e(%rbp)
jmp    1134 <read_raw_varint64+0x24>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_raw_varint64>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%eax
mov    %rsi,%r14
mov    %rdi,%rbx
movq   $0x0,(%rdi)
xor    %ebp,%ebp
lea    0x7(%rsp),%r15
nopl   (%rax)
mov    %r15,%rdi
mov    %r14,%rsi
mov    %eax,%edx
call   1030 <read_raw_byte@plt>
movzbl 0x7(%rsp),%edx
mov    %edx,%esi
and    $0x7f,%esi
mov    %ebp,%ecx
shl    %cl,%rsi
or     %rsi,(%rbx)
test   %dl,%dl
jns    1170 <read_raw_varint64+0x60>
shl    $0x10,%ebp
add    $0x70000,%ebp
mov    %ebp,%ecx
sar    $0x10,%ecx
cmp    $0x3fffff,%ebp
mov    %ecx,%ebp
jle    1130 <read_raw_varint64+0x20>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i32 @read_raw_varint64(i64* nocapture %0, i8* %1, i32 %2) {
%4 = alloca i8
store i64 0, i64* %0
br label %10
5: ; preds = %10
%6 = shl nsw i32 %11, 16
%7 = add i32 %6, 458752
%8 = ashr exact i32 %7, 16
%9 = icmp slt i32 %7, 4194304
br i1 %9, label %10, label %22
10: ; preds = %3, %5
%11 = phi i32 [ 0, %3 ], [ %8, %5 ]
%12 = phi i32 [ %2, %3 ], [ %13, %5 ]
%13 = call i32 @read_raw_byte(i8* %4, i8* %1, i32 %12)
%14 = load i8, i8* %4
%15 = and i8 %14, 127
%16 = zext i8 %15 to i64
%17 = zext i32 %11 to i64
%18 = shl i64 %16, %17
%19 = load i64, i64* %0
%20 = or i64 %18, %19
store i64 %20, i64* %0
%21 = icmp sgt i8 %14, -1
br i1 %21, label %22, label %5
22: ; preds = %5, %10
%23 = phi i32 [ %13, %10 ], [ -1, %5 ]
ret i32 %23
}