#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*/ value;
typedef int uint8_t;
typedef scalar_t__ uint64_t;

/* Variables and functions */
int next(int);
int /*<<< orphan*/ printf(char *, int);
uint64_t varint(int fd) {
  union {
    uint8_t bytes[8];
    uint64_t u64;
  } value;
  unsigned count = 0;
  value.u64 = 0;
  do {
    uint8_t byte = next(fd);
    value.bytes[count] = byte & 0x7f;
    printf("value is 0x%0x\n", value.bytes[count]);
    printf("byte is 0x%0x\n", byte);
    if (!(byte & 0x80)) {
      break;
    }
  } while (++count < sizeof(value));
  return value.u64;
}


x86 assembly (clang, -O0):
<varint>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x0,-0x2c(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x4(%rbp),%edi
call   1040 <next@plt>
mov    %eax,-0x30(%rbp)
mov    -0x30(%rbp),%ecx
and    $0x7f,%ecx
mov    -0x2c(%rbp),%eax
mov    %ecx,-0x28(%rbp,%rax,4)
mov    -0x2c(%rbp),%eax
mov    -0x28(%rbp,%rax,4),%esi
lea    0xea0(%rip),%rdi
call   1030 <printf@plt>
mov    -0x30(%rbp),%esi
lea    0xea1(%rip),%rdi
call   1030 <printf@plt>
mov    -0x30(%rbp),%eax
and    $0x80,%eax
cmp    $0x0,%eax
jne    118a <varint+0x6a>
jmp    11a4 <varint+0x84>
jmp    118f <varint+0x6f>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
mov    %eax,%eax
cmp    $0x20,%rax
jb     113a <varint+0x1a>
mov    -0x28(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<varint>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %edi,%r12d
movq   $0x0,(%rsp)
xor    %ebx,%ebx
lea    0xec0(%rip),%r14
lea    0xec9(%rip),%r15
nopw   0x0(%rax,%rax,1)
mov    %r12d,%edi
call   1040 <next@plt>
mov    %eax,%ebp
mov    %eax,%esi
and    $0x7f,%esi
mov    %esi,(%rsp,%rbx,4)
mov    %r14,%rdi
call   1030 <printf@plt>
mov    %r15,%rdi
mov    %ebp,%esi
call   1030 <printf@plt>
test   $0x80,%bpl
je     1187 <varint+0x67>
lea    0x1(%rbx),%rax
cmp    $0x1f,%rbx
mov    %rax,%rbx
jb     1150 <varint+0x30>
mov    (%rsp),%rax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%union.anon = type { i64, [24 x i8] }
@.str = [16 x i8] c"value is 0x%0x\0A\00"
@.str.1 = [15 x i8] c"byte is 0x%0x\0A\00"
define i64 @varint(i32 %0) {
%2 = alloca i32
%3 = alloca %union.anon
%4 = alloca i32
%5 = alloca i32
store i32 %0, i32* %2
store i32 0, i32* %4
%6 = bitcast %union.anon* %3 to i64*
store i64 0, i64* %6
br label %7
7: ; preds = %29, %1
%8 = load i32, i32* %2
%9 = call i32 @next(i32 %8)
store i32 %9, i32* %5
%10 = load i32, i32* %5
%11 = and i32 %10, 127
%12 = bitcast %union.anon* %3 to [8 x i32]*
%13 = load i32, i32* %4
%14 = zext i32 %13 to i64
%15 = getelementptr inbounds [8 x i32], [8 x i32]* %12, i64 0, i64 %14
store i32 %11, i32* %15
%16 = bitcast %union.anon* %3 to [8 x i32]*
%17 = load i32, i32* %4
%18 = zext i32 %17 to i64
%19 = getelementptr inbounds [8 x i32], [8 x i32]* %16, i64 0, i64 %18
%20 = load i32, i32* %19
%21 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %20)
%22 = load i32, i32* %5
%23 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i32 %22)
%24 = load i32, i32* %5
%25 = and i32 %24, 128
%26 = icmp ne i32 %25, 0
br i1 %26, label %28, label %27
27: ; preds = %7
br label %34
28: ; preds = %7
br label %29
29: ; preds = %28
%30 = load i32, i32* %4
%31 = add i32 %30, 1
store i32 %31, i32* %4
%32 = zext i32 %31 to i64
%33 = icmp ult i64 %32, 32
br i1 %33, label %7, label %34
34: ; preds = %29, %27
%35 = bitcast %union.anon* %3 to i64*
%36 = load i64, i64* %35
ret i64 %36
}

LLVM IR (-O2):
%union.anon = type { i64, [24 x i8] }
@.str = [16 x i8] c"value is 0x%0x\0A\00"
@.str.1 = [15 x i8] c"byte is 0x%0x\0A\00"
define i64 @varint(i32 %0) {
%2 = alloca %union.anon
%3 = bitcast %union.anon* %2 to i8*
%4 = getelementptr inbounds %union.anon, %union.anon* %2, i64 0, i32 0
store i64 0, i64* %4
%5 = bitcast %union.anon* %2 to [8 x i32]*
br label %6
6: ; preds = %6, %1
%7 = phi i64 [ %15, %6 ], [ 0, %1 ]
%8 = tail call i32 @next(i32 %0)
%9 = and i32 %8, 127
%10 = getelementptr inbounds [8 x i32], [8 x i32]* %5, i64 0, i64 %7
store i32 %9, i32* %10
%11 = tail call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %9)
%12 = tail call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i32 %8)
%13 = and i32 %8, 128
%14 = icmp ne i32 %13, 0
%15 = add nuw nsw i64 %7, 1
%16 = icmp ult i64 %7, 31
%17 = select i1 %14, i1 %16, i1 false
br i1 %17, label %6, label %18
18: ; preds = %6
%19 = load i64, i64* %4
ret i64 %19
}