#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 nlmsgerr {
  int error;
};

/* Variables and functions */
int EBADMSG;
scalar_t__ NLMSG_DATA(struct nlmsghdr const *);
scalar_t__ NLMSG_HDRLEN;
int nlmsg_get_error(const struct nlmsghdr *nlh) {
  const struct nlmsgerr *err = (const struct nlmsgerr *)NLMSG_DATA(nlh);
  if (nlh->nlmsg_len < sizeof(struct nlmsgerr) + NLMSG_HDRLEN)
    return EBADMSG;
  return -err->error;
}


x86 assembly (clang, -O0):
<nlmsg_get_error>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <NLMSG_DATA@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    0x2eb9(%rip),%rcx
mov    (%rcx),%rcx
add    $0x4,%rcx
cmp    %rcx,%rax
jae    1158 <nlmsg_get_error+0x48>
mov    0x2e92(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1163 <nlmsg_get_error+0x53>
mov    -0x18(%rbp),%rcx
xor    %eax,%eax
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<nlmsg_get_error>:
push   %rbx
mov    %rdi,%rbx
call   1030 <NLMSG_DATA@plt>
mov    0x2ed0(%rip),%rcx
mov    (%rcx),%rcx
add    $0x4,%rcx
cmp    %rcx,(%rbx)
jae    1137 <nlmsg_get_error+0x27>
mov    0x2ead(%rip),%rax
mov    (%rax),%ecx
jmp    113b <nlmsg_get_error+0x2b>
xor    %ecx,%ecx
sub    (%rax),%ecx
mov    %ecx,%eax
pop    %rbx
ret

LLVM IR (-O0):
%struct.nlmsghdr = type { i64 }
%struct.nlmsgerr = type { i32 }
@NLMSG_HDRLEN = global i64 0
@EBADMSG = global i32 0
define i32 @nlmsg_get_error(%struct.nlmsghdr* %0) {
%2 = alloca i32
%3 = alloca %struct.nlmsghdr*
%4 = alloca %struct.nlmsgerr*
store %struct.nlmsghdr* %0, %struct.nlmsghdr** %3
%5 = load %struct.nlmsghdr*, %struct.nlmsghdr** %3
%6 = call i64 @NLMSG_DATA(%struct.nlmsghdr* %5)
%7 = inttoptr i64 %6 to %struct.nlmsgerr*
store %struct.nlmsgerr* %7, %struct.nlmsgerr** %4
%8 = load %struct.nlmsghdr*, %struct.nlmsghdr** %3
%9 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %8, i32 0, i32 0
%10 = load i64, i64* %9
%11 = load i64, i64* @NLMSG_HDRLEN
%12 = add i64 4, %11
%13 = icmp ult i64 %10, %12
br i1 %13, label %14, label %16
14: ; preds = %1
%15 = load i32, i32* @EBADMSG
store i32 %15, i32* %2
br label %21
16: ; preds = %1
%17 = load %struct.nlmsgerr*, %struct.nlmsgerr** %4
%18 = getelementptr inbounds %struct.nlmsgerr, %struct.nlmsgerr* %17, i32 0, i32 0
%19 = load i32, i32* %18
%20 = sub nsw i32 0, %19
store i32 %20, i32* %2
br label %21
21: ; preds = %16, %14
%22 = load i32, i32* %2
ret i32 %22
}

LLVM IR (-O2):
%struct.nlmsghdr = type { i64 }
%struct.nlmsgerr = type { i32 }
@NLMSG_HDRLEN = global i64 0
@EBADMSG = global i32 0
define i32 @nlmsg_get_error(%struct.nlmsghdr* %0) {
%2 = tail call i64 @NLMSG_DATA(%struct.nlmsghdr* %0)
%3 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %0, i64 0, i32 0
%4 = load i64, i64* %3
%5 = load i64, i64* @NLMSG_HDRLEN
%6 = add i64 %5, 4
%7 = icmp ult i64 %4, %6
br i1 %7, label %8, label %10
8: ; preds = %1
%9 = load i32, i32* @EBADMSG
br label %15
10: ; preds = %1
%11 = inttoptr i64 %2 to %struct.nlmsgerr*
%12 = getelementptr inbounds %struct.nlmsgerr, %struct.nlmsgerr* %11, i64 0, i32 0
%13 = load i32, i32* %12
%14 = sub nsw i32 0, %13
br label %15
15: ; preds = %10, %8
%16 = phi i32 [ %9, %8 ], [ %14, %10 ]
ret i32 %16
}