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

/* Variables and functions */
scalar_t__ *tlv_get_param(scalar_t__ *, scalar_t__);
scalar_t__ tlv_get_type(scalar_t__ *);
uint8_t *tlv_find_param(uint8_t *p_tlv, uint16_t i_type, uint16_t n) {
  uint8_t *p_param;
  uint8_t j = 0;
  while ((p_param = tlv_get_param(p_tlv, j)) != NULL) {
    j++;
    if (tlv_get_type(p_param) == i_type) {
      if (!n)
        return p_param;
      n--;
    }
  }
  return NULL;
}


x86 assembly (clang, -O0):
<tlv_find_param>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   1040 <tlv_get_param@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     119f <tlv_find_param+0x7f>
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rdi
call   1030 <tlv_get_type@plt>
cmp    -0x18(%rbp),%rax
jne    119a <tlv_find_param+0x7a>
cmpq   $0x0,-0x20(%rbp)
jne    118e <tlv_find_param+0x6e>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11a7 <tlv_find_param+0x87>
mov    -0x20(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x20(%rbp)
jmp    113c <tlv_find_param+0x1c>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tlv_find_param>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdx,%r12
mov    %rsi,%r14
mov    %rdi,%r15
xor    %r13d,%r13d
xor    %esi,%esi
call   1040 <tlv_get_param@plt>
test   %rax,%rax
je     1182 <tlv_find_param+0x62>
mov    $0x1,%ebx
mov    %rax,%r13
jmp    1167 <tlv_find_param+0x47>
nopl   0x0(%rax,%rax,1)
mov    %r15,%rdi
mov    %rbx,%rsi
call   1040 <tlv_get_param@plt>
mov    %rax,%r13
add    $0x1,%rbx
test   %rax,%rax
je     117f <tlv_find_param+0x5f>
mov    %r13,%rdi
call   1030 <tlv_get_type@plt>
cmp    %r14,%rax
jne    1150 <tlv_find_param+0x30>
test   %r12,%r12
je     1182 <tlv_find_param+0x62>
add    $0xffffffffffffffff,%r12
jmp    1150 <tlv_find_param+0x30>
xor    %r13d,%r13d
mov    %r13,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

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

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