#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;
struct bps_data {
  int dummy;
};

/* Variables and functions */
int bps_read(struct bps_data *);
uint64_t bps_decode(struct bps_data *bps) {
  uint64_t data = 0, shift = 1;
  for (;;) {
    uint8_t x = bps_read(bps);
    data += (x & 0x7f) * shift;
    if (x & 0x80)
      break;
    shift <<= 7;
    data += shift;
  }
  return data;
}


x86 assembly (clang, -O0):
<bps_decode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
movl   $0x1,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <bps_read@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
and    $0x7f,%eax
imul   -0x10(%rbp),%eax
add    -0xc(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    -0x14(%rbp),%eax
and    $0x80,%eax
cmp    $0x0,%eax
je     115c <bps_decode+0x4c>
jmp    1173 <bps_decode+0x63>
mov    -0x10(%rbp),%eax
shl    $0x7,%eax
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
add    -0xc(%rbp),%eax
mov    %eax,-0xc(%rbp)
jmp    112a <bps_decode+0x1a>
mov    -0xc(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bps_decode>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%r14
xor    %ebp,%ebp
mov    $0x1,%ebx
xchg   %ax,%ax
mov    %r14,%rdi
call   1030 <bps_read@plt>
mov    %eax,%ecx
and    $0x7f,%eax
imul   %ebx,%eax
add    %ebp,%eax
shl    $0x7,%ebx
lea    (%rax,%rbx,1),%ebp
test   $0x80,%cl
je     1120 <bps_decode+0x10>
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.bps_data = type { i32 }
define i32 @bps_decode(%struct.bps_data* %0) {
%2 = alloca %struct.bps_data*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store %struct.bps_data* %0, %struct.bps_data** %2
store i32 0, i32* %3
store i32 1, i32* %4
br label %6
6: ; preds = %19, %1
%7 = load %struct.bps_data*, %struct.bps_data** %2
%8 = call i32 @bps_read(%struct.bps_data* %7)
store i32 %8, i32* %5
%9 = load i32, i32* %5
%10 = and i32 %9, 127
%11 = load i32, i32* %4
%12 = mul nsw i32 %10, %11
%13 = load i32, i32* %3
%14 = add nsw i32 %13, %12
store i32 %14, i32* %3
%15 = load i32, i32* %5
%16 = and i32 %15, 128
%17 = icmp ne i32 %16, 0
br i1 %17, label %18, label %19
18: ; preds = %6
br label %25
19: ; preds = %6
%20 = load i32, i32* %4
%21 = shl i32 %20, 7
store i32 %21, i32* %4
%22 = load i32, i32* %4
%23 = load i32, i32* %3
%24 = add nsw i32 %23, %22
store i32 %24, i32* %3
br label %6
25: ; preds = %18
%26 = load i32, i32* %3
ret i32 %26
}

LLVM IR (-O2):
%struct.bps_data = type { i32 }
define i32 @bps_decode(%struct.bps_data* %0) {
br label %2
2: ; preds = %2, %1
%3 = phi i32 [ 1, %1 ], [ %11, %2 ]
%4 = phi i32 [ 0, %1 ], [ %12, %2 ]
%5 = tail call i32 @bps_read(%struct.bps_data* %0)
%6 = and i32 %5, 127
%7 = mul nsw i32 %6, %3
%8 = add nsw i32 %7, %4
%9 = and i32 %5, 128
%10 = icmp eq i32 %9, 0
%11 = shl i32 %3, 7
%12 = add nsw i32 %8, %11
br i1 %10, label %2, label %13
13: ; preds = %2
ret i32 %8
}