#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 rtattr {
  scalar_t__ rta_type;
};
struct nlmsghdr {
  int nlmsg_len;
  scalar_t__ nlmsg_type;
};
struct genlmsghdr {
  int cmd;
};
typedef int __u16;

/* Variables and functions */
scalar_t__ CTRL_ATTR_FAMILY_ID;
int CTRL_CMD_NEWFAMILY;
int GENL_HDRLEN;
scalar_t__ GENL_ID_CTRL;
struct genlmsghdr *NLMSG_DATA(struct nlmsghdr *);
scalar_t__ NLMSG_LENGTH(int);
scalar_t__ RTA_DATA(struct rtattr *);
struct rtattr *RTA_NEXT(struct rtattr *, int);
scalar_t__ RTA_OK(struct rtattr *, int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ error(int, int /*<<< orphan*/, char *, ...);
int genl_parse_getfamily(struct nlmsghdr *nlh) {
  struct genlmsghdr *ghdr = NLMSG_DATA(nlh);
  int len = nlh->nlmsg_len;
  struct rtattr *attrs;
  if (nlh->nlmsg_type != GENL_ID_CTRL)
    error(1, errno, "Not a controller message, len=%d type=0x%x\n",
          nlh->nlmsg_len, nlh->nlmsg_type);
  len -= NLMSG_LENGTH(GENL_HDRLEN);
  if (len < 0)
    error(1, errno, "wrong controller message len %d\n", len);
  if (ghdr->cmd != CTRL_CMD_NEWFAMILY)
    error(1, errno, "Unknown controller command %d\n", ghdr->cmd);
  attrs = (struct rtattr *)((char *)ghdr + GENL_HDRLEN);
  while (RTA_OK(attrs, len)) {
    if (attrs->rta_type == CTRL_ATTR_FAMILY_ID)
      return *(__u16 *)RTA_DATA(attrs);
    attrs = RTA_NEXT(attrs, len);
  }
  error(1, errno, "can't find CTRL_ATTR_FAMILY_ID attr");
  return -1;
}


x86 assembly (clang, -O0):
<genl_parse_getfamily>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <NLMSG_DATA@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    0x2e37(%rip),%rcx
cmp    (%rcx),%rax
je     11c4 <genl_parse_getfamily+0x64>
mov    0x2e1f(%rip),%rax
mov    (%rax),%esi
mov    -0x10(%rbp),%rax
mov    (%rax),%ecx
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%r8
mov    $0x1,%edi
lea    0xe43(%rip),%rdx
mov    $0x0,%al
call   1070 <error@plt>
mov    0x2e1d(%rip),%rax
mov    (%rax),%edi
call   1040 <NLMSG_LENGTH@plt>
mov    %rax,%rcx
movslq -0x1c(%rbp),%rax
sub    %rcx,%rax
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1208 <genl_parse_getfamily+0xa8>
mov    0x2dd0(%rip),%rax
mov    (%rax),%esi
mov    -0x1c(%rbp),%ecx
mov    $0x1,%edi
lea    0xe2b(%rip),%rdx
mov    $0x0,%al
call   1070 <error@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
mov    0x2dc3(%rip),%rcx
cmp    (%rcx),%eax
je     123f <genl_parse_getfamily+0xdf>
mov    0x2d9c(%rip),%rax
mov    (%rax),%esi
mov    -0x18(%rbp),%rax
mov    (%rax),%ecx
mov    $0x1,%edi
lea    0xe15(%rip),%rdx
mov    $0x0,%al
call   1070 <error@plt>
mov    -0x18(%rbp),%rax
mov    0x2d9e(%rip),%rcx
movslq (%rcx),%rcx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x1c(%rbp),%esi
call   1050 <RTA_OK@plt>
cmp    $0x0,%rax
je     12a9 <genl_parse_getfamily+0x149>
mov    -0x28(%rbp),%rax
mov    (%rax),%rax
mov    0x2d68(%rip),%rcx
cmp    (%rcx),%rax
jne    1294 <genl_parse_getfamily+0x134>
mov    -0x28(%rbp),%rdi
call   1080 <RTA_DATA@plt>
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    12cc <genl_parse_getfamily+0x16c>
mov    -0x28(%rbp),%rdi
mov    -0x1c(%rbp),%esi
call   1030 <RTA_NEXT@plt>
mov    %rax,-0x28(%rbp)
jmp    1254 <genl_parse_getfamily+0xf4>
mov    0x2d10(%rip),%rax
mov    (%rax),%esi
mov    $0x1,%edi
lea    0xdae(%rip),%rdx
mov    $0x0,%al
call   1070 <error@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<genl_parse_getfamily>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%r14
call   1060 <NLMSG_DATA@plt>
mov    %rax,%rbx
mov    (%r14),%ebp
mov    0x8(%r14),%r8
mov    0x2e4b(%rip),%rax
cmp    (%rax),%r8
je     11a0 <genl_parse_getfamily+0x40>
mov    0x2e37(%rip),%rax
mov    (%rax),%esi
lea    0xe6e(%rip),%rdx
mov    $0x1,%edi
mov    %ebp,%ecx
xor    %eax,%eax
call   1070 <error@plt>
mov    0x2e41(%rip),%r14
mov    (%r14),%edi
call   1040 <NLMSG_LENGTH@plt>
sub    %eax,%ebp
jns    11d1 <genl_parse_getfamily+0x71>
mov    0x2e06(%rip),%rax
mov    (%rax),%esi
lea    0xe69(%rip),%rdx
mov    $0x1,%edi
mov    %ebp,%ecx
xor    %eax,%eax
call   1070 <error@plt>
mov    (%rbx),%ecx
mov    0x2dfe(%rip),%rax
cmp    (%rax),%ecx
je     11fa <genl_parse_getfamily+0x9a>
mov    0x2ddb(%rip),%rax
mov    (%rax),%esi
lea    0xe5f(%rip),%rdx
mov    $0x1,%edi
xor    %eax,%eax
call   1070 <error@plt>
movslq (%r14),%rax
add    %rax,%rbx
mov    %rbx,%rdi
mov    %ebp,%esi
call   1050 <RTA_OK@plt>
test   %rax,%rax
je     1244 <genl_parse_getfamily+0xe4>
mov    0x2dca(%rip),%r14
cs nopw 0x0(%rax,%rax,1)
mov    (%rbx),%rax
cmp    (%r14),%rax
je     1267 <genl_parse_getfamily+0x107>
mov    %rbx,%rdi
mov    %ebp,%esi
call   1030 <RTA_NEXT@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %ebp,%esi
call   1050 <RTA_OK@plt>
test   %rax,%rax
jne    1220 <genl_parse_getfamily+0xc0>
mov    0x2d75(%rip),%rax
mov    (%rax),%esi
lea    0xe18(%rip),%rdx
mov    $0x1,%edi
xor    %eax,%eax
call   1070 <error@plt>
mov    $0xffffffff,%eax
jmp    1271 <genl_parse_getfamily+0x111>
mov    %rbx,%rdi
call   1080 <RTA_DATA@plt>
mov    (%rax),%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.nlmsghdr = type { i32, i64 }
%struct.genlmsghdr = type { i32 }
%struct.rtattr = type { i64 }
@GENL_ID_CTRL = global i64 0
@errno = global i32 0
@.str = [44 x i8] c"Not a controller message, len=%d type=0x%x\0A\00"
@GENL_HDRLEN = global i32 0
@.str.1 = [33 x i8] c"wrong controller message len %d\0A\00"
@CTRL_CMD_NEWFAMILY = global i32 0
@.str.2 = [31 x i8] c"Unknown controller command %d\0A\00"
@CTRL_ATTR_FAMILY_ID = global i64 0
@.str.3 = [36 x i8] c"can't find CTRL_ATTR_FAMILY_ID attr\00"
define i32 @genl_parse_getfamily(%struct.nlmsghdr* %0) {
%2 = alloca i32
%3 = alloca %struct.nlmsghdr*
%4 = alloca %struct.genlmsghdr*
%5 = alloca i32
%6 = alloca %struct.rtattr*
store %struct.nlmsghdr* %0, %struct.nlmsghdr** %3
%7 = load %struct.nlmsghdr*, %struct.nlmsghdr** %3
%8 = call %struct.genlmsghdr* @NLMSG_DATA(%struct.nlmsghdr* %7)
store %struct.genlmsghdr* %8, %struct.genlmsghdr** %4
%9 = load %struct.nlmsghdr*, %struct.nlmsghdr** %3
%10 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %9, i32 0, i32 0
%11 = load i32, i32* %10
store i32 %11, i32* %5
%12 = load %struct.nlmsghdr*, %struct.nlmsghdr** %3
%13 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %12, i32 0, i32 1
%14 = load i64, i64* %13
%15 = load i64, i64* @GENL_ID_CTRL
%16 = icmp ne i64 %14, %15
br i1 %16, label %17, label %26
17: ; preds = %1
%18 = load i32, i32* @errno
%19 = load %struct.nlmsghdr*, %struct.nlmsghdr** %3
%20 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %19, i32 0, i32 0
%21 = load i32, i32* %20
%22 = load %struct.nlmsghdr*, %struct.nlmsghdr** %3
%23 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %22, i32 0, i32 1
%24 = load i64, i64* %23
%25 = call i32 (i32, i32, i8*, ...) @error(i32 1, i32 %18, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0), i32 %21, i64 %24)
br label %26
26: ; preds = %17, %1
%27 = load i32, i32* @GENL_HDRLEN
%28 = call i64 @NLMSG_LENGTH(i32 %27)
%29 = load i32, i32* %5
%30 = sext i32 %29 to i64
%31 = sub nsw i64 %30, %28
%32 = trunc i64 %31 to i32
store i32 %32, i32* %5
%33 = load i32, i32* %5
%34 = icmp slt i32 %33, 0
br i1 %34, label %35, label %39
35: ; preds = %26
%36 = load i32, i32* @errno
%37 = load i32, i32* %5
%38 = call i32 (i32, i32, i8*, ...) @error(i32 1, i32 %36, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i32 %37)
br label %39
39: ; preds = %35, %26
%40 = load %struct.genlmsghdr*, %struct.genlmsghdr** %4
%41 = getelementptr inbounds %struct.genlmsghdr, %struct.genlmsghdr* %40, i32 0, i32 0
%42 = load i32, i32* %41
%43 = load i32, i32* @CTRL_CMD_NEWFAMILY
%44 = icmp ne i32 %42, %43
br i1 %44, label %45, label %51
45: ; preds = %39
%46 = load i32, i32* @errno
%47 = load %struct.genlmsghdr*, %struct.genlmsghdr** %4
%48 = getelementptr inbounds %struct.genlmsghdr, %struct.genlmsghdr* %47, i32 0, i32 0
%49 = load i32, i32* %48
%50 = call i32 (i32, i32, i8*, ...) @error(i32 1, i32 %46, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i32 %49)
br label %51
51: ; preds = %45, %39
%52 = load %struct.genlmsghdr*, %struct.genlmsghdr** %4
%53 = bitcast %struct.genlmsghdr* %52 to i8*
%54 = load i32, i32* @GENL_HDRLEN
%55 = sext i32 %54 to i64
%56 = getelementptr inbounds i8, i8* %53, i64 %55
%57 = bitcast i8* %56 to %struct.rtattr*
store %struct.rtattr* %57, %struct.rtattr** %6
br label %58
58: ; preds = %74, %51
%59 = load %struct.rtattr*, %struct.rtattr** %6
%60 = load i32, i32* %5
%61 = call i64 @RTA_OK(%struct.rtattr* %59, i32 %60)
%62 = icmp ne i64 %61, 0
br i1 %62, label %63, label %78
63: ; preds = %58
%64 = load %struct.rtattr*, %struct.rtattr** %6
%65 = getelementptr inbounds %struct.rtattr, %struct.rtattr* %64, i32 0, i32 0
%66 = load i64, i64* %65
%67 = load i64, i64* @CTRL_ATTR_FAMILY_ID
%68 = icmp eq i64 %66, %67
br i1 %68, label %69, label %74
69: ; preds = %63
%70 = load %struct.rtattr*, %struct.rtattr** %6
%71 = call i64 @RTA_DATA(%struct.rtattr* %70)
%72 = inttoptr i64 %71 to i32*
%73 = load i32, i32* %72
store i32 %73, i32* %2
br label %81
74: ; preds = %63
%75 = load %struct.rtattr*, %struct.rtattr** %6
%76 = load i32, i32* %5
%77 = call %struct.rtattr* @RTA_NEXT(%struct.rtattr* %75, i32 %76)
store %struct.rtattr* %77, %struct.rtattr** %6
br label %58
78: ; preds = %58
%79 = load i32, i32* @errno
%80 = call i32 (i32, i32, i8*, ...) @error(i32 1, i32 %79, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %2
br label %81
81: ; preds = %78, %69
%82 = load i32, i32* %2
ret i32 %82
}

LLVM IR (-O2):
%struct.nlmsghdr = type { i32, i64 }
%struct.genlmsghdr = type { i32 }
%struct.rtattr = type { i64 }
@GENL_ID_CTRL = global i64 0
@errno = global i32 0
@.str = [44 x i8] c"Not a controller message, len=%d type=0x%x\0A\00"
@GENL_HDRLEN = global i32 0
@.str.1 = [33 x i8] c"wrong controller message len %d\0A\00"
@CTRL_CMD_NEWFAMILY = global i32 0
@.str.2 = [31 x i8] c"Unknown controller command %d\0A\00"
@CTRL_ATTR_FAMILY_ID = global i64 0
@.str.3 = [36 x i8] c"can't find CTRL_ATTR_FAMILY_ID attr\00"
define i32 @genl_parse_getfamily(%struct.nlmsghdr* %0) {
%2 = tail call %struct.genlmsghdr* @NLMSG_DATA(%struct.nlmsghdr* %0)
%3 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %0, i64 0, i32 0
%4 = load i32, i32* %3
%5 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %0, i64 0, i32 1
%6 = load i64, i64* %5
%7 = load i64, i64* @GENL_ID_CTRL
%8 = icmp eq i64 %6, %7
br i1 %8, label %12, label %9
9: ; preds = %1
%10 = load i32, i32* @errno
%11 = tail call i32 (i32, i32, i8*, ...) @error(i32 1, i32 %10, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0), i32 %4, i64 %6)
br label %12
12: ; preds = %9, %1
%13 = load i32, i32* @GENL_HDRLEN
%14 = tail call i64 @NLMSG_LENGTH(i32 %13)
%15 = trunc i64 %14 to i32
%16 = sub i32 %4, %15
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %12
%19 = load i32, i32* @errno
%20 = tail call i32 (i32, i32, i8*, ...) @error(i32 1, i32 %19, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i32 %16)
br label %21
21: ; preds = %18, %12
%22 = getelementptr inbounds %struct.genlmsghdr, %struct.genlmsghdr* %2, i64 0, i32 0
%23 = load i32, i32* %22
%24 = load i32, i32* @CTRL_CMD_NEWFAMILY
%25 = icmp eq i32 %23, %24
br i1 %25, label %29, label %26
26: ; preds = %21
%27 = load i32, i32* @errno
%28 = tail call i32 (i32, i32, i8*, ...) @error(i32 1, i32 %27, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i32 %23)
br label %29
29: ; preds = %26, %21
%30 = bitcast %struct.genlmsghdr* %2 to i8*
%31 = load i32, i32* @GENL_HDRLEN
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds i8, i8* %30, i64 %32
%34 = bitcast i8* %33 to %struct.rtattr*
%35 = tail call i64 @RTA_OK(%struct.rtattr* %34, i32 %16)
%36 = icmp eq i64 %35, 0
br i1 %36, label %51, label %37
37: ; preds = %29, %47
%38 = phi %struct.rtattr* [ %48, %47 ], [ %34, %29 ]
%39 = getelementptr inbounds %struct.rtattr, %struct.rtattr* %38, i64 0, i32 0
%40 = load i64, i64* %39
%41 = load i64, i64* @CTRL_ATTR_FAMILY_ID
%42 = icmp eq i64 %40, %41
br i1 %42, label %43, label %47
43: ; preds = %37
%44 = tail call i64 @RTA_DATA(%struct.rtattr* %38)
%45 = inttoptr i64 %44 to i32*
%46 = load i32, i32* %45
br label %54
47: ; preds = %37
%48 = tail call %struct.rtattr* @RTA_NEXT(%struct.rtattr* %38, i32 %16)
%49 = tail call i64 @RTA_OK(%struct.rtattr* %48, i32 %16)
%50 = icmp eq i64 %49, 0
br i1 %50, label %51, label %37
51: ; preds = %47, %29
%52 = load i32, i32* @errno
%53 = tail call i32 (i32, i32, i8*, ...) @error(i32 1, i32 %52, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0))
br label %54
54: ; preds = %51, %43
%55 = phi i32 [ %46, %43 ], [ -1, %51 ]
ret i32 %55
}