#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;
typedef scalar_t__ int16_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_bandwidth(uint8_t *p_tlv, int16_t i_bandwidth) {
  uint8_t *p_tlv_n = tlv_find_param(p_tlv, 0, 0);
  int i;
  if (!tlv_validate_param(p_tlv, p_tlv_n, sizeof(int16_t) + 2))
    return false;
  tlv_set_type(p_tlv_n, 0x0006);
  tlv_set_length(p_tlv_n, sizeof(int16_t));
  for (i = 0; i < sizeof(int16_t); i++)
    p_tlv_n[4 + i] =
        ((uint16_t)(i_bandwidth) >> 8 * (sizeof(int16_t) - i - 1)) & 0xff;
  tlv_append_empty(p_tlv_n);
  return true;
}


x86 assembly (clang, -O0):
<emmg_append_bandwidth>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%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    $0xa,%edx
call   1050 <tlv_validate_param@plt>
cmp    $0x0,%eax
jne    1198 <emmg_append_bandwidth+0x48>
movl   $0x0,-0x4(%rbp)
jmp    121b <emmg_append_bandwidth+0xcb>
mov    -0x20(%rbp),%rdi
mov    $0x6,%esi
call   1070 <tlv_set_type@plt>
mov    -0x20(%rbp),%rdi
mov    $0x8,%esi
call   1040 <tlv_set_length@plt>
movl   $0x0,-0x24(%rbp)
movslq -0x24(%rbp),%rax
cmp    $0x8,%rax
jae    120b <emmg_append_bandwidth+0xbb>
mov    -0x18(%rbp),%rax
mov    %eax,%edx
movslq -0x24(%rbp),%rcx
mov    $0x8,%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    11bb <emmg_append_bandwidth+0x6b>
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_bandwidth>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r15
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    $0xa,%edx
call   1050 <tlv_validate_param@plt>
test   %eax,%eax
je     11ca <emmg_append_bandwidth+0x7a>
mov    %rbx,%rdi
mov    $0x6,%esi
call   1070 <tlv_set_type@plt>
mov    %rbx,%rdi
mov    $0x8,%esi
call   1040 <tlv_set_length@plt>
mov    %r15,%rcx
mov    %ecx,%eax
shr    $0x18,%eax
mov    %eax,0x20(%rbx)
mov    %ecx,%eax
shr    $0x10,%eax
movzbl %al,%eax
mov    %eax,0x24(%rbx)
movzbl %ch,%eax
mov    %eax,0x28(%rbx)
movzbl %cl,%eax
mov    %eax,0x2c(%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_bandwidth(i32* %0, i64 %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i64
%6 = alloca i32*
%7 = alloca i32
store i32* %0, i32** %4
store i64 %1, i64* %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 10)
%13 = icmp ne i32 %12, 0
br i1 %13, label %15, label %14
14: ; preds = %2
store i32 0, i32* %3
br label %46
15: ; preds = %2
%16 = load i32*, i32** %6
%17 = call i32 @tlv_set_type(i32* %16, i32 6)
%18 = load i32*, i32** %6
%19 = call i32 @tlv_set_length(i32* %18, i32 8)
store i32 0, i32* %7
br label %20
20: ; preds = %40, %15
%21 = load i32, i32* %7
%22 = sext i32 %21 to i64
%23 = icmp ult i64 %22, 8
br i1 %23, label %24, label %43
24: ; preds = %20
%25 = load i64, i64* %5
%26 = trunc i64 %25 to i32
%27 = load i32, i32* %7
%28 = sext i32 %27 to i64
%29 = sub i64 8, %28
%30 = sub i64 %29, 1
%31 = mul i64 8, %30
%32 = trunc i64 %31 to i32
%33 = ashr i32 %26, %32
%34 = and i32 %33, 255
%35 = load i32*, i32** %6
%36 = load i32, i32* %7
%37 = add nsw i32 4, %36
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds i32, i32* %35, i64 %38
store i32 %34, i32* %39
br label %40
40: ; preds = %24
%41 = load i32, i32* %7
%42 = add nsw i32 %41, 1
store i32 %42, i32* %7
br label %20
43: ; preds = %20
%44 = load i32*, i32** %6
%45 = call i32 @tlv_append_empty(i32* %44)
store i32 1, i32* %3
br label %46
46: ; preds = %43, %14
%47 = load i32, i32* %3
ret i32 %47
}

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