#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 length_length(int);
void *render_byte(int, void *);
void *render_length(int data_len, void *dest) {
  if (data_len <= 127) {
    return render_byte(data_len, dest);
  } else {
    int size = length_length(data_len) - 1;
    int i;
    dest = render_byte(size | 0x80, dest);
    for (i = size - 1; i >= 0; i--) {
      int v = (data_len >> (8 * i)) & 0xFF;
      dest = render_byte(v, dest);
    }
    return dest;
  }
}


x86 assembly (clang, -O0):
<render_length>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
cmpl   $0x7f,-0xc(%rbp)
jg     114e <render_length+0x2e>
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
call   1040 <render_byte@plt>
mov    %rax,-0x8(%rbp)
jmp    11be <render_length+0x9e>
mov    -0xc(%rbp),%edi
call   1030 <length_length@plt>
sub    $0x1,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%edi
or     $0x80,%edi
mov    -0x18(%rbp),%rsi
call   1040 <render_byte@plt>
mov    %rax,-0x18(%rbp)
mov    -0x1c(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jl     11b6 <render_length+0x96>
mov    -0xc(%rbp),%eax
mov    -0x20(%rbp),%ecx
shl    $0x3,%ecx
sar    %cl,%eax
and    $0xff,%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%edi
mov    -0x18(%rbp),%rsi
call   1040 <render_byte@plt>
mov    %rax,-0x18(%rbp)
mov    -0x20(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x20(%rbp)
jmp    117b <render_length+0x5b>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<render_length>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbp
mov    %edi,%r15d
cmp    $0x7f,%edi
jg     1147 <render_length+0x27>
mov    %r15d,%edi
mov    %rbp,%rsi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <render_byte@plt>
mov    %r15d,%edi
call   1030 <length_length@plt>
mov    %eax,%r14d
lea    -0x1(%r14),%ebx
mov    %ebx,%edi
or     $0x80,%edi
mov    %rbp,%rsi
call   1040 <render_byte@plt>
cmp    $0x2,%r14d
jl     119a <render_length+0x7a>
lea    -0x10(,%r14,8),%ebp
ff
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r15d,%edx
mov    %ebp,%ecx
sar    %cl,%edx
movzbl %dl,%edi
mov    %rax,%rsi
call   1040 <render_byte@plt>
add    $0xfffffff8,%ebp
add    $0xffffffff,%ebx
jne    1180 <render_length+0x60>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i8* @render_length(i32 %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
%9 = load i32, i32* %4
%10 = icmp sle i32 %9, 127
br i1 %10, label %11, label %15
11: ; preds = %2
%12 = load i32, i32* %4
%13 = load i8*, i8** %5
%14 = call i8* @render_byte(i32 %12, i8* %13)
store i8* %14, i8** %3
br label %42
15: ; preds = %2
%16 = load i32, i32* %4
%17 = call i32 @length_length(i32 %16)
%18 = sub nsw i32 %17, 1
store i32 %18, i32* %6
%19 = load i32, i32* %6
%20 = or i32 %19, 128
%21 = load i8*, i8** %5
%22 = call i8* @render_byte(i32 %20, i8* %21)
store i8* %22, i8** %5
%23 = load i32, i32* %6
%24 = sub nsw i32 %23, 1
store i32 %24, i32* %7
br label %25
25: ; preds = %37, %15
%26 = load i32, i32* %7
%27 = icmp sge i32 %26, 0
br i1 %27, label %28, label %40
28: ; preds = %25
%29 = load i32, i32* %4
%30 = load i32, i32* %7
%31 = mul nsw i32 8, %30
%32 = ashr i32 %29, %31
%33 = and i32 %32, 255
store i32 %33, i32* %8
%34 = load i32, i32* %8
%35 = load i8*, i8** %5
%36 = call i8* @render_byte(i32 %34, i8* %35)
store i8* %36, i8** %5
br label %37
37: ; preds = %28
%38 = load i32, i32* %7
%39 = add nsw i32 %38, -1
store i32 %39, i32* %7
br label %25
40: ; preds = %25
%41 = load i8*, i8** %5
store i8* %41, i8** %3
br label %42
42: ; preds = %40, %11
%43 = load i8*, i8** %3
ret i8* %43
}

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