#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 */
void *read_byte(void *, int *);
void *read_length(void *src, int *len) {
  int v;
  src = read_byte(src, &v);
  if (v <= 127) {
    *len = v;
  } else {
    int size = v & 0x7F;
    int val = 0;
    int i;
    for (i = 0; i < size; i++) {
      src = read_byte(src, &v);
      val = (val << 8) + v;
    }
    *len = val;
  }
  return src;
}


x86 assembly (clang, -O0):
<read_length>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
lea    -0x14(%rbp),%rsi
call   1030 <read_byte@plt>
mov    %rax,-0x8(%rbp)
cmpl   $0x7f,-0x14(%rbp)
jg     1149 <read_length+0x39>
mov    -0x14(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
jmp    11a0 <read_length+0x90>
mov    -0x14(%rbp),%eax
and    $0x7f,%eax
mov    %eax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    1197 <read_length+0x87>
mov    -0x8(%rbp),%rdi
lea    -0x14(%rbp),%rsi
call   1030 <read_byte@plt>
mov    %rax,-0x8(%rbp)
mov    -0x1c(%rbp),%eax
shl    $0x8,%eax
add    -0x14(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    1160 <read_length+0x50>
mov    -0x1c(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_length>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
lea    0x4(%rsp),%rsi
call   1030 <read_byte@plt>
mov    0x4(%rsp),%ebx
cmp    $0x80,%ebx
jge    1134 <read_length+0x24>
mov    %ebx,%ebp
jmp    1157 <read_length+0x47>
xor    %ebp,%ebp
and    $0x7f,%ebx
je     1157 <read_length+0x47>
lea    0x4(%rsp),%r15
mov    %rax,%rdi
mov    %r15,%rsi
call   1030 <read_byte@plt>
shl    $0x8,%ebp
add    0x4(%rsp),%ebp
add    $0xffffffff,%ebx
jne    1140 <read_length+0x30>
mov    %ebp,(%r14)
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i8* @read_length(i8* %0, i32* nocapture writeonly %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = call i8* @read_byte(i8* %0, i32* %3)
%6 = load i32, i32* %3
%7 = icmp slt i32 %6, 128
br i1 %7, label %21, label %8
8: ; preds = %2
%9 = and i32 %6, 127
%10 = icmp eq i32 %9, 0
br i1 %10, label %21, label %11
11: ; preds = %8, %11
%12 = phi i32 [ %19, %11 ], [ 0, %8 ]
%13 = phi i32 [ %18, %11 ], [ 0, %8 ]
%14 = phi i8* [ %15, %11 ], [ %5, %8 ]
%15 = call i8* @read_byte(i8* %14, i32* %3)
%16 = shl i32 %13, 8
%17 = load i32, i32* %3
%18 = add nsw i32 %17, %16
%19 = add nuw nsw i32 %12, 1
%20 = icmp eq i32 %19, %9
br i1 %20, label %21, label %11
21: ; preds = %11, %8, %2
%22 = phi i32 [ %6, %2 ], [ 0, %8 ], [ %18, %11 ]
%23 = phi i8* [ %5, %2 ], [ %5, %8 ], [ %15, %11 ]
store i32 %22, i32* %1
ret i8* %23
}