#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 /*<<< orphan*/ assert(int);
int insert_value(unsigned char *x, int val) {
  assert(val >= 0);
  if (val <= 127) {
    *x = val;
    return 1;
  } else if (val <= 16383) {
    *x++ = (val >> 8) | 0x80;
    *x = (val & 0xff);
    return 2;
  } else {
    int a = (val >> 24);
    assert(a <= 63);
    *x++ = a | 0xc0;
    *x++ = ((val >> 16) & 0xff);
    *x++ = ((val >> 8) & 0xff);
    *x = (val & 0xff);
    return 4;
  }
}


x86 assembly (clang, -O0):
<insert_value>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
setge  %al
and    $0x1,%al
movzbl %al,%edi
call   1030 <assert@plt>
cmpl   $0x7f,-0x14(%rbp)
jg     1151 <insert_value+0x41>
mov    -0x14(%rbp),%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %cl,(%rax)
movl   $0x1,-0x4(%rbp)
jmp    1220 <insert_value+0x110>
cmpl   $0x3fff,-0x14(%rbp)
jg     1198 <insert_value+0x88>
mov    -0x14(%rbp),%eax
sar    $0x8,%eax
or     $0x80,%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x10(%rbp)
mov    %cl,(%rax)
mov    -0x14(%rbp),%eax
and    $0xff,%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %cl,(%rax)
movl   $0x2,-0x4(%rbp)
jmp    1220 <insert_value+0x110>
mov    -0x14(%rbp),%eax
sar    $0x18,%eax
mov    %eax,-0x18(%rbp)
cmpl   $0x3f,-0x18(%rbp)
setle  %al
and    $0x1,%al
movzbl %al,%edi
call   1030 <assert@plt>
mov    -0x18(%rbp),%eax
or     $0xc0,%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x10(%rbp)
mov    %cl,(%rax)
mov    -0x14(%rbp),%eax
sar    $0x10,%eax
and    $0xff,%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x10(%rbp)
mov    %cl,(%rax)
mov    -0x14(%rbp),%eax
sar    $0x8,%eax
and    $0xff,%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x10(%rbp)
mov    %cl,(%rax)
mov    -0x14(%rbp),%eax
and    $0xff,%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %cl,(%rax)
movl   $0x4,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<insert_value>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%ebx
mov    %rdi,%rbp
mov    %esi,%edi
not    %edi
shr    $0x1f,%edi
call   1030 <assert@plt>
mov    $0x1,%eax
cmp    $0x80,%ebx
jl     1183 <insert_value+0x73>
cmp    $0x3fff,%ebx
ja     114f <insert_value+0x3f>
mov    %ebx,%eax
shr    $0x8,%eax
or     $0x80,%al
mov    %al,0x0(%rbp)
add    $0x1,%rbp
mov    $0x2,%eax
jmp    1183 <insert_value+0x73>
mov    %ebx,%r14d
shr    $0x18,%r14d
xor    %edi,%edi
cmp    $0x40000000,%ebx
setb   %dil
call   1030 <assert@plt>
or     $0xc0,%r14b
mov    %r14b,0x0(%rbp)
mov    %ebx,%eax
shr    $0x10,%eax
mov    %al,0x1(%rbp)
mov    %bh,0x2(%rbp)
add    $0x3,%rbp
mov    $0x4,%eax
mov    %bl,0x0(%rbp)
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i32 @insert_value(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%7 = load i32, i32* %5
%8 = icmp sge i32 %7, 0
%9 = zext i1 %8 to i32
%10 = call i32 @assert(i32 %9)
%11 = load i32, i32* %5
%12 = icmp sle i32 %11, 127
br i1 %12, label %13, label %17
13: ; preds = %2
%14 = load i32, i32* %5
%15 = trunc i32 %14 to i8
%16 = load i8*, i8** %4
store i8 %15, i8* %16
store i32 1, i32* %3
br label %59
17: ; preds = %2
%18 = load i32, i32* %5
%19 = icmp sle i32 %18, 16383
br i1 %19, label %20, label %31
20: ; preds = %17
%21 = load i32, i32* %5
%22 = ashr i32 %21, 8
%23 = or i32 %22, 128
%24 = trunc i32 %23 to i8
%25 = load i8*, i8** %4
%26 = getelementptr inbounds i8, i8* %25, i32 1
store i8* %26, i8** %4
store i8 %24, i8* %25
%27 = load i32, i32* %5
%28 = and i32 %27, 255
%29 = trunc i32 %28 to i8
%30 = load i8*, i8** %4
store i8 %29, i8* %30
store i32 2, i32* %3
br label %59
31: ; preds = %17
%32 = load i32, i32* %5
%33 = ashr i32 %32, 24
store i32 %33, i32* %6
%34 = load i32, i32* %6
%35 = icmp sle i32 %34, 63
%36 = zext i1 %35 to i32
%37 = call i32 @assert(i32 %36)
%38 = load i32, i32* %6
%39 = or i32 %38, 192
%40 = trunc i32 %39 to i8
%41 = load i8*, i8** %4
%42 = getelementptr inbounds i8, i8* %41, i32 1
store i8* %42, i8** %4
store i8 %40, i8* %41
%43 = load i32, i32* %5
%44 = ashr i32 %43, 16
%45 = and i32 %44, 255
%46 = trunc i32 %45 to i8
%47 = load i8*, i8** %4
%48 = getelementptr inbounds i8, i8* %47, i32 1
store i8* %48, i8** %4
store i8 %46, i8* %47
%49 = load i32, i32* %5
%50 = ashr i32 %49, 8
%51 = and i32 %50, 255
%52 = trunc i32 %51 to i8
%53 = load i8*, i8** %4
%54 = getelementptr inbounds i8, i8* %53, i32 1
store i8* %54, i8** %4
store i8 %52, i8* %53
%55 = load i32, i32* %5
%56 = and i32 %55, 255
%57 = trunc i32 %56 to i8
%58 = load i8*, i8** %4
store i8 %57, i8* %58
store i32 4, i32* %3
br label %59
59: ; preds = %31, %20, %13
%60 = load i32, i32* %3
ret i32 %60
}

LLVM IR (-O2):
define i32 @insert_value(i8* nocapture writeonly %0, i32 %1) {
%3 = xor i32 %1, -1
%4 = lshr i32 %3, 31
%5 = tail call i32 @assert(i32 %4)
%6 = icmp slt i32 %1, 128
br i1 %6, label %28, label %7
7: ; preds = %2
%8 = icmp ult i32 %1, 16384
br i1 %8, label %9, label %14
9: ; preds = %7
%10 = lshr i32 %1, 8
%11 = trunc i32 %10 to i8
%12 = or i8 %11, -128
%13 = getelementptr inbounds i8, i8* %0, i64 1
store i8 %12, i8* %0
br label %28
14: ; preds = %7
%15 = lshr i32 %1, 24
%16 = icmp ult i32 %1, 1073741824
%17 = zext i1 %16 to i32
%18 = tail call i32 @assert(i32 %17)
%19 = trunc i32 %15 to i8
%20 = or i8 %19, -64
%21 = getelementptr inbounds i8, i8* %0, i64 1
store i8 %20, i8* %0
%22 = lshr i32 %1, 16
%23 = trunc i32 %22 to i8
%24 = getelementptr inbounds i8, i8* %0, i64 2
store i8 %23, i8* %21
%25 = lshr i32 %1, 8
%26 = trunc i32 %25 to i8
%27 = getelementptr inbounds i8, i8* %0, i64 3
store i8 %26, i8* %24
br label %28
28: ; preds = %2, %14, %9
%29 = phi i8* [ %27, %14 ], [ %13, %9 ], [ %0, %2 ]
%30 = phi i32 [ 4, %14 ], [ 2, %9 ], [ 1, %2 ]
%31 = trunc i32 %1 to i8
store i8 %31, i8* %29
ret i32 %30
}