#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();
long mgetsleb128(void) {
  int c;
  long value = 0;
  long shift = 0;
  while (1) {
    c = mgetchar();
    if (c == EOF)
      exit(1);
    value += (c & 0x7fL) << shift;
    shift += 7;
    if (!(c & 0x80)) {
      if (c & 0x40)
        return value | (-1L << shift);
      else
        return value;
    }
  }
}


x86 assembly (clang, -O0):
<mgetsleb128>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    $0x0,%al
call   1030 <mgetchar@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    0x2e9c(%rip),%rcx
cmp    (%rcx),%eax
jne    115e <mgetsleb128+0x3e>
mov    $0x1,%edi
call   1040 <exit@plt>
movslq -0xc(%rbp),%rax
and    $0x7f,%rax
mov    -0x20(%rbp),%rcx
shl    %cl,%rax
add    -0x18(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x20(%rbp),%rax
add    $0x7,%rax
mov    %rax,-0x20(%rbp)
mov    -0xc(%rbp),%eax
and    $0x80,%eax
cmp    $0x0,%eax
jne    11cf <mgetsleb128+0xaf>
mov    -0xc(%rbp),%eax
and    $0x40,%eax
cmp    $0x0,%eax
je     11c2 <mgetsleb128+0xa2>
mov    -0x18(%rbp),%rax
mov    -0x20(%rbp),%rcx
mov    $0xffffffffffffffff,%rdx
shl    %cl,%rdx
mov    %rdx,%rcx
or     %rcx,%rax
mov    %rax,-0x8(%rbp)
jmp    11d4 <mgetsleb128+0xb4>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11d4 <mgetsleb128+0xb4>
jmp    1138 <mgetsleb128+0x18>
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mgetsleb128>:
push   %r15
push   %r14
push   %rbx
mov    $0xfffffffffffffff9,%rbx
mov    0x2eb5(%rip),%r15
xor    %r14d,%r14d
cs nopw 0x0(%rax,%rax,1)
xor    %eax,%eax
call   1030 <mgetchar@plt>
cmp    (%r15),%eax
je     1181 <mgetsleb128+0x61>
add    $0x7,%rbx
mov    %eax,%edx
and    $0x7f,%edx
mov    %ebx,%ecx
shl    %cl,%rdx
add    %rdx,%r14
test   $0x80,%al
jne    1140 <mgetsleb128+0x20>
mov    $0xffffffffffffff80,%rdx
mov    %ebx,%ecx
shl    %cl,%rdx
xor    %ecx,%ecx
test   $0x40,%al
cmovne %rdx,%rcx
or     %rcx,%r14
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 @mgetsleb128() {
%1 = alloca i64
%2 = alloca i32
%3 = alloca i64
%4 = alloca i64
store i64 0, i64* %3
store i64 0, i64* %4
br label %5
5: ; preds = %0, %36
%6 = call i32 (...) @mgetchar()
store i32 %6, i32* %2
%7 = load i32, i32* %2
%8 = load i32, i32* @EOF
%9 = icmp eq i32 %7, %8
br i1 %9, label %10, label %12
10: ; preds = %5
%11 = call i32 @exit(i32 1)
unreachable
12: ; preds = %5
%13 = load i32, i32* %2
%14 = sext i32 %13 to i64
%15 = and i64 %14, 127
%16 = load i64, i64* %4
%17 = shl i64 %15, %16
%18 = load i64, i64* %3
%19 = add nsw i64 %18, %17
store i64 %19, i64* %3
%20 = load i64, i64* %4
%21 = add nsw i64 %20, 7
store i64 %21, i64* %4
%22 = load i32, i32* %2
%23 = and i32 %22, 128
%24 = icmp ne i32 %23, 0
br i1 %24, label %36, label %25
25: ; preds = %12
%26 = load i32, i32* %2
%27 = and i32 %26, 64
%28 = icmp ne i32 %27, 0
br i1 %28, label %29, label %34
29: ; preds = %25
%30 = load i64, i64* %3
%31 = load i64, i64* %4
%32 = shl i64 -1, %31
%33 = or i64 %30, %32
store i64 %33, i64* %1
br label %37
34: ; preds = %25
%35 = load i64, i64* %3
store i64 %35, i64* %1
br label %37
36: ; preds = %12
br label %5
37: ; preds = %34, %29
%38 = load i64, i64* %1
ret i64 %38
}

LLVM IR (-O2):
@EOF = global i32 0
define i64 @mgetsleb128() {
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
%18 = and i32 %4, 64
%19 = icmp eq i32 %18, 0
%20 = shl i64 -128, %3
%21 = select i1 %19, i64 0, i64 %20
%22 = or i64 %13, %21
ret i64 %22
}