#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 /*<<< orphan*/ nl_family;
};
struct nlmsghdr {
  int nlmsg_len;
  scalar_t__ nlmsg_type;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ nladdr;

/* Variables and functions */
int /*<<< orphan*/ AF_NETLINK;
scalar_t__ NLMSG_ERROR;
struct nlmsghdr *NLMSG_NEXT(struct nlmsghdr *, int);
scalar_t__ NLMSG_OK(struct nlmsghdr *, int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ error(int, int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ nl_error(struct nlmsghdr *);
int recvfrom(int, void *, int, int /*<<< orphan*/, void *, int *);
int sendto(int, void *, int, int /*<<< orphan*/, void *, int);
int do_nl_req(int fd, struct nlmsghdr *nh, int len, int max) {
  struct sockaddr_nl nladdr = {.nl_family = AF_NETLINK};
  socklen_t addr_len;
  void *data = nh;
  int rem, ret;
  int err = 0;
  nh->nlmsg_len = len;
  ret = sendto(fd, data, len, 0, (void *)&nladdr, sizeof(nladdr));
  if (ret != len)
    error(1, errno, "send netlink: %uB != %uB\n", ret, len);
  if (max == 0)
    return 0;
  addr_len = sizeof(nladdr);
  rem = ret = recvfrom(fd, data, max, 0, (void *)&nladdr, &addr_len);
  if (ret < 0)
    error(1, errno, "recv netlink: %uB\n", ret);
  for (; NLMSG_OK(nh, rem); nh = NLMSG_NEXT(nh, rem)) {
    if (nh->nlmsg_type == NLMSG_ERROR) {
      nl_error(nh);
      err = 1;
    }
  }
  if (err)
    error(1, 0, "bailing out due to netlink error[s]");
  return ret;
}


x86 assembly (clang, -O0):
<do_nl_req>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
mov    0x2e64(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
movl   $0x0,-0x3c(%rbp)
mov    -0x14(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x8(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    -0x14(%rbp),%edx
lea    -0x20(%rbp),%r8
xor    %ecx,%ecx
mov    $0x4,%r9d
call   1050 <sendto@plt>
mov    %eax,-0x38(%rbp)
mov    -0x38(%rbp),%eax
cmp    -0x14(%rbp),%eax
je     11e6 <do_nl_req+0x86>
mov    0x2e06(%rip),%rax
mov    (%rax),%esi
mov    -0x38(%rbp),%ecx
mov    -0x14(%rbp),%r8d
mov    $0x1,%edi
lea    0xe21(%rip),%rdx
mov    $0x0,%al
call   1070 <error@plt>
cmpl   $0x0,-0x18(%rbp)
jne    11fc <do_nl_req+0x9c>
movl   $0x0,-0x4(%rbp)
jmp    12cd <do_nl_req+0x16d>
movl   $0x4,-0x24(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    -0x18(%rbp),%edx
lea    -0x20(%rbp),%r8
xor    %ecx,%ecx
lea    -0x24(%rbp),%r9
call   1060 <recvfrom@plt>
mov    %eax,-0x38(%rbp)
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x38(%rbp)
jge    124b <do_nl_req+0xeb>
mov    0x2d9d(%rip),%rax
mov    (%rax),%esi
mov    -0x38(%rbp),%ecx
mov    $0x1,%edi
lea    0xdd6(%rip),%rdx
mov    $0x0,%al
call   1070 <error@plt>
jmp    1250 <do_nl_req+0xf0>
mov    -0x10(%rbp),%rdi
mov    -0x34(%rbp),%esi
call   1030 <NLMSG_OK@plt>
cmp    $0x0,%rax
je     12a8 <do_nl_req+0x148>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    0x2d73(%rip),%rcx
cmp    (%rcx),%rax
jne    128e <do_nl_req+0x12e>
mov    -0x10(%rbp),%rdi
call   1040 <nl_error@plt>
movl   $0x1,-0x3c(%rbp)
jmp    1293 <do_nl_req+0x133>
mov    -0x10(%rbp),%rdi
mov    -0x34(%rbp),%esi
call   1080 <NLMSG_NEXT@plt>
mov    %rax,-0x10(%rbp)
jmp    1250 <do_nl_req+0xf0>
cmpl   $0x0,-0x3c(%rbp)
je     12c7 <do_nl_req+0x167>
mov    $0x1,%edi
xor    %esi,%esi
lea    0xd6d(%rip),%rdx
mov    $0x0,%al
call   1070 <error@plt>
mov    -0x38(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_nl_req>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %ecx,%r14d
mov    %edx,%r12d
mov    %rsi,%rbx
mov    %edi,%r15d
mov    0x2e61(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    %edx,(%rsi)
xor    %ebp,%ebp
lea    0x8(%rsp),%r8
xor    %ecx,%ecx
mov    $0x4,%r9d
call   1050 <sendto@plt>
cmp    %r12d,%eax
je     11c1 <do_nl_req+0x61>
mov    0x2e29(%rip),%rcx
mov    (%rcx),%esi
lea    0xe50(%rip),%rdx
mov    $0x1,%edi
mov    %eax,%ecx
mov    %r12d,%r8d
xor    %eax,%eax
call   1070 <error@plt>
test   %r14d,%r14d
je     127f <do_nl_req+0x11f>
movl   $0x4,0xc(%rsp)
lea    0x8(%rsp),%r8
lea    0xc(%rsp),%r9
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %r14d,%edx
xor    %ecx,%ecx
call   1060 <recvfrom@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    1210 <do_nl_req+0xb0>
mov    0x2dd7(%rip),%rax
mov    (%rax),%esi
lea    0xe18(%rip),%rdx
mov    $0x1,%edi
mov    %ebp,%ecx
xor    %eax,%eax
call   1070 <error@plt>
mov    %rbx,%rdi
mov    %ebp,%esi
call   1030 <NLMSG_OK@plt>
test   %rax,%rax
je     127f <do_nl_req+0x11f>
xor    %r15d,%r15d
mov    0x2dbf(%rip),%r14
jmp    124c <do_nl_req+0xec>
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %ebp,%esi
call   1080 <NLMSG_NEXT@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %ebp,%esi
call   1030 <NLMSG_OK@plt>
test   %rax,%rax
je     1265 <do_nl_req+0x105>
mov    0x8(%rbx),%rax
cmp    (%r14),%rax
jne    1230 <do_nl_req+0xd0>
mov    %rbx,%rdi
call   1040 <nl_error@plt>
mov    $0x1,%r15d
jmp    1230 <do_nl_req+0xd0>
test   %r15d,%r15d
je     127f <do_nl_req+0x11f>
lea    0xdbc(%rip),%rdx
mov    $0x1,%edi
xor    %esi,%esi
xor    %eax,%eax
call   1070 <error@plt>
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.nlmsghdr = type { i32, i64 }
%struct.sockaddr_nl = type { i32 }
@AF_NETLINK = global i32 0
@errno = global i32 0
@.str = [26 x i8] c"send netlink: %uB != %uB\0A\00"
@.str.1 = [19 x i8] c"recv netlink: %uB\0A\00"
@NLMSG_ERROR = global i64 0
@.str.2 = [36 x i8] c"bailing out due to netlink error[s]\00"
define i32 @do_nl_req(i32 %0, %struct.nlmsghdr* %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.nlmsghdr*
%8 = alloca i32
%9 = alloca i32
%10 = alloca %struct.sockaddr_nl
%11 = alloca i32
%12 = alloca i8*
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
store i32 %0, i32* %6
store %struct.nlmsghdr* %1, %struct.nlmsghdr** %7
store i32 %2, i32* %8
store i32 %3, i32* %9
%16 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %10, i32 0, i32 0
%17 = load i32, i32* @AF_NETLINK
store i32 %17, i32* %16
%18 = load %struct.nlmsghdr*, %struct.nlmsghdr** %7
%19 = bitcast %struct.nlmsghdr* %18 to i8*
store i8* %19, i8** %12
store i32 0, i32* %15
%20 = load i32, i32* %8
%21 = load %struct.nlmsghdr*, %struct.nlmsghdr** %7
%22 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %21, i32 0, i32 0
store i32 %20, i32* %22
%23 = load i32, i32* %6
%24 = load i8*, i8** %12
%25 = load i32, i32* %8
%26 = bitcast %struct.sockaddr_nl* %10 to i8*
%27 = call i32 @sendto(i32 %23, i8* %24, i32 %25, i32 0, i8* %26, i32 4)
store i32 %27, i32* %14
%28 = load i32, i32* %14
%29 = load i32, i32* %8
%30 = icmp ne i32 %28, %29
br i1 %30, label %31, label %36
31: ; preds = %4
%32 = load i32, i32* @errno
%33 = load i32, i32* %14
%34 = load i32, i32* %8
%35 = call i32 (i32, i32, i8*, ...) @error(i32 1, i32 %32, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i32 %33, i32 %34)
br label %36
36: ; preds = %31, %4
%37 = load i32, i32* %9
%38 = icmp eq i32 %37, 0
br i1 %38, label %39, label %40
39: ; preds = %36
store i32 0, i32* %5
br label %79
40: ; preds = %36
store i32 4, i32* %11
%41 = load i32, i32* %6
%42 = load i8*, i8** %12
%43 = load i32, i32* %9
%44 = bitcast %struct.sockaddr_nl* %10 to i8*
%45 = call i32 @recvfrom(i32 %41, i8* %42, i32 %43, i32 0, i8* %44, i32* %11)
store i32 %45, i32* %14
store i32 %45, i32* %13
%46 = load i32, i32* %14
%47 = icmp slt i32 %46, 0
br i1 %47, label %48, label %52
48: ; preds = %40
%49 = load i32, i32* @errno
%50 = load i32, i32* %14
%51 = call i32 (i32, i32, i8*, ...) @error(i32 1, i32 %49, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i32 %50)
br label %52
52: ; preds = %48, %40
br label %53
53: ; preds = %68, %52
%54 = load %struct.nlmsghdr*, %struct.nlmsghdr** %7
%55 = load i32, i32* %13
%56 = call i64 @NLMSG_OK(%struct.nlmsghdr* %54, i32 %55)
%57 = icmp ne i64 %56, 0
br i1 %57, label %58, label %72
58: ; preds = %53
%59 = load %struct.nlmsghdr*, %struct.nlmsghdr** %7
%60 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %59, i32 0, i32 1
%61 = load i64, i64* %60
%62 = load i64, i64* @NLMSG_ERROR
%63 = icmp eq i64 %61, %62
br i1 %63, label %64, label %67
64: ; preds = %58
%65 = load %struct.nlmsghdr*, %struct.nlmsghdr** %7
%66 = call i32 @nl_error(%struct.nlmsghdr* %65)
store i32 1, i32* %15
br label %67
67: ; preds = %64, %58
br label %68
68: ; preds = %67
%69 = load %struct.nlmsghdr*, %struct.nlmsghdr** %7
%70 = load i32, i32* %13
%71 = call %struct.nlmsghdr* @NLMSG_NEXT(%struct.nlmsghdr* %69, i32 %70)
store %struct.nlmsghdr* %71, %struct.nlmsghdr** %7
br label %53
72: ; preds = %53
%73 = load i32, i32* %15
%74 = icmp ne i32 %73, 0
br i1 %74, label %75, label %77
75: ; preds = %72
%76 = call i32 (i32, i32, i8*, ...) @error(i32 1, i32 0, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0))
br label %77
77: ; preds = %75, %72
%78 = load i32, i32* %14
store i32 %78, i32* %5
br label %79
79: ; preds = %77, %39
%80 = load i32, i32* %5
ret i32 %80
}

LLVM IR (-O2):
%struct.nlmsghdr = type { i32, i64 }
%struct.sockaddr_nl = type { i32 }
@AF_NETLINK = global i32 0
@errno = global i32 0
@.str = [26 x i8] c"send netlink: %uB != %uB\0A\00"
@.str.1 = [19 x i8] c"recv netlink: %uB\0A\00"
@NLMSG_ERROR = global i64 0
@.str.2 = [36 x i8] c"bailing out due to netlink error[s]\00"
define i32 @do_nl_req(i32 %0, %struct.nlmsghdr* %1, i32 %2, i32 %3) {
%5 = alloca %struct.sockaddr_nl
%6 = alloca i32
%7 = bitcast %struct.sockaddr_nl* %5 to i8*
%8 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %5, i64 0, i32 0
%9 = load i32, i32* @AF_NETLINK
store i32 %9, i32* %8
%10 = bitcast i32* %6 to i8*
%11 = bitcast %struct.nlmsghdr* %1 to i8*
%12 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %1, i64 0, i32 0
store i32 %2, i32* %12
%13 = call i32 @sendto(i32 %0, i8* %11, i32 %2, i32 0, i8* %7, i32 4)
%14 = icmp eq i32 %13, %2
br i1 %14, label %18, label %15
15: ; preds = %4
%16 = load i32, i32* @errno
%17 = call i32 (i32, i32, i8*, ...) @error(i32 1, i32 %16, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i32 %13, i32 %2)
br label %18
18: ; preds = %15, %4
%19 = icmp eq i32 %3, 0
br i1 %19, label %47, label %20
20: ; preds = %18
store i32 4, i32* %6
%21 = call i32 @recvfrom(i32 %0, i8* %11, i32 %3, i32 0, i8* %7, i32* %6)
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %20
%24 = load i32, i32* @errno
%25 = call i32 (i32, i32, i8*, ...) @error(i32 1, i32 %24, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i32 %21)
br label %26
26: ; preds = %23, %20
%27 = call i64 @NLMSG_OK(%struct.nlmsghdr* %1, i32 %21)
%28 = icmp eq i64 %27, 0
br i1 %28, label %47, label %29
29: ; preds = %26, %38
%30 = phi i32 [ %39, %38 ], [ 0, %26 ]
%31 = phi %struct.nlmsghdr* [ %40, %38 ], [ %1, %26 ]
%32 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %31, i64 0, i32 1
%33 = load i64, i64* %32
%34 = load i64, i64* @NLMSG_ERROR
%35 = icmp eq i64 %33, %34
br i1 %35, label %36, label %38
36: ; preds = %29
%37 = call i32 @nl_error(%struct.nlmsghdr* %31)
br label %38
38: ; preds = %29, %36
%39 = phi i32 [ 1, %36 ], [ %30, %29 ]
%40 = call %struct.nlmsghdr* @NLMSG_NEXT(%struct.nlmsghdr* %31, i32 %21)
%41 = call i64 @NLMSG_OK(%struct.nlmsghdr* %40, i32 %21)
%42 = icmp eq i64 %41, 0
br i1 %42, label %43, label %29
43: ; preds = %38
%44 = icmp eq i32 %39, 0
br i1 %44, label %47, label %45
45: ; preds = %43
%46 = call i32 (i32, i32, i8*, ...) @error(i32 1, i32 0, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0))
br label %47
47: ; preds = %26, %43, %45, %18
%48 = phi i32 [ 0, %18 ], [ %21, %45 ], [ %21, %43 ], [ %21, %26 ]
ret i32 %48
}