#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 */
typedef scalar_t__ uint32_t;
struct rtattr {
  scalar_t__ rta_type;
};
struct nlmsghdr {
  int nlmsg_len;
};
struct nlmsgerr {
  int error;
};

/* Variables and functions */
scalar_t__ NLMSGERR_ATTR_MSG;
scalar_t__ NLMSGERR_ATTR_OFFS;
scalar_t__ NLMSG_DATA(struct nlmsghdr *);
scalar_t__ RTA_DATA(struct rtattr *);
struct rtattr *RTA_NEXT(struct rtattr *, int);
scalar_t__ RTA_OK(struct rtattr *, int);
int /*<<< orphan*/ error(int, int /*<<< orphan*/, char *, int, int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ memcpy(scalar_t__ *, scalar_t__, int);
int /*<<< orphan*/ stderr;
void nl_error(struct nlmsghdr *nh) {
  struct nlmsgerr *err = (struct nlmsgerr *)NLMSG_DATA(nh);
  int len = nh->nlmsg_len - sizeof(*nh);
  uint32_t off;
  if (len < sizeof(struct nlmsgerr))
    error(1, 0, "netlink error message truncated %d min %ld", len,
          sizeof(struct nlmsgerr));
  if (!err->error) {
    struct rtattr *attrs = (struct rtattr *)NLMSG_DATA(nh);
    while (RTA_OK(attrs, len)) {
      if (attrs->rta_type == NLMSGERR_ATTR_MSG)
        fprintf(stderr, "netlink ext ack msg: %s\n", (char *)RTA_DATA(attrs));
      if (attrs->rta_type == NLMSGERR_ATTR_OFFS) {
        memcpy(&off, RTA_DATA(attrs), 4);
        fprintf(stderr, "netlink err off %d\n", (int)off);
      }
      attrs = RTA_NEXT(attrs, len);
    }
  } else {
    fprintf(stderr, "netlink error %d", err->error);
  }
}


x86 assembly (clang, -O0):
<nl_error>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1070 <NLMSG_DATA@plt>
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%rax
movslq (%rax),%rax
sub    $0x4,%rax
mov    %eax,-0x14(%rbp)
movslq -0x14(%rbp),%rax
cmp    $0x4,%rax
jae    11c1 <nl_error+0x51>
mov    -0x14(%rbp),%ecx
mov    $0x1,%edi
xor    %esi,%esi
lea    0xe4a(%rip),%rdx
mov    $0x4,%r8d
call   1080 <error@plt>
mov    -0x10(%rbp),%rax
cmpl   $0x0,(%rax)
jne    1299 <nl_error+0x129>
mov    -0x8(%rbp),%rdi
call   1070 <NLMSG_DATA@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1040 <RTA_OK@plt>
cmp    $0x0,%rax
je     1294 <nl_error+0x124>
mov    -0x28(%rbp),%rax
mov    (%rax),%rax
mov    0x2dc9(%rip),%rcx
cmp    (%rcx),%rax
jne    1231 <nl_error+0xc1>
mov    0x2dd1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x28(%rbp),%rdi
call   1090 <RTA_DATA@plt>
mov    -0x2c(%rbp),%edi
mov    %rax,%rdx
lea    0xe01(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x28(%rbp),%rax
mov    (%rax),%rax
mov    0x2d99(%rip),%rcx
cmp    (%rcx),%rax
jne    127f <nl_error+0x10f>
mov    -0x28(%rbp),%rdi
call   1090 <RTA_DATA@plt>
mov    %rax,%rsi
lea    -0x20(%rbp),%rdi
mov    $0x4,%edx
call   1060 <memcpy@plt>
mov    0x2d77(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rax
mov    %eax,%edx
lea    0xdcc(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x28(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1030 <RTA_NEXT@plt>
mov    %rax,-0x28(%rbp)
jmp    11db <nl_error+0x6b>
jmp    12b6 <nl_error+0x146>
mov    0x2d40(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%edx
lea    0xda9(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<nl_error>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%rbx
call   1070 <NLMSG_DATA@plt>
mov    %rax,%r14
mov    (%rbx),%r13d
add    $0xfffffffc,%r13d
cmp    $0x3,%r13d
ja     11af <nl_error+0x3f>
lea    0xe66(%rip),%rdx
mov    $0x1,%edi
xor    %esi,%esi
mov    %r13d,%ecx
mov    $0x4,%r8d
call   1080 <error@plt>
mov    (%r14),%edx
test   %edx,%edx
je     11db <nl_error+0x6b>
mov    0x2e23(%rip),%rax
mov    (%rax),%edi
lea    0xe92(%rip),%rsi
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <fprintf@plt>
mov    %rbx,%rdi
call   1070 <NLMSG_DATA@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r13d,%esi
call   1040 <RTA_OK@plt>
test   %rax,%rax
je     129b <nl_error+0x12b>
mov    0x2dc7(%rip),%r14
mov    0x2dd8(%rip),%r15
mov    0x2dc9(%rip),%r12
jmp    123e <nl_error+0xce>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %r13d,%esi
call   1030 <RTA_NEXT@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r13d,%esi
call   1040 <RTA_OK@plt>
test   %rax,%rax
je     129b <nl_error+0x12b>
mov    (%rbx),%rax
cmp    (%r14),%rax
jne    1267 <nl_error+0xf7>
mov    (%r15),%ebp
mov    %rbx,%rdi
call   1090 <RTA_DATA@plt>
mov    %ebp,%edi
lea    0xdd1(%rip),%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    (%rbx),%rax
cmp    (%r12),%rax
jne    1220 <nl_error+0xb0>
mov    %rbx,%rdi
call   1090 <RTA_DATA@plt>
mov    %rsp,%rdi
mov    %rax,%rsi
mov    $0x4,%edx
call   1060 <memcpy@plt>
mov    (%r15),%edi
mov    (%rsp),%edx
lea    0xdb2(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    1220 <nl_error+0xb0>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.nlmsghdr = type { i32 }
%struct.nlmsgerr = type { i32 }
%struct.rtattr = type { i64 }
@.str = [43 x i8] c"netlink error message truncated %d min %ld\00"
@NLMSGERR_ATTR_MSG = global i64 0
@stderr = global i32 0
@.str.1 = [25 x i8] c"netlink ext ack msg: %s\0A\00"
@NLMSGERR_ATTR_OFFS = global i64 0
@.str.2 = [20 x i8] c"netlink err off %d\0A\00"
@.str.3 = [17 x i8] c"netlink error %d\00"
define void @nl_error(%struct.nlmsghdr* %0) {
%2 = alloca %struct.nlmsghdr*
%3 = alloca %struct.nlmsgerr*
%4 = alloca i32
%5 = alloca i64
%6 = alloca %struct.rtattr*
store %struct.nlmsghdr* %0, %struct.nlmsghdr** %2
%7 = load %struct.nlmsghdr*, %struct.nlmsghdr** %2
%8 = call i64 @NLMSG_DATA(%struct.nlmsghdr* %7)
%9 = inttoptr i64 %8 to %struct.nlmsgerr*
store %struct.nlmsgerr* %9, %struct.nlmsgerr** %3
%10 = load %struct.nlmsghdr*, %struct.nlmsghdr** %2
%11 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %10, i32 0, i32 0
%12 = load i32, i32* %11
%13 = sext i32 %12 to i64
%14 = sub i64 %13, 4
%15 = trunc i64 %14 to i32
store i32 %15, i32* %4
%16 = load i32, i32* %4
%17 = sext i32 %16 to i64
%18 = icmp ult i64 %17, 4
br i1 %18, label %19, label %22
19: ; preds = %1
%20 = load i32, i32* %4
%21 = call i32 @error(i32 1, i32 0, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0), i32 %20, i32 4)
br label %22
22: ; preds = %19, %1
%23 = load %struct.nlmsgerr*, %struct.nlmsgerr** %3
%24 = getelementptr inbounds %struct.nlmsgerr, %struct.nlmsgerr* %23, i32 0, i32 0
%25 = load i32, i32* %24
%26 = icmp ne i32 %25, 0
br i1 %26, label %67, label %27
27: ; preds = %22
%28 = load %struct.nlmsghdr*, %struct.nlmsghdr** %2
%29 = call i64 @NLMSG_DATA(%struct.nlmsghdr* %28)
%30 = inttoptr i64 %29 to %struct.rtattr*
store %struct.rtattr* %30, %struct.rtattr** %6
br label %31
31: ; preds = %62, %27
%32 = load %struct.rtattr*, %struct.rtattr** %6
%33 = load i32, i32* %4
%34 = call i64 @RTA_OK(%struct.rtattr* %32, i32 %33)
%35 = icmp ne i64 %34, 0
br i1 %35, label %36, label %66
36: ; preds = %31
%37 = load %struct.rtattr*, %struct.rtattr** %6
%38 = getelementptr inbounds %struct.rtattr, %struct.rtattr* %37, i32 0, i32 0
%39 = load i64, i64* %38
%40 = load i64, i64* @NLMSGERR_ATTR_MSG
%41 = icmp eq i64 %39, %40
br i1 %41, label %42, label %48
42: ; preds = %36
%43 = load i32, i32* @stderr
%44 = load %struct.rtattr*, %struct.rtattr** %6
%45 = call i64 @RTA_DATA(%struct.rtattr* %44)
%46 = inttoptr i64 %45 to i8*
%47 = call i32 (i32, i8*, ...) @fprintf(i32 %43, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %46)
br label %48
48: ; preds = %42, %36
%49 = load %struct.rtattr*, %struct.rtattr** %6
%50 = getelementptr inbounds %struct.rtattr, %struct.rtattr* %49, i32 0, i32 0
%51 = load i64, i64* %50
%52 = load i64, i64* @NLMSGERR_ATTR_OFFS
%53 = icmp eq i64 %51, %52
br i1 %53, label %54, label %62
54: ; preds = %48
%55 = load %struct.rtattr*, %struct.rtattr** %6
%56 = call i64 @RTA_DATA(%struct.rtattr* %55)
%57 = call i32 @memcpy(i64* %5, i64 %56, i32 4)
%58 = load i32, i32* @stderr
%59 = load i64, i64* %5
%60 = trunc i64 %59 to i32
%61 = call i32 (i32, i8*, ...) @fprintf(i32 %58, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i32 %60)
br label %62
62: ; preds = %54, %48
%63 = load %struct.rtattr*, %struct.rtattr** %6
%64 = load i32, i32* %4
%65 = call %struct.rtattr* @RTA_NEXT(%struct.rtattr* %63, i32 %64)
store %struct.rtattr* %65, %struct.rtattr** %6
br label %31
66: ; preds = %31
br label %73
67: ; preds = %22
%68 = load i32, i32* @stderr
%69 = load %struct.nlmsgerr*, %struct.nlmsgerr** %3
%70 = getelementptr inbounds %struct.nlmsgerr, %struct.nlmsgerr* %69, i32 0, i32 0
%71 = load i32, i32* %70
%72 = call i32 (i32, i8*, ...) @fprintf(i32 %68, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i32 %71)
br label %73
73: ; preds = %67, %66
ret void
}

LLVM IR (-O2):
%struct.nlmsghdr = type { i32 }
%struct.nlmsgerr = type { i32 }
%struct.rtattr = type { i64 }
@.str = [43 x i8] c"netlink error message truncated %d min %ld\00"
@NLMSGERR_ATTR_MSG = global i64 0
@stderr = global i32 0
@.str.1 = [25 x i8] c"netlink ext ack msg: %s\0A\00"
@NLMSGERR_ATTR_OFFS = global i64 0
@.str.2 = [20 x i8] c"netlink err off %d\0A\00"
@.str.3 = [17 x i8] c"netlink error %d\00"
define void @nl_error(%struct.nlmsghdr* %0) {
%2 = alloca i64
%3 = tail call i64 @NLMSG_DATA(%struct.nlmsghdr* %0)
%4 = inttoptr i64 %3 to %struct.nlmsgerr*
%5 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %0, i64 0, i32 0
%6 = load i32, i32* %5
%7 = add i32 %6, -4
%8 = bitcast i64* %2 to i8*
%9 = icmp ult i32 %7, 4
br i1 %9, label %10, label %12
10: ; preds = %1
%11 = tail call i32 @error(i32 1, i32 0, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0), i32 %7, i32 4)
br label %12
12: ; preds = %10, %1
%13 = getelementptr inbounds %struct.nlmsgerr, %struct.nlmsgerr* %4, i64 0, i32 0
%14 = load i32, i32* %13
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %48
16: ; preds = %12
%17 = tail call i64 @NLMSG_DATA(%struct.nlmsghdr* %0)
%18 = inttoptr i64 %17 to %struct.rtattr*
%19 = call i64 @RTA_OK(%struct.rtattr* %18, i32 %7)
%20 = icmp eq i64 %19, 0
br i1 %20, label %51, label %21
21: ; preds = %16, %44
%22 = phi %struct.rtattr* [ %45, %44 ], [ %18, %16 ]
%23 = getelementptr inbounds %struct.rtattr, %struct.rtattr* %22, i64 0, i32 0
%24 = load i64, i64* %23
%25 = load i64, i64* @NLMSGERR_ATTR_MSG
%26 = icmp eq i64 %24, %25
br i1 %26, label %27, label %33
27: ; preds = %21
%28 = load i32, i32* @stderr
%29 = call i64 @RTA_DATA(%struct.rtattr* %22)
%30 = inttoptr i64 %29 to i8*
%31 = call i32 (i32, i8*, ...) @fprintf(i32 %28, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %30)
%32 = load i64, i64* %23
br label %33
33: ; preds = %27, %21
%34 = phi i64 [ %32, %27 ], [ %24, %21 ]
%35 = load i64, i64* @NLMSGERR_ATTR_OFFS
%36 = icmp eq i64 %34, %35
br i1 %36, label %37, label %44
37: ; preds = %33
%38 = call i64 @RTA_DATA(%struct.rtattr* %22)
%39 = call i32 @memcpy(i64* %2, i64 %38, i32 4)
%40 = load i32, i32* @stderr
%41 = load i64, i64* %2
%42 = trunc i64 %41 to i32
%43 = call i32 (i32, i8*, ...) @fprintf(i32 %40, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i32 %42)
br label %44
44: ; preds = %37, %33
%45 = call %struct.rtattr* @RTA_NEXT(%struct.rtattr* %22, i32 %7)
%46 = call i64 @RTA_OK(%struct.rtattr* %45, i32 %7)
%47 = icmp eq i64 %46, 0
br i1 %47, label %51, label %21
48: ; preds = %12
%49 = load i32, i32* @stderr
%50 = tail call i32 (i32, i8*, ...) @fprintf(i32 %49, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i32 %14)
br label %51
51: ; preds = %44, %16, %48
ret void
}