#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 int uint32_t;
struct rtmsg {
  int dummy;
};
struct nlmsghdr {
  scalar_t__ nlmsg_type;
};

/* Variables and functions */
scalar_t__ NLMSG_DATA(struct nlmsghdr *);
scalar_t__ NLMSG_DONE;
scalar_t__ NLMSG_ERROR;
struct nlmsghdr *NLMSG_NEXT(struct nlmsghdr *, size_t);
scalar_t__ NLMSG_OK(struct nlmsghdr *, int);
scalar_t__ RTM_NEWROUTE;
int /*<<< orphan*/ RTM_PAYLOAD(struct nlmsghdr *);
int /*<<< orphan*/ RTM_RTA(struct rtmsg *);
int /*<<< orphan*/ parse_rt_msg(struct rtmsg *, int /*<<< orphan*/,
                                int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
uint32_t parse_nl_msg(void *buf, size_t len) {
  struct nlmsghdr *nl = NULL;
  for (nl = (struct nlmsghdr *)buf;
       NLMSG_OK(nl, (uint32_t)len) && nl->nlmsg_type != NLMSG_DONE;
       nl = NLMSG_NEXT(nl, len)) {
    if (nl->nlmsg_type == NLMSG_ERROR) {
      printf("error");
      return -1;
    }
    if (nl->nlmsg_type == RTM_NEWROUTE) {
      struct rtmsg *rt;
      rt = (struct rtmsg *)NLMSG_DATA(nl);
      parse_rt_msg(rt, RTM_RTA(rt), RTM_PAYLOAD(nl));
      continue;
    }
  }
  return nl->nlmsg_type;
}


x86 assembly (clang, -O0):
<parse_nl_msg>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    %eax,%esi
call   1050 <NLMSG_OK@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    $0x0,%rcx
mov    %al,-0x29(%rbp)
je     11c8 <parse_nl_msg+0x58>
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    0x2e19(%rip),%rcx
cmp    (%rcx),%rax
setne  %al
mov    %al,-0x29(%rbp)
mov    -0x29(%rbp),%al
test   $0x1,%al
jne    11d8 <parse_nl_msg+0x68>
jmp    1276 <parse_nl_msg+0x106>
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    0x2e02(%rip),%rcx
cmp    (%rcx),%rax
jne    1207 <parse_nl_msg+0x97>
lea    0xe0a(%rip),%rdi
call   1060 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1280 <parse_nl_msg+0x110>
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    0x2db3(%rip),%rcx
cmp    (%rcx),%rax
jne    125b <parse_nl_msg+0xeb>
mov    -0x20(%rbp),%rdi
call   1080 <NLMSG_DATA@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%rdi
call   1030 <RTM_RTA@plt>
mov    %eax,-0x30(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <RTM_PAYLOAD@plt>
mov    -0x38(%rbp),%rdi
mov    -0x30(%rbp),%esi
mov    %eax,%edx
call   1070 <parse_rt_msg@plt>
jmp    1260 <parse_nl_msg+0xf0>
jmp    1260 <parse_nl_msg+0xf0>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1090 <NLMSG_NEXT@plt>
mov    %rax,-0x20(%rbp)
jmp    1190 <parse_nl_msg+0x20>
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<parse_nl_msg>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
call   1050 <NLMSG_OK@plt>
test   %rax,%rax
je     121f <parse_nl_msg+0xaf>
mov    0x2e42(%rip),%r12
mov    0x2e4b(%rip),%r15
mov    0x2e24(%rip),%rbp
mov    %r14,(%rsp)
jmp    11ce <parse_nl_msg+0x5e>
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %r14,%rsi
call   1090 <NLMSG_NEXT@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r14d,%esi
call   1050 <NLMSG_OK@plt>
test   %rax,%rax
je     121f <parse_nl_msg+0xaf>
mov    (%rbx),%rax
cmp    (%r12),%rax
je     121f <parse_nl_msg+0xaf>
cmp    (%r15),%rax
je     1223 <parse_nl_msg+0xb3>
cmp    0x0(%rbp),%rax
jne    11b0 <parse_nl_msg+0x40>
mov    %rbx,%rdi
call   1080 <NLMSG_DATA@plt>
mov    %r12,%r14
mov    %r15,%r12
mov    %rax,%r15
mov    %rax,%rdi
call   1030 <RTM_RTA@plt>
mov    %eax,%r13d
mov    %rbx,%rdi
call   1040 <RTM_PAYLOAD@plt>
mov    %r15,%rdi
mov    %r12,%r15
mov    %r14,%r12
mov    (%rsp),%r14
mov    %r13d,%esi
mov    %eax,%edx
call   1070 <parse_rt_msg@plt>
jmp    11b0 <parse_nl_msg+0x40>
mov    (%rbx),%eax
jmp    1234 <parse_nl_msg+0xc4>
lea    0xdd6(%rip),%rdi
call   1060 <printf@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.nlmsghdr = type { i64 }
%struct.rtmsg = type { i32 }
@NLMSG_DONE = global i64 0
@NLMSG_ERROR = global i64 0
@.str = [6 x i8] c"error\00"
@RTM_NEWROUTE = global i64 0
define i32 @parse_nl_msg(i8* %0, i64 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64
%6 = alloca %struct.nlmsghdr*
%7 = alloca %struct.rtmsg*
store i8* %0, i8** %4
store i64 %1, i64* %5
store %struct.nlmsghdr* null, %struct.nlmsghdr** %6
%8 = load i8*, i8** %4
%9 = bitcast i8* %8 to %struct.nlmsghdr*
store %struct.nlmsghdr* %9, %struct.nlmsghdr** %6
br label %10
10: ; preds = %49, %2
%11 = load %struct.nlmsghdr*, %struct.nlmsghdr** %6
%12 = load i64, i64* %5
%13 = trunc i64 %12 to i32
%14 = call i64 @NLMSG_OK(%struct.nlmsghdr* %11, i32 %13)
%15 = icmp ne i64 %14, 0
br i1 %15, label %16, label %22
16: ; preds = %10
%17 = load %struct.nlmsghdr*, %struct.nlmsghdr** %6
%18 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %17, i32 0, i32 0
%19 = load i64, i64* %18
%20 = load i64, i64* @NLMSG_DONE
%21 = icmp ne i64 %19, %20
br label %22
22: ; preds = %16, %10
%23 = phi i1 [ false, %10 ], [ %21, %16 ]
br i1 %23, label %24, label %53
24: ; preds = %22
%25 = load %struct.nlmsghdr*, %struct.nlmsghdr** %6
%26 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %25, i32 0, i32 0
%27 = load i64, i64* %26
%28 = load i64, i64* @NLMSG_ERROR
%29 = icmp eq i64 %27, %28
br i1 %29, label %30, label %32
30: ; preds = %24
%31 = call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %58
32: ; preds = %24
%33 = load %struct.nlmsghdr*, %struct.nlmsghdr** %6
%34 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %33, i32 0, i32 0
%35 = load i64, i64* %34
%36 = load i64, i64* @RTM_NEWROUTE
%37 = icmp eq i64 %35, %36
br i1 %37, label %38, label %48
38: ; preds = %32
%39 = load %struct.nlmsghdr*, %struct.nlmsghdr** %6
%40 = call i64 @NLMSG_DATA(%struct.nlmsghdr* %39)
%41 = inttoptr i64 %40 to %struct.rtmsg*
store %struct.rtmsg* %41, %struct.rtmsg** %7
%42 = load %struct.rtmsg*, %struct.rtmsg** %7
%43 = load %struct.rtmsg*, %struct.rtmsg** %7
%44 = call i32 @RTM_RTA(%struct.rtmsg* %43)
%45 = load %struct.nlmsghdr*, %struct.nlmsghdr** %6
%46 = call i32 @RTM_PAYLOAD(%struct.nlmsghdr* %45)
%47 = call i32 @parse_rt_msg(%struct.rtmsg* %42, i32 %44, i32 %46)
br label %49
48: ; preds = %32
br label %49
49: ; preds = %48, %38
%50 = load %struct.nlmsghdr*, %struct.nlmsghdr** %6
%51 = load i64, i64* %5
%52 = call %struct.nlmsghdr* @NLMSG_NEXT(%struct.nlmsghdr* %50, i64 %51)
store %struct.nlmsghdr* %52, %struct.nlmsghdr** %6
br label %10
53: ; preds = %22
%54 = load %struct.nlmsghdr*, %struct.nlmsghdr** %6
%55 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %54, i32 0, i32 0
%56 = load i64, i64* %55
%57 = trunc i64 %56 to i32
store i32 %57, i32* %3
br label %58
58: ; preds = %53, %30
%59 = load i32, i32* %3
ret i32 %59
}

LLVM IR (-O2):
%struct.nlmsghdr = type { i64 }
%struct.rtmsg = type { i32 }
@NLMSG_DONE = global i64 0
@NLMSG_ERROR = global i64 0
@.str = [6 x i8] c"error\00"
@RTM_NEWROUTE = global i64 0
define i32 @parse_nl_msg(i8* %0, i64 %1) {
%3 = bitcast i8* %0 to %struct.nlmsghdr*
%4 = trunc i64 %1 to i32
%5 = tail call i64 @NLMSG_OK(%struct.nlmsghdr* %3, i32 %4)
%6 = icmp eq i64 %5, 0
br i1 %6, label %31, label %7
7: ; preds = %2, %27
%8 = phi %struct.nlmsghdr* [ %28, %27 ], [ %3, %2 ]
%9 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %8, i64 0, i32 0
%10 = load i64, i64* %9
%11 = load i64, i64* @NLMSG_DONE
%12 = icmp eq i64 %10, %11
br i1 %12, label %31, label %13
13: ; preds = %7
%14 = load i64, i64* @NLMSG_ERROR
%15 = icmp eq i64 %10, %14
br i1 %15, label %16, label %18
16: ; preds = %13
%17 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
br label %36
18: ; preds = %13
%19 = load i64, i64* @RTM_NEWROUTE
%20 = icmp eq i64 %10, %19
br i1 %20, label %21, label %27
21: ; preds = %18
%22 = tail call i64 @NLMSG_DATA(%struct.nlmsghdr* %8)
%23 = inttoptr i64 %22 to %struct.rtmsg*
%24 = tail call i32 @RTM_RTA(%struct.rtmsg* %23)
%25 = tail call i32 @RTM_PAYLOAD(%struct.nlmsghdr* %8)
%26 = tail call i32 @parse_rt_msg(%struct.rtmsg* %23, i32 %24, i32 %25)
br label %27
27: ; preds = %18, %21
%28 = tail call %struct.nlmsghdr* @NLMSG_NEXT(%struct.nlmsghdr* %8, i64 %1)
%29 = tail call i64 @NLMSG_OK(%struct.nlmsghdr* %28, i32 %4)
%30 = icmp eq i64 %29, 0
br i1 %30, label %31, label %7
31: ; preds = %7, %27, %2
%32 = phi %struct.nlmsghdr* [ %3, %2 ], [ %28, %27 ], [ %8, %7 ]
%33 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %32, i64 0, i32 0
%34 = load i64, i64* %33
%35 = trunc i64 %34 to i32
br label %36
36: ; preds = %31, %16
%37 = phi i32 [ -1, %16 ], [ %35, %31 ]
ret i32 %37
}