#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_bit();
int read_byte();
int read_offset() {
  int value;
  int i;
  value = read_byte();
  if (value < 128) {
    return value;
  } else {
    i = read_bit();
    i = i << 1 | read_bit();
    i = i << 1 | read_bit();
    i = i << 1 | read_bit();
    return (value & 127 | i << 7) + 128;
  }
}


x86 assembly (clang, -O0):
<read_offset>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    $0x0,%al
call   1030 <read_byte@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x80,-0x8(%rbp)
jge    114a <read_offset+0x2a>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11b8 <read_offset+0x98>
mov    $0x0,%al
call   1040 <read_bit@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x18(%rbp)
mov    $0x0,%al
call   1040 <read_bit@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%eax
or     %ecx,%eax
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    $0x0,%al
call   1040 <read_bit@plt>
mov    %eax,%ecx
mov    -0x14(%rbp),%eax
or     %ecx,%eax
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x10(%rbp)
mov    $0x0,%al
call   1040 <read_bit@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%eax
or     %ecx,%eax
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%eax
and    $0x7f,%eax
mov    -0xc(%rbp),%ecx
shl    $0x7,%ecx
or     %ecx,%eax
add    $0x80,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_offset>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
xor    %eax,%eax
call   1030 <read_byte@plt>
mov    %eax,%r15d
cmp    $0x80,%eax
jl     117c <read_offset+0x5c>
xor    %eax,%eax
call   1040 <read_bit@plt>
mov    %eax,%r14d
xor    %eax,%eax
call   1040 <read_bit@plt>
mov    %eax,%ebp
shl    $0x2,%r14d
add    %eax,%ebp
or     %r14d,%ebp
xor    %eax,%eax
call   1040 <read_bit@plt>
mov    %eax,%ebx
or     %ebp,%ebx
xor    %eax,%eax
call   1040 <read_bit@plt>
and    $0x7f,%r15d
shl    $0x8,%ebx
shl    $0x7,%eax
or     %ebx,%eax
add    %eax,%r15d
add    $0x80,%r15d
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i32 @read_offset() {
%1 = tail call i32 (...) @read_byte()
%2 = icmp slt i32 %1, 128
br i1 %2, label %18, label %3
3: ; preds = %0
%4 = tail call i32 (...) @read_bit()
%5 = tail call i32 (...) @read_bit()
%6 = shl i32 %4, 2
%7 = shl i32 %5, 1
%8 = or i32 %7, %6
%9 = tail call i32 (...) @read_bit()
%10 = or i32 %8, %9
%11 = tail call i32 (...) @read_bit()
%12 = and i32 %1, 127
%13 = shl i32 %10, 8
%14 = shl i32 %11, 7
%15 = or i32 %14, %13
%16 = or i32 %12, 128
%17 = add i32 %16, %15
br label %18
18: ; preds = %0, %3
%19 = phi i32 [ %17, %3 ], [ %1, %0 ]
ret i32 %19
}