#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*/ fwrite(unsigned char *, int, int, int /*<<< orphan*/ *);
void write_variable_length_quantity(FILE *out, unsigned long value) {
  unsigned char buffer[4];
  int offset = 3;
  while (1) {
    buffer[offset] = (unsigned char)(value & 0x7F);
    if (offset < 3)
      buffer[offset] |= 0x80;
    value >>= 7;
    if ((value == 0) || (offset == 0))
      break;
    offset--;
  }
  fwrite(buffer + offset, 1, 4 - offset, out);
}


x86 assembly (clang, -O0):
<write_variable_length_quantity>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x3,-0x18(%rbp)
mov    -0x10(%rbp),%rax
and    $0x7f,%rax
mov    %al,%cl
movslq -0x18(%rbp),%rax
mov    %cl,-0x14(%rbp,%rax,1)
cmpl   $0x3,-0x18(%rbp)
jge    1156 <write_variable_length_quantity+0x46>
movslq -0x18(%rbp),%rax
movzbl -0x14(%rbp,%rax,1),%ecx
or     $0x80,%ecx
mov    %cl,-0x14(%rbp,%rax,1)
mov    -0x10(%rbp),%rax
shr    $0x7,%rax
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1177 <write_variable_length_quantity+0x67>
cmpl   $0x0,-0x18(%rbp)
jne    117c <write_variable_length_quantity+0x6c>
jmp    118a <write_variable_length_quantity+0x7a>
mov    -0x18(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x18(%rbp)
jmp    1127 <write_variable_length_quantity+0x17>
lea    -0x14(%rbp),%rdi
movslq -0x18(%rbp),%rax
add    %rax,%rdi
mov    $0x4,%edx
sub    -0x18(%rbp),%edx
mov    -0x8(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fwrite@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_variable_length_quantity>:
push   %rax
mov    %rdi,%rcx
mov    %esi,%eax
and    $0x7f,%al
mov    %al,0x7(%rsp)
cmp    $0x80,%rsi
jae    1131 <write_variable_length_quantity+0x21>
mov    $0x1,%edx
mov    $0x3,%eax
jmp    117d <write_variable_length_quantity+0x6d>
mov    %rsi,%rax
shr    $0x7,%rax
or     $0x80,%al
mov    %al,0x6(%rsp)
mov    $0x2,%eax
cmp    $0x4000,%rsi
jb     1176 <write_variable_length_quantity+0x66>
mov    %rsi,%rax
shr    $0xe,%rax
or     $0x80,%al
mov    %al,0x5(%rsp)
mov    $0x1,%eax
cmp    $0x200000,%rsi
jb     1176 <write_variable_length_quantity+0x66>
shr    $0x15,%rsi
or     $0x80,%sil
mov    %sil,0x4(%rsp)
xor    %eax,%eax
mov    $0x4,%edx
sub    %eax,%edx
lea    (%rsp,%rax,1),%rdi
add    $0x4,%rdi
mov    $0x1,%esi
call   1030 <fwrite@plt>
pop    %rax
ret

LLVM IR (-O0):
define void @write_variable_length_quantity(i32* %0, i64 %1) {
%3 = alloca i32*
%4 = alloca i64
%5 = alloca [4 x i8]
%6 = alloca i32
store i32* %0, i32** %3
store i64 %1, i64* %4
store i32 3, i32* %6
br label %7
7: ; preds = %2, %33
%8 = load i64, i64* %4
%9 = and i64 %8, 127
%10 = trunc i64 %9 to i8
%11 = load i32, i32* %6
%12 = sext i32 %11 to i64
%13 = getelementptr inbounds [4 x i8], [4 x i8]* %5, i64 0, i64 %12
store i8 %10, i8* %13
%14 = load i32, i32* %6
%15 = icmp slt i32 %14, 3
br i1 %15, label %16, label %24
16: ; preds = %7
%17 = load i32, i32* %6
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds [4 x i8], [4 x i8]* %5, i64 0, i64 %18
%20 = load i8, i8* %19
%21 = zext i8 %20 to i32
%22 = or i32 %21, 128
%23 = trunc i32 %22 to i8
store i8 %23, i8* %19
br label %24
24: ; preds = %16, %7
%25 = load i64, i64* %4
%26 = lshr i64 %25, 7
store i64 %26, i64* %4
%27 = load i64, i64* %4
%28 = icmp eq i64 %27, 0
br i1 %28, label %32, label %29
29: ; preds = %24
%30 = load i32, i32* %6
%31 = icmp eq i32 %30, 0
br i1 %31, label %32, label %33
32: ; preds = %29, %24
br label %36
33: ; preds = %29
%34 = load i32, i32* %6
%35 = add nsw i32 %34, -1
store i32 %35, i32* %6
br label %7
36: ; preds = %32
%37 = getelementptr inbounds [4 x i8], [4 x i8]* %5, i64 0, i64 0
%38 = load i32, i32* %6
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds i8, i8* %37, i64 %39
%41 = load i32, i32* %6
%42 = sub nsw i32 4, %41
%43 = load i32*, i32** %3
%44 = call i32 @fwrite(i8* %40, i32 1, i32 %42, i32* %43)
ret void
}

LLVM IR (-O2):
define void @write_variable_length_quantity(i32* nocapture %0, i64 %1) {
%3 = alloca [4 x i8]
%4 = getelementptr inbounds [4 x i8], [4 x i8]* %3, i64 0, i64 0
%5 = trunc i64 %1 to i8
%6 = and i8 %5, 127
%7 = getelementptr inbounds [4 x i8], [4 x i8]* %3, i64 0, i64 3
store i8 %6, i8* %7
%8 = icmp ult i64 %1, 128
br i1 %8, label %30, label %9
9: ; preds = %2
%10 = lshr i64 %1, 7
%11 = trunc i64 %10 to i8
%12 = getelementptr inbounds [4 x i8], [4 x i8]* %3, i64 0, i64 2
%13 = or i8 %11, -128
store i8 %13, i8* %12
%14 = icmp ult i64 %1, 16384
br i1 %14, label %26, label %15
15: ; preds = %9
%16 = lshr i64 %1, 14
%17 = trunc i64 %16 to i8
%18 = getelementptr inbounds [4 x i8], [4 x i8]* %3, i64 0, i64 1
%19 = or i8 %17, -128
store i8 %19, i8* %18
%20 = icmp ult i64 %1, 2097152
br i1 %20, label %26, label %21
21: ; preds = %15
%22 = lshr i64 %1, 21
%23 = trunc i64 %22 to i8
%24 = getelementptr inbounds [4 x i8], [4 x i8]* %3, i64 0, i64 0
%25 = or i8 %23, -128
store i8 %25, i8* %24
br label %26
26: ; preds = %21, %15, %9
%27 = phi i64 [ 2, %9 ], [ 1, %15 ], [ 0, %21 ]
%28 = trunc i64 %27 to i32
%29 = sub nuw nsw i32 4, %28
br label %30
30: ; preds = %26, %2
%31 = phi i64 [ 3, %2 ], [ %27, %26 ]
%32 = phi i32 [ 1, %2 ], [ %29, %26 ]
%33 = getelementptr inbounds [4 x i8], [4 x i8]* %3, i64 0, i64 %31
%34 = call i32 @fwrite(i8* %33, i32 1, i32 %32, i32* %0)
ret void
}