#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 uint16_t;
struct tlv_iterator {
  int dummy;
};

/* Variables and functions */
int *malloc(int);
int /*<<< orphan*/ memcpy(int *, int /*<<< orphan*/, int);
int ntohs(int);
int /*<<< orphan*/ tlv_iter_get_data(struct tlv_iterator *);
int tlv_iter_get_len(struct tlv_iterator *);
int tlv_iter_decode_u16_array(struct tlv_iterator *tlv_iter, uint16_t **u16a,
                              size_t *no_items) {
  uint16_t opt_len;
  uint16_t *u16a_res;
  size_t i;
  opt_len = tlv_iter_get_len(tlv_iter);
  if (opt_len % sizeof(uint16_t) != 0) {
    return (-1);
  }
  *no_items = opt_len / sizeof(uint16_t);
  u16a_res = malloc(sizeof(uint16_t) * *no_items);
  if (u16a_res == NULL) {
    return (-2);
  }
  memcpy(u16a_res, tlv_iter_get_data(tlv_iter), opt_len);
  for (i = 0; i < *no_items; i++) {
    u16a_res[i] = ntohs(u16a_res[i]);
  }
  *u16a = u16a_res;
  return (0);
}


x86 assembly (clang, -O0):
<tlv_iter_decode_u16_array>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <tlv_iter_get_len@plt>
mov    %eax,-0x24(%rbp)
movslq -0x24(%rbp),%rax
and    $0x3,%rax
cmp    $0x0,%rax
je     118e <tlv_iter_decode_u16_array+0x3e>
movl   $0xffffffff,-0x4(%rbp)
jmp    1242 <tlv_iter_decode_u16_array+0xf2>
movslq -0x24(%rbp),%rcx
shr    $0x2,%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11ca <tlv_iter_decode_u16_array+0x7a>
movl   $0xfffffffe,-0x4(%rbp)
jmp    1242 <tlv_iter_decode_u16_array+0xf2>
mov    -0x30(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <tlv_iter_get_data@plt>
mov    -0x40(%rbp),%rdi
mov    %eax,%esi
mov    -0x24(%rbp),%edx
call   1050 <memcpy@plt>
movq   $0x0,-0x38(%rbp)
mov    -0x38(%rbp),%rax
mov    -0x20(%rbp),%rcx
cmp    (%rcx),%rax
jae    1230 <tlv_iter_decode_u16_array+0xe0>
mov    -0x30(%rbp),%rax
mov    -0x38(%rbp),%rcx
mov    (%rax,%rcx,4),%edi
call   1070 <ntohs@plt>
mov    %eax,%edx
mov    -0x30(%rbp),%rax
mov    -0x38(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x38(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x38(%rbp)
jmp    11f1 <tlv_iter_decode_u16_array+0xa1>
mov    -0x30(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tlv_iter_decode_u16_array>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdx,%r15
mov    %rsi,%r14
mov    %rdi,%r12
call   1040 <tlv_iter_get_len@plt>
mov    %eax,%ebp
movslq %eax,%rcx
mov    $0xffffffff,%eax
test   $0x3,%cl
jne    11d0 <tlv_iter_decode_u16_array+0x80>
shr    $0x2,%rcx
mov    %rcx,(%r15)
mov    %ebp,%edi
and    $0xfffffffc,%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     11cb <tlv_iter_decode_u16_array+0x7b>
mov    %rax,%rbx
mov    %r12,%rdi
call   1030 <tlv_iter_get_data@plt>
mov    %rbx,%rdi
mov    %eax,%esi
mov    %ebp,%edx
call   1050 <memcpy@plt>
cmpq   $0x0,(%r15)
je     11c4 <tlv_iter_decode_u16_array+0x74>
xor    %ebp,%ebp
nopw   0x0(%rax,%rax,1)
mov    (%rbx,%rbp,4),%edi
call   1070 <ntohs@plt>
mov    %eax,(%rbx,%rbp,4)
add    $0x1,%rbp
cmp    (%r15),%rbp
jb     11b0 <tlv_iter_decode_u16_array+0x60>
mov    %rbx,(%r14)
xor    %eax,%eax
jmp    11d0 <tlv_iter_decode_u16_array+0x80>
mov    $0xfffffffe,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.tlv_iterator = type { i32 }
define i32 @tlv_iter_decode_u16_array(%struct.tlv_iterator* %0, i32** %1, i64* %2) {
%4 = alloca i32
%5 = alloca %struct.tlv_iterator*
%6 = alloca i32**
%7 = alloca i64*
%8 = alloca i32
%9 = alloca i32*
%10 = alloca i64
store %struct.tlv_iterator* %0, %struct.tlv_iterator** %5
store i32** %1, i32*** %6
store i64* %2, i64** %7
%11 = load %struct.tlv_iterator*, %struct.tlv_iterator** %5
%12 = call i32 @tlv_iter_get_len(%struct.tlv_iterator* %11)
store i32 %12, i32* %8
%13 = load i32, i32* %8
%14 = sext i32 %13 to i64
%15 = urem i64 %14, 4
%16 = icmp ne i64 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %3
store i32 -1, i32* %4
br label %57
18: ; preds = %3
%19 = load i32, i32* %8
%20 = sext i32 %19 to i64
%21 = udiv i64 %20, 4
%22 = load i64*, i64** %7
store i64 %21, i64* %22
%23 = load i64*, i64** %7
%24 = load i64, i64* %23
%25 = mul i64 4, %24
%26 = trunc i64 %25 to i32
%27 = call i32* @malloc(i32 %26)
store i32* %27, i32** %9
%28 = load i32*, i32** %9
%29 = icmp eq i32* %28, null
br i1 %29, label %30, label %31
30: ; preds = %18
store i32 -2, i32* %4
br label %57
31: ; preds = %18
%32 = load i32*, i32** %9
%33 = load %struct.tlv_iterator*, %struct.tlv_iterator** %5
%34 = call i32 @tlv_iter_get_data(%struct.tlv_iterator* %33)
%35 = load i32, i32* %8
%36 = call i32 @memcpy(i32* %32, i32 %34, i32 %35)
store i64 0, i64* %10
br label %37
37: ; preds = %51, %31
%38 = load i64, i64* %10
%39 = load i64*, i64** %7
%40 = load i64, i64* %39
%41 = icmp ult i64 %38, %40
br i1 %41, label %42, label %54
42: ; preds = %37
%43 = load i32*, i32** %9
%44 = load i64, i64* %10
%45 = getelementptr inbounds i32, i32* %43, i64 %44
%46 = load i32, i32* %45
%47 = call i32 @ntohs(i32 %46)
%48 = load i32*, i32** %9
%49 = load i64, i64* %10
%50 = getelementptr inbounds i32, i32* %48, i64 %49
store i32 %47, i32* %50
br label %51
51: ; preds = %42
%52 = load i64, i64* %10
%53 = add i64 %52, 1
store i64 %53, i64* %10
br label %37
54: ; preds = %37
%55 = load i32*, i32** %9
%56 = load i32**, i32*** %6
store i32* %55, i32** %56
store i32 0, i32* %4
br label %57
57: ; preds = %54, %30, %17
%58 = load i32, i32* %4
ret i32 %58
}

LLVM IR (-O2):
%struct.tlv_iterator = type { i32 }
define i32 @tlv_iter_decode_u16_array(%struct.tlv_iterator* %0, i32** nocapture writeonly %1, i64* nocapture %2) {
%4 = tail call i32 @tlv_iter_get_len(%struct.tlv_iterator* %0)
%5 = sext i32 %4 to i64
%6 = and i64 %5, 3
%7 = icmp eq i64 %6, 0
br i1 %7, label %8, label %27
8: ; preds = %3
%9 = lshr i64 %5, 2
store i64 %9, i64* %2
%10 = and i32 %4, -4
%11 = tail call i32* @malloc(i32 %10)
%12 = icmp eq i32* %11, null
br i1 %12, label %27, label %13
13: ; preds = %8
%14 = tail call i32 @tlv_iter_get_data(%struct.tlv_iterator* %0)
%15 = tail call i32 @memcpy(i32* %11, i32 %14, i32 %4)
%16 = load i64, i64* %2
%17 = icmp eq i64 %16, 0
br i1 %17, label %26, label %18
18: ; preds = %13, %18
%19 = phi i64 [ %23, %18 ], [ 0, %13 ]
%20 = getelementptr inbounds i32, i32* %11, i64 %19
%21 = load i32, i32* %20
%22 = tail call i32 @ntohs(i32 %21)
store i32 %22, i32* %20
%23 = add nuw i64 %19, 1
%24 = load i64, i64* %2
%25 = icmp ult i64 %23, %24
br i1 %25, label %18, label %26
26: ; preds = %18, %13
store i32* %11, i32** %1
br label %27
27: ; preds = %8, %3, %26
%28 = phi i32 [ 0, %26 ], [ -1, %3 ], [ -2, %8 ]
ret i32 %28
}