#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 /*<<< orphan*/ nlmsg_len;
};
struct msghdr {
  int member_1;
  int member_3;
  int /*<<< orphan*/ member_6;
  int /*<<< orphan*/ member_5;
  int /*<<< orphan*/ *member_4;
  struct iovec *member_2;
  struct sockaddr_nl *member_0;
};
struct iovec {
  int /*<<< orphan*/ member_1;
  struct nlmsghdr *member_0;
};

/* Variables and functions */
int /*<<< orphan*/ AF_NETLINK;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
struct sockaddr_nl *malloc(int);
int /*<<< orphan*/ memset(struct sockaddr_nl *, int /*<<< orphan*/, int);
scalar_t__ sendmsg(int, struct msghdr *, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int _nlmsg_send(int fd, struct nlmsghdr *nlmsg) {
  struct sockaddr_nl *sa = malloc(sizeof(struct sockaddr_nl));
  memset(sa, 0, sizeof(struct sockaddr_nl));
  sa->nl_family = AF_NETLINK;
  struct iovec iov = {nlmsg, nlmsg->nlmsg_len};
  struct msghdr msg = {sa, sizeof(*sa), &iov, 1, NULL, 0, 0};
  if (sendmsg(fd, &msg, 0) < 0) {
    fprintf(stderr, "failed to get socket: %s\n", strerror(errno));
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<_nlmsg_send>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x4,%edi
call   1060 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1040 <memset@plt>
mov    0x2e64(%rip),%rax
mov    (%rax),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rax
mov    %eax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
movslq (%rax),%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    %eax,-0x50(%rbp)
movl   $0x4,-0x4c(%rbp)
lea    -0x28(%rbp),%rax
mov    %eax,-0x48(%rbp)
movl   $0x1,-0x44(%rbp)
movq   $0x0,-0x40(%rbp)
movq   $0x0,-0x38(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x50(%rbp),%rsi
xor    %edx,%edx
call   1030 <sendmsg@plt>
cmp    $0x0,%rax
jge    1222 <_nlmsg_send+0xd2>
mov    0x2de7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x54(%rbp)
mov    0x2dd3(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x54(%rbp),%edi
mov    %rax,%rdx
lea    0xdef(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1229 <_nlmsg_send+0xd9>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_nlmsg_send>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x38,%rsp
mov    %rsi,%rbp
mov    %edi,%r14d
mov    $0x4,%edi
call   1060 <malloc@plt>
mov    %rax,%rbx
xor    %r15d,%r15d
mov    %rax,%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1040 <memset@plt>
mov    0x2e62(%rip),%rax
mov    (%rax),%eax
mov    %eax,(%rbx)
mov    %ebp,(%rsp)
movslq 0x0(%rbp),%rax
mov    %rax,0x8(%rsp)
mov    %ebx,0x10(%rsp)
movl   $0x4,0x14(%rsp)
mov    %rsp,%rax
mov    %eax,0x18(%rsp)
movl   $0x1,0x1c(%rsp)
xorps  %xmm0,%xmm0
movups %xmm0,0x20(%rsp)
movq   $0x0,0x30(%rsp)
lea    0x10(%rsp),%rsi
mov    %r14d,%edi
xor    %edx,%edx
call   1030 <sendmsg@plt>
test   %rax,%rax
jns    1204 <_nlmsg_send+0xb4>
mov    0x2dfb(%rip),%rax
mov    (%rax),%ebx
mov    0x2dea(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xe0c(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1050 <fprintf@plt>
mov    $0x1,%r15d
mov    %r15d,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.nlmsghdr = type { i32 }
%struct.sockaddr_nl = type { i32 }
%struct.iovec = type { i32, %struct.nlmsghdr* }
%struct.msghdr = type { i32, i32, i32, i32, i32*, %struct.iovec*, %struct.sockaddr_nl* }
@AF_NETLINK = global i32 0
@stderr = global i32 0
@.str = [26 x i8] c"failed to get socket: %s\0A\00"
@errno = global i32 0
define i32 @_nlmsg_send(i32 %0, %struct.nlmsghdr* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.nlmsghdr*
%6 = alloca %struct.sockaddr_nl*
%7 = alloca %struct.iovec
%8 = alloca %struct.msghdr
store i32 %0, i32* %4
store %struct.nlmsghdr* %1, %struct.nlmsghdr** %5
%9 = call %struct.sockaddr_nl* @malloc(i32 4)
store %struct.sockaddr_nl* %9, %struct.sockaddr_nl** %6
%10 = load %struct.sockaddr_nl*, %struct.sockaddr_nl** %6
%11 = call i32 @memset(%struct.sockaddr_nl* %10, i32 0, i32 4)
%12 = load i32, i32* @AF_NETLINK
%13 = load %struct.sockaddr_nl*, %struct.sockaddr_nl** %6
%14 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %13, i32 0, i32 0
store i32 %12, i32* %14
%15 = getelementptr inbounds %struct.iovec, %struct.iovec* %7, i32 0, i32 0
%16 = load %struct.nlmsghdr*, %struct.nlmsghdr** %5
%17 = ptrtoint %struct.nlmsghdr* %16 to i32
store i32 %17, i32* %15
%18 = getelementptr inbounds %struct.iovec, %struct.iovec* %7, i32 0, i32 1
%19 = load %struct.nlmsghdr*, %struct.nlmsghdr** %5
%20 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %19, i32 0, i32 0
%21 = load i32, i32* %20
%22 = sext i32 %21 to i64
%23 = inttoptr i64 %22 to %struct.nlmsghdr*
store %struct.nlmsghdr* %23, %struct.nlmsghdr** %18
%24 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %8, i32 0, i32 0
%25 = load %struct.sockaddr_nl*, %struct.sockaddr_nl** %6
%26 = ptrtoint %struct.sockaddr_nl* %25 to i32
store i32 %26, i32* %24
%27 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %8, i32 0, i32 1
store i32 4, i32* %27
%28 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %8, i32 0, i32 2
%29 = ptrtoint %struct.iovec* %7 to i32
store i32 %29, i32* %28
%30 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %8, i32 0, i32 3
store i32 1, i32* %30
%31 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %8, i32 0, i32 4
store i32* null, i32** %31
%32 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %8, i32 0, i32 5
store %struct.iovec* null, %struct.iovec** %32
%33 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %8, i32 0, i32 6
store %struct.sockaddr_nl* null, %struct.sockaddr_nl** %33
%34 = load i32, i32* %4
%35 = call i64 @sendmsg(i32 %34, %struct.msghdr* %8, i32 0)
%36 = icmp slt i64 %35, 0
br i1 %36, label %37, label %42
37: ; preds = %2
%38 = load i32, i32* @stderr
%39 = load i32, i32* @errno
%40 = call i8* @strerror(i32 %39)
%41 = call i32 @fprintf(i32 %38, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i8* %40)
store i32 1, i32* %3
br label %43
42: ; preds = %2
store i32 0, i32* %3
br label %43
43: ; preds = %42, %37
%44 = load i32, i32* %3
ret i32 %44
}

LLVM IR (-O2):
%struct.nlmsghdr = type { i32 }
%struct.iovec = type { i32, %struct.nlmsghdr* }
%struct.msghdr = type { i32, i32, i32, i32, i32*, %struct.iovec*, %struct.sockaddr_nl* }
%struct.sockaddr_nl = type { i32 }
@AF_NETLINK = global i32 0
@stderr = global i32 0
@.str = [26 x i8] c"failed to get socket: %s\0A\00"
@errno = global i32 0
define i32 @_nlmsg_send(i32 %0, %struct.nlmsghdr* %1) {
%3 = alloca %struct.iovec
%4 = alloca %struct.msghdr
%5 = tail call %struct.sockaddr_nl* @malloc(i32 4)
%6 = tail call i32 @memset(%struct.sockaddr_nl* %5, i32 0, i32 4)
%7 = load i32, i32* @AF_NETLINK
%8 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %5, i64 0, i32 0
store i32 %7, i32* %8
%9 = bitcast %struct.iovec* %3 to i8*
%10 = getelementptr inbounds %struct.iovec, %struct.iovec* %3, i64 0, i32 0
%11 = ptrtoint %struct.nlmsghdr* %1 to i64
%12 = trunc i64 %11 to i32
store i32 %12, i32* %10
%13 = getelementptr inbounds %struct.iovec, %struct.iovec* %3, i64 0, i32 1
%14 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %1, i64 0, i32 0
%15 = load i32, i32* %14
%16 = sext i32 %15 to i64
%17 = inttoptr i64 %16 to %struct.nlmsghdr*
store %struct.nlmsghdr* %17, %struct.nlmsghdr** %13
%18 = bitcast %struct.msghdr* %4 to i8*
%19 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %4, i64 0, i32 0
%20 = ptrtoint %struct.sockaddr_nl* %5 to i64
%21 = trunc i64 %20 to i32
store i32 %21, i32* %19
%22 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %4, i64 0, i32 1
store i32 4, i32* %22
%23 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %4, i64 0, i32 2
%24 = ptrtoint %struct.iovec* %3 to i64
%25 = trunc i64 %24 to i32
store i32 %25, i32* %23
%26 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %4, i64 0, i32 3
store i32 1, i32* %26
%27 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %4, i64 0, i32 4
%28 = bitcast i32** %27 to i8*
%29 = call i64 @sendmsg(i32 %0, %struct.msghdr* %4, i32 0)
%30 = icmp slt i64 %29, 0
br i1 %30, label %31, label %36
31: ; preds = %2
%32 = load i32, i32* @stderr
%33 = load i32, i32* @errno
%34 = call i8* @strerror(i32 %33)
%35 = call i32 @fprintf(i32 %32, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i8* %34)
br label %36
36: ; preds = %2, %31
%37 = phi i32 [ 1, %31 ], [ 0, %2 ]
ret i32 %37
}