#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 */
struct nlattr *nla_next(struct nlattr *, int *);
scalar_t__ nla_ok(struct nlattr *, int);
int validate_nla(struct nlattr *, int, struct nla_policy const *);
int nla_validate(struct nlattr *head, int len, int maxtype,
                 const struct nla_policy *policy) {
  struct nlattr *nla;
  int rem, err;
  for (nla = head, rem = len; nla_ok(nla, rem); nla = nla_next(nla, &(rem))) {
    err = validate_nla(nla, maxtype, policy);
    if (err < 0)
      goto errout;
  }
  err = 0;
errout:
  return err;
}


x86 assembly (clang, -O0):
<nla_validate>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %rcx,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x24(%rbp),%esi
call   1030 <nla_ok@plt>
cmp    $0x0,%rax
je     11a7 <nla_validate+0x77>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%esi
mov    -0x18(%rbp),%rdx
call   1040 <validate_nla@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jge    118c <nla_validate+0x5c>
jmp    11ae <nla_validate+0x7e>
jmp    1191 <nla_validate+0x61>
mov    -0x20(%rbp),%rdi
lea    -0x24(%rbp),%rsi
call   1050 <nla_next@plt>
mov    %rax,-0x20(%rbp)
jmp    1154 <nla_validate+0x24>
movl   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<nla_validate>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rcx,%r15
mov    %edx,%ebp
mov    %rdi,%rbx
mov    %esi,0xc(%rsp)
call   1030 <nla_ok@plt>
xor    %r14d,%r14d
test   %rax,%rax
je     1195 <nla_validate+0x65>
lea    0xc(%rsp),%r12
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %ebp,%esi
mov    %r15,%rdx
call   1040 <validate_nla@plt>
test   %eax,%eax
js     1192 <nla_validate+0x62>
mov    %rbx,%rdi
mov    %r12,%rsi
call   1050 <nla_next@plt>
mov    %rax,%rbx
mov    0xc(%rsp),%esi
mov    %rax,%rdi
call   1030 <nla_ok@plt>
test   %rax,%rax
jne    1160 <nla_validate+0x30>
jmp    1195 <nla_validate+0x65>
mov    %eax,%r14d
mov    %r14d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.nlattr = type { i32 }
%struct.nla_policy = type { i32 }
define i32 @nla_validate(%struct.nlattr* %0, i32 %1, i32 %2, %struct.nla_policy* %3) {
%5 = alloca %struct.nlattr*
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.nla_policy*
%9 = alloca %struct.nlattr*
%10 = alloca i32
%11 = alloca i32
store %struct.nlattr* %0, %struct.nlattr** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store %struct.nla_policy* %3, %struct.nla_policy** %8
%12 = load %struct.nlattr*, %struct.nlattr** %5
store %struct.nlattr* %12, %struct.nlattr** %9
%13 = load i32, i32* %6
store i32 %13, i32* %10
br label %14
14: ; preds = %28, %4
%15 = load %struct.nlattr*, %struct.nlattr** %9
%16 = load i32, i32* %10
%17 = call i64 @nla_ok(%struct.nlattr* %15, i32 %16)
%18 = icmp ne i64 %17, 0
br i1 %18, label %19, label %31
19: ; preds = %14
%20 = load %struct.nlattr*, %struct.nlattr** %9
%21 = load i32, i32* %7
%22 = load %struct.nla_policy*, %struct.nla_policy** %8
%23 = call i32 @validate_nla(%struct.nlattr* %20, i32 %21, %struct.nla_policy* %22)
store i32 %23, i32* %11
%24 = load i32, i32* %11
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %27
26: ; preds = %19
br label %32
27: ; preds = %19
br label %28
28: ; preds = %27
%29 = load %struct.nlattr*, %struct.nlattr** %9
%30 = call %struct.nlattr* @nla_next(%struct.nlattr* %29, i32* %10)
store %struct.nlattr* %30, %struct.nlattr** %9
br label %14
31: ; preds = %14
store i32 0, i32* %11
br label %32
32: ; preds = %31, %26
%33 = load i32, i32* %11
ret i32 %33
}

LLVM IR (-O2):
%struct.nlattr = type { i32 }
%struct.nla_policy = type { i32 }
define i32 @nla_validate(%struct.nlattr* %0, i32 %1, i32 %2, %struct.nla_policy* %3) {
%5 = alloca i32
%6 = bitcast i32* %5 to i8*
store i32 %1, i32* %5
%7 = call i64 @nla_ok(%struct.nlattr* %0, i32 %1)
%8 = icmp eq i64 %7, 0
br i1 %8, label %18, label %9
9: ; preds = %4, %13
%10 = phi %struct.nlattr* [ %14, %13 ], [ %0, %4 ]
%11 = call i32 @validate_nla(%struct.nlattr* %10, i32 %2, %struct.nla_policy* %3)
%12 = icmp slt i32 %11, 0
br i1 %12, label %18, label %13
13: ; preds = %9
%14 = call %struct.nlattr* @nla_next(%struct.nlattr* %10, i32* %5)
%15 = load i32, i32* %5
%16 = call i64 @nla_ok(%struct.nlattr* %14, i32 %15)
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %9
18: ; preds = %9, %13, %4
%19 = phi i32 [ 0, %4 ], [ 0, %13 ], [ %11, %9 ]
ret i32 %19
}