#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 nlmsghdr {
  scalar_t__ nlmsg_len;
};
struct nla_policy {
  int dummy;
};
typedef scalar_t__ __u32;

/* Variables and functions */
int EINVAL;
int nla_validate(int /*<<< orphan*/, int /*<<< orphan*/, int,
                 struct nla_policy const *);
int /*<<< orphan*/ nlmsg_attrdata(struct nlmsghdr *, int);
int /*<<< orphan*/ nlmsg_attrlen(struct nlmsghdr *, int);
scalar_t__ nlmsg_msg_size(int);
int nlmsg_validate(struct nlmsghdr *nlh, int hdrlen, int maxtype,
                   const struct nla_policy *policy) {
  if (nlh->nlmsg_len < (__u32)nlmsg_msg_size(hdrlen))
    return -EINVAL;
  return nla_validate(nlmsg_attrdata(nlh, hdrlen), nlmsg_attrlen(nlh, hdrlen),
                      maxtype, policy);
}


x86 assembly (clang, -O0):
<nlmsg_validate>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
mov    -0x14(%rbp),%edi
call   1050 <nlmsg_msg_size@plt>
mov    %rax,%rcx
mov    -0x28(%rbp),%rax
cmp    %rcx,%rax
jge    118c <nlmsg_validate+0x4c>
xor    %eax,%eax
mov    0x2e5e(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    11bb <nlmsg_validate+0x7b>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1030 <nlmsg_attrdata@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1040 <nlmsg_attrlen@plt>
mov    -0x2c(%rbp),%edi
mov    %eax,%esi
mov    -0x18(%rbp),%edx
mov    -0x20(%rbp),%rcx
call   1060 <nla_validate@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<nlmsg_validate>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rcx,%r14
mov    %edx,%r15d
mov    %esi,%ebx
mov    %rdi,%rbp
mov    (%rdi),%r12
mov    %esi,%edi
call   1050 <nlmsg_msg_size@plt>
cmp    %rax,%r12
jge    1176 <nlmsg_validate+0x36>
mov    0x2e77(%rip),%rcx
xor    %eax,%eax
sub    (%rcx),%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %rbp,%rdi
mov    %ebx,%esi
call   1030 <nlmsg_attrdata@plt>
mov    %eax,%r12d
mov    %rbp,%rdi
mov    %ebx,%esi
call   1040 <nlmsg_attrlen@plt>
mov    %r12d,%edi
mov    %eax,%esi
mov    %r15d,%edx
mov    %r14,%rcx
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1060 <nla_validate@plt>

LLVM IR (-O0):
%struct.nlmsghdr = type { i64 }
%struct.nla_policy = type { i32 }
@EINVAL = global i32 0
define i32 @nlmsg_validate(%struct.nlmsghdr* %0, i32 %1, i32 %2, %struct.nla_policy* %3) {
%5 = alloca i32
%6 = alloca %struct.nlmsghdr*
%7 = alloca i32
%8 = alloca i32
%9 = alloca %struct.nla_policy*
store %struct.nlmsghdr* %0, %struct.nlmsghdr** %6
store i32 %1, i32* %7
store i32 %2, i32* %8
store %struct.nla_policy* %3, %struct.nla_policy** %9
%10 = load %struct.nlmsghdr*, %struct.nlmsghdr** %6
%11 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %10, i32 0, i32 0
%12 = load i64, i64* %11
%13 = load i32, i32* %7
%14 = call i64 @nlmsg_msg_size(i32 %13)
%15 = icmp slt i64 %12, %14
br i1 %15, label %16, label %19
16: ; preds = %4
%17 = load i32, i32* @EINVAL
%18 = sub nsw i32 0, %17
store i32 %18, i32* %5
br label %29
19: ; preds = %4
%20 = load %struct.nlmsghdr*, %struct.nlmsghdr** %6
%21 = load i32, i32* %7
%22 = call i32 @nlmsg_attrdata(%struct.nlmsghdr* %20, i32 %21)
%23 = load %struct.nlmsghdr*, %struct.nlmsghdr** %6
%24 = load i32, i32* %7
%25 = call i32 @nlmsg_attrlen(%struct.nlmsghdr* %23, i32 %24)
%26 = load i32, i32* %8
%27 = load %struct.nla_policy*, %struct.nla_policy** %9
%28 = call i32 @nla_validate(i32 %22, i32 %25, i32 %26, %struct.nla_policy* %27)
store i32 %28, i32* %5
br label %29
29: ; preds = %19, %16
%30 = load i32, i32* %5
ret i32 %30
}

LLVM IR (-O2):
%struct.nlmsghdr = type { i64 }
%struct.nla_policy = type { i32 }
@EINVAL = global i32 0
define i32 @nlmsg_validate(%struct.nlmsghdr* %0, i32 %1, i32 %2, %struct.nla_policy* %3) {
%5 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %0, i64 0, i32 0
%6 = load i64, i64* %5
%7 = tail call i64 @nlmsg_msg_size(i32 %1)
%8 = icmp slt i64 %6, %7
br i1 %8, label %9, label %12
9: ; preds = %4
%10 = load i32, i32* @EINVAL
%11 = sub nsw i32 0, %10
br label %16
12: ; preds = %4
%13 = tail call i32 @nlmsg_attrdata(%struct.nlmsghdr* %0, i32 %1)
%14 = tail call i32 @nlmsg_attrlen(%struct.nlmsghdr* %0, i32 %1)
%15 = tail call i32 @nla_validate(i32 %13, i32 %14, i32 %2, %struct.nla_policy* %3)
br label %16
16: ; preds = %12, %9
%17 = phi i32 [ %11, %9 ], [ %15, %12 ]
ret i32 %17
}