#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;

/* Variables and functions */
int UINT64_C(int);
int /*<<< orphan*/ error(char *);
int read_byte();
uint64_t read_var_restricted(void) {
  uint64_t value = 0;
  uint8_t byte;
  int padding = 0;
  for (;;) {
    byte = read_byte();
    if (byte != 0x80)
      break;
    ++padding;
    if (padding > 8)
      error("more than 8 consecutive padding bytes");
  }
  for (;;) {
    if (value & UINT64_C(0xe000000000000000))
      error("value too big (more than 64 bits)");
    value = (value << 7) | (byte & 0x7f);
    if ((byte & 0x80) == 0)
      break;
    byte = read_byte();
  }
  return value;
}


x86 assembly (clang, -O0):
<read_var_restricted>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0xc(%rbp)
mov    $0x0,%al
call   1030 <read_byte@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x80,-0x8(%rbp)
je     1162 <read_var_restricted+0x32>
jmp    1186 <read_var_restricted+0x56>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
cmpl   $0x8,-0xc(%rbp)
jle    1181 <read_var_restricted+0x51>
lea    0xe84(%rip),%rdi
call   1050 <error@plt>
jmp    1146 <read_var_restricted+0x16>
jmp    118b <read_var_restricted+0x5b>
mov    -0x4(%rbp),%eax
mov    %eax,-0x10(%rbp)
xor    %edi,%edi
call   1040 <UINT64_C@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%eax
and    %ecx,%eax
cmp    $0x0,%eax
je     11b4 <read_var_restricted+0x84>
lea    0xe77(%rip),%rdi
call   1050 <error@plt>
mov    -0x4(%rbp),%eax
shl    $0x7,%eax
mov    -0x8(%rbp),%ecx
and    $0x7f,%ecx
or     %ecx,%eax
mov    %eax,-0x4(%rbp)
mov    -0x8(%rbp),%eax
and    $0x80,%eax
cmp    $0x0,%eax
jne    11db <read_var_restricted+0xab>
jmp    11ea <read_var_restricted+0xba>
mov    $0x0,%al
call   1030 <read_byte@plt>
mov    %eax,-0x8(%rbp)
jmp    118b <read_var_restricted+0x5b>
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_var_restricted>:
push   %rbp
push   %r14
push   %rbx
xor    %eax,%eax
call   1030 <read_byte@plt>
mov    %eax,%ebx
cmp    $0x80,%eax
jne    1172 <read_var_restricted+0x42>
lea    0xeb5(%rip),%r14
xor    %ebp,%ebp
jmp    1161 <read_var_restricted+0x31>
nop
xor    %eax,%eax
call   1030 <read_byte@plt>
add    $0x1,%ebp
cmp    $0x80,%eax
jne    1170 <read_var_restricted+0x40>
cmp    $0x8,%ebp
jb     1150 <read_var_restricted+0x20>
mov    %r14,%rdi
call   1050 <error@plt>
jmp    1150 <read_var_restricted+0x20>
mov    %eax,%ebx
lea    0xead(%rip),%r14
xor    %ebp,%ebp
nopl   0x0(%rax,%rax,1)
xor    %edi,%edi
call   1040 <UINT64_C@plt>
test   %ebp,%eax
je     1193 <read_var_restricted+0x63>
mov    %r14,%rdi
call   1050 <error@plt>
mov    %ebp,%eax
shl    $0x7,%eax
mov    %ebx,%ebp
and    $0x7f,%ebp
or     %eax,%ebp
test   $0x80,%bl
je     11af <read_var_restricted+0x7f>
xor    %eax,%eax
call   1030 <read_byte@plt>
mov    %eax,%ebx
jmp    1180 <read_var_restricted+0x50>
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [38 x i8] c"more than 8 consecutive padding bytes\00"
@.str.1 = [34 x i8] c"value too big (more than 64 bits)\00"
define i32 @read_var_restricted() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
store i32 0, i32* %1
store i32 0, i32* %3
br label %4
4: ; preds = %16, %0
%5 = call i32 (...) @read_byte()
store i32 %5, i32* %2
%6 = load i32, i32* %2
%7 = icmp ne i32 %6, 128
br i1 %7, label %8, label %9
8: ; preds = %4
br label %17
9: ; preds = %4
%10 = load i32, i32* %3
%11 = add nsw i32 %10, 1
store i32 %11, i32* %3
%12 = load i32, i32* %3
%13 = icmp sgt i32 %12, 8
br i1 %13, label %14, label %16
14: ; preds = %9
%15 = call i32 @error(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0))
br label %16
16: ; preds = %14, %9
br label %4
17: ; preds = %8
br label %18
18: ; preds = %35, %17
%19 = load i32, i32* %1
%20 = call i32 @UINT64_C(i32 0)
%21 = and i32 %19, %20
%22 = icmp ne i32 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %18
%24 = call i32 @error(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0))
br label %25
25: ; preds = %23, %18
%26 = load i32, i32* %1
%27 = shl i32 %26, 7
%28 = load i32, i32* %2
%29 = and i32 %28, 127
%30 = or i32 %27, %29
store i32 %30, i32* %1
%31 = load i32, i32* %2
%32 = and i32 %31, 128
%33 = icmp eq i32 %32, 0
br i1 %33, label %34, label %35
34: ; preds = %25
br label %37
35: ; preds = %25
%36 = call i32 (...) @read_byte()
store i32 %36, i32* %2
br label %18
37: ; preds = %34
%38 = load i32, i32* %1
ret i32 %38
}

LLVM IR (-O2):
@.str = [38 x i8] c"more than 8 consecutive padding bytes\00"
@.str.1 = [34 x i8] c"value too big (more than 64 bits)\00"
define i32 @read_var_restricted() {
%1 = tail call i32 (...) @read_byte()
%2 = icmp eq i32 %1, 128
br i1 %2, label %3, label %12
3: ; preds = %0, %9
%4 = phi i32 [ %5, %9 ], [ 0, %0 ]
%5 = add nuw nsw i32 %4, 1
%6 = icmp ugt i32 %4, 7
br i1 %6, label %7, label %9
7: ; preds = %3
%8 = tail call i32 @error(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0))
br label %9
9: ; preds = %7, %3
%10 = tail call i32 (...) @read_byte()
%11 = icmp eq i32 %10, 128
br i1 %11, label %3, label %12
12: ; preds = %9, %0
%13 = phi i32 [ %1, %0 ], [ %10, %9 ]
br label %14
14: ; preds = %12, %28
%15 = phi i32 [ %25, %28 ], [ 0, %12 ]
%16 = phi i32 [ %29, %28 ], [ %13, %12 ]
%17 = tail call i32 @UINT64_C(i32 0)
%18 = and i32 %17, %15
%19 = icmp eq i32 %18, 0
br i1 %19, label %22, label %20
20: ; preds = %14
%21 = tail call i32 @error(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0))
br label %22
22: ; preds = %20, %14
%23 = shl i32 %15, 7
%24 = and i32 %16, 127
%25 = or i32 %24, %23
%26 = and i32 %16, 128
%27 = icmp eq i32 %26, 0
br i1 %27, label %30, label %28
28: ; preds = %22
%29 = tail call i32 (...) @read_byte()
br label %14
30: ; preds = %22
ret i32 %25
}