#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 int uint8_t;
typedef int uint64_t;
typedef int int64_t;

/* Variables and functions */
const uint8_t *read_int64(int64_t *v, const uint8_t *buffer,
                          const uint8_t *buffer_end) {
  uint8_t i = 0;
  uint64_t tmp = 0;
  while (1) {
    if (buffer >= buffer_end)
      return NULL;
    tmp |= ((uint64_t)((*buffer) & 0x7f)) << ((i++) * 7);
    if ((*buffer++) >> 7)
      continue;
    break;
  }
  *v = tmp;
  return buffer;
}


x86 assembly (clang, -O0):
<read_int64>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
mov    -0x18(%rbp),%rax
cmp    -0x20(%rbp),%rax
jb     1139 <read_int64+0x39>
movq   $0x0,-0x8(%rbp)
jmp    1190 <read_int64+0x90>
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
and    $0x7f,%eax
mov    -0x24(%rbp),%ecx
mov    %ecx,%edx
add    $0x1,%edx
mov    %edx,-0x24(%rbp)
imul   $0x7,%ecx,%ecx
shl    %cl,%eax
or     -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,%rcx
add    $0x4,%rcx
mov    %rcx,-0x18(%rbp)
mov    (%rax),%eax
sar    $0x7,%eax
cmp    $0x0,%eax
je     117a <read_int64+0x7a>
jmp    111e <read_int64+0x1e>
jmp    117f <read_int64+0x7f>
mov    -0x28(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_int64>:
mov    %rsi,%rax
xor    %ecx,%ecx
xor    %r9d,%r9d
nopl   0x0(%rax,%rax,1)
cmp    %rdx,%rax
jae    1134 <read_int64+0x34>
mov    (%rax),%r8d
mov    %r8d,%esi
and    $0x7f,%esi
shl    %cl,%esi
or     %esi,%r9d
add    $0x4,%rax
add    $0x7,%ecx
cmp    $0x7f,%r8d
ja     1110 <read_int64+0x10>
mov    %r9d,(%rdi)
ret
xor    %eax,%eax
ret

LLVM IR (-O0):
define i32* @read_int64(i32* %0, i32* %1, i32* %2) {
%4 = alloca i32*
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i32
store i32* %0, i32** %5
store i32* %1, i32** %6
store i32* %2, i32** %7
store i32 0, i32* %8
store i32 0, i32* %9
br label %10
10: ; preds = %3, %30
%11 = load i32*, i32** %6
%12 = load i32*, i32** %7
%13 = icmp uge i32* %11, %12
br i1 %13, label %14, label %15
14: ; preds = %10
store i32* null, i32** %4
br label %36
15: ; preds = %10
%16 = load i32*, i32** %6
%17 = load i32, i32* %16
%18 = and i32 %17, 127
%19 = load i32, i32* %8
%20 = add nsw i32 %19, 1
store i32 %20, i32* %8
%21 = mul nsw i32 %19, 7
%22 = shl i32 %18, %21
%23 = load i32, i32* %9
%24 = or i32 %23, %22
store i32 %24, i32* %9
%25 = load i32*, i32** %6
%26 = getelementptr inbounds i32, i32* %25, i32 1
store i32* %26, i32** %6
%27 = load i32, i32* %25
%28 = ashr i32 %27, 7
%29 = icmp ne i32 %28, 0
br i1 %29, label %30, label %31
30: ; preds = %15
br label %10
31: ; preds = %15
br label %32
32: ; preds = %31
%33 = load i32, i32* %9
%34 = load i32*, i32** %5
store i32 %33, i32* %34
%35 = load i32*, i32** %6
store i32* %35, i32** %4
br label %36
36: ; preds = %32, %14
%37 = load i32*, i32** %4
ret i32* %37
}

LLVM IR (-O2):
define i32* @read_int64(i32* nocapture writeonly %0, i32* %1, i32* readnone %2) {
br label %4
4: ; preds = %9, %3
%5 = phi i32* [ %1, %3 ], [ %16, %9 ]
%6 = phi i32 [ 0, %3 ], [ %12, %9 ]
%7 = phi i32 [ 0, %3 ], [ %15, %9 ]
%8 = icmp ult i32* %5, %2
br i1 %8, label %9, label %19
9: ; preds = %4
%10 = load i32, i32* %5
%11 = and i32 %10, 127
%12 = add nuw nsw i32 %6, 1
%13 = mul nsw i32 %6, 7
%14 = shl i32 %11, %13
%15 = or i32 %14, %7
%16 = getelementptr inbounds i32, i32* %5, i64 1
%17 = icmp ult i32 %10, 128
br i1 %17, label %18, label %4
18: ; preds = %9
store i32 %15, i32* %0
br label %19
19: ; preds = %4, %18
%20 = phi i32* [ %16, %18 ], [ null, %4 ]
ret i32* %20
}