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

/* 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 validate_nla(struct nlattr *, int, struct nla_policy *);
int nla_parse(struct nlattr *tb[], int maxtype, struct nlattr *head, int len,
              struct nla_policy *policy) {
  struct nlattr *nla;
  int rem, err;
  memset(tb, 0, sizeof(struct nlattr *) * (maxtype + 1));
  for (nla = head, rem = len; nla_ok(nla, rem); nla = nla_next(nla, &(rem))) {
    int type = nla_type(nla);
    if (type > maxtype)
      continue;
    if (policy) {
      err = validate_nla(nla, maxtype, policy);
      if (err < 0)
        goto errout;
    }
    if (tb[type])
      fprintf(stderr,
              "Attribute of type %#x found multiple times in message, "
              "previous attribute is being ignored.\n",
              type);
    tb[type] = nla;
  }
  err = 0;
errout:
  return err;
}


x86 assembly (clang, -O0):
<nla_parse>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    %r8,-0x28(%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),%rax
mov    %rax,-0x30(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x34(%rbp),%esi
call   1050 <nla_ok@plt>
cmp    $0x0,%rax
je     1257 <nla_parse+0xf7>
mov    -0x30(%rbp),%rdi
call   1060 <nla_type@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
cmp    -0xc(%rbp),%eax
jle    11d4 <nla_parse+0x74>
jmp    1241 <nla_parse+0xe1>
cmpq   $0x0,-0x28(%rbp)
je     1206 <nla_parse+0xa6>
mov    -0x30(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x28(%rbp),%rdx
call   1070 <validate_nla@plt>
mov    %eax,-0x38(%rbp)
cmpl   $0x0,-0x38(%rbp)
jge    1201 <nla_parse+0xa1>
jmp    125e <nla_parse+0xfe>
jmp    1206 <nla_parse+0xa6>
mov    -0x8(%rbp),%rax
movslq -0x3c(%rbp),%rcx
cmpq   $0x0,(%rax,%rcx,8)
je     1231 <nla_parse+0xd1>
mov    0x2dc0(%rip),%rax
mov    (%rax),%edi
mov    -0x3c(%rbp),%edx
lea    0xdd4(%rip),%rsi
call   1040 <fprintf@plt>
mov    -0x30(%rbp),%rdx
mov    -0x8(%rbp),%rax
movslq -0x3c(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x30(%rbp),%rdi
lea    -0x34(%rbp),%rsi
call   1080 <nla_next@plt>
mov    %rax,-0x30(%rbp)
jmp    11a1 <nla_parse+0x41>
movl   $0x0,-0x38(%rbp)
mov    -0x38(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<nla_parse>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %r8,%r12
mov    %ecx,%r14d
mov    %rdx,%rbx
mov    %esi,%r15d
lea    0x8(,%r15,8),%edx
mov    %rdi,0x10(%rsp)
xor    %esi,%esi
call   1030 <memset@plt>
mov    %r14d,0xc(%rsp)
mov    %rbx,%rdi
mov    %r14d,%esi
call   1050 <nla_ok@plt>
test   %rax,%rax
je     1228 <nla_parse+0xc8>
lea    0xc(%rsp),%rbp
jmp    11d8 <nla_parse+0x78>
xchg   %ax,%ax
mov    0x10(%rsp),%rax
mov    %rbx,(%rax,%r13,8)
mov    %rbx,%rdi
mov    %rbp,%rsi
call   1080 <nla_next@plt>
mov    %rax,%rbx
mov    0xc(%rsp),%esi
mov    %rax,%rdi
call   1050 <nla_ok@plt>
test   %rax,%rax
je     1228 <nla_parse+0xc8>
mov    %rbx,%rdi
call   1060 <nla_type@plt>
cmp    %r15d,%eax
jg     11b9 <nla_parse+0x59>
mov    %eax,%r14d
test   %r12,%r12
je     11ff <nla_parse+0x9f>
mov    %rbx,%rdi
mov    %r15d,%esi
mov    %r12,%rdx
call   1070 <validate_nla@plt>
test   %eax,%eax
js     122a <nla_parse+0xca>
movslq %r14d,%r13
mov    0x10(%rsp),%rax
cmpq   $0x0,(%rax,%r13,8)
je     11b0 <nla_parse+0x50>
mov    0x2dcb(%rip),%rax
mov    (%rax),%edi
lea    0xde2(%rip),%rsi
mov    %r14d,%edx
call   1040 <fprintf@plt>
jmp    11b0 <nla_parse+0x50>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.nlattr = type { i32 }
%struct.nla_policy = type { i32 }
@stderr = global i32 0
@.str = [93 x i8] c"Attribute of type %#x found multiple times in message, previous attribute is being ignored.\0A\00"
define i32 @nla_parse(%struct.nlattr** %0, i32 %1, %struct.nlattr* %2, i32 %3, %struct.nla_policy* %4) {
%6 = alloca %struct.nlattr**
%7 = alloca i32
%8 = alloca %struct.nlattr*
%9 = alloca i32
%10 = alloca %struct.nla_policy*
%11 = alloca %struct.nlattr*
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
store %struct.nlattr** %0, %struct.nlattr*** %6
store i32 %1, i32* %7
store %struct.nlattr* %2, %struct.nlattr** %8
store i32 %3, i32* %9
store %struct.nla_policy* %4, %struct.nla_policy** %10
%15 = load %struct.nlattr**, %struct.nlattr*** %6
%16 = load i32, i32* %7
%17 = add nsw i32 %16, 1
%18 = sext i32 %17 to i64
%19 = mul i64 8, %18
%20 = trunc i64 %19 to i32
%21 = call i32 @memset(%struct.nlattr** %15, i32 0, i32 %20)
%22 = load %struct.nlattr*, %struct.nlattr** %8
store %struct.nlattr* %22, %struct.nlattr** %11
%23 = load i32, i32* %9
store i32 %23, i32* %12
br label %24
24: ; preds = %65, %5
%25 = load %struct.nlattr*, %struct.nlattr** %11
%26 = load i32, i32* %12
%27 = call i64 @nla_ok(%struct.nlattr* %25, i32 %26)
%28 = icmp ne i64 %27, 0
br i1 %28, label %29, label %68
29: ; preds = %24
%30 = load %struct.nlattr*, %struct.nlattr** %11
%31 = call i32 @nla_type(%struct.nlattr* %30)
store i32 %31, i32* %14
%32 = load i32, i32* %14
%33 = load i32, i32* %7
%34 = icmp sgt i32 %32, %33
br i1 %34, label %35, label %36
35: ; preds = %29
br label %65
36: ; preds = %29
%37 = load %struct.nla_policy*, %struct.nla_policy** %10
%38 = icmp ne %struct.nla_policy* %37, null
br i1 %38, label %39, label %48
39: ; preds = %36
%40 = load %struct.nlattr*, %struct.nlattr** %11
%41 = load i32, i32* %7
%42 = load %struct.nla_policy*, %struct.nla_policy** %10
%43 = call i32 @validate_nla(%struct.nlattr* %40, i32 %41, %struct.nla_policy* %42)
store i32 %43, i32* %13
%44 = load i32, i32* %13
%45 = icmp slt i32 %44, 0
br i1 %45, label %46, label %47
46: ; preds = %39
br label %69
47: ; preds = %39
br label %48
48: ; preds = %47, %36
%49 = load %struct.nlattr**, %struct.nlattr*** %6
%50 = load i32, i32* %14
%51 = sext i32 %50 to i64
%52 = getelementptr inbounds %struct.nlattr*, %struct.nlattr** %49, i64 %51
%53 = load %struct.nlattr*, %struct.nlattr** %52
%54 = icmp ne %struct.nlattr* %53, null
br i1 %54, label %55, label %59
55: ; preds = %48
%56 = load i32, i32* @stderr
%57 = load i32, i32* %14
%58 = call i32 @fprintf(i32 %56, i8* getelementptr inbounds ([93 x i8], [93 x i8]* @.str, i64 0, i64 0), i32 %57)
br label %59
59: ; preds = %55, %48
%60 = load %struct.nlattr*, %struct.nlattr** %11
%61 = load %struct.nlattr**, %struct.nlattr*** %6
%62 = load i32, i32* %14
%63 = sext i32 %62 to i64
%64 = getelementptr inbounds %struct.nlattr*, %struct.nlattr** %61, i64 %63
store %struct.nlattr* %60, %struct.nlattr** %64
br label %65
65: ; preds = %59, %35
%66 = load %struct.nlattr*, %struct.nlattr** %11
%67 = call %struct.nlattr* @nla_next(%struct.nlattr* %66, i32* %12)
store %struct.nlattr* %67, %struct.nlattr** %11
br label %24
68: ; preds = %24
store i32 0, i32* %13
br label %69
69: ; preds = %68, %46
%70 = load i32, i32* %13
ret i32 %70
}

LLVM IR (-O2):
%struct.nlattr = type { i32 }
%struct.nla_policy = type { i32 }
@stderr = global i32 0
@.str = [93 x i8] c"Attribute of type %#x found multiple times in message, previous attribute is being ignored.\0A\00"
define i32 @nla_parse(%struct.nlattr** %0, i32 %1, %struct.nlattr* %2, i32 %3, %struct.nla_policy* %4) {
%6 = alloca i32
%7 = bitcast i32* %6 to i8*
%8 = shl i32 %1, 3
%9 = add i32 %8, 8
%10 = tail call i32 @memset(%struct.nlattr** %0, i32 0, i32 %9)
store i32 %3, i32* %6
%11 = call i64 @nla_ok(%struct.nlattr* %2, i32 %3)
%12 = icmp eq i64 %11, 0
br i1 %12, label %37, label %13
13: ; preds = %5
%14 = icmp eq %struct.nla_policy* %4, null
br label %15
15: ; preds = %13, %32
%16 = phi %struct.nlattr* [ %2, %13 ], [ %33, %32 ]
%17 = call i32 @nla_type(%struct.nlattr* %16)
%18 = icmp sgt i32 %17, %1
br i1 %18, label %32, label %19
19: ; preds = %15
br i1 %14, label %23, label %20
20: ; preds = %19
%21 = call i32 @validate_nla(%struct.nlattr* %16, i32 %1, %struct.nla_policy* %4)
%22 = icmp slt i32 %21, 0
br i1 %22, label %37, label %23
23: ; preds = %20, %19
%24 = sext i32 %17 to i64
%25 = getelementptr inbounds %struct.nlattr*, %struct.nlattr** %0, i64 %24
%26 = load %struct.nlattr*, %struct.nlattr** %25
%27 = icmp eq %struct.nlattr* %26, null
br i1 %27, label %31, label %28
28: ; preds = %23
%29 = load i32, i32* @stderr
%30 = call i32 @fprintf(i32 %29, i8* getelementptr inbounds ([93 x i8], [93 x i8]* @.str, i64 0, i64 0), i32 %17)
br label %31
31: ; preds = %28, %23
store %struct.nlattr* %16, %struct.nlattr** %25
br label %32
32: ; preds = %31, %15
%33 = call %struct.nlattr* @nla_next(%struct.nlattr* %16, i32* %6)
%34 = load i32, i32* %6
%35 = call i64 @nla_ok(%struct.nlattr* %33, i32 %34)
%36 = icmp eq i64 %35, 0
br i1 %36, label %37, label %15
37: ; preds = %32, %20, %5
%38 = phi i32 [ 0, %5 ], [ %21, %20 ], [ 0, %32 ]
ret i32 %38
}