#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 {
  scalar_t__ nl_groups;
  scalar_t__ nl_pid;
  int /*<<< orphan*/ nl_family;
};
struct nlmsghdr {
  int /*<<< orphan*/ nlmsg_len;
};
struct msghdr {
  int msg_namelen;
  int msg_iovlen;
  struct iovec *msg_iov;
  struct sockaddr_nl *msg_name;
};
struct iovec {
  int /*<<< orphan*/ iov_len;
  struct nlmsghdr *iov_base;
};
typedef int /*<<< orphan*/ nladdr;

/* Variables and functions */
int /*<<< orphan*/ AF_NETLINK;
int /*<<< orphan*/ MSG_NOSIGNAL;
int /*<<< orphan*/ memset(struct sockaddr_nl *, int /*<<< orphan*/, int);
int sendmsg(int, struct msghdr *, int /*<<< orphan*/);
int __netlink_send(int fd, struct nlmsghdr *nlmsghdr) {
  int ret;
  struct sockaddr_nl nladdr;
  struct iovec iov = {
      .iov_base = nlmsghdr,
      .iov_len = nlmsghdr->nlmsg_len,
  };
  struct msghdr msg = {
      .msg_name = &nladdr,
      .msg_namelen = sizeof(nladdr),
      .msg_iov = &iov,
      .msg_iovlen = 1,
  };
  memset(&nladdr, 0, sizeof(nladdr));
  nladdr.nl_family = AF_NETLINK;
  nladdr.nl_pid = 0;
  nladdr.nl_groups = 0;
  ret = sendmsg(fd, &msg, MSG_NOSIGNAL);
  if (ret < 0)
    return -1;
  return ret;
}


x86 assembly (clang, -O0):
<__netlink_send>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
movl   $0x18,-0x58(%rbp)
movl   $0x1,-0x54(%rbp)
lea    -0x40(%rbp),%rax
mov    %rax,-0x50(%rbp)
lea    -0x30(%rbp),%rax
mov    %rax,-0x48(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1040 <memset@plt>
mov    0x2e73(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e44(%rip),%rax
mov    (%rax),%edx
lea    -0x58(%rbp),%rsi
call   1030 <sendmsg@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11b8 <__netlink_send+0x98>
movl   $0xffffffff,-0x4(%rbp)
jmp    11be <__netlink_send+0x9e>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<__netlink_send>:
push   %rbx
sub    $0x50,%rsp
mov    %edi,%ebx
mov    (%rsi),%eax
mov    %eax,0x8(%rsp)
mov    %rsi,0x10(%rsp)
movabs $0x100000018,%rax
mov    %rax,0x18(%rsp)
lea    0x8(%rsp),%rax
mov    %rax,0x20(%rsp)
lea    0x30(%rsp),%rdi
mov    %rdi,0x28(%rsp)
xor    %esi,%esi
mov    $0x18,%edx
call   1040 <memset@plt>
mov    0x2e80(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x40(%rsp)
xorps  %xmm0,%xmm0
movaps %xmm0,0x30(%rsp)
mov    0x2e5b(%rip),%rax
mov    (%rax),%edx
lea    0x18(%rsp),%rsi
mov    %ebx,%edi
call   1030 <sendmsg@plt>
test   %eax,%eax
mov    $0xffffffff,%ecx
cmovs  %ecx,%eax
add    $0x50,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.nlmsghdr = type { i32 }
%struct.sockaddr_nl = type { i64, i64, i32 }
%struct.iovec = type { i32, %struct.nlmsghdr* }
%struct.msghdr = type { i32, i32, %struct.iovec*, %struct.sockaddr_nl* }
@AF_NETLINK = global i32 0
@MSG_NOSIGNAL = global i32 0
define i32 @__netlink_send(i32 %0, %struct.nlmsghdr* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.nlmsghdr*
%6 = alloca i32
%7 = alloca %struct.sockaddr_nl
%8 = alloca %struct.iovec
%9 = alloca %struct.msghdr
store i32 %0, i32* %4
store %struct.nlmsghdr* %1, %struct.nlmsghdr** %5
%10 = getelementptr inbounds %struct.iovec, %struct.iovec* %8, i32 0, i32 0
%11 = load %struct.nlmsghdr*, %struct.nlmsghdr** %5
%12 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %11, i32 0, i32 0
%13 = load i32, i32* %12
store i32 %13, i32* %10
%14 = getelementptr inbounds %struct.iovec, %struct.iovec* %8, i32 0, i32 1
%15 = load %struct.nlmsghdr*, %struct.nlmsghdr** %5
store %struct.nlmsghdr* %15, %struct.nlmsghdr** %14
%16 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %9, i32 0, i32 0
store i32 24, i32* %16
%17 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %9, i32 0, i32 1
store i32 1, i32* %17
%18 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %9, i32 0, i32 2
store %struct.iovec* %8, %struct.iovec** %18
%19 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %9, i32 0, i32 3
store %struct.sockaddr_nl* %7, %struct.sockaddr_nl** %19
%20 = call i32 @memset(%struct.sockaddr_nl* %7, i32 0, i32 24)
%21 = load i32, i32* @AF_NETLINK
%22 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %7, i32 0, i32 2
store i32 %21, i32* %22
%23 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %7, i32 0, i32 1
store i64 0, i64* %23
%24 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %7, i32 0, i32 0
store i64 0, i64* %24
%25 = load i32, i32* %4
%26 = load i32, i32* @MSG_NOSIGNAL
%27 = call i32 @sendmsg(i32 %25, %struct.msghdr* %9, i32 %26)
store i32 %27, i32* %6
%28 = load i32, i32* %6
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %31
30: ; preds = %2
store i32 -1, i32* %3
br label %33
31: ; preds = %2
%32 = load i32, i32* %6
store i32 %32, i32* %3
br label %33
33: ; preds = %31, %30
%34 = load i32, i32* %3
ret i32 %34
}

LLVM IR (-O2):
%struct.nlmsghdr = type { i32 }
%struct.sockaddr_nl = type { i64, i64, i32 }
%struct.iovec = type { i32, %struct.nlmsghdr* }
%struct.msghdr = type { i32, i32, %struct.iovec*, %struct.sockaddr_nl* }
@AF_NETLINK = global i32 0
@MSG_NOSIGNAL = global i32 0
define i32 @__netlink_send(i32 %0, %struct.nlmsghdr* %1) {
%3 = alloca %struct.sockaddr_nl
%4 = alloca %struct.iovec
%5 = alloca %struct.msghdr
%6 = bitcast %struct.sockaddr_nl* %3 to i8*
%7 = bitcast %struct.iovec* %4 to i8*
%8 = getelementptr inbounds %struct.iovec, %struct.iovec* %4, i64 0, i32 0
%9 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %1, i64 0, i32 0
%10 = load i32, i32* %9
store i32 %10, i32* %8
%11 = getelementptr inbounds %struct.iovec, %struct.iovec* %4, i64 0, i32 1
store %struct.nlmsghdr* %1, %struct.nlmsghdr** %11
%12 = bitcast %struct.msghdr* %5 to i8*
%13 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %5, i64 0, i32 0
store i32 24, i32* %13
%14 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %5, i64 0, i32 1
store i32 1, i32* %14
%15 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %5, i64 0, i32 2
store %struct.iovec* %4, %struct.iovec** %15
%16 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %5, i64 0, i32 3
store %struct.sockaddr_nl* %3, %struct.sockaddr_nl** %16
%17 = call i32 @memset(%struct.sockaddr_nl* %3, i32 0, i32 24)
%18 = load i32, i32* @AF_NETLINK
%19 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %3, i64 0, i32 2
store i32 %18, i32* %19
%20 = bitcast %struct.sockaddr_nl* %3 to i8*
%21 = load i32, i32* @MSG_NOSIGNAL
%22 = call i32 @sendmsg(i32 %0, %struct.msghdr* %5, i32 %21)
%23 = icmp sgt i32 %22, -1
%24 = select i1 %23, i32 %22, i32 -1
ret i32 %24
}