#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 /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ assert(int);
unsigned char fgetc_err(int /*<<< orphan*/ *);
int ReadVarLength(FILE *inf, unsigned long *ptr) {
  unsigned char c = 0;
  unsigned ctr = 0;
  int readcount = 0;
  unsigned long sum = 0;
  (assert((inf != NULL) && (ptr != NULL)));
  for (ctr = 1; ctr < 5; ctr++) {
    c = fgetc_err(inf);
    readcount++;
    sum <<= 7;
    sum += (c & 0x7F);
    if ((c & 0x80) == 0) {
      *ptr = sum;
      return readcount;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<ReadVarLength>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movb   $0x0,-0x19(%rbp)
movl   $0x0,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
movq   $0x0,-0x30(%rbp)
xor    %eax,%eax
cmpq   $0x0,-0x10(%rbp)
mov    %al,-0x31(%rbp)
je     1165 <ReadVarLength+0x45>
cmpq   $0x0,-0x18(%rbp)
setne  %al
mov    %al,-0x31(%rbp)
mov    -0x31(%rbp),%al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
movl   $0x1,-0x20(%rbp)
cmpl   $0x5,-0x20(%rbp)
jae    11f0 <ReadVarLength+0xd0>
mov    -0x10(%rbp),%rdi
call   1030 <fgetc_err@plt>
mov    %al,-0x19(%rbp)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
mov    -0x30(%rbp),%rax
shl    $0x7,%rax
mov    %rax,-0x30(%rbp)
movzbl -0x19(%rbp),%eax
and    $0x7f,%eax
cltq
add    -0x30(%rbp),%rax
mov    %rax,-0x30(%rbp)
movzbl -0x19(%rbp),%eax
and    $0x80,%eax
cmp    $0x0,%eax
jne    11dd <ReadVarLength+0xbd>
mov    -0x30(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11f7 <ReadVarLength+0xd7>
jmp    11e2 <ReadVarLength+0xc2>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    1179 <ReadVarLength+0x59>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ReadVarLength>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%r15
test   %rdi,%rdi
setne  %al
test   %rsi,%rsi
setne  %cl
and    %al,%cl
movzbl %cl,%edi
call   1040 <assert@plt>
mov    %r15,%rdi
call   1030 <fgetc_err@plt>
movzbl %al,%ecx
mov    %ecx,%ebx
and    $0x7f,%ebx
mov    $0x1,%eax
test   $0x80,%cl
je     11c6 <ReadVarLength+0xa6>
mov    %r15,%rdi
call   1030 <fgetc_err@plt>
mov    %rbx,%rcx
shl    $0x7,%rcx
movzbl %al,%edx
mov    %edx,%ebx
and    $0x7f,%ebx
or     %rcx,%rbx
mov    $0x2,%eax
test   $0x80,%dl
je     11c6 <ReadVarLength+0xa6>
mov    %r15,%rdi
call   1030 <fgetc_err@plt>
mov    %rbx,%rcx
shl    $0x7,%rcx
movzbl %al,%edx
mov    %edx,%ebx
and    $0x7f,%ebx
or     %rcx,%rbx
mov    $0x3,%eax
test   $0x80,%dl
je     11c6 <ReadVarLength+0xa6>
mov    %r15,%rdi
call   1030 <fgetc_err@plt>
mov    %eax,%ecx
xor    %eax,%eax
test   $0x80,%cl
jne    11c9 <ReadVarLength+0xa9>
shl    $0x7,%rbx
movzbl %cl,%eax
and    $0x7f,%eax
or     %rax,%rbx
mov    $0x4,%eax
mov    %rbx,(%r14)
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i32 @ReadVarLength(i32* %0, i64* %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i64*
%6 = alloca i8
%7 = alloca i32
%8 = alloca i32
%9 = alloca i64
store i32* %0, i32** %4
store i64* %1, i64** %5
store i8 0, i8* %6
store i32 0, i32* %7
store i32 0, i32* %8
store i64 0, i64* %9
%10 = load i32*, i32** %4
%11 = icmp ne i32* %10, null
br i1 %11, label %12, label %15
12: ; preds = %2
%13 = load i64*, i64** %5
%14 = icmp ne i64* %13, null
br label %15
15: ; preds = %12, %2
%16 = phi i1 [ false, %2 ], [ %14, %12 ]
%17 = zext i1 %16 to i32
%18 = call i32 @assert(i32 %17)
store i32 1, i32* %7
br label %19
19: ; preds = %44, %15
%20 = load i32, i32* %7
%21 = icmp ult i32 %20, 5
br i1 %21, label %22, label %47
22: ; preds = %19
%23 = load i32*, i32** %4
%24 = call zeroext i8 @fgetc_err(i32* %23)
store i8 %24, i8* %6
%25 = load i32, i32* %8
%26 = add nsw i32 %25, 1
store i32 %26, i32* %8
%27 = load i64, i64* %9
%28 = shl i64 %27, 7
store i64 %28, i64* %9
%29 = load i8, i8* %6
%30 = zext i8 %29 to i32
%31 = and i32 %30, 127
%32 = sext i32 %31 to i64
%33 = load i64, i64* %9
%34 = add i64 %33, %32
store i64 %34, i64* %9
%35 = load i8, i8* %6
%36 = zext i8 %35 to i32
%37 = and i32 %36, 128
%38 = icmp eq i32 %37, 0
br i1 %38, label %39, label %43
39: ; preds = %22
%40 = load i64, i64* %9
%41 = load i64*, i64** %5
store i64 %40, i64* %41
%42 = load i32, i32* %8
store i32 %42, i32* %3
br label %48
43: ; preds = %22
br label %44
44: ; preds = %43
%45 = load i32, i32* %7
%46 = add i32 %45, 1
store i32 %46, i32* %7
br label %19
47: ; preds = %19
store i32 0, i32* %3
br label %48
48: ; preds = %47, %39
%49 = load i32, i32* %3
ret i32 %49
}

LLVM IR (-O2):
define i32 @ReadVarLength(i32* %0, i64* writeonly %1) {
%3 = icmp ne i32* %0, null
%4 = icmp ne i64* %1, null
%5 = and i1 %3, %4
%6 = zext i1 %5 to i32
%7 = tail call i32 @assert(i32 %6)
%8 = tail call zeroext i8 @fgetc_err(i32* %0)
%9 = zext i8 %8 to i32
%10 = and i32 %9, 127
%11 = zext i32 %10 to i64
%12 = and i32 %9, 128
%13 = icmp eq i32 %12, 0
br i1 %13, label %41, label %14
14: ; preds = %2
%15 = tail call zeroext i8 @fgetc_err(i32* %0)
%16 = shl nuw nsw i64 %11, 7
%17 = zext i8 %15 to i32
%18 = and i32 %17, 127
%19 = zext i32 %18 to i64
%20 = or i64 %16, %19
%21 = and i32 %17, 128
%22 = icmp eq i32 %21, 0
br i1 %22, label %41, label %23
23: ; preds = %14
%24 = tail call zeroext i8 @fgetc_err(i32* %0)
%25 = shl nuw nsw i64 %20, 7
%26 = zext i8 %24 to i32
%27 = and i32 %26, 127
%28 = zext i32 %27 to i64
%29 = or i64 %25, %28
%30 = and i32 %26, 128
%31 = icmp eq i32 %30, 0
br i1 %31, label %41, label %32
32: ; preds = %23
%33 = tail call zeroext i8 @fgetc_err(i32* %0)
%34 = shl nuw nsw i64 %29, 7
%35 = zext i8 %33 to i32
%36 = and i32 %35, 127
%37 = zext i32 %36 to i64
%38 = or i64 %34, %37
%39 = and i32 %35, 128
%40 = icmp eq i32 %39, 0
br i1 %40, label %41, label %44
41: ; preds = %32, %23, %14, %2
%42 = phi i32 [ 1, %2 ], [ 2, %14 ], [ 3, %23 ], [ 4, %32 ]
%43 = phi i64 [ %11, %2 ], [ %20, %14 ], [ %29, %23 ], [ %38, %32 ]
store i64 %43, i64* %1
br label %44
44: ; preds = %32, %41
%45 = phi i32 [ %42, %41 ], [ 0, %32 ]
ret i32 %45
}