#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 */
scalar_t__ read(int, unsigned char *, int);
int read_var_len(int fd, long *value) {
  unsigned char c;
  int bytes;
  *value = 0;
  bytes = 0;
  do {
    (*value) <<= 7;
    bytes += read(fd, &c, 1);
    (*value) += (c & 0x7f);
  } while ((c & 0x80) == 0x80);
  return bytes;
}


x86 assembly (clang, -O0):
<read_var_len>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
movq   $0x0,(%rax)
movl   $0x0,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rcx
shl    $0x7,%rcx
mov    %rcx,(%rax)
mov    -0x4(%rbp),%edi
lea    -0x11(%rbp),%rsi
mov    $0x1,%edx
call   1030 <read@plt>
mov    %rax,%rcx
movslq -0x18(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x18(%rbp)
movzbl -0x11(%rbp),%eax
and    $0x7f,%eax
movslq %eax,%rcx
mov    -0x10(%rbp),%rax
add    (%rax),%rcx
mov    %rcx,(%rax)
movzbl -0x11(%rbp),%eax
and    $0x80,%eax
cmp    $0x80,%eax
je     1131 <read_var_len+0x21>
mov    -0x18(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_var_len>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %edi,%r15d
xor    %eax,%eax
lea    0x7(%rsp),%r14
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
shl    $0x7,%rax
mov    %rax,(%rbx)
mov    %r15d,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1030 <read@plt>
movslq %ebp,%rbp
add    %rax,%rbp
movzbl 0x7(%rsp),%ecx
mov    %ecx,%eax
and    $0x7f,%eax
add    (%rbx),%rax
mov    %rax,(%rbx)
test   %cl,%cl
js     1130 <read_var_len+0x20>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @read_var_len(i32 %0, i64* %1) {
%3 = alloca i32
%4 = alloca i64*
%5 = alloca i8
%6 = alloca i32
store i32 %0, i32* %3
store i64* %1, i64** %4
%7 = load i64*, i64** %4
store i64 0, i64* %7
store i32 0, i32* %6
br label %8
8: ; preds = %25, %2
%9 = load i64*, i64** %4
%10 = load i64, i64* %9
%11 = shl i64 %10, 7
store i64 %11, i64* %9
%12 = load i32, i32* %3
%13 = call i64 @read(i32 %12, i8* %5, i32 1)
%14 = load i32, i32* %6
%15 = sext i32 %14 to i64
%16 = add nsw i64 %15, %13
%17 = trunc i64 %16 to i32
store i32 %17, i32* %6
%18 = load i8, i8* %5
%19 = zext i8 %18 to i32
%20 = and i32 %19, 127
%21 = sext i32 %20 to i64
%22 = load i64*, i64** %4
%23 = load i64, i64* %22
%24 = add nsw i64 %23, %21
store i64 %24, i64* %22
br label %25
25: ; preds = %8
%26 = load i8, i8* %5
%27 = zext i8 %26 to i32
%28 = and i32 %27, 128
%29 = icmp eq i32 %28, 128
br i1 %29, label %8, label %30
30: ; preds = %25
%31 = load i32, i32* %6
ret i32 %31
}

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