#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*/ tlv_append_empty(int *);
int *tlv_find_param(int *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ tlv_set_length(int *, int);
int /*<<< orphan*/ tlv_set_type(int *, int);
int /*<<< orphan*/ tlv_validate_param(int *, int *, int);
bool emmg_append_errorstatus(uint8_t *p_tlv, uint16_t i_errorstatus) {
  uint8_t *p_tlv_n = tlv_find_param(p_tlv, 0, 0);
  int i;
  if (!tlv_validate_param(p_tlv, p_tlv_n, sizeof(uint16_t) + 2))
    return false;
  tlv_set_type(p_tlv_n, 0x7000);
  tlv_set_length(p_tlv_n, sizeof(uint16_t));
  for (i = 0; i < sizeof(uint16_t); i++)
    p_tlv_n[4 + i] =
        ((uint16_t)(i_errorstatus) >> 8 * (sizeof(uint16_t) - i - 1)) & 0xff;
  tlv_append_empty(p_tlv_n);
  return true;
}


x86 assembly (clang, -O0):
<emmg_append_errorstatus>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
xor    %edx,%edx
mov    %edx,%esi
call   1030 <tlv_find_param@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    $0x6,%edx
call   1050 <tlv_validate_param@plt>
cmp    $0x0,%eax
jne    1197 <emmg_append_errorstatus+0x47>
movl   $0x0,-0x4(%rbp)
jmp    1217 <emmg_append_errorstatus+0xc7>
mov    -0x20(%rbp),%rdi
mov    $0x7000,%esi
call   1070 <tlv_set_type@plt>
mov    -0x20(%rbp),%rdi
mov    $0x4,%esi
call   1040 <tlv_set_length@plt>
movl   $0x0,-0x24(%rbp)
movslq -0x24(%rbp),%rax
cmp    $0x4,%rax
jae    1207 <emmg_append_errorstatus+0xb7>
mov    -0x14(%rbp),%edx
movslq -0x24(%rbp),%rcx
mov    $0x4,%eax
sub    %rcx,%rax
sub    $0x1,%rax
shl    $0x3,%rax
mov    %eax,%ecx
sar    %cl,%edx
and    $0xff,%edx
mov    -0x20(%rbp),%rax
mov    -0x24(%rbp),%ecx
add    $0x4,%ecx
movslq %ecx,%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    11ba <emmg_append_errorstatus+0x6a>
mov    -0x20(%rbp),%rdi
call   1060 <tlv_append_empty@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<emmg_append_errorstatus>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%r15d
mov    %rdi,%rbp
xor    %r14d,%r14d
xor    %esi,%esi
xor    %edx,%edx
call   1030 <tlv_find_param@plt>
mov    %rax,%rbx
mov    %rbp,%rdi
mov    %rax,%rsi
mov    $0x6,%edx
call   1050 <tlv_validate_param@plt>
test   %eax,%eax
je     11cc <emmg_append_errorstatus+0x7c>
mov    %rbx,%rdi
mov    $0x7000,%esi
call   1070 <tlv_set_type@plt>
mov    %rbx,%rdi
mov    $0x4,%esi
call   1040 <tlv_set_length@plt>
mov    %r15d,%ecx
mov    %r15d,%eax
shr    $0x18,%eax
mov    %eax,0x10(%rbx)
mov    %r15d,%eax
shr    $0x10,%eax
movzbl %al,%eax
mov    %eax,0x14(%rbx)
movzbl %ch,%eax
mov    %eax,0x18(%rbx)
movzbl %cl,%eax
mov    %eax,0x1c(%rbx)
mov    %rbx,%rdi
call   1060 <tlv_append_empty@plt>
mov    $0x1,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @emmg_append_errorstatus(i32* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i32
store i32* %0, i32** %4
store i32 %1, i32* %5
%8 = load i32*, i32** %4
%9 = call i32* @tlv_find_param(i32* %8, i32 0, i32 0)
store i32* %9, i32** %6
%10 = load i32*, i32** %4
%11 = load i32*, i32** %6
%12 = call i32 @tlv_validate_param(i32* %10, i32* %11, i32 6)
%13 = icmp ne i32 %12, 0
br i1 %13, label %15, label %14
14: ; preds = %2
store i32 0, i32* %3
br label %45
15: ; preds = %2
%16 = load i32*, i32** %6
%17 = call i32 @tlv_set_type(i32* %16, i32 28672)
%18 = load i32*, i32** %6
%19 = call i32 @tlv_set_length(i32* %18, i32 4)
store i32 0, i32* %7
br label %20
20: ; preds = %39, %15
%21 = load i32, i32* %7
%22 = sext i32 %21 to i64
%23 = icmp ult i64 %22, 4
br i1 %23, label %24, label %42
24: ; preds = %20
%25 = load i32, i32* %5
%26 = load i32, i32* %7
%27 = sext i32 %26 to i64
%28 = sub i64 4, %27
%29 = sub i64 %28, 1
%30 = mul i64 8, %29
%31 = trunc i64 %30 to i32
%32 = ashr i32 %25, %31
%33 = and i32 %32, 255
%34 = load i32*, i32** %6
%35 = load i32, i32* %7
%36 = add nsw i32 4, %35
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds i32, i32* %34, i64 %37
store i32 %33, i32* %38
br label %39
39: ; preds = %24
%40 = load i32, i32* %7
%41 = add nsw i32 %40, 1
store i32 %41, i32* %7
br label %20
42: ; preds = %20
%43 = load i32*, i32** %6
%44 = call i32 @tlv_append_empty(i32* %43)
store i32 1, i32* %3
br label %45
45: ; preds = %42, %14
%46 = load i32, i32* %3
ret i32 %46
}

LLVM IR (-O2):
define i32 @emmg_append_errorstatus(i32* %0, i32 %1) {
%3 = tail call i32* @tlv_find_param(i32* %0, i32 0, i32 0)
%4 = tail call i32 @tlv_validate_param(i32* %0, i32* %3, i32 6)
%5 = icmp eq i32 %4, 0
br i1 %5, label %20, label %6
6: ; preds = %2
%7 = tail call i32 @tlv_set_type(i32* %3, i32 28672)
%8 = tail call i32 @tlv_set_length(i32* %3, i32 4)
%9 = lshr i32 %1, 24
%10 = getelementptr inbounds i32, i32* %3, i64 4
store i32 %9, i32* %10
%11 = lshr i32 %1, 16
%12 = and i32 %11, 255
%13 = getelementptr inbounds i32, i32* %3, i64 5
store i32 %12, i32* %13
%14 = lshr i32 %1, 8
%15 = and i32 %14, 255
%16 = getelementptr inbounds i32, i32* %3, i64 6
store i32 %15, i32* %16
%17 = and i32 %1, 255
%18 = getelementptr inbounds i32, i32* %3, i64 7
store i32 %17, i32* %18
%19 = tail call i32 @tlv_append_empty(i32* %3)
br label %20
20: ; preds = %2, %6
%21 = phi i32 [ 1, %6 ], [ 0, %2 ]
ret i32 %21
}