#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 /*<<< orphan*/ uint8_t;
typedef scalar_t__ uint16_t;

/* Variables and functions */
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ *, int /*<<< orphan*/ const *,
                          scalar_t__);
int /*<<< orphan*/ tlv_append_empty(int /*<<< orphan*/ *);
int /*<<< orphan*/ *tlv_find_param(int /*<<< orphan*/ *, int /*<<< orphan*/,
                                   int /*<<< orphan*/);
int /*<<< orphan*/ tlv_set_length(int /*<<< orphan*/ *, scalar_t__);
int /*<<< orphan*/ tlv_set_type(int /*<<< orphan*/ *, scalar_t__);
int /*<<< orphan*/ tlv_validate_param(int /*<<< orphan*/ *,
                                      int /*<<< orphan*/ *, scalar_t__);
bool tlv_append_data(uint8_t *p_tlv, uint16_t i_type, const uint8_t *p_data,
                     uint16_t i_length) {
  uint8_t *p_tlv_n = tlv_find_param(p_tlv, 0, 0);
  if (!tlv_validate_param(p_tlv, p_tlv_n, i_length + 2))
    return false;
  tlv_set_type(p_tlv_n, i_type);
  tlv_set_length(p_tlv_n, i_length);
  memcpy(p_tlv_n + 4, p_data, i_length);
  tlv_append_empty(p_tlv_n);
  return true;
}


x86 assembly (clang, -O0):
<tlv_append_data>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
xor    %edx,%edx
mov    %edx,%esi
call   1040 <tlv_find_param@plt>
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x28(%rbp),%rdx
add    $0x2,%rdx
call   1060 <tlv_validate_param@plt>
cmp    $0x0,%eax
jne    11b3 <tlv_append_data+0x53>
movl   $0x0,-0x4(%rbp)
jmp    11f2 <tlv_append_data+0x92>
mov    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1080 <tlv_set_type@plt>
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1050 <tlv_set_length@plt>
mov    -0x30(%rbp),%rdi
add    $0x10,%rdi
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1030 <memcpy@plt>
mov    -0x30(%rbp),%rdi
call   1070 <tlv_append_empty@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tlv_append_data>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r15
mov    %rdx,%r14
mov    %rsi,%r12
mov    %rdi,%rbp
xor    %r13d,%r13d
xor    %esi,%esi
xor    %edx,%edx
call   1040 <tlv_find_param@plt>
mov    %rax,%rbx
lea    0x2(%r15),%rdx
mov    %rbp,%rdi
mov    %rax,%rsi
call   1060 <tlv_validate_param@plt>
test   %eax,%eax
je     11cf <tlv_append_data+0x6f>
mov    %rbx,%rdi
mov    %r12,%rsi
call   1080 <tlv_set_type@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
call   1050 <tlv_set_length@plt>
mov    %rbx,%rdi
add    $0x10,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
call   1030 <memcpy@plt>
mov    %rbx,%rdi
call   1070 <tlv_append_empty@plt>
mov    $0x1,%r13d
mov    %r13d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @tlv_append_data(i32* %0, i64 %1, i32* %2, i64 %3) {
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i64
%8 = alloca i32*
%9 = alloca i64
%10 = alloca i32*
store i32* %0, i32** %6
store i64 %1, i64* %7
store i32* %2, i32** %8
store i64 %3, i64* %9
%11 = load i32*, i32** %6
%12 = call i32* @tlv_find_param(i32* %11, i32 0, i32 0)
store i32* %12, i32** %10
%13 = load i32*, i32** %6
%14 = load i32*, i32** %10
%15 = load i64, i64* %9
%16 = add nsw i64 %15, 2
%17 = call i32 @tlv_validate_param(i32* %13, i32* %14, i64 %16)
%18 = icmp ne i32 %17, 0
br i1 %18, label %20, label %19
19: ; preds = %4
store i32 0, i32* %5
br label %34
20: ; preds = %4
%21 = load i32*, i32** %10
%22 = load i64, i64* %7
%23 = call i32 @tlv_set_type(i32* %21, i64 %22)
%24 = load i32*, i32** %10
%25 = load i64, i64* %9
%26 = call i32 @tlv_set_length(i32* %24, i64 %25)
%27 = load i32*, i32** %10
%28 = getelementptr inbounds i32, i32* %27, i64 4
%29 = load i32*, i32** %8
%30 = load i64, i64* %9
%31 = call i32 @memcpy(i32* %28, i32* %29, i64 %30)
%32 = load i32*, i32** %10
%33 = call i32 @tlv_append_empty(i32* %32)
store i32 1, i32* %5
br label %34
34: ; preds = %20, %19
%35 = load i32, i32* %5
ret i32 %35
}

LLVM IR (-O2):
define i32 @tlv_append_data(i32* %0, i64 %1, i32* %2, i64 %3) {
%5 = tail call i32* @tlv_find_param(i32* %0, i32 0, i32 0)
%6 = add nsw i64 %3, 2
%7 = tail call i32 @tlv_validate_param(i32* %0, i32* %5, i64 %6)
%8 = icmp eq i32 %7, 0
br i1 %8, label %15, label %9
9: ; preds = %4
%10 = tail call i32 @tlv_set_type(i32* %5, i64 %1)
%11 = tail call i32 @tlv_set_length(i32* %5, i64 %3)
%12 = getelementptr inbounds i32, i32* %5, i64 4
%13 = tail call i32 @memcpy(i32* %12, i32* %2, i64 %3)
%14 = tail call i32 @tlv_append_empty(i32* %5)
br label %15
15: ; preds = %4, %9
%16 = phi i32 [ 1, %9 ], [ 0, %4 ]
ret i32 %16
}