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

/* Variables and functions */
int write(int, ...);
int writeVarInt_stream(int32_t input, int fd) {
  int i = 0;
  unsigned char n = 0;
  while ((input & -128) != 0) {
    n = (input & 127) | 128;
    if (write(fd, &n, 1) != 1) {
      return -1;
    }
    input >>= 7;
  }
  if (write(fd, &input, 1) != 1)
    return -1;
  return i;
}


x86 assembly (clang, -O0):
<writeVarInt_stream>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
movb   $0x0,-0x11(%rbp)
mov    -0x8(%rbp),%eax
and    $0xffffff80,%eax
cmp    $0x0,%eax
je     117c <writeVarInt_stream+0x6c>
mov    -0x8(%rbp),%eax
and    $0x7f,%eax
or     $0x80,%eax
mov    %al,-0x11(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x11(%rbp),%rsi
mov    $0x1,%edx
mov    $0x0,%al
call   1030 <write@plt>
cmp    $0x1,%eax
je     116e <writeVarInt_stream+0x5e>
movl   $0xffffffff,-0x4(%rbp)
jmp    11aa <writeVarInt_stream+0x9a>
mov    -0x8(%rbp),%eax
sar    $0x7,%eax
mov    %eax,-0x8(%rbp)
jmp    1129 <writeVarInt_stream+0x19>
mov    -0xc(%rbp),%edi
lea    -0x8(%rbp),%rsi
mov    $0x1,%edx
mov    $0x0,%al
call   1030 <write@plt>
cmp    $0x1,%eax
je     11a4 <writeVarInt_stream+0x94>
movl   $0xffffffff,-0x4(%rbp)
jmp    11aa <writeVarInt_stream+0x9a>
mov    -0x10(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<writeVarInt_stream>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %esi,%ebx
mov    %edi,0xc(%rsp)
movb   $0x0,0xb(%rsp)
cmp    $0x80,%edi
jb     1171 <writeVarInt_stream+0x61>
mov    %edi,%ebp
lea    0xb(%rsp),%r14
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %ebp,%eax
or     $0x80,%al
mov    %al,0xb(%rsp)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x1,%edx
xor    %eax,%eax
call   1030 <write@plt>
cmp    $0x1,%eax
jne    1192 <writeVarInt_stream+0x82>
mov    %ebp,%eax
sar    $0x7,%eax
mov    %eax,0xc(%rsp)
cmp    $0x3fff,%ebp
mov    %eax,%ebp
ja     1140 <writeVarInt_stream+0x30>
lea    0xc(%rsp),%rsi
mov    %ebx,%edi
mov    $0x1,%edx
xor    %eax,%eax
call   1030 <write@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0x1,%ecx
setne  %al
neg    %eax
jmp    1197 <writeVarInt_stream+0x87>
mov    $0xffffffff,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i32 @writeVarInt_stream(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8
store i32 %0, i32* %4
store i32 %1, i32* %5
store i32 0, i32* %6
store i8 0, i8* %7
br label %8
8: ; preds = %21, %2
%9 = load i32, i32* %4
%10 = and i32 %9, -128
%11 = icmp ne i32 %10, 0
br i1 %11, label %12, label %24
12: ; preds = %8
%13 = load i32, i32* %4
%14 = and i32 %13, 127
%15 = or i32 %14, 128
%16 = trunc i32 %15 to i8
store i8 %16, i8* %7
%17 = load i32, i32* %5
%18 = call i32 (i32, ...) @write(i32 %17, i8* %7, i32 1)
%19 = icmp ne i32 %18, 1
br i1 %19, label %20, label %21
20: ; preds = %12
store i32 -1, i32* %3
br label %31
21: ; preds = %12
%22 = load i32, i32* %4
%23 = ashr i32 %22, 7
store i32 %23, i32* %4
br label %8
24: ; preds = %8
%25 = load i32, i32* %5
%26 = call i32 (i32, ...) @write(i32 %25, i32* %4, i32 1)
%27 = icmp ne i32 %26, 1
br i1 %27, label %28, label %29
28: ; preds = %24
store i32 -1, i32* %3
br label %31
29: ; preds = %24
%30 = load i32, i32* %6
store i32 %30, i32* %3
br label %31
31: ; preds = %29, %28, %20
%32 = load i32, i32* %3
ret i32 %32
}

LLVM IR (-O2):
define i32 @writeVarInt_stream(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8
store i32 %0, i32* %3
store i8 0, i8* %4
%5 = icmp ult i32 %0, 128
br i1 %5, label %15, label %6
6: ; preds = %2, %12
%7 = phi i32 [ %13, %12 ], [ %0, %2 ]
%8 = trunc i32 %7 to i8
%9 = or i8 %8, -128
store i8 %9, i8* %4
%10 = call i32 (i32, ...) @write(i32 %1, i8* %4, i32 1)
%11 = icmp eq i32 %10, 1
br i1 %11, label %12, label %19
12: ; preds = %6
%13 = ashr i32 %7, 7
store i32 %13, i32* %3
%14 = icmp ult i32 %7, 16384
br i1 %14, label %15, label %6
15: ; preds = %12, %2
%16 = call i32 (i32, ...) @write(i32 %1, i32* %3, i32 1)
%17 = icmp ne i32 %16, 1
%18 = sext i1 %17 to i32
br label %19
19: ; preds = %6, %15
%20 = phi i32 [ %18, %15 ], [ -1, %6 ]
ret i32 %20
}