#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*/ uint32_t;
typedef int uint16_t;
struct tlv_iterator {
  int dummy;
};
typedef int /*<<< orphan*/ nu32;

/* Variables and functions */
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ *, char const *, int);
int /*<<< orphan*/ ntohl(int /*<<< orphan*/);
char *tlv_iter_get_data(struct tlv_iterator *);
int tlv_iter_get_len(struct tlv_iterator *);
int tlv_iter_decode_u32(struct tlv_iterator *tlv_iter, uint32_t *res) {
  const char *opt_data;
  uint16_t opt_len;
  uint32_t nu32;
  opt_len = tlv_iter_get_len(tlv_iter);
  opt_data = tlv_iter_get_data(tlv_iter);
  if (opt_len != sizeof(nu32)) {
    return (-1);
  }
  memcpy(&nu32, opt_data, sizeof(nu32));
  *res = ntohl(nu32);
  return (0);
}


x86 assembly (clang, -O0):
<tlv_iter_decode_u32>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <tlv_iter_get_len@plt>
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <tlv_iter_get_data@plt>
mov    %rax,-0x20(%rbp)
movslq -0x24(%rbp),%rax
cmp    $0x4,%rax
je     1183 <tlv_iter_decode_u32+0x43>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ac <tlv_iter_decode_u32+0x6c>
mov    -0x20(%rbp),%rsi
lea    -0x28(%rbp),%rdi
mov    $0x4,%edx
call   1050 <memcpy@plt>
mov    -0x28(%rbp),%edi
call   1060 <ntohl@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tlv_iter_decode_u32>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
call   1040 <tlv_iter_get_len@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1030 <tlv_iter_get_data@plt>
mov    $0xffffffff,%ecx
cmp    $0x4,%ebp
jne    1187 <tlv_iter_decode_u32+0x47>
lea    0xc(%rsp),%rdi
mov    %rax,%rsi
mov    $0x4,%edx
call   1050 <memcpy@plt>
mov    0xc(%rsp),%edi
call   1060 <ntohl@plt>
mov    %eax,(%r14)
xor    %ecx,%ecx
mov    %ecx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.tlv_iterator = type { i32 }
define i32 @tlv_iter_decode_u32(%struct.tlv_iterator* %0, i32* %1) {
%3 = alloca i32
%4 = alloca %struct.tlv_iterator*
%5 = alloca i32*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
store %struct.tlv_iterator* %0, %struct.tlv_iterator** %4
store i32* %1, i32** %5
%9 = load %struct.tlv_iterator*, %struct.tlv_iterator** %4
%10 = call i32 @tlv_iter_get_len(%struct.tlv_iterator* %9)
store i32 %10, i32* %7
%11 = load %struct.tlv_iterator*, %struct.tlv_iterator** %4
%12 = call i8* @tlv_iter_get_data(%struct.tlv_iterator* %11)
store i8* %12, i8** %6
%13 = load i32, i32* %7
%14 = sext i32 %13 to i64
%15 = icmp ne i64 %14, 4
br i1 %15, label %16, label %17
16: ; preds = %2
store i32 -1, i32* %3
br label %23
17: ; preds = %2
%18 = load i8*, i8** %6
%19 = call i32 @memcpy(i32* %8, i8* %18, i32 4)
%20 = load i32, i32* %8
%21 = call i32 @ntohl(i32 %20)
%22 = load i32*, i32** %5
store i32 %21, i32* %22
store i32 0, i32* %3
br label %23
23: ; preds = %17, %16
%24 = load i32, i32* %3
ret i32 %24
}

LLVM IR (-O2):
%struct.tlv_iterator = type { i32 }
define i32 @tlv_iter_decode_u32(%struct.tlv_iterator* %0, i32* nocapture writeonly %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = tail call i32 @tlv_iter_get_len(%struct.tlv_iterator* %0)
%6 = tail call i8* @tlv_iter_get_data(%struct.tlv_iterator* %0)
%7 = icmp eq i32 %5, 4
br i1 %7, label %8, label %12
8: ; preds = %2
%9 = call i32 @memcpy(i32* %3, i8* %6, i32 4)
%10 = load i32, i32* %3
%11 = call i32 @ntohl(i32 %10)
store i32 %11, i32* %1
br label %12
12: ; preds = %2, %8
%13 = phi i32 [ 0, %8 ], [ -1, %2 ]
ret i32 %13
}