#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 uint8_t;
typedef int uint64_t;

/* Variables and functions */
int sdnv_encode(uint64_t val, uint8_t *bp) {
  size_t val_len = 0;
  uint64_t tmp = val;
  do {
    tmp = tmp >> 7;
    val_len++;
  } while (tmp != 0);
  bp += val_len;
  uint8_t high_bit = 0;
  do {
    --bp;
    *bp = (uint8_t)(high_bit | (val & 0x7f));
    high_bit = (1 << 7);
    val = val >> 7;
  } while (val != 0);
  return val_len;
}


x86 assembly (clang, -O0):
<sdnv_encode>:
push   %rbp
mov    %rsp,%rbp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
sar    $0x7,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    1119 <sdnv_encode+0x19>
mov    -0x18(%rbp),%rax
shl    $0x2,%rax
add    -0x10(%rbp),%rax
mov    %rax,-0x10(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x10(%rbp),%rax
add    $0xfffffffffffffffc,%rax
mov    %rax,-0x10(%rbp)
mov    -0x20(%rbp),%ecx
mov    -0x4(%rbp),%eax
and    $0x7f,%eax
or     %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x80,-0x20(%rbp)
mov    -0x4(%rbp),%eax
sar    $0x7,%eax
mov    %eax,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
jne    114f <sdnv_encode+0x4f>
mov    -0x18(%rbp),%rax
pop    %rbp
ret

x86 assembly (clang, -O2):
<sdnv_encode>:
add    $0xfffffffffffffff8,%rsi
xor    %eax,%eax
mov    %edi,%edx
mov    %edi,%ecx
nopw   0x0(%rax,%rax,1)
sar    $0x7,%ecx
add    $0x4,%rsi
add    $0x1,%eax
cmp    $0x7f,%edx
mov    %ecx,%edx
ja     1110 <sdnv_encode+0x10>
mov    %edi,%ecx
and    $0x7f,%ecx
mov    %ecx,0x4(%rsi)
cmp    $0x80,%edi
jb     115e <sdnv_encode+0x5e>
mov    %edi,%ecx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
sar    $0x7,%ecx
mov    %ecx,%edx
or     $0x80,%edx
movzbl %dl,%edx
mov    %edx,(%rsi)
add    $0xfffffffffffffffc,%rsi
cmp    $0x3fff,%edi
mov    %ecx,%edi
ja     1140 <sdnv_encode+0x40>
ret

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

LLVM IR (-O2):
define i32 @sdnv_encode(i32 %0, i32* nocapture writeonly %1) {
br label %3
3: ; preds = %3, %2
%4 = phi i64 [ 0, %2 ], [ %7, %3 ]
%5 = phi i32 [ %0, %2 ], [ %6, %3 ]
%6 = ashr i32 %5, 7
%7 = add i64 %4, 1
%8 = icmp ult i32 %5, 128
br i1 %8, label %9, label %3
9: ; preds = %3
%10 = getelementptr inbounds i32, i32* %1, i64 %4
%11 = and i32 %0, 127
store i32 %11, i32* %10
%12 = icmp ult i32 %0, 128
br i1 %12, label %21, label %13
13: ; preds = %9, %13
%14 = phi i32 [ %16, %13 ], [ %0, %9 ]
%15 = phi i32* [ %17, %13 ], [ %10, %9 ]
%16 = ashr i32 %14, 7
%17 = getelementptr inbounds i32, i32* %15, i64 -1
%18 = and i32 %16, 127
%19 = or i32 %18, 128
store i32 %19, i32* %17
%20 = icmp ult i32 %14, 16384
br i1 %20, label %21, label %13
21: ; preds = %13, %9
%22 = trunc i64 %7 to i32
ret i32 %22
}