#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 */
struct nlattr {
  int dummy;
};
typedef int __u16;

/* Variables and functions */
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ memset(struct nlattr **, int /*<<< orphan*/, int);
struct nlattr *nla_next(struct nlattr *, int *);
scalar_t__ nla_ok(struct nlattr *, int);
int nla_type(struct nlattr *);
int /*<<< orphan*/ stderr;
int nla_parse(struct nlattr *arr[], int max, struct nlattr *nla, int len) {
  memset(arr, 0, sizeof(struct nlattr *) * (max + 1));
  for (; nla_ok(nla, len); nla = nla_next(nla, &(len))) {
    __u16 type = nla_type(nla);
    if (type > 0 && type <= max)
      arr[type] = (struct nlattr *)nla;
  }
  if (len > 0)
    fprintf(stderr,
            "[WARN]"
            "netlink: %d bytes leftover after parsing attributes."
            "\n",
            len);
  ;
  return 0;
}


x86 assembly (clang, -O0):
<nla_parse>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%eax
add    $0x1,%eax
cltq
shl    $0x3,%rax
mov    %eax,%edx
xor    %esi,%esi
call   1030 <memset@plt>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%esi
call   1050 <nla_ok@plt>
cmp    $0x0,%rax
je     11e2 <nla_parse+0x92>
mov    -0x18(%rbp),%rdi
call   1060 <nla_type@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jle    11c7 <nla_parse+0x77>
mov    -0x20(%rbp),%eax
cmp    -0xc(%rbp),%eax
jg     11c7 <nla_parse+0x77>
mov    -0x18(%rbp),%rdx
mov    -0x8(%rbp),%rax
movslq -0x20(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
jmp    11cc <nla_parse+0x7c>
mov    -0x18(%rbp),%rdi
lea    -0x1c(%rbp),%rsi
call   1070 <nla_next@plt>
mov    %rax,-0x18(%rbp)
jmp    117f <nla_parse+0x2f>
cmpl   $0x0,-0x1c(%rbp)
jle    1204 <nla_parse+0xb4>
mov    0x2ded(%rip),%rax
mov    (%rax),%edi
mov    -0x1c(%rbp),%edx
lea    0xe01(%rip),%rsi
call   1040 <fprintf@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<nla_parse>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %ecx,%ebp
mov    %rdx,%rbx
mov    %esi,%r15d
mov    %rdi,%r14
mov    %ecx,0xc(%rsp)
lea    0x8(,%r15,8),%edx
xor    %esi,%esi
call   1030 <memset@plt>
mov    %rbx,%rdi
mov    %ebp,%esi
call   1050 <nla_ok@plt>
test   %rax,%rax
je     11cc <nla_parse+0x7c>
lea    0xc(%rsp),%r12
jmp    11af <nla_parse+0x5f>
mov    %rbx,%rdi
mov    %r12,%rsi
call   1070 <nla_next@plt>
mov    %rax,%rbx
mov    0xc(%rsp),%esi
mov    %rax,%rdi
call   1050 <nla_ok@plt>
test   %rax,%rax
je     11c8 <nla_parse+0x78>
mov    %rbx,%rdi
call   1060 <nla_type@plt>
test   %eax,%eax
jle    1190 <nla_parse+0x40>
cmp    %r15d,%eax
jg     1190 <nla_parse+0x40>
mov    %eax,%eax
mov    %rbx,(%r14,%rax,8)
jmp    1190 <nla_parse+0x40>
mov    0xc(%rsp),%ebp
test   %ebp,%ebp
jle    11e7 <nla_parse+0x97>
mov    0x2e09(%rip),%rax
mov    (%rax),%edi
lea    0xe20(%rip),%rsi
mov    %ebp,%edx
call   1040 <fprintf@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.nlattr = type { i32 }
@stderr = global i32 0
@.str = [60 x i8] c"[WARN]netlink: %d bytes leftover after parsing attributes.\0A\00"
define i32 @nla_parse(%struct.nlattr** %0, i32 %1, %struct.nlattr* %2, i32 %3) {
%5 = alloca %struct.nlattr**
%6 = alloca i32
%7 = alloca %struct.nlattr*
%8 = alloca i32
%9 = alloca i32
store %struct.nlattr** %0, %struct.nlattr*** %5
store i32 %1, i32* %6
store %struct.nlattr* %2, %struct.nlattr** %7
store i32 %3, i32* %8
%10 = load %struct.nlattr**, %struct.nlattr*** %5
%11 = load i32, i32* %6
%12 = add nsw i32 %11, 1
%13 = sext i32 %12 to i64
%14 = mul i64 8, %13
%15 = trunc i64 %14 to i32
%16 = call i32 @memset(%struct.nlattr** %10, i32 0, i32 %15)
br label %17
17: ; preds = %38, %4
%18 = load %struct.nlattr*, %struct.nlattr** %7
%19 = load i32, i32* %8
%20 = call i64 @nla_ok(%struct.nlattr* %18, i32 %19)
%21 = icmp ne i64 %20, 0
br i1 %21, label %22, label %41
22: ; preds = %17
%23 = load %struct.nlattr*, %struct.nlattr** %7
%24 = call i32 @nla_type(%struct.nlattr* %23)
store i32 %24, i32* %9
%25 = load i32, i32* %9
%26 = icmp sgt i32 %25, 0
br i1 %26, label %27, label %37
27: ; preds = %22
%28 = load i32, i32* %9
%29 = load i32, i32* %6
%30 = icmp sle i32 %28, %29
br i1 %30, label %31, label %37
31: ; preds = %27
%32 = load %struct.nlattr*, %struct.nlattr** %7
%33 = load %struct.nlattr**, %struct.nlattr*** %5
%34 = load i32, i32* %9
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds %struct.nlattr*, %struct.nlattr** %33, i64 %35
store %struct.nlattr* %32, %struct.nlattr** %36
br label %37
37: ; preds = %31, %27, %22
br label %38
38: ; preds = %37
%39 = load %struct.nlattr*, %struct.nlattr** %7
%40 = call %struct.nlattr* @nla_next(%struct.nlattr* %39, i32* %8)
store %struct.nlattr* %40, %struct.nlattr** %7
br label %17
41: ; preds = %17
%42 = load i32, i32* %8
%43 = icmp sgt i32 %42, 0
br i1 %43, label %44, label %48
44: ; preds = %41
%45 = load i32, i32* @stderr
%46 = load i32, i32* %8
%47 = call i32 @fprintf(i32 %45, i8* getelementptr inbounds ([60 x i8], [60 x i8]* @.str, i64 0, i64 0), i32 %46)
br label %48
48: ; preds = %44, %41
ret i32 0
}

LLVM IR (-O2):
%struct.nlattr = type { i32 }
@stderr = global i32 0
@.str = [60 x i8] c"[WARN]netlink: %d bytes leftover after parsing attributes.\0A\00"
define i32 @nla_parse(%struct.nlattr** %0, i32 %1, %struct.nlattr* %2, i32 %3) {
%5 = alloca i32
store i32 %3, i32* %5
%6 = shl i32 %1, 3
%7 = add i32 %6, 8
%8 = tail call i32 @memset(%struct.nlattr** %0, i32 0, i32 %7)
%9 = call i64 @nla_ok(%struct.nlattr* %2, i32 %3)
%10 = icmp eq i64 %9, 0
br i1 %10, label %27, label %11
11: ; preds = %4, %20
%12 = phi %struct.nlattr* [ %21, %20 ], [ %2, %4 ]
%13 = call i32 @nla_type(%struct.nlattr* %12)
%14 = icmp slt i32 %13, 1
%15 = icmp sgt i32 %13, %1
%16 = or i1 %14, %15
br i1 %16, label %20, label %17
17: ; preds = %11
%18 = zext i32 %13 to i64
%19 = getelementptr inbounds %struct.nlattr*, %struct.nlattr** %0, i64 %18
store %struct.nlattr* %12, %struct.nlattr** %19
br label %20
20: ; preds = %17, %11
%21 = call %struct.nlattr* @nla_next(%struct.nlattr* %12, i32* %5)
%22 = load i32, i32* %5
%23 = call i64 @nla_ok(%struct.nlattr* %21, i32 %22)
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %11
25: ; preds = %20
%26 = load i32, i32* %5
br label %27
27: ; preds = %25, %4
%28 = phi i32 [ %26, %25 ], [ %3, %4 ]
%29 = icmp sgt i32 %28, 0
br i1 %29, label %30, label %33
30: ; preds = %27
%31 = load i32, i32* @stderr
%32 = call i32 @fprintf(i32 %31, i8* getelementptr inbounds ([60 x i8], [60 x i8]* @.str, i64 0, i64 0), i32 %28)
br label %33
33: ; preds = %30, %27
ret i32 0
}