#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 */

/* Variables and functions */
int /*<<< orphan*/ PF_NETLINK;
int /*<<< orphan*/ SOCK_RAW;
int __netlink_enumerate(int, int, int, int, int (*)(void *, struct nlmsghdr *),
                        void *);
int /*<<< orphan*/ close(int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int __rtnetlink_enumerate(int link_af, int addr_af,
                          int (*cb)(void *ctx, struct nlmsghdr *h), void *ctx) {
  int fd, r;
  fd = socket(PF_NETLINK, SOCK_RAW, 0);
  if (fd < 0)
    return -1;
  r = __netlink_enumerate(fd, 1, 18, link_af, cb, ctx);
  if (!r)
    r = __netlink_enumerate(fd, 2, 22, addr_af, cb, ctx);
  close(fd);
  return r;
}


x86 assembly (clang, -O0):
<__rtnetlink_enumerate>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    0x2e8b(%rip),%rax
mov    (%rax),%edi
mov    0x2e92(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1050 <socket@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    1178 <__rtnetlink_enumerate+0x48>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d0 <__rtnetlink_enumerate+0xa0>
mov    -0x24(%rbp),%edi
mov    -0x8(%rbp),%ecx
mov    -0x18(%rbp),%r8
mov    -0x20(%rbp),%r9
mov    $0x1,%esi
mov    $0x12,%edx
call   1040 <__netlink_enumerate@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jne    11c2 <__rtnetlink_enumerate+0x92>
mov    -0x24(%rbp),%edi
mov    -0xc(%rbp),%ecx
mov    -0x18(%rbp),%r8
mov    -0x20(%rbp),%r9
mov    $0x2,%esi
mov    $0x16,%edx
call   1040 <__netlink_enumerate@plt>
mov    %eax,-0x28(%rbp)
mov    -0x24(%rbp),%edi
call   1030 <close@plt>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<__rtnetlink_enumerate>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rcx,%r14
mov    %rdx,%r12
mov    %esi,%r15d
mov    %edi,%ebx
mov    0x2e8e(%rip),%rax
mov    (%rax),%edi
mov    0x2e95(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1050 <socket@plt>
test   %eax,%eax
js     11a6 <__rtnetlink_enumerate+0x76>
mov    %eax,%ebp
mov    %eax,%edi
mov    $0x1,%esi
mov    $0x12,%edx
mov    %ebx,%ecx
mov    %r12,%r8
mov    %r14,%r9
call   1040 <__netlink_enumerate@plt>
mov    %eax,%ebx
test   %eax,%eax
jne    119d <__rtnetlink_enumerate+0x6d>
mov    %ebp,%edi
mov    $0x2,%esi
mov    $0x16,%edx
mov    %r15d,%ecx
mov    %r12,%r8
mov    %r14,%r9
call   1040 <__netlink_enumerate@plt>
mov    %eax,%ebx
mov    %ebp,%edi
call   1030 <close@plt>
jmp    11ab <__rtnetlink_enumerate+0x7b>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.nlmsghdr = type opaque
%struct.nlmsghdr.0 = type opaque
@PF_NETLINK = global i32 0
@SOCK_RAW = global i32 0
define i32 @__rtnetlink_enumerate(i32 %0, i32 %1, i32 (i8*, %struct.nlmsghdr*)* %2, i8* %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32 (i8*, %struct.nlmsghdr*)*
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32
store i32 %0, i32* %6
store i32 %1, i32* %7
store i32 (i8*, %struct.nlmsghdr*)* %2, i32 (i8*, %struct.nlmsghdr*)** %8
store i8* %3, i8** %9
%12 = load i32, i32* @PF_NETLINK
%13 = load i32, i32* @SOCK_RAW
%14 = call i32 @socket(i32 %12, i32 %13, i32 0)
store i32 %14, i32* %10
%15 = load i32, i32* %10
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %4
store i32 -1, i32* %5
br label %38
18: ; preds = %4
%19 = load i32, i32* %10
%20 = load i32, i32* %6
%21 = load i32 (i8*, %struct.nlmsghdr*)*, i32 (i8*, %struct.nlmsghdr*)** %8
%22 = bitcast i32 (i8*, %struct.nlmsghdr*)* %21 to i32 (i8*, %struct.nlmsghdr.0*)*
%23 = load i8*, i8** %9
%24 = call i32 @__netlink_enumerate(i32 %19, i32 1, i32 18, i32 %20, i32 (i8*, %struct.nlmsghdr.0*)* %22, i8* %23)
store i32 %24, i32* %11
%25 = load i32, i32* %11
%26 = icmp ne i32 %25, 0
br i1 %26, label %34, label %27
27: ; preds = %18
%28 = load i32, i32* %10
%29 = load i32, i32* %7
%30 = load i32 (i8*, %struct.nlmsghdr*)*, i32 (i8*, %struct.nlmsghdr*)** %8
%31 = bitcast i32 (i8*, %struct.nlmsghdr*)* %30 to i32 (i8*, %struct.nlmsghdr.0*)*
%32 = load i8*, i8** %9
%33 = call i32 @__netlink_enumerate(i32 %28, i32 2, i32 22, i32 %29, i32 (i8*, %struct.nlmsghdr.0*)* %31, i8* %32)
store i32 %33, i32* %11
br label %34
34: ; preds = %27, %18
%35 = load i32, i32* %10
%36 = call i32 @close(i32 %35)
%37 = load i32, i32* %11
store i32 %37, i32* %5
br label %38
38: ; preds = %34, %17
%39 = load i32, i32* %5
ret i32 %39
}

LLVM IR (-O2):
%struct.nlmsghdr = type opaque
%struct.nlmsghdr.0 = type opaque
@PF_NETLINK = global i32 0
@SOCK_RAW = global i32 0
define i32 @__rtnetlink_enumerate(i32 %0, i32 %1, i32 (i8*, %struct.nlmsghdr*)* %2, i8* %3) {
%5 = load i32, i32* @PF_NETLINK
%6 = load i32, i32* @SOCK_RAW
%7 = tail call i32 @socket(i32 %5, i32 %6, i32 0)
%8 = icmp slt i32 %7, 0
br i1 %8, label %18, label %9
9: ; preds = %4
%10 = bitcast i32 (i8*, %struct.nlmsghdr*)* %2 to i32 (i8*, %struct.nlmsghdr.0*)*
%11 = tail call i32 @__netlink_enumerate(i32 %7, i32 1, i32 18, i32 %0, i32 (i8*, %struct.nlmsghdr.0*)* %10, i8* %3)
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %9
%14 = tail call i32 @__netlink_enumerate(i32 %7, i32 2, i32 22, i32 %1, i32 (i8*, %struct.nlmsghdr.0*)* %10, i8* %3)
br label %15
15: ; preds = %13, %9
%16 = phi i32 [ %11, %9 ], [ %14, %13 ]
%17 = tail call i32 @close(i32 %7)
br label %18
18: ; preds = %4, %15
%19 = phi i32 [ %16, %15 ], [ -1, %4 ]
ret i32 %19
}