#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 sockaddr_nl {
  int dummy;
};
struct sockaddr {
  int dummy;
};
struct nlmsghdr {
  scalar_t__ nlmsg_type;
  int /*<<< orphan*/ nlmsg_len;
};
struct nlmsgerr {
  int error;
};
typedef int socklen_t;

/* Variables and functions */
int /*<<< orphan*/ NETLINK_ROUTE;
struct nlmsgerr *NLMSG_DATA(struct nlmsghdr *);
scalar_t__ NLMSG_ERROR;
int /*<<< orphan*/ PF_NETLINK;
int /*<<< orphan*/ SOCK_RAW;
int /*<<< orphan*/ close(int);
int errno;
int recvfrom(int, struct nlmsghdr *, int, int /*<<< orphan*/, struct sockaddr *,
             int *);
int sendto(int, struct nlmsghdr *, int /*<<< orphan*/, int /*<<< orphan*/,
           struct sockaddr *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
__attribute__((visibility("hidden"))) int
rtnetlink_request(struct nlmsghdr *msg, int buflen, struct sockaddr_nl *adr) {
  int rsk;
  int n;
  int e;
  rsk = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
  if (rsk < 0)
    return -1;
  n = sendto(rsk, msg, msg->nlmsg_len, 0, (struct sockaddr *)adr,
             sizeof(struct sockaddr_nl));
  if (n >= 0) {
    socklen_t adrlen = sizeof(struct sockaddr_nl);
    n = recvfrom(rsk, msg, buflen, 0, (struct sockaddr *)adr, &adrlen);
  }
  e = errno;
  close(rsk);
  errno = e;
  if (n < 0)
    return -1;
  if (msg->nlmsg_type == NLMSG_ERROR) {
    struct nlmsgerr *err = NLMSG_DATA(msg);
    errno = -err->error;
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<rtnetlink_request>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    0x2e66(%rip),%rax
mov    (%rax),%edi
mov    0x2e75(%rip),%rax
mov    (%rax),%esi
mov    0x2e3c(%rip),%rax
mov    (%rax),%edx
call   1070 <socket@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    119c <rtnetlink_request+0x4c>
movl   $0xffffffff,-0x4(%rbp)
jmp    1269 <rtnetlink_request+0x119>
mov    -0x24(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%edx
mov    -0x20(%rbp),%r8
xor    %ecx,%ecx
mov    $0x4,%r9d
call   1030 <sendto@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jl     11eb <rtnetlink_request+0x9b>
movl   $0x4,-0x30(%rbp)
mov    -0x24(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
mov    -0x20(%rbp),%r8
xor    %ecx,%ecx
lea    -0x30(%rbp),%r9
call   1060 <recvfrom@plt>
mov    %eax,-0x28(%rbp)
mov    0x2dd6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
mov    -0x2c(%rbp),%ecx
mov    0x2dbf(%rip),%rax
mov    %ecx,(%rax)
cmpl   $0x0,-0x28(%rbp)
jge    1221 <rtnetlink_request+0xd1>
movl   $0xffffffff,-0x4(%rbp)
jmp    1269 <rtnetlink_request+0x119>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    0x2db1(%rip),%rcx
cmp    (%rcx),%rax
jne    1262 <rtnetlink_request+0x112>
mov    -0x10(%rbp),%rdi
call   1050 <NLMSG_DATA@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
xor    %ecx,%ecx
sub    (%rax),%ecx
mov    0x2d74(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1269 <rtnetlink_request+0x119>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rtnetlink_request>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r15
mov    %esi,%r12d
mov    %rdi,%r13
mov    0x2e65(%rip),%rax
mov    (%rax),%edi
mov    0x2e74(%rip),%rax
mov    (%rax),%esi
mov    0x2e3b(%rip),%rax
mov    (%rax),%edx
call   1070 <socket@plt>
mov    $0xffffffff,%r14d
test   %eax,%eax
js     1212 <rtnetlink_request+0xc2>
mov    %eax,%ebp
mov    0x8(%r13),%edx
mov    %eax,%edi
mov    %r13,%rsi
xor    %ecx,%ecx
mov    %r15,%r8
mov    $0x4,%r9d
call   1030 <sendto@plt>
test   %eax,%eax
js     11d0 <rtnetlink_request+0x80>
movl   $0x4,0x4(%rsp)
lea    0x4(%rsp),%r9
mov    %ebp,%edi
mov    %r13,%rsi
mov    %r12d,%edx
xor    %ecx,%ecx
mov    %r15,%r8
call   1060 <recvfrom@plt>
mov    %eax,%ebx
mov    0x2def(%rip),%r15
mov    (%r15),%r12d
mov    %ebp,%edi
call   1040 <close@plt>
mov    %r12d,(%r15)
test   %ebx,%ebx
js     1212 <rtnetlink_request+0xc2>
mov    0x0(%r13),%rax
mov    0x2deb(%rip),%rcx
xor    %r14d,%r14d
cmp    (%rcx),%rax
jne    1212 <rtnetlink_request+0xc2>
mov    %r13,%rdi
call   1050 <NLMSG_DATA@plt>
xor    %ecx,%ecx
sub    (%rax),%ecx
mov    %ecx,(%r15)
mov    $0xffffffff,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.nlmsghdr = type { i64, i32 }
%struct.sockaddr_nl = type { i32 }
%struct.nlmsgerr = type { i32 }
%struct.sockaddr = type { i32 }
@PF_NETLINK = global i32 0
@SOCK_RAW = global i32 0
@NETLINK_ROUTE = global i32 0
@errno = global i32 0
@NLMSG_ERROR = global i64 0
define hidden i32 @rtnetlink_request(%struct.nlmsghdr* %0, i32 %1, %struct.sockaddr_nl* %2) {
%4 = alloca i32
%5 = alloca %struct.nlmsghdr*
%6 = alloca i32
%7 = alloca %struct.sockaddr_nl*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca %struct.nlmsgerr*
store %struct.nlmsghdr* %0, %struct.nlmsghdr** %5
store i32 %1, i32* %6
store %struct.sockaddr_nl* %2, %struct.sockaddr_nl** %7
%13 = load i32, i32* @PF_NETLINK
%14 = load i32, i32* @SOCK_RAW
%15 = load i32, i32* @NETLINK_ROUTE
%16 = call i32 @socket(i32 %13, i32 %14, i32 %15)
store i32 %16, i32* %8
%17 = load i32, i32* %8
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %20
19: ; preds = %3
store i32 -1, i32* %4
br label %60
20: ; preds = %3
%21 = load i32, i32* %8
%22 = load %struct.nlmsghdr*, %struct.nlmsghdr** %5
%23 = load %struct.nlmsghdr*, %struct.nlmsghdr** %5
%24 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %23, i32 0, i32 1
%25 = load i32, i32* %24
%26 = load %struct.sockaddr_nl*, %struct.sockaddr_nl** %7
%27 = bitcast %struct.sockaddr_nl* %26 to %struct.sockaddr*
%28 = call i32 @sendto(i32 %21, %struct.nlmsghdr* %22, i32 %25, i32 0, %struct.sockaddr* %27, i32 4)
store i32 %28, i32* %9
%29 = load i32, i32* %9
%30 = icmp sge i32 %29, 0
br i1 %30, label %31, label %38
31: ; preds = %20
store i32 4, i32* %11
%32 = load i32, i32* %8
%33 = load %struct.nlmsghdr*, %struct.nlmsghdr** %5
%34 = load i32, i32* %6
%35 = load %struct.sockaddr_nl*, %struct.sockaddr_nl** %7
%36 = bitcast %struct.sockaddr_nl* %35 to %struct.sockaddr*
%37 = call i32 @recvfrom(i32 %32, %struct.nlmsghdr* %33, i32 %34, i32 0, %struct.sockaddr* %36, i32* %11)
store i32 %37, i32* %9
br label %38
38: ; preds = %31, %20
%39 = load i32, i32* @errno
store i32 %39, i32* %10
%40 = load i32, i32* %8
%41 = call i32 @close(i32 %40)
%42 = load i32, i32* %10
store i32 %42, i32* @errno
%43 = load i32, i32* %9
%44 = icmp slt i32 %43, 0
br i1 %44, label %45, label %46
45: ; preds = %38
store i32 -1, i32* %4
br label %60
46: ; preds = %38
%47 = load %struct.nlmsghdr*, %struct.nlmsghdr** %5
%48 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %47, i32 0, i32 0
%49 = load i64, i64* %48
%50 = load i64, i64* @NLMSG_ERROR
%51 = icmp eq i64 %49, %50
br i1 %51, label %52, label %59
52: ; preds = %46
%53 = load %struct.nlmsghdr*, %struct.nlmsghdr** %5
%54 = call %struct.nlmsgerr* @NLMSG_DATA(%struct.nlmsghdr* %53)
store %struct.nlmsgerr* %54, %struct.nlmsgerr** %12
%55 = load %struct.nlmsgerr*, %struct.nlmsgerr** %12
%56 = getelementptr inbounds %struct.nlmsgerr, %struct.nlmsgerr* %55, i32 0, i32 0
%57 = load i32, i32* %56
%58 = sub nsw i32 0, %57
store i32 %58, i32* @errno
store i32 -1, i32* %4
br label %60
59: ; preds = %46
store i32 0, i32* %4
br label %60
60: ; preds = %59, %52, %45, %19
%61 = load i32, i32* %4
ret i32 %61
}

LLVM IR (-O2):
%struct.nlmsghdr = type { i64, i32 }
%struct.sockaddr_nl = type { i32 }
%struct.sockaddr = type { i32 }
%struct.nlmsgerr = type { i32 }
@PF_NETLINK = global i32 0
@SOCK_RAW = global i32 0
@NETLINK_ROUTE = global i32 0
@errno = global i32 0
@NLMSG_ERROR = global i64 0
define hidden i32 @rtnetlink_request(%struct.nlmsghdr* %0, i32 %1, %struct.sockaddr_nl* %2) {
%4 = alloca i32
%5 = load i32, i32* @PF_NETLINK
%6 = load i32, i32* @SOCK_RAW
%7 = load i32, i32* @NETLINK_ROUTE
%8 = tail call i32 @socket(i32 %5, i32 %6, i32 %7)
%9 = icmp slt i32 %8, 0
br i1 %9, label %34, label %10
10: ; preds = %3
%11 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %0, i64 0, i32 1
%12 = load i32, i32* %11
%13 = bitcast %struct.sockaddr_nl* %2 to %struct.sockaddr*
%14 = tail call i32 @sendto(i32 %8, %struct.nlmsghdr* %0, i32 %12, i32 0, %struct.sockaddr* %13, i32 4)
%15 = icmp sgt i32 %14, -1
br i1 %15, label %16, label %19
16: ; preds = %10
%17 = bitcast i32* %4 to i8*
store i32 4, i32* %4
%18 = call i32 @recvfrom(i32 %8, %struct.nlmsghdr* %0, i32 %1, i32 0, %struct.sockaddr* %13, i32* %4)
br label %19
19: ; preds = %16, %10
%20 = phi i32 [ %18, %16 ], [ %14, %10 ]
%21 = load i32, i32* @errno
%22 = call i32 @close(i32 %8)
store i32 %21, i32* @errno
%23 = icmp slt i32 %20, 0
br i1 %23, label %34, label %24
24: ; preds = %19
%25 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %0, i64 0, i32 0
%26 = load i64, i64* %25
%27 = load i64, i64* @NLMSG_ERROR
%28 = icmp eq i64 %26, %27
br i1 %28, label %29, label %34
29: ; preds = %24
%30 = call %struct.nlmsgerr* @NLMSG_DATA(%struct.nlmsghdr* %0)
%31 = getelementptr inbounds %struct.nlmsgerr, %struct.nlmsgerr* %30, i64 0, i32 0
%32 = load i32, i32* %31
%33 = sub nsw i32 0, %32
store i32 %33, i32* @errno
br label %34
34: ; preds = %24, %19, %3, %29
%35 = phi i32 [ -1, %29 ], [ -1, %3 ], [ -1, %19 ], [ 0, %24 ]
ret i32 %35
}