#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*/ putc(unsigned long, int /*<<< orphan*/ *);
int variable_byte_encode(unsigned long int x, FILE *stream) {
  int bytes_written = 0;
  while (x > 127) {
    putc((x & 127) | 128, stream);
    x >>= 7;
    bytes_written++;
  }
  putc(x, stream);
  return ++bytes_written;
}


x86 assembly (clang, -O0):
<variable_byte_encode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
cmpq   $0x7f,-0x8(%rbp)
jbe    1164 <variable_byte_encode+0x54>
mov    -0x8(%rbp),%rdi
and    $0x7f,%rdi
or     $0x80,%rdi
mov    -0x10(%rbp),%rsi
call   1030 <putc@plt>
mov    -0x8(%rbp),%rax
shr    $0x7,%rax
mov    %rax,-0x8(%rbp)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1127 <variable_byte_encode+0x17>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <putc@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<variable_byte_encode>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbp
mov    $0x1,%r15d
cmp    $0x80,%rdi
jb     1168 <variable_byte_encode+0x58>
mov    $0x1,%r15d
mov    %rbp,%rbx
cs nopw 0x0(%rax,%rax,1)
nop
mov    %ebp,%eax
or     $0x80,%eax
movzbl %al,%edi
mov    %r14,%rsi
call   1030 <putc@plt>
shr    $0x7,%rbx
add    $0x1,%r15d
cmp    $0x3fff,%rbp
mov    %rbx,%rbp
ja     1140 <variable_byte_encode+0x30>
jmp    116b <variable_byte_encode+0x5b>
mov    %rbp,%rbx
mov    %rbx,%rdi
mov    %r14,%rsi
call   1030 <putc@plt>
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @variable_byte_encode(i64 %0, i32* %1) {
%3 = alloca i64
%4 = alloca i32*
%5 = alloca i32
store i64 %0, i64* %3
store i32* %1, i32** %4
store i32 0, i32* %5
br label %6
6: ; preds = %9, %2
%7 = load i64, i64* %3
%8 = icmp ugt i64 %7, 127
br i1 %8, label %9, label %19
9: ; preds = %6
%10 = load i64, i64* %3
%11 = and i64 %10, 127
%12 = or i64 %11, 128
%13 = load i32*, i32** %4
%14 = call i32 @putc(i64 %12, i32* %13)
%15 = load i64, i64* %3
%16 = lshr i64 %15, 7
store i64 %16, i64* %3
%17 = load i32, i32* %5
%18 = add nsw i32 %17, 1
store i32 %18, i32* %5
br label %6
19: ; preds = %6
%20 = load i64, i64* %3
%21 = load i32*, i32** %4
%22 = call i32 @putc(i64 %20, i32* %21)
%23 = load i32, i32* %5
%24 = add nsw i32 %23, 1
store i32 %24, i32* %5
ret i32 %24
}

LLVM IR (-O2):
define i32 @variable_byte_encode(i64 %0, i32* nocapture %1) {
%3 = icmp ugt i64 %0, 127
br i1 %3, label %4, label %13
4: ; preds = %2, %4
%5 = phi i32 [ %11, %4 ], [ 1, %2 ]
%6 = phi i64 [ %10, %4 ], [ %0, %2 ]
%7 = and i64 %6, 127
%8 = or i64 %7, 128
%9 = tail call i32 @putc(i64 %8, i32* %1)
%10 = lshr i64 %6, 7
%11 = add nuw nsw i32 %5, 1
%12 = icmp ugt i64 %6, 16383
br i1 %12, label %4, label %13
13: ; preds = %4, %2
%14 = phi i64 [ %0, %2 ], [ %10, %4 ]
%15 = phi i32 [ 1, %2 ], [ %11, %4 ]
%16 = tail call i32 @putc(i64 %14, i32* %1)
ret i32 %15
}