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

/* Variables and functions */
int read(int, char *, int);
int readVarInt_stream(int32_t *output, int fd) {
  *output = 0;
  int v2 = 0;
  signed char v3;
  do {
    if (read(fd, &v3, 1) != 1) {
      return v2;
    }
    *output |= (v3 & 127) << (v2++ * 7);
    if (v2 > 5)
      return v2;
  } while ((v3 & 128) == 128);
  return v2;
}


x86 assembly (clang, -O0):
<readVarInt_stream>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
movl   $0x0,-0x18(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x19(%rbp),%rsi
mov    $0x1,%edx
call   1030 <read@plt>
cmp    $0x1,%eax
je     1152 <readVarInt_stream+0x42>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11a8 <readVarInt_stream+0x98>
movsbl -0x19(%rbp),%edx
and    $0x7f,%edx
mov    -0x18(%rbp),%eax
mov    %eax,%ecx
add    $0x1,%ecx
mov    %ecx,-0x18(%rbp)
imul   $0x7,%eax,%ecx
shl    %cl,%edx
mov    -0x10(%rbp),%rax
movsbl (%rax),%ecx
or     %edx,%ecx
mov    %cl,(%rax)
cmpl   $0x5,-0x18(%rbp)
jle    1189 <readVarInt_stream+0x79>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11a8 <readVarInt_stream+0x98>
jmp    118e <readVarInt_stream+0x7e>
movsbl -0x19(%rbp),%eax
and    $0x80,%eax
cmp    $0x80,%eax
je     112d <readVarInt_stream+0x1d>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readVarInt_stream>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %esi,%r14d
mov    %rdi,%r12
movb   $0x0,(%rdi)
xor    %ebp,%ebp
lea    0xf(%rsp),%r15
xor    %ebx,%ebx
xchg   %ax,%ax
mov    %r14d,%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1030 <read@plt>
cmp    $0x1,%eax
jne    116a <readVarInt_stream+0x5a>
movzbl 0xf(%rsp),%edx
mov    %edx,%esi
and    $0x7f,%esi
lea    0x1(%rbx),%eax
mov    %ebp,%ecx
shl    %cl,%esi
or     %sil,(%r12)
cmp    $0x4,%ebx
ja     116c <readVarInt_stream+0x5c>
add    $0x7,%ebp
mov    %eax,%ebx
test   %dl,%dl
js     1130 <readVarInt_stream+0x20>
jmp    116c <readVarInt_stream+0x5c>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @readVarInt_stream(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8
store i8* %0, i8** %4
store i32 %1, i32* %5
%8 = load i8*, i8** %4
store i8 0, i8* %8
store i32 0, i32* %6
br label %9
9: ; preds = %33, %2
%10 = load i32, i32* %5
%11 = call i32 @read(i32 %10, i8* %7, i32 1)
%12 = icmp ne i32 %11, 1
br i1 %12, label %13, label %15
13: ; preds = %9
%14 = load i32, i32* %6
store i32 %14, i32* %3
br label %40
15: ; preds = %9
%16 = load i8, i8* %7
%17 = sext i8 %16 to i32
%18 = and i32 %17, 127
%19 = load i32, i32* %6
%20 = add nsw i32 %19, 1
store i32 %20, i32* %6
%21 = mul nsw i32 %19, 7
%22 = shl i32 %18, %21
%23 = load i8*, i8** %4
%24 = load i8, i8* %23
%25 = sext i8 %24 to i32
%26 = or i32 %25, %22
%27 = trunc i32 %26 to i8
store i8 %27, i8* %23
%28 = load i32, i32* %6
%29 = icmp sgt i32 %28, 5
br i1 %29, label %30, label %32
30: ; preds = %15
%31 = load i32, i32* %6
store i32 %31, i32* %3
br label %40
32: ; preds = %15
br label %33
33: ; preds = %32
%34 = load i8, i8* %7
%35 = sext i8 %34 to i32
%36 = and i32 %35, 128
%37 = icmp eq i32 %36, 128
br i1 %37, label %9, label %38
38: ; preds = %33
%39 = load i32, i32* %6
store i32 %39, i32* %3
br label %40
40: ; preds = %38, %30, %13
%41 = load i32, i32* %3
ret i32 %41
}

LLVM IR (-O2):
define i32 @readVarInt_stream(i8* nocapture %0, i32 %1) {
%3 = alloca i8
store i8 0, i8* %0
br label %4
4: ; preds = %8, %2
%5 = phi i32 [ 0, %2 ], [ %12, %8 ]
%6 = call i32 @read(i32 %1, i8* %3, i32 1)
%7 = icmp eq i32 %6, 1
br i1 %7, label %8, label %21
8: ; preds = %4
%9 = load i8, i8* %3
%10 = and i8 %9, 127
%11 = zext i8 %10 to i32
%12 = add nuw nsw i32 %5, 1
%13 = mul nuw nsw i32 %5, 7
%14 = shl i32 %11, %13
%15 = load i8, i8* %0
%16 = trunc i32 %14 to i8
%17 = or i8 %15, %16
store i8 %17, i8* %0
%18 = icmp ult i32 %5, 5
%19 = icmp slt i8 %9, 0
%20 = select i1 %18, i1 %19, i1 false
br i1 %20, label %4, label %21
21: ; preds = %8, %4
%22 = phi i32 [ %5, %4 ], [ %12, %8 ]
ret i32 %22
}