#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 {
  unsigned short rta_type;
};

/* Variables and functions */
struct rtattr *IFLA_RTA(char *);
struct rtattr *RTA_NEXT(struct rtattr *, unsigned int);
scalar_t__ RTA_OK(struct rtattr *, unsigned int);
#define RTM_DELLINK 133
#define RTM_DELROUTE 132
#define RTM_GETLINK 131
#define RTM_GETROUTE 130
#define RTM_NEWLINK 129
#define RTM_NEWROUTE 128
struct rtattr *RTM_RTA(char *);
unsigned int __IFLA_MAX;
unsigned int __RTA_MAX;
int /*<<< orphan*/ memset(struct rtattr **, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, ...);
int rtnetlink_get_dumptbl(struct rtattr *tbl[], unsigned int tblcount,
                          char *msgdat, unsigned int bounds, int type) {
  unsigned short rtatype;
  struct rtattr *rta;
  switch (type) {
  case RTM_GETROUTE:
  case RTM_NEWROUTE:
  case RTM_DELROUTE:
    if (tblcount != __RTA_MAX) {
      printf("bad tblcount\n");
      return -1;
    }
    rta = RTM_RTA(msgdat);
    break;
  case RTM_GETLINK:
  case RTM_NEWLINK:
  case RTM_DELLINK:
    if (tblcount != __IFLA_MAX) {
      printf("bad tblcount\n");
      return -1;
    }
    rta = IFLA_RTA(msgdat);
    break;
  default:
    printf("dumptbl error\n");
    return -1;
  }
  memset(tbl, 0, sizeof(struct rtattr *) * tblcount);
  while (RTA_OK(rta, bounds)) {
    rtatype = rta->rta_type;
    if (rtatype >= tblcount) {
      printf("unexpected rtatype: %d\n", rtatype);
      return -1;
    }
    if (tbl[rtatype] == NULL) {
      tbl[rtatype] = rta;
    }
    rta = RTA_NEXT(rta, bounds);
  }
  return 0;
}


x86 assembly (clang, -O0):
<rtnetlink_get_dumptbl>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
mov    %r8d,-0x28(%rbp)
mov    -0x28(%rbp),%eax
mov    %eax,-0x3c(%rbp)
sub    $0x80,%eax
je     11ef <rtnetlink_get_dumptbl+0x8f>
jmp    1190 <rtnetlink_get_dumptbl+0x30>
mov    -0x3c(%rbp),%eax
sub    $0x81,%eax
je     122d <rtnetlink_get_dumptbl+0xcd>
jmp    11a3 <rtnetlink_get_dumptbl+0x43>
mov    -0x3c(%rbp),%eax
sub    $0x82,%eax
je     11ef <rtnetlink_get_dumptbl+0x8f>
jmp    11b6 <rtnetlink_get_dumptbl+0x56>
mov    -0x3c(%rbp),%eax
sub    $0x83,%eax
je     122d <rtnetlink_get_dumptbl+0xcd>
jmp    11c9 <rtnetlink_get_dumptbl+0x69>
mov    -0x3c(%rbp),%eax
sub    $0x84,%eax
je     11ef <rtnetlink_get_dumptbl+0x8f>
jmp    11dc <rtnetlink_get_dumptbl+0x7c>
mov    -0x3c(%rbp),%eax
sub    $0x85,%eax
je     122d <rtnetlink_get_dumptbl+0xcd>
jmp    126b <rtnetlink_get_dumptbl+0x10b>
mov    -0x14(%rbp),%eax
mov    0x2ddf(%rip),%rcx
cmp    (%rcx),%eax
je     121b <rtnetlink_get_dumptbl+0xbb>
lea    0xdf8(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1324 <rtnetlink_get_dumptbl+0x1c4>
mov    -0x20(%rbp),%rdi
call   1040 <RTM_RTA@plt>
mov    %rax,-0x38(%rbp)
jmp    1285 <rtnetlink_get_dumptbl+0x125>
mov    -0x14(%rbp),%eax
mov    0x2db1(%rip),%rcx
cmp    (%rcx),%eax
je     1259 <rtnetlink_get_dumptbl+0xf9>
lea    0xdba(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1324 <rtnetlink_get_dumptbl+0x1c4>
mov    -0x20(%rbp),%rdi
call   1080 <IFLA_RTA@plt>
mov    %rax,-0x38(%rbp)
jmp    1285 <rtnetlink_get_dumptbl+0x125>
lea    0xd9c(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1324 <rtnetlink_get_dumptbl+0x1c4>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%eax
shl    $0x3,%rax
mov    %eax,%edx
xor    %esi,%esi
call   1070 <memset@plt>
mov    -0x38(%rbp),%rdi
mov    -0x24(%rbp),%esi
call   1050 <RTA_OK@plt>
cmp    $0x0,%rax
je     131d <rtnetlink_get_dumptbl+0x1bd>
mov    -0x38(%rbp),%rax
mov    (%rax),%ax
mov    %ax,-0x2a(%rbp)
movzwl -0x2a(%rbp),%eax
cmp    -0x14(%rbp),%eax
jb     12e5 <rtnetlink_get_dumptbl+0x185>
movzwl -0x2a(%rbp),%esi
lea    0xd4b(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1324 <rtnetlink_get_dumptbl+0x1c4>
mov    -0x10(%rbp),%rax
movzwl -0x2a(%rbp),%ecx
cmpq   $0x0,(%rax,%rcx,8)
jne    1308 <rtnetlink_get_dumptbl+0x1a8>
mov    -0x38(%rbp),%rdx
mov    -0x10(%rbp),%rax
movzwl -0x2a(%rbp),%ecx
mov    %rdx,(%rax,%rcx,8)
mov    -0x38(%rbp),%rdi
mov    -0x24(%rbp),%esi
call   1030 <RTA_NEXT@plt>
mov    %rax,-0x38(%rbp)
jmp    1299 <rtnetlink_get_dumptbl+0x139>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rtnetlink_get_dumptbl>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
add    $0xffffff80,%r8d
cmp    $0x5,%r8d
ja     1222 <rtnetlink_get_dumptbl+0xb2>
mov    %ecx,%ebx
mov    %esi,%r14d
mov    %rdi,%r15
mov    $0x15,%eax
bt     %r8d,%eax
jae    11af <rtnetlink_get_dumptbl+0x3f>
mov    0x2e38(%rip),%rax
cmp    %r14d,(%rax)
jne    1214 <rtnetlink_get_dumptbl+0xa4>
mov    %rdx,%rdi
call   1040 <RTM_RTA@plt>
jmp    11c3 <rtnetlink_get_dumptbl+0x53>
mov    0x2e32(%rip),%rax
cmp    %r14d,(%rax)
jne    1214 <rtnetlink_get_dumptbl+0xa4>
mov    %rdx,%rdi
call   1090 <IFLA_RTA@plt>
mov    %rax,%rbp
lea    0x0(,%r14,8),%edx
xor    %r12d,%r12d
mov    %r15,%rdi
xor    %esi,%esi
call   1080 <memset@plt>
mov    %rbp,%rdi
jmp    11f0 <rtnetlink_get_dumptbl+0x80>
mov    %rbp,%rdi
mov    %ebx,%esi
call   1030 <RTA_NEXT@plt>
mov    %rax,%rbp
mov    %rax,%rdi
mov    %ebx,%esi
call   1060 <RTA_OK@plt>
test   %rax,%rax
je     1244 <rtnetlink_get_dumptbl+0xd4>
movzwl 0x0(%rbp),%esi
cmp    %r14d,%esi
jae    1230 <rtnetlink_get_dumptbl+0xc0>
mov    %esi,%eax
cmpq   $0x0,(%r15,%rax,8)
jne    11e0 <rtnetlink_get_dumptbl+0x70>
mov    %rbp,(%r15,%rax,8)
jmp    11e0 <rtnetlink_get_dumptbl+0x70>
lea    0xdfd(%rip),%rdi
call   1050 <puts@plt>
jmp    123e <rtnetlink_get_dumptbl+0xce>
lea    0xdfc(%rip),%rdi
call   1050 <puts@plt>
jmp    123e <rtnetlink_get_dumptbl+0xce>
lea    0xdc9(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
mov    $0xffffffff,%r12d
mov    %r12d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.rtattr = type { i16 }
@__RTA_MAX = global i32 0
@.str = [14 x i8] c"bad tblcount\0A\00"
@__IFLA_MAX = global i32 0
@.str.1 = [15 x i8] c"dumptbl error\0A\00"
@.str.2 = [24 x i8] c"unexpected rtatype: %d\0A\00"
define i32 @rtnetlink_get_dumptbl(%struct.rtattr** %0, i32 %1, i8* %2, i32 %3, i32 %4) {
%6 = alloca i32
%7 = alloca %struct.rtattr**
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i16
%13 = alloca %struct.rtattr*
store %struct.rtattr** %0, %struct.rtattr*** %7
store i32 %1, i32* %8
store i8* %2, i8** %9
store i32 %3, i32* %10
store i32 %4, i32* %11
%14 = load i32, i32* %11
switch i32 %14, label %33 [
i32 130, label %15
i32 128, label %15
i32 132, label %15
i32 131, label %24
i32 129, label %24
i32 133, label %24
]
15: ; preds = %5, %5, %5
%16 = load i32, i32* %8
%17 = load i32, i32* @__RTA_MAX
%18 = icmp ne i32 %16, %17
br i1 %18, label %19, label %21
19: ; preds = %15
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %6
br label %77
21: ; preds = %15
%22 = load i8*, i8** %9
%23 = call %struct.rtattr* @RTM_RTA(i8* %22)
store %struct.rtattr* %23, %struct.rtattr** %13
br label %35
24: ; preds = %5, %5, %5
%25 = load i32, i32* %8
%26 = load i32, i32* @__IFLA_MAX
%27 = icmp ne i32 %25, %26
br i1 %27, label %28, label %30
28: ; preds = %24
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %6
br label %77
30: ; preds = %24
%31 = load i8*, i8** %9
%32 = call %struct.rtattr* @IFLA_RTA(i8* %31)
store %struct.rtattr* %32, %struct.rtattr** %13
br label %35
33: ; preds = %5
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %6
br label %77
35: ; preds = %30, %21
%36 = load %struct.rtattr**, %struct.rtattr*** %7
%37 = load i32, i32* %8
%38 = zext i32 %37 to i64
%39 = mul i64 8, %38
%40 = trunc i64 %39 to i32
%41 = call i32 @memset(%struct.rtattr** %36, i32 0, i32 %40)
br label %42
42: ; preds = %72, %35
%43 = load %struct.rtattr*, %struct.rtattr** %13
%44 = load i32, i32* %10
%45 = call i64 @RTA_OK(%struct.rtattr* %43, i32 %44)
%46 = icmp ne i64 %45, 0
br i1 %46, label %47, label %76
47: ; preds = %42
%48 = load %struct.rtattr*, %struct.rtattr** %13
%49 = getelementptr inbounds %struct.rtattr, %struct.rtattr* %48, i32 0, i32 0
%50 = load i16, i16* %49
store i16 %50, i16* %12
%51 = load i16, i16* %12
%52 = zext i16 %51 to i32
%53 = load i32, i32* %8
%54 = icmp uge i32 %52, %53
br i1 %54, label %55, label %59
55: ; preds = %47
%56 = load i16, i16* %12
%57 = zext i16 %56 to i32
%58 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i32 %57)
store i32 -1, i32* %6
br label %77
59: ; preds = %47
%60 = load %struct.rtattr**, %struct.rtattr*** %7
%61 = load i16, i16* %12
%62 = zext i16 %61 to i64
%63 = getelementptr inbounds %struct.rtattr*, %struct.rtattr** %60, i64 %62
%64 = load %struct.rtattr*, %struct.rtattr** %63
%65 = icmp eq %struct.rtattr* %64, null
br i1 %65, label %66, label %72
66: ; preds = %59
%67 = load %struct.rtattr*, %struct.rtattr** %13
%68 = load %struct.rtattr**, %struct.rtattr*** %7
%69 = load i16, i16* %12
%70 = zext i16 %69 to i64
%71 = getelementptr inbounds %struct.rtattr*, %struct.rtattr** %68, i64 %70
store %struct.rtattr* %67, %struct.rtattr** %71
br label %72
72: ; preds = %66, %59
%73 = load %struct.rtattr*, %struct.rtattr** %13
%74 = load i32, i32* %10
%75 = call %struct.rtattr* @RTA_NEXT(%struct.rtattr* %73, i32 %74)
store %struct.rtattr* %75, %struct.rtattr** %13
br label %42
76: ; preds = %42
store i32 0, i32* %6
br label %77
77: ; preds = %76, %55, %33, %28, %19
%78 = load i32, i32* %6
ret i32 %78
}

LLVM IR (-O2):
%struct.rtattr = type { i16 }
@__RTA_MAX = global i32 0
@__IFLA_MAX = global i32 0
@.str.2 = [24 x i8] c"unexpected rtatype: %d\0A\00"
@str.3 = [13 x i8] c"bad tblcount\00"
@str.4 = [14 x i8] c"dumptbl error\00"
define i32 @rtnetlink_get_dumptbl(%struct.rtattr** %0, i32 %1, i8* %2, i32 %3, i32 %4) {
switch i32 %4, label %20 [
i32 130, label %6
i32 128, label %6
i32 132, label %6
i32 131, label %13
i32 129, label %13
i32 133, label %13
]
6: ; preds = %5, %5, %5
%7 = load i32, i32* @__RTA_MAX
%8 = icmp eq i32 %7, %1
br i1 %8, label %11, label %9
9: ; preds = %6
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str.3, i64 0, i64 0))
br label %47
11: ; preds = %6
%12 = tail call %struct.rtattr* @RTM_RTA(i8* %2)
br label %22
13: ; preds = %5, %5, %5
%14 = load i32, i32* @__IFLA_MAX
%15 = icmp eq i32 %14, %1
br i1 %15, label %18, label %16
16: ; preds = %13
%17 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str.3, i64 0, i64 0))
br label %47
18: ; preds = %13
%19 = tail call %struct.rtattr* @IFLA_RTA(i8* %2)
br label %22
20: ; preds = %5
%21 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str.4, i64 0, i64 0))
br label %47
22: ; preds = %18, %11
%23 = phi %struct.rtattr* [ %19, %18 ], [ %12, %11 ]
%24 = shl i32 %1, 3
%25 = tail call i32 @memset(%struct.rtattr** %0, i32 0, i32 %24)
%26 = tail call i64 @RTA_OK(%struct.rtattr* %23, i32 %3)
%27 = icmp eq i64 %26, 0
br i1 %27, label %47, label %28
28: ; preds = %22, %43
%29 = phi %struct.rtattr* [ %44, %43 ], [ %23, %22 ]
%30 = getelementptr inbounds %struct.rtattr, %struct.rtattr* %29, i64 0, i32 0
%31 = load i16, i16* %30
%32 = zext i16 %31 to i32
%33 = icmp ult i32 %32, %1
br i1 %33, label %37, label %34
34: ; preds = %28
%35 = zext i16 %31 to i32
%36 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i32 %35)
br label %47
37: ; preds = %28
%38 = zext i16 %31 to i64
%39 = getelementptr inbounds %struct.rtattr*, %struct.rtattr** %0, i64 %38
%40 = load %struct.rtattr*, %struct.rtattr** %39
%41 = icmp eq %struct.rtattr* %40, null
br i1 %41, label %42, label %43
42: ; preds = %37
store %struct.rtattr* %29, %struct.rtattr** %39
br label %43
43: ; preds = %42, %37
%44 = tail call %struct.rtattr* @RTA_NEXT(%struct.rtattr* %29, i32 %3)
%45 = tail call i64 @RTA_OK(%struct.rtattr* %44, i32 %3)
%46 = icmp eq i64 %45, 0
br i1 %46, label %47, label %28
47: ; preds = %43, %22, %34, %20, %16, %9
%48 = phi i32 [ -1, %20 ], [ -1, %16 ], [ -1, %34 ], [ -1, %9 ], [ 0, %22 ], [ 0, %43 ]
ret i32 %48
}