#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*/ *tlv_get_param(int /*<<< orphan*/ *, scalar_t__);
scalar_t__ tlv_get_type(int /*<<< orphan*/ *);
uint16_t tlv_count_param(uint8_t *p_tlv, uint16_t i_type) {
  uint16_t i_count = 0;
  uint8_t *p_tlv_n;
  uint16_t j = 0;
  while ((p_tlv_n = tlv_get_param(p_tlv, j)) != NULL) {
    j++;
    if (tlv_get_type(p_tlv_n) == i_type)
      i_count++;
  }
  return i_count;
}


x86 assembly (clang, -O0):
<tlv_count_param>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1040 <tlv_get_param@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     118b <tlv_count_param+0x6b>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <tlv_get_type@plt>
cmp    -0x10(%rbp),%rax
jne    1186 <tlv_count_param+0x66>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    1140 <tlv_count_param+0x20>
mov    -0x18(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tlv_count_param>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r15
xor    %r12d,%r12d
xor    %esi,%esi
call   1040 <tlv_get_param@plt>
test   %rax,%rax
je     1177 <tlv_count_param+0x57>
mov    $0x1,%ebx
xor    %r12d,%r12d
cs nopw 0x0(%rax,%rax,1)
nop
mov    %rax,%rdi
call   1030 <tlv_get_type@plt>
xor    %ecx,%ecx
cmp    %r14,%rax
sete   %cl
add    %rcx,%r12
mov    %r15,%rdi
mov    %rbx,%rsi
call   1040 <tlv_get_param@plt>
add    $0x1,%rbx
test   %rax,%rax
jne    1150 <tlv_count_param+0x30>
mov    %r12,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i64 @tlv_count_param(i32* %0, i64 %1) {
%3 = alloca i32*
%4 = alloca i64
%5 = alloca i64
%6 = alloca i32*
%7 = alloca i64
store i32* %0, i32** %3
store i64 %1, i64* %4
store i64 0, i64* %5
store i64 0, i64* %7
br label %8
8: ; preds = %23, %2
%9 = load i32*, i32** %3
%10 = load i64, i64* %7
%11 = call i32* @tlv_get_param(i32* %9, i64 %10)
store i32* %11, i32** %6
%12 = icmp ne i32* %11, null
br i1 %12, label %13, label %24
13: ; preds = %8
%14 = load i64, i64* %7
%15 = add nsw i64 %14, 1
store i64 %15, i64* %7
%16 = load i32*, i32** %6
%17 = call i64 @tlv_get_type(i32* %16)
%18 = load i64, i64* %4
%19 = icmp eq i64 %17, %18
br i1 %19, label %20, label %23
20: ; preds = %13
%21 = load i64, i64* %5
%22 = add nsw i64 %21, 1
store i64 %22, i64* %5
br label %23
23: ; preds = %20, %13
br label %8
24: ; preds = %8
%25 = load i64, i64* %5
ret i64 %25
}

LLVM IR (-O2):
define i64 @tlv_count_param(i32* %0, i64 %1) {
%3 = tail call i32* @tlv_get_param(i32* %0, i64 0)
%4 = icmp eq i32* %3, null
br i1 %4, label %16, label %5
5: ; preds = %2, %5
%6 = phi i32* [ %14, %5 ], [ %3, %2 ]
%7 = phi i64 [ %9, %5 ], [ 0, %2 ]
%8 = phi i64 [ %13, %5 ], [ 0, %2 ]
%9 = add nuw nsw i64 %7, 1
%10 = tail call i64 @tlv_get_type(i32* %6)
%11 = icmp eq i64 %10, %1
%12 = zext i1 %11 to i64
%13 = add nuw nsw i64 %8, %12
%14 = tail call i32* @tlv_get_param(i32* %0, i64 %9)
%15 = icmp eq i32* %14, null
br i1 %15, label %16, label %5
16: ; preds = %5, %2
%17 = phi i64 [ 0, %2 ], [ %13, %5 ]
ret i64 %17
}