#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 EOF;
int /*<<< orphan*/ exit(int);
int mgetchar();
unsigned long mgetuleb128(void) {
  int c;
  long value = 0;
  long shift = 0;
  int i = 0;
  while (1) {
    i++;
    c = mgetchar();
    if (c == EOF)
      exit(1);
    value += (c & 0x7fL) << shift;
    shift += 7;
    if (!(c & 0x80)) {
      return value;
    }
  }
}


x86 assembly (clang, -O0):
<mgetuleb128>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movq   $0x0,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
mov    $0x0,%al
call   1030 <mgetchar@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
mov    0x2e8c(%rip),%rcx
cmp    (%rcx),%eax
jne    116e <mgetuleb128+0x4e>
mov    $0x1,%edi
call   1040 <exit@plt>
movslq -0x4(%rbp),%rax
and    $0x7f,%rax
mov    -0x18(%rbp),%rcx
shl    %cl,%rax
add    -0x10(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x18(%rbp),%rax
add    $0x7,%rax
mov    %rax,-0x18(%rbp)
mov    -0x4(%rbp),%eax
and    $0x80,%eax
cmp    $0x0,%eax
jne    11ac <mgetuleb128+0x8c>
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret
jmp    113f <mgetuleb128+0x1f>

x86 assembly (clang, -O2):
<mgetuleb128>:
push   %r15
push   %r14
push   %rbx
mov    0x2ebc(%rip),%r15
xor    %r14d,%r14d
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1030 <mgetchar@plt>
cmp    (%r15),%eax
je     116a <mgetuleb128+0x4a>
mov    %eax,%edx
and    $0x7f,%edx
mov    %ebx,%ecx
shl    %cl,%rdx
add    %rdx,%r14
add    $0x7,%rbx
test   $0x80,%al
jne    1140 <mgetuleb128+0x20>
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %r15
ret
mov    $0x1,%edi
call   1040 <exit@plt>

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

LLVM IR (-O2):
@EOF = global i32 0
define i64 @mgetuleb128() {
br label %1
1: ; preds = %9, %0
%2 = phi i64 [ 0, %0 ], [ %13, %9 ]
%3 = phi i64 [ 0, %0 ], [ %14, %9 ]
%4 = tail call i32 (...) @mgetchar()
%5 = load i32, i32* @EOF
%6 = icmp eq i32 %4, %5
br i1 %6, label %7, label %9
7: ; preds = %1
%8 = tail call i32 @exit(i32 1)
unreachable
9: ; preds = %1
%10 = and i32 %4, 127
%11 = zext i32 %10 to i64
%12 = shl i64 %11, %3
%13 = add nsw i64 %12, %2
%14 = add nuw nsw i64 %3, 7
%15 = and i32 %4, 128
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %1
17: ; preds = %9
ret i64 %13
}