#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(long, int /*<<< orphan*/ *);
int write_var_len(long val, FILE *fp) {
  long buf = val & 0x7f;
  int wrote = 0;
  while ((val >>= 7) > 0) {
    buf <<= 8;
    buf |= 0x80;
    buf += (val & 0x7f);
  }
  while (1) {
    putc(buf, fp);
    wrote++;
    if (buf & 0x80)
      buf >>= 8;
    else
      break;
  }
  return wrote;
}


x86 assembly (clang, -O0):
<write_var_len>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rax
and    $0x7f,%rax
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x8(%rbp),%rax
sar    $0x7,%rax
mov    %rax,-0x8(%rbp)
cmp    $0x0,%rax
jle    1178 <write_var_len+0x68>
mov    -0x18(%rbp),%rax
shl    $0x8,%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
or     $0x80,%rax
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rax
and    $0x7f,%rax
add    -0x18(%rbp),%rax
mov    %rax,-0x18(%rbp)
jmp    1133 <write_var_len+0x23>
jmp    117d <write_var_len+0x6d>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <putc@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%rax
and    $0x80,%rax
cmp    $0x0,%rax
je     11b8 <write_var_len+0xa8>
mov    -0x18(%rbp),%rax
sar    $0x8,%rax
mov    %rax,-0x18(%rbp)
jmp    11bd <write_var_len+0xad>
jmp    11c2 <write_var_len+0xb2>
jmp    117d <write_var_len+0x6d>
mov    -0x1c(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_var_len>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %edi,%eax
and    $0x7f,%eax
cmp    $0x80,%rdi
jl     1156 <write_var_len+0x46>
mov    %rdi,%rcx
nopl   0x0(%rax,%rax,1)
sar    $0x7,%rcx
shl    $0x8,%rax
movzbl %cl,%ebx
or     %rax,%rbx
or     $0x80,%rbx
mov    %rbx,%rax
cmp    $0x3fff,%rdi
mov    %rcx,%rdi
jg     1130 <write_var_len+0x20>
jmp    1159 <write_var_len+0x49>
mov    %rax,%rbx
xor    %r15d,%r15d
mov    %rbx,%rbp
nop
mov    %rbx,%rdi
mov    %r14,%rsi
call   1030 <putc@plt>
add    $0x1,%r15d
sar    $0x8,%rbp
test   $0x80,%bl
mov    %rbp,%rbx
jne    1160 <write_var_len+0x50>
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i32 @write_var_len(i64 %0, i32* nocapture %1) {
%3 = and i64 %0, 127
%4 = icmp sgt i64 %0, 127
br i1 %4, label %5, label %14
5: ; preds = %2, %5
%6 = phi i64 [ %8, %5 ], [ %0, %2 ]
%7 = phi i64 [ %12, %5 ], [ %3, %2 ]
%8 = ashr i64 %6, 7
%9 = shl i64 %7, 8
%10 = and i64 %8, 127
%11 = or i64 %9, %10
%12 = or i64 %11, 128
%13 = icmp sgt i64 %6, 16383
br i1 %13, label %5, label %14
14: ; preds = %5, %2
%15 = phi i64 [ %3, %2 ], [ %12, %5 ]
br label %16
16: ; preds = %14, %16
%17 = phi i64 [ %23, %16 ], [ %15, %14 ]
%18 = phi i32 [ %20, %16 ], [ 0, %14 ]
%19 = tail call i32 @putc(i64 %17, i32* %1)
%20 = add nuw nsw i32 %18, 1
%21 = and i64 %17, 128
%22 = icmp eq i64 %21, 0
%23 = ashr i64 %17, 8
br i1 %22, label %24, label %16
24: ; preds = %16
ret i32 %20
}