#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 uint32_t;

/* Variables and functions */
int /*<<< orphan*/ htod(int, char *);
int ltlv_nb_chunks(int);
int /*<<< orphan*/ memmove(char *, char *, int);
uint32_t ltlv_split_value(char *src, uint32_t len) {
  int nb_chunks = ltlv_nb_chunks(len), w_idx = 0;
  uint32_t remaining = len;
  for (int i = 0; i < nb_chunks; i++) {
    uint32_t size =
        ((((1 << ((3) * 8)) - 1)) < (remaining) ? (((1 << ((3) * 8)) - 1))
                                                : (remaining));
    memmove(src + w_idx + (1 + 3), src + w_idx, remaining);
    src[w_idx] = 253;
    htod(size, src + w_idx + 1);
    w_idx += size + (1 + 3);
    remaining -= size;
  }
  return w_idx;
}


x86 assembly (clang, -O0):
<ltlv_split_value>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0xc(%rbp),%edi
call   1050 <ltlv_nb_chunks@plt>
mov    %eax,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x10(%rbp),%eax
jge    11fb <ltlv_split_value+0xcb>
mov    $0xffffff,%eax
cmp    -0x18(%rbp),%eax
jge    1185 <ltlv_split_value+0x55>
mov    $0xffffff,%eax
mov    %eax,-0x24(%rbp)
jmp    118b <ltlv_split_value+0x5b>
mov    -0x18(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
movslq -0x14(%rbp),%rax
add    %rax,%rdi
add    $0x4,%rdi
mov    -0x8(%rbp),%rsi
movslq -0x14(%rbp),%rax
add    %rax,%rsi
mov    -0x18(%rbp),%edx
call   1040 <memmove@plt>
mov    -0x8(%rbp),%rax
movslq -0x14(%rbp),%rcx
movb   $0xfd,(%rax,%rcx,1)
mov    -0x20(%rbp),%edi
mov    -0x8(%rbp),%rsi
movslq -0x14(%rbp),%rax
add    %rax,%rsi
add    $0x1,%rsi
call   1030 <htod@plt>
mov    -0x20(%rbp),%eax
add    $0x4,%eax
add    -0x14(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x20(%rbp),%ecx
mov    -0x18(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x18(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    115e <ltlv_split_value+0x2e>
mov    -0x14(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ltlv_split_value>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %esi,%r12d
mov    %rdi,%r14
mov    %esi,%edi
call   1050 <ltlv_nb_chunks@plt>
test   %eax,%eax
jle    119c <ltlv_split_value+0x6c>
mov    %eax,%r15d
xor    %eax,%eax
xchg   %ax,%ax
cmp    $0xffffff,%r12d
mov    $0xffffff,%ebp
cmovl  %r12d,%ebp
movslq %eax,%rbx
lea    (%r14,%rbx,1),%rsi
lea    (%r14,%rbx,1),%rdi
add    $0x4,%rdi
mov    %r12d,%edx
call   1040 <memmove@plt>
movb   $0xfd,(%r14,%rbx,1)
lea    (%r14,%rbx,1),%rsi
add    $0x1,%rsi
mov    %ebp,%edi
call   1030 <htod@plt>
lea    (%rbx,%rbp,1),%eax
add    $0x4,%eax
sub    %ebp,%r12d
add    $0xffffffff,%r15d
jne    1150 <ltlv_split_value+0x20>
jmp    119e <ltlv_split_value+0x6e>
xor    %eax,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @ltlv_split_value(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
%10 = load i32, i32* %4
%11 = call i32 @ltlv_nb_chunks(i32 %10)
store i32 %11, i32* %5
store i32 0, i32* %6
%12 = load i32, i32* %4
store i32 %12, i32* %7
store i32 0, i32* %8
br label %13
13: ; preds = %54, %2
%14 = load i32, i32* %8
%15 = load i32, i32* %5
%16 = icmp slt i32 %14, %15
br i1 %16, label %17, label %57
17: ; preds = %13
%18 = load i32, i32* %7
%19 = icmp slt i32 16777215, %18
br i1 %19, label %20, label %21
20: ; preds = %17
br label %23
21: ; preds = %17
%22 = load i32, i32* %7
br label %23
23: ; preds = %21, %20
%24 = phi i32 [ 16777215, %20 ], [ %22, %21 ]
store i32 %24, i32* %9
%25 = load i8*, i8** %3
%26 = load i32, i32* %6
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds i8, i8* %25, i64 %27
%29 = getelementptr inbounds i8, i8* %28, i64 4
%30 = load i8*, i8** %3
%31 = load i32, i32* %6
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds i8, i8* %30, i64 %32
%34 = load i32, i32* %7
%35 = call i32 @memmove(i8* %29, i8* %33, i32 %34)
%36 = load i8*, i8** %3
%37 = load i32, i32* %6
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds i8, i8* %36, i64 %38
store i8 -3, i8* %39
%40 = load i32, i32* %9
%41 = load i8*, i8** %3
%42 = load i32, i32* %6
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds i8, i8* %41, i64 %43
%45 = getelementptr inbounds i8, i8* %44, i64 1
%46 = call i32 @htod(i32 %40, i8* %45)
%47 = load i32, i32* %9
%48 = add nsw i32 %47, 4
%49 = load i32, i32* %6
%50 = add nsw i32 %49, %48
store i32 %50, i32* %6
%51 = load i32, i32* %9
%52 = load i32, i32* %7
%53 = sub nsw i32 %52, %51
store i32 %53, i32* %7
br label %54
54: ; preds = %23
%55 = load i32, i32* %8
%56 = add nsw i32 %55, 1
store i32 %56, i32* %8
br label %13
57: ; preds = %13
%58 = load i32, i32* %6
ret i32 %58
}

LLVM IR (-O2):
define i32 @ltlv_split_value(i8* %0, i32 %1) {
%3 = tail call i32 @ltlv_nb_chunks(i32 %1)
%4 = icmp sgt i32 %3, 0
br i1 %4, label %7, label %5
5: ; preds = %7, %2
%6 = phi i32 [ 0, %2 ], [ %20, %7 ]
ret i32 %6
7: ; preds = %2, %7
%8 = phi i32 [ %20, %7 ], [ 0, %2 ]
%9 = phi i32 [ %22, %7 ], [ 0, %2 ]
%10 = phi i32 [ %21, %7 ], [ %1, %2 ]
%11 = icmp slt i32 %10, 16777215
%12 = select i1 %11, i32 %10, i32 16777215
%13 = sext i32 %8 to i64
%14 = getelementptr inbounds i8, i8* %0, i64 %13
%15 = getelementptr inbounds i8, i8* %14, i64 4
%16 = tail call i32 @memmove(i8* %15, i8* %14, i32 %10)
store i8 -3, i8* %14
%17 = getelementptr inbounds i8, i8* %14, i64 1
%18 = tail call i32 @htod(i32 %12, i8* %17)
%19 = add nsw i32 %12, 4
%20 = add nsw i32 %19, %8
%21 = sub nsw i32 %10, %12
%22 = add nuw nsw i32 %9, 1
%23 = icmp eq i32 %22, %3
br i1 %23, label %5, label %7
}