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

/* Variables and functions */
size_t CTRL_ATTR_FAMILY_ID;
int /*<<< orphan*/ CTRL_ATTR_MAX;
int CTRL_CMD_NEWFAMILY;
int GENL_HDRLEN;
scalar_t__ GENL_ID_CTRL;
struct genlmsghdr *NLMSG_DATA(struct nlmsghdr *);
scalar_t__ NLMSG_LENGTH(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ parse_rtattr(struct rtattr **, int /*<<< orphan*/,
                                struct rtattr *, int);
int rta_getattr_u16(struct rtattr *);
int /*<<< orphan*/ stderr;
int genl_parse_getfamily(struct nlmsghdr *nlh) {
  struct rtattr *tb[CTRL_ATTR_MAX + 1];
  struct genlmsghdr *ghdr = NLMSG_DATA(nlh);
  int len = nlh->nlmsg_len;
  struct rtattr *attrs;
  if (nlh->nlmsg_type != GENL_ID_CTRL) {
    fprintf(stderr,
            "Not a controller message, nlmsg_len=%d "
            "nlmsg_type=0x%x\n",
            nlh->nlmsg_len, nlh->nlmsg_type);
    return -1;
  }
  len -= NLMSG_LENGTH(GENL_HDRLEN);
  if (len < 0) {
    fprintf(stderr, "wrong controller message len %d\n", len);
    return -1;
  }
  if (ghdr->cmd != CTRL_CMD_NEWFAMILY) {
    fprintf(stderr, "Unknown controller command %d\n", ghdr->cmd);
    return -1;
  }
  attrs = (struct rtattr *)((char *)ghdr + GENL_HDRLEN);
  parse_rtattr(tb, CTRL_ATTR_MAX, attrs, len);
  if (tb[CTRL_ATTR_FAMILY_ID] == NULL) {
    fprintf(stderr, "Missing family id TLV\n");
    return -1;
  }
  return rta_getattr_u16(tb[CTRL_ATTR_FAMILY_ID]);
}


x86 assembly (clang, -O0):
<genl_parse_getfamily>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e4d(%rip),%rax
mov    (%rax),%eax
add    $0x1,%eax
mov    %rsp,%rcx
mov    %rcx,-0x18(%rbp)
lea    0xf(,%rax,8),%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x48(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <NLMSG_DATA@plt>
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    0x2e0f(%rip),%rcx
cmp    (%rcx),%rax
je     11f2 <genl_parse_getfamily+0xa2>
mov    0x2e07(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%edx
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rcx
lea    0xe28(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x3c(%rbp)
jmp    131b <genl_parse_getfamily+0x1cb>
mov    0x2def(%rip),%rax
mov    (%rax),%edi
call   1030 <NLMSG_LENGTH@plt>
mov    %rax,%rcx
movslq -0x2c(%rbp),%rax
sub    %rcx,%rax
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    1244 <genl_parse_getfamily+0xf4>
mov    0x2daa(%rip),%rax
mov    (%rax),%edi
mov    -0x2c(%rbp),%edx
lea    0xe0e(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x3c(%rbp)
jmp    131b <genl_parse_getfamily+0x1cb>
mov    -0x28(%rbp),%rax
mov    (%rax),%eax
mov    0x2d87(%rip),%rcx
cmp    (%rcx),%eax
je     1289 <genl_parse_getfamily+0x139>
mov    0x2d68(%rip),%rax
mov    (%rax),%edi
mov    -0x28(%rbp),%rax
mov    (%rax),%edx
lea    0xdea(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x3c(%rbp)
jmp    131b <genl_parse_getfamily+0x1cb>
mov    -0x48(%rbp),%rdi
mov    -0x28(%rbp),%rax
mov    0x2d50(%rip),%rcx
movslq (%rcx),%rcx
add    %rcx,%rax
mov    %rax,-0x38(%rbp)
mov    0x2d07(%rip),%rax
mov    (%rax),%esi
mov    -0x38(%rbp),%rdx
mov    -0x2c(%rbp),%ecx
call   1070 <parse_rtattr@plt>
mov    -0x48(%rbp),%rax
mov    0x2d1e(%rip),%rcx
mov    (%rcx),%rcx
cmpq   $0x0,(%rax,%rcx,8)
jne    12fa <genl_parse_getfamily+0x1aa>
mov    0x2cf1(%rip),%rax
mov    (%rax),%edi
lea    0xd98(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x3c(%rbp)
jmp    131b <genl_parse_getfamily+0x1cb>
mov    -0x48(%rbp),%rax
mov    0x2cdb(%rip),%rcx
mov    (%rcx),%rcx
mov    (%rax,%rcx,8),%rdi
call   1040 <rta_getattr_u16@plt>
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x3c(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<genl_parse_getfamily>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    0x2e48(%rip),%r13
mov    0x0(%r13),%eax
add    $0x1,%eax
mov    %rsp,%r12
lea    0xf(,%rax,8),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r12
mov    %r12,%rsp
call   1060 <NLMSG_DATA@plt>
mov    %rax,%r15
mov    (%rbx),%r14d
mov    0x8(%rbx),%rcx
mov    0x2e26(%rip),%rax
cmp    (%rax),%rcx
jne    11f2 <genl_parse_getfamily+0xa2>
mov    0x2e42(%rip),%rbx
mov    (%rbx),%edi
call   1030 <NLMSG_LENGTH@plt>
sub    %eax,%r14d
js     120e <genl_parse_getfamily+0xbe>
mov    (%r15),%edx
mov    0x2e1c(%rip),%rax
cmp    (%rax),%edx
jne    1223 <genl_parse_getfamily+0xd3>
movslq (%rbx),%rax
add    %rax,%r15
mov    0x0(%r13),%esi
mov    %r12,%rdi
mov    %r15,%rdx
mov    %r14d,%ecx
call   1070 <parse_rtattr@plt>
mov    0x2e01(%rip),%rax
mov    (%rax),%rax
mov    (%r12,%rax,8),%rdi
test   %rdi,%rdi
je     124e <genl_parse_getfamily+0xfe>
call   1040 <rta_getattr_u16@plt>
jmp    123f <genl_parse_getfamily+0xef>
mov    0x2dcf(%rip),%rax
mov    (%rax),%edi
lea    0xdfe(%rip),%rsi
mov    %r14d,%edx
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    123a <genl_parse_getfamily+0xea>
mov    0x2db3(%rip),%rax
mov    (%rax),%edi
lea    0xe1a(%rip),%rsi
mov    %r14d,%edx
jmp    1233 <genl_parse_getfamily+0xe3>
mov    0x2d9e(%rip),%rax
mov    (%rax),%edi
lea    0xe26(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0xffffffff,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d73(%rip),%rax
mov    (%rax),%edi
lea    0xe1a(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    123a <genl_parse_getfamily+0xea>

LLVM IR (-O0):
%struct.nlmsghdr = type { i32, i64 }
%struct.genlmsghdr = type { i32 }
%struct.rtattr = type { i32 }
@CTRL_ATTR_MAX = global i32 0
@GENL_ID_CTRL = global i64 0
@stderr = global i32 0
@.str = [56 x i8] c"Not a controller message, nlmsg_len=%d nlmsg_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 = [23 x i8] c"Missing family id TLV\0A\00"
define i32 @genl_parse_getfamily(%struct.nlmsghdr* %0) {
%2 = alloca i32
%3 = alloca %struct.nlmsghdr*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca %struct.genlmsghdr*
%7 = alloca i32
%8 = alloca %struct.rtattr*
%9 = alloca i32
store %struct.nlmsghdr* %0, %struct.nlmsghdr** %3
%10 = load i32, i32* @CTRL_ATTR_MAX
%11 = add nsw i32 %10, 1
%12 = zext i32 %11 to i64
%13 = call i8* @llvm.stacksave()
store i8* %13, i8** %4
%14 = alloca %struct.rtattr*, i64 %12
store i64 %12, i64* %5
%15 = load %struct.nlmsghdr*, %struct.nlmsghdr** %3
%16 = call %struct.genlmsghdr* @NLMSG_DATA(%struct.nlmsghdr* %15)
store %struct.genlmsghdr* %16, %struct.genlmsghdr** %6
%17 = load %struct.nlmsghdr*, %struct.nlmsghdr** %3
%18 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %17, i32 0, i32 0
%19 = load i32, i32* %18
store i32 %19, i32* %7
%20 = load %struct.nlmsghdr*, %struct.nlmsghdr** %3
%21 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %20, i32 0, i32 1
%22 = load i64, i64* %21
%23 = load i64, i64* @GENL_ID_CTRL
%24 = icmp ne i64 %22, %23
br i1 %24, label %25, label %34
25: ; preds = %1
%26 = load i32, i32* @stderr
%27 = load %struct.nlmsghdr*, %struct.nlmsghdr** %3
%28 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %27, i32 0, i32 0
%29 = load i32, i32* %28
%30 = load %struct.nlmsghdr*, %struct.nlmsghdr** %3
%31 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %30, i32 0, i32 1
%32 = load i64, i64* %31
%33 = call i32 (i32, i8*, ...) @fprintf(i32 %26, i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str, i64 0, i64 0), i32 %29, i64 %32)
store i32 -1, i32* %2
store i32 1, i32* %9
br label %82
34: ; preds = %1
%35 = load i32, i32* @GENL_HDRLEN
%36 = call i64 @NLMSG_LENGTH(i32 %35)
%37 = load i32, i32* %7
%38 = sext i32 %37 to i64
%39 = sub nsw i64 %38, %36
%40 = trunc i64 %39 to i32
store i32 %40, i32* %7
%41 = load i32, i32* %7
%42 = icmp slt i32 %41, 0
br i1 %42, label %43, label %47
43: ; preds = %34
%44 = load i32, i32* @stderr
%45 = load i32, i32* %7
%46 = call i32 (i32, i8*, ...) @fprintf(i32 %44, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i32 %45)
store i32 -1, i32* %2
store i32 1, i32* %9
br label %82
47: ; preds = %34
%48 = load %struct.genlmsghdr*, %struct.genlmsghdr** %6
%49 = getelementptr inbounds %struct.genlmsghdr, %struct.genlmsghdr* %48, i32 0, i32 0
%50 = load i32, i32* %49
%51 = load i32, i32* @CTRL_CMD_NEWFAMILY
%52 = icmp ne i32 %50, %51
br i1 %52, label %53, label %59
53: ; preds = %47
%54 = load i32, i32* @stderr
%55 = load %struct.genlmsghdr*, %struct.genlmsghdr** %6
%56 = getelementptr inbounds %struct.genlmsghdr, %struct.genlmsghdr* %55, i32 0, i32 0
%57 = load i32, i32* %56
%58 = call i32 (i32, i8*, ...) @fprintf(i32 %54, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i32 %57)
store i32 -1, i32* %2
store i32 1, i32* %9
br label %82
59: ; preds = %47
%60 = load %struct.genlmsghdr*, %struct.genlmsghdr** %6
%61 = bitcast %struct.genlmsghdr* %60 to i8*
%62 = load i32, i32* @GENL_HDRLEN
%63 = sext i32 %62 to i64
%64 = getelementptr inbounds i8, i8* %61, i64 %63
%65 = bitcast i8* %64 to %struct.rtattr*
store %struct.rtattr* %65, %struct.rtattr** %8
%66 = load i32, i32* @CTRL_ATTR_MAX
%67 = load %struct.rtattr*, %struct.rtattr** %8
%68 = load i32, i32* %7
%69 = call i32 @parse_rtattr(%struct.rtattr** %14, i32 %66, %struct.rtattr* %67, i32 %68)
%70 = load i64, i64* @CTRL_ATTR_FAMILY_ID
%71 = getelementptr inbounds %struct.rtattr*, %struct.rtattr** %14, i64 %70
%72 = load %struct.rtattr*, %struct.rtattr** %71
%73 = icmp eq %struct.rtattr* %72, null
br i1 %73, label %74, label %77
74: ; preds = %59
%75 = load i32, i32* @stderr
%76 = call i32 (i32, i8*, ...) @fprintf(i32 %75, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %2
store i32 1, i32* %9
br label %82
77: ; preds = %59
%78 = load i64, i64* @CTRL_ATTR_FAMILY_ID
%79 = getelementptr inbounds %struct.rtattr*, %struct.rtattr** %14, i64 %78
%80 = load %struct.rtattr*, %struct.rtattr** %79
%81 = call i32 @rta_getattr_u16(%struct.rtattr* %80)
store i32 %81, i32* %2
store i32 1, i32* %9
br label %82
82: ; preds = %77, %74, %53, %43, %25
%83 = load i8*, i8** %4
%84 = load i32, i32* %2
ret i32 %84
}

LLVM IR (-O2):
%struct.nlmsghdr = type { i32, i64 }
%struct.rtattr = type { i32 }
%struct.genlmsghdr = type { i32 }
@CTRL_ATTR_MAX = global i32 0
@GENL_ID_CTRL = global i64 0
@stderr = global i32 0
@.str = [56 x i8] c"Not a controller message, nlmsg_len=%d nlmsg_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 = [23 x i8] c"Missing family id TLV\0A\00"
define i32 @genl_parse_getfamily(%struct.nlmsghdr* %0) {
%2 = load i32, i32* @CTRL_ATTR_MAX
%3 = add nsw i32 %2, 1
%4 = zext i32 %3 to i64
%5 = alloca %struct.rtattr*, i64 %4
%6 = tail call %struct.genlmsghdr* @NLMSG_DATA(%struct.nlmsghdr* %0)
%7 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %0, i64 0, i32 0
%8 = load i32, i32* %7
%9 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %0, i64 0, i32 1
%10 = load i64, i64* %9
%11 = load i64, i64* @GENL_ID_CTRL
%12 = icmp eq i64 %10, %11
br i1 %12, label %16, label %13
13: ; preds = %1
%14 = load i32, i32* @stderr
%15 = tail call i32 (i32, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str, i64 0, i64 0), i32 %8, i64 %10)
br label %50
16: ; preds = %1
%17 = load i32, i32* @GENL_HDRLEN
%18 = tail call i64 @NLMSG_LENGTH(i32 %17)
%19 = trunc i64 %18 to i32
%20 = sub i32 %8, %19
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %16
%23 = load i32, i32* @stderr
%24 = tail call i32 (i32, i8*, ...) @fprintf(i32 %23, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i32 %20)
br label %50
25: ; preds = %16
%26 = getelementptr inbounds %struct.genlmsghdr, %struct.genlmsghdr* %6, i64 0, i32 0
%27 = load i32, i32* %26
%28 = load i32, i32* @CTRL_CMD_NEWFAMILY
%29 = icmp eq i32 %27, %28
br i1 %29, label %33, label %30
30: ; preds = %25
%31 = load i32, i32* @stderr
%32 = tail call i32 (i32, i8*, ...) @fprintf(i32 %31, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i32 %27)
br label %50
33: ; preds = %25
%34 = bitcast %struct.genlmsghdr* %6 to i8*
%35 = load i32, i32* @GENL_HDRLEN
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds i8, i8* %34, i64 %36
%38 = bitcast i8* %37 to %struct.rtattr*
%39 = load i32, i32* @CTRL_ATTR_MAX
%40 = call i32 @parse_rtattr(%struct.rtattr** %5, i32 %39, %struct.rtattr* %38, i32 %20)
%41 = load i64, i64* @CTRL_ATTR_FAMILY_ID
%42 = getelementptr inbounds %struct.rtattr*, %struct.rtattr** %5, i64 %41
%43 = load %struct.rtattr*, %struct.rtattr** %42
%44 = icmp eq %struct.rtattr* %43, null
br i1 %44, label %45, label %48
45: ; preds = %33
%46 = load i32, i32* @stderr
%47 = call i32 (i32, i8*, ...) @fprintf(i32 %46, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0))
br label %50
48: ; preds = %33
%49 = call i32 @rta_getattr_u16(%struct.rtattr* %43)
br label %50
50: ; preds = %48, %45, %30, %22, %13
%51 = phi i32 [ -1, %13 ], [ -1, %22 ], [ -1, %30 ], [ -1, %45 ], [ %49, %48 ]
ret i32 %51
}