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

/* Variables and functions */
int /*<<< orphan*/ memcpy(int *, int const *, int);
uint8_t tlv_encode_next(uint8_t **data, uint16_t *dlength, uint8_t type,
                        uint16_t length, const uint8_t *value) {
  uint8_t *iptr = *data;
  if (length + 2 > *dlength) {
    return 0;
  }
  while (length > 0) {
    *iptr++ = type;
    uint8_t piece = length > 255 ? 255 : length;
    *iptr++ = piece;
    memcpy(iptr, value, piece);
    value += piece;
    iptr += piece;
    length -= piece;
    *dlength -= piece + 2;
  }
  *data = iptr;
  return 1;
}


x86 assembly (clang, -O0):
<tlv_encode_next>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %ecx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%eax
add    $0x2,%eax
mov    -0x18(%rbp),%rcx
cmp    (%rcx),%eax
jle    1153 <tlv_encode_next+0x43>
movl   $0x0,-0x4(%rbp)
jmp    121c <tlv_encode_next+0x10c>
jmp    1158 <tlv_encode_next+0x48>
cmpl   $0x0,-0x20(%rbp)
jle    120a <tlv_encode_next+0xfa>
mov    -0x1c(%rbp),%ecx
mov    -0x30(%rbp),%rax
mov    %rax,%rdx
add    $0x4,%rdx
mov    %rdx,-0x30(%rbp)
mov    %ecx,(%rax)
cmpl   $0xff,-0x20(%rbp)
jle    1190 <tlv_encode_next+0x80>
mov    $0xff,%eax
mov    %eax,-0x38(%rbp)
jmp    1196 <tlv_encode_next+0x86>
mov    -0x20(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    -0x38(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%ecx
mov    -0x30(%rbp),%rax
mov    %rax,%rdx
add    $0x4,%rdx
mov    %rdx,-0x30(%rbp)
mov    %ecx,(%rax)
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x34(%rbp),%edx
call   1030 <memcpy@plt>
mov    -0x34(%rbp),%ecx
mov    -0x28(%rbp),%rax
movslq %ecx,%rcx
shl    $0x2,%rcx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x34(%rbp),%ecx
mov    -0x30(%rbp),%rax
movslq %ecx,%rcx
shl    $0x2,%rcx
add    %rcx,%rax
mov    %rax,-0x30(%rbp)
mov    -0x34(%rbp),%ecx
mov    -0x20(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x20(%rbp)
mov    -0x34(%rbp),%edx
add    $0x2,%edx
mov    -0x18(%rbp),%rax
mov    (%rax),%ecx
sub    %edx,%ecx
mov    %ecx,(%rax)
jmp    1158 <tlv_encode_next+0x48>
mov    -0x30(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tlv_encode_next>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %ecx,%ebx
lea    0x2(%rbx),%ecx
xor    %eax,%eax
cmp    (%rsi),%ecx
jg     118c <tlv_encode_next+0x7c>
mov    %rdi,(%rsp)
mov    (%rdi),%r14
test   %ebx,%ebx
jle    1180 <tlv_encode_next+0x70>
mov    %r8,%r15
mov    %edx,%r12d
mov    %rsi,%r13
nopw   0x0(%rax,%rax,1)
mov    %r12d,(%r14)
cmp    $0xff,%ebx
mov    $0xff,%ebp
cmovb  %ebx,%ebp
lea    0x8(%r14),%rdi
mov    %ebp,0x4(%r14)
mov    %r15,%rsi
mov    %ebp,%edx
call   1030 <memcpy@plt>
lea    (%r15,%rbp,4),%r15
lea    (%r14,%rbp,4),%r14
add    $0x8,%r14
mov    0x0(%r13),%eax
sub    %ebp,%eax
add    $0xfffffffe,%eax
mov    %eax,0x0(%r13)
sub    %ebp,%ebx
jg     1140 <tlv_encode_next+0x30>
mov    (%rsp),%rax
mov    %r14,(%rax)
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i32 @tlv_encode_next(i32** nocapture %0, i32* nocapture %1, i32 %2, i32 %3, i32* %4) {
%6 = load i32*, i32** %0
%7 = add nsw i32 %3, 2
%8 = load i32, i32* %1
%9 = icmp sgt i32 %7, %8
br i1 %9, label %31, label %10
10: ; preds = %5
%11 = icmp sgt i32 %3, 0
br i1 %11, label %12, label %29
12: ; preds = %10, %12
%13 = phi i32* [ %23, %12 ], [ %6, %10 ]
%14 = phi i32* [ %22, %12 ], [ %4, %10 ]
%15 = phi i32 [ %24, %12 ], [ %3, %10 ]
%16 = getelementptr inbounds i32, i32* %13, i64 1
store i32 %2, i32* %13
%17 = icmp ult i32 %15, 255
%18 = select i1 %17, i32 %15, i32 255
%19 = getelementptr inbounds i32, i32* %13, i64 2
store i32 %18, i32* %16
%20 = tail call i32 @memcpy(i32* %19, i32* %14, i32 %18)
%21 = zext i32 %18 to i64
%22 = getelementptr inbounds i32, i32* %14, i64 %21
%23 = getelementptr inbounds i32, i32* %19, i64 %21
%24 = sub nsw i32 %15, %18
%25 = load i32, i32* %1
%26 = sub nuw nsw i32 -2, %18
%27 = add i32 %26, %25
store i32 %27, i32* %1
%28 = icmp sgt i32 %24, 0
br i1 %28, label %12, label %29
29: ; preds = %12, %10
%30 = phi i32* [ %6, %10 ], [ %23, %12 ]
store i32* %30, i32** %0
br label %31
31: ; preds = %5, %29
%32 = phi i32 [ 1, %29 ], [ 0, %5 ]
ret i32 %32
}