#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 */

/* Variables and functions */
int write(int, unsigned char *, int);
int write_var_len(int fd, long value) {
  unsigned char rep[4];
  int bytes;
  bytes = 1;
  rep[3] = value & 0x7f;
  value >>= 7;
  while (value > 0) {
    rep[3 - bytes] = (value & 0x7f) | 0x80;
    bytes++;
    value >>= 7;
  }
  return (write(fd, &rep[4 - bytes], bytes));
}


x86 assembly (clang, -O0):
<write_var_len>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x1,-0x18(%rbp)
mov    -0x10(%rbp),%rax
and    $0x7f,%rax
mov    %al,-0x11(%rbp)
mov    -0x10(%rbp),%rax
sar    $0x7,%rax
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jle    1180 <write_var_len+0x70>
mov    -0x10(%rbp),%rax
and    $0x7f,%rax
or     $0x80,%rax
mov    %al,%cl
mov    $0x3,%eax
sub    -0x18(%rbp),%eax
cltq
mov    %cl,-0x14(%rbp,%rax,1)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
sar    $0x7,%rax
mov    %rax,-0x10(%rbp)
jmp    113d <write_var_len+0x2d>
mov    -0x4(%rbp),%edi
mov    $0x4,%eax
sub    -0x18(%rbp),%eax
cltq
lea    -0x14(%rbp),%rsi
add    %rax,%rsi
mov    -0x18(%rbp),%edx
call   1030 <write@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_var_len>:
push   %rax
mov    %esi,%eax
and    $0x7f,%al
mov    %al,0x7(%rsp)
mov    $0x1,%edx
cmp    $0x80,%rsi
jl     1154 <write_var_len+0x44>
xor    %eax,%eax
mov    %rsi,%rcx
nopl   0x0(%rax)
sar    $0x7,%rcx
mov    %ecx,%edx
or     $0x80,%dl
mov    %dl,0x6(%rsp,%rax,1)
add    $0xffffffffffffffff,%rax
cmp    $0x3fff,%rsi
mov    %rcx,%rsi
jg     1130 <write_var_len+0x20>
mov    $0x1,%edx
sub    %eax,%edx
mov    $0x4,%eax
sub    %edx,%eax
cltq
lea    (%rsp,%rax,1),%rsi
add    $0x4,%rsi
call   1030 <write@plt>
pop    %rcx
ret

LLVM IR (-O0):
define i32 @write_var_len(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 1, i32* %6
%7 = load i64, i64* %4
%8 = and i64 %7, 127
%9 = trunc i64 %8 to i8
%10 = getelementptr inbounds [4 x i8], [4 x i8]* %5, i64 0, i64 3
store i8 %9, i8* %10
%11 = load i64, i64* %4
%12 = ashr i64 %11, 7
store i64 %12, i64* %4
br label %13
13: ; preds = %16, %2
%14 = load i64, i64* %4
%15 = icmp sgt i64 %14, 0
br i1 %15, label %16, label %29
16: ; preds = %13
%17 = load i64, i64* %4
%18 = and i64 %17, 127
%19 = or i64 %18, 128
%20 = trunc i64 %19 to i8
%21 = load i32, i32* %6
%22 = sub nsw i32 3, %21
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds [4 x i8], [4 x i8]* %5, i64 0, i64 %23
store i8 %20, i8* %24
%25 = load i32, i32* %6
%26 = add nsw i32 %25, 1
store i32 %26, i32* %6
%27 = load i64, i64* %4
%28 = ashr i64 %27, 7
store i64 %28, i64* %4
br label %13
29: ; preds = %13
%30 = load i32, i32* %3
%31 = load i32, i32* %6
%32 = sub nsw i32 4, %31
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds [4 x i8], [4 x i8]* %5, i64 0, i64 %33
%35 = load i32, i32* %6
%36 = call i32 @write(i32 %30, i8* %34, i32 %35)
ret i32 %36
}

LLVM IR (-O2):
define i32 @write_var_len(i32 %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 sgt i64 %1, 127
br i1 %8, label %9, label %21
9: ; preds = %2, %9
%10 = phi i64 [ %17, %9 ], [ 1, %2 ]
%11 = phi i64 [ %12, %9 ], [ %1, %2 ]
%12 = ashr i64 %11, 7
%13 = trunc i64 %12 to i8
%14 = or i8 %13, -128
%15 = sub nsw i64 3, %10
%16 = getelementptr inbounds [4 x i8], [4 x i8]* %3, i64 0, i64 %15
store i8 %14, i8* %16
%17 = add nuw i64 %10, 1
%18 = icmp sgt i64 %11, 16383
br i1 %18, label %9, label %19
19: ; preds = %9
%20 = trunc i64 %17 to i32
br label %21
21: ; preds = %19, %2
%22 = phi i32 [ 1, %2 ], [ %20, %19 ]
%23 = sub nsw i32 4, %22
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds [4 x i8], [4 x i8]* %3, i64 0, i64 %24
%26 = call i32 @write(i32 %0, i8* %25, i32 %22)
ret i32 %26
}