#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 {
  size_t nlmsg_len;
  scalar_t__ nlmsg_type;
};
struct nlmsgerr {
  int error;
};

/* Variables and functions */
scalar_t__ NLMSG_DATA(struct nlmsghdr *);
scalar_t__ NLMSG_ERROR;
int /*<<< orphan*/ NLMSG_OK(struct nlmsghdr *, size_t);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ stderr;
char *strerror(int);
int netlink_qualify(struct nlmsghdr *n, size_t length) {
  if (!NLMSG_OK(n, length) || length < sizeof(struct nlmsghdr) ||
      length < n->nlmsg_len) {
    fprintf(stderr, "netlink: packet too small or truncated. %u!=%u!=%u",
            length, sizeof(struct nlmsghdr), n->nlmsg_len);
    return -1;
  }
  if (n->nlmsg_type == NLMSG_ERROR) {
    struct nlmsgerr *e = (struct nlmsgerr *)NLMSG_DATA(n);
    if (e->error) {
      fprintf(stderr, "netlink error: %s\n", strerror(-e->error));
    }
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<netlink_qualify>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1030 <NLMSG_OK@plt>
cmp    $0x0,%eax
je     1182 <netlink_qualify+0x42>
cmpq   $0x10,-0x18(%rbp)
jb     1182 <netlink_qualify+0x42>
mov    -0x18(%rbp),%rax
mov    -0x10(%rbp),%rcx
cmp    (%rcx),%rax
jae    11b5 <netlink_qualify+0x75>
mov    0x2e4f(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
mov    -0x10(%rbp),%rax
mov    (%rax),%r8
lea    0xe63(%rip),%rsi
mov    $0x10,%ecx
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1227 <netlink_qualify+0xe7>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    0x2e24(%rip),%rcx
cmp    (%rcx),%rax
jne    1220 <netlink_qualify+0xe0>
mov    -0x10(%rbp),%rdi
call   1050 <NLMSG_DATA@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmpl   $0x0,(%rax)
je     1214 <netlink_qualify+0xd4>
mov    0x2dea(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%rax
xor    %edi,%edi
sub    (%rax),%edi
call   1060 <strerror@plt>
mov    -0x24(%rbp),%edi
mov    %rax,%rdx
lea    0xe26(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1227 <netlink_qualify+0xe7>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<netlink_qualify>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%rbx
mov    %rdi,%r14
call   1030 <NLMSG_OK@plt>
mov    (%r14),%r8
cmp    $0x10,%rbx
jb     11ab <netlink_qualify+0x6b>
test   %eax,%eax
je     11ab <netlink_qualify+0x6b>
cmp    %rbx,%r8
ja     11ab <netlink_qualify+0x6b>
mov    0x8(%r14),%rax
mov    0x2e7c(%rip),%rcx
xor    %ebx,%ebx
cmp    (%rcx),%rax
jne    11cf <netlink_qualify+0x8f>
mov    %r14,%rdi
call   1050 <NLMSG_DATA@plt>
mov    (%rax),%edi
mov    $0xffffffff,%ebx
test   %edi,%edi
je     11cf <netlink_qualify+0x8f>
mov    0x2e4b(%rip),%rax
mov    (%rax),%ebp
neg    %edi
call   1060 <strerror@plt>
lea    0xe96(%rip),%rsi
mov    %ebp,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1040 <fprintf@plt>
jmp    11cf <netlink_qualify+0x8f>
mov    0x2e26(%rip),%rax
mov    (%rax),%edi
lea    0xe45(%rip),%rsi
mov    $0x10,%ecx
mov    %rbx,%rdx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.nlmsghdr = type { i64, i64 }
%struct.nlmsgerr = type { i32 }
@stderr = global i32 0
@.str = [51 x i8] c"netlink: packet too small or truncated. %u!=%u!=%u\00"
@NLMSG_ERROR = global i64 0
@.str.1 = [19 x i8] c"netlink error: %s\0A\00"
define i32 @netlink_qualify(%struct.nlmsghdr* %0, i64 %1) {
%3 = alloca i32
%4 = alloca %struct.nlmsghdr*
%5 = alloca i64
%6 = alloca %struct.nlmsgerr*
store %struct.nlmsghdr* %0, %struct.nlmsghdr** %4
store i64 %1, i64* %5
%7 = load %struct.nlmsghdr*, %struct.nlmsghdr** %4
%8 = load i64, i64* %5
%9 = call i32 @NLMSG_OK(%struct.nlmsghdr* %7, i64 %8)
%10 = icmp ne i32 %9, 0
br i1 %10, label %11, label %20
11: ; preds = %2
%12 = load i64, i64* %5
%13 = icmp ult i64 %12, 16
br i1 %13, label %20, label %14
14: ; preds = %11
%15 = load i64, i64* %5
%16 = load %struct.nlmsghdr*, %struct.nlmsghdr** %4
%17 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %16, i32 0, i32 0
%18 = load i64, i64* %17
%19 = icmp ult i64 %15, %18
br i1 %19, label %20, label %27
20: ; preds = %14, %11, %2
%21 = load i32, i32* @stderr
%22 = load i64, i64* %5
%23 = load %struct.nlmsghdr*, %struct.nlmsghdr** %4
%24 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %23, i32 0, i32 0
%25 = load i64, i64* %24
%26 = call i32 (i32, i8*, ...) @fprintf(i32 %21, i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str, i64 0, i64 0), i64 %22, i64 16, i64 %25)
store i32 -1, i32* %3
br label %51
27: ; preds = %14
%28 = load %struct.nlmsghdr*, %struct.nlmsghdr** %4
%29 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %28, i32 0, i32 1
%30 = load i64, i64* %29
%31 = load i64, i64* @NLMSG_ERROR
%32 = icmp eq i64 %30, %31
br i1 %32, label %33, label %50
33: ; preds = %27
%34 = load %struct.nlmsghdr*, %struct.nlmsghdr** %4
%35 = call i64 @NLMSG_DATA(%struct.nlmsghdr* %34)
%36 = inttoptr i64 %35 to %struct.nlmsgerr*
store %struct.nlmsgerr* %36, %struct.nlmsgerr** %6
%37 = load %struct.nlmsgerr*, %struct.nlmsgerr** %6
%38 = getelementptr inbounds %struct.nlmsgerr, %struct.nlmsgerr* %37, i32 0, i32 0
%39 = load i32, i32* %38
%40 = icmp ne i32 %39, 0
br i1 %40, label %41, label %49
41: ; preds = %33
%42 = load i32, i32* @stderr
%43 = load %struct.nlmsgerr*, %struct.nlmsgerr** %6
%44 = getelementptr inbounds %struct.nlmsgerr, %struct.nlmsgerr* %43, i32 0, i32 0
%45 = load i32, i32* %44
%46 = sub nsw i32 0, %45
%47 = call i8* @strerror(i32 %46)
%48 = call i32 (i32, i8*, ...) @fprintf(i32 %42, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %47)
br label %49
49: ; preds = %41, %33
store i32 -1, i32* %3
br label %51
50: ; preds = %27
store i32 0, i32* %3
br label %51
51: ; preds = %50, %49, %20
%52 = load i32, i32* %3
ret i32 %52
}

LLVM IR (-O2):
%struct.nlmsghdr = type { i64, i64 }
%struct.nlmsgerr = type { i32 }
@stderr = global i32 0
@.str = [51 x i8] c"netlink: packet too small or truncated. %u!=%u!=%u\00"
@NLMSG_ERROR = global i64 0
@.str.1 = [19 x i8] c"netlink error: %s\0A\00"
define i32 @netlink_qualify(%struct.nlmsghdr* %0, i64 %1) {
%3 = tail call i32 @NLMSG_OK(%struct.nlmsghdr* %0, i64 %1)
%4 = icmp eq i32 %3, 0
%5 = icmp ult i64 %1, 16
%6 = or i1 %5, %4
%7 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %0, i64 0, i32 0
%8 = load i64, i64* %7
%9 = icmp ugt i64 %8, %1
%10 = select i1 %6, i1 true, i1 %9
br i1 %10, label %11, label %14
11: ; preds = %2
%12 = load i32, i32* @stderr
%13 = tail call i32 (i32, i8*, ...) @fprintf(i32 %12, i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str, i64 0, i64 0), i64 %1, i64 16, i64 %8)
br label %30
14: ; preds = %2
%15 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %0, i64 0, i32 1
%16 = load i64, i64* %15
%17 = load i64, i64* @NLMSG_ERROR
%18 = icmp eq i64 %16, %17
br i1 %18, label %19, label %30
19: ; preds = %14
%20 = tail call i64 @NLMSG_DATA(%struct.nlmsghdr* %0)
%21 = inttoptr i64 %20 to %struct.nlmsgerr*
%22 = getelementptr inbounds %struct.nlmsgerr, %struct.nlmsgerr* %21, i64 0, i32 0
%23 = load i32, i32* %22
%24 = icmp eq i32 %23, 0
br i1 %24, label %30, label %25
25: ; preds = %19
%26 = load i32, i32* @stderr
%27 = sub nsw i32 0, %23
%28 = tail call i8* @strerror(i32 %27)
%29 = tail call i32 (i32, i8*, ...) @fprintf(i32 %26, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %28)
br label %30
30: ; preds = %14, %19, %25, %11
%31 = phi i32 [ -1, %11 ], [ -1, %25 ], [ -1, %19 ], [ 0, %14 ]
ret i32 %31
}