#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 int uint16_t;
typedef int int16_t;

/* Variables and functions */
scalar_t__ *tlv_find_param(scalar_t__ *, int, int);
int16_t emmg_find_datastreamid(uint8_t *p_tlv, uint16_t n) {
  const uint8_t *p_tlv_n = tlv_find_param(p_tlv, 0x0004, n);
  int16_t i_datastreamid = (int16_t)(p_tlv_n[4]) << 8 * (sizeof(int16_t) - 1);
  int i;
  for (i = 1; i < sizeof(int16_t); i++)
    i_datastreamid |= (uint16_t)(p_tlv_n[4 + i])
                      << 8 * (sizeof(int16_t) - i - 1);
  return i_datastreamid;
}


x86 assembly (clang, -O0):
<emmg_find_datastreamid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%edx
mov    $0x4,%esi
call   1030 <tlv_find_param@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    0x20(%rax),%rax
shl    $0x18,%eax
mov    %eax,-0x1c(%rbp)
movl   $0x1,-0x20(%rbp)
movslq -0x20(%rbp),%rax
cmp    $0x4,%rax
jae    1192 <emmg_find_datastreamid+0x82>
mov    -0x18(%rbp),%rax
mov    -0x20(%rbp),%ecx
add    $0x4,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,8),%rax
movslq -0x20(%rbp),%rdx
mov    $0x4,%ecx
sub    %rdx,%rcx
sub    $0x1,%rcx
shl    $0x3,%rcx
shl    %cl,%eax
or     -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    1149 <emmg_find_datastreamid+0x39>
mov    -0x1c(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<emmg_find_datastreamid>:
push   %rax
mov    %esi,%edx
mov    $0x4,%esi
call   1030 <tlv_find_param@plt>
mov    0x20(%rax),%ecx
mov    0x28(%rax),%edx
shl    $0x18,%ecx
shl    $0x10,%edx
or     %ecx,%edx
mov    0x30(%rax),%ecx
shl    $0x8,%ecx
or     %edx,%ecx
or     0x38(%rax),%ecx
mov    %ecx,%eax
pop    %rcx
ret

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

LLVM IR (-O2):
define i32 @emmg_find_datastreamid(i64* %0, i32 %1) {
%3 = tail call i64* @tlv_find_param(i64* %0, i32 4, i32 %1)
%4 = getelementptr inbounds i64, i64* %3, i64 4
%5 = load i64, i64* %4
%6 = trunc i64 %5 to i32
%7 = shl i32 %6, 24
%8 = getelementptr inbounds i64, i64* %3, i64 5
%9 = load i64, i64* %8
%10 = trunc i64 %9 to i32
%11 = shl i32 %10, 16
%12 = or i32 %11, %7
%13 = getelementptr inbounds i64, i64* %3, i64 6
%14 = load i64, i64* %13
%15 = trunc i64 %14 to i32
%16 = shl i32 %15, 8
%17 = or i32 %16, %12
%18 = getelementptr inbounds i64, i64* %3, i64 7
%19 = load i64, i64* %18
%20 = trunc i64 %19 to i32
%21 = or i32 %17, %20
ret i32 %21
}