#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 nlmsghdr {
  int nlmsg_len;
  int msg_namelen;
  int msg_iovlen;
  struct iovec *msg_iov;
  void *msg_name;
  scalar_t__ nlmsg_flags;
  int /*<<< orphan*/ nlmsg_pid;
};
struct msghdr {
  int nlmsg_len;
  int msg_namelen;
  int msg_iovlen;
  struct iovec *msg_iov;
  void *msg_name;
  scalar_t__ nlmsg_flags;
  int /*<<< orphan*/ nlmsg_pid;
};
struct iovec {
  int iov_len;
  void *iov_base;
};

/* Variables and functions */
int NLMSG_SPACE(size_t);
int /*<<< orphan*/ getpid();
scalar_t__ malloc(int);
int /*<<< orphan*/ memset(struct nlmsghdr *, int /*<<< orphan*/, int);
void build_nl_msg(struct sockaddr_nl *dest_addr, struct nlmsghdr **nlh,
                  struct iovec **iov, struct msghdr *msg, size_t data_len) {
  *nlh = (struct nlmsghdr *)malloc(NLMSG_SPACE(data_len));
  memset(*nlh, 0, NLMSG_SPACE(data_len));
  (*nlh)->nlmsg_len = NLMSG_SPACE(data_len);
  (*nlh)->nlmsg_pid = getpid();
  (*nlh)->nlmsg_flags = 0;
  (*iov)->iov_base = (void *)*nlh;
  (*iov)->iov_len = (*nlh)->nlmsg_len;
  memset(msg, 0, sizeof(struct msghdr));
  msg->msg_name = (void *)dest_addr;
  msg->msg_namelen = sizeof(struct sockaddr_nl);
  msg->msg_iov = *iov;
  msg->msg_iovlen = 1;
}


x86 assembly (clang, -O0):
<build_nl_msg>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
call   1050 <NLMSG_SPACE@plt>
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rdi
call   1050 <NLMSG_SPACE@plt>
mov    -0x30(%rbp),%rdi
mov    %eax,%edx
xor    %esi,%esi
call   1040 <memset@plt>
mov    -0x28(%rbp),%rdi
call   1050 <NLMSG_SPACE@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %ecx,(%rax)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %ecx,0x28(%rax)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
movq   $0x0,0x20(%rax)
mov    -0x10(%rbp),%rax
mov    (%rax),%rcx
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    %rcx,0x8(%rax)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%ecx
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    %ecx,(%rax)
mov    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x30,%edx
call   1040 <memset@plt>
mov    -0x8(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,0x18(%rax)
mov    -0x20(%rbp),%rax
movl   $0x4,0x4(%rax)
mov    -0x18(%rbp),%rax
mov    (%rax),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,0x10(%rax)
mov    -0x20(%rbp),%rax
movl   $0x1,0x8(%rax)
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<build_nl_msg>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r8,%rbx
mov    %rcx,%r12
mov    %rdx,%r14
mov    %rsi,%r13
mov    %rdi,%r15
mov    %r8,%rdi
call   1050 <NLMSG_SPACE@plt>
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,%rbp
mov    %rax,0x0(%r13)
mov    %rbx,%rdi
call   1050 <NLMSG_SPACE@plt>
mov    %rbp,%rdi
xor    %esi,%esi
mov    %eax,%edx
call   1040 <memset@plt>
mov    %rbx,%rdi
call   1050 <NLMSG_SPACE@plt>
mov    0x0(%r13),%rcx
mov    %eax,(%rcx)
xor    %eax,%eax
call   1030 <getpid@plt>
mov    0x0(%r13),%rcx
mov    %eax,0x28(%rcx)
movq   $0x0,0x20(%rcx)
mov    (%r14),%rax
mov    %rcx,0x8(%rax)
mov    0x0(%r13),%rax
mov    (%rax),%eax
mov    (%r14),%rcx
mov    %eax,(%rcx)
mov    %r12,%rdi
xor    %esi,%esi
mov    $0x30,%edx
call   1040 <memset@plt>
mov    %r15,0x18(%r12)
movl   $0x4,0x4(%r12)
mov    (%r14),%rax
mov    %rax,0x10(%r12)
movl   $0x1,0x8(%r12)
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_nl = type { i32 }
%struct.nlmsghdr = type { i32, i32, i32, %struct.iovec*, i8*, i64, i32 }
%struct.iovec = type { i32, i8* }
%struct.msghdr = type { i32, i32, i32, %struct.iovec*, i8*, i64, i32 }
define void @build_nl_msg(%struct.sockaddr_nl* %0, %struct.nlmsghdr** %1, %struct.iovec** %2, %struct.msghdr* %3, i64 %4) {
%6 = alloca %struct.sockaddr_nl*
%7 = alloca %struct.nlmsghdr**
%8 = alloca %struct.iovec**
%9 = alloca %struct.msghdr*
%10 = alloca i64
store %struct.sockaddr_nl* %0, %struct.sockaddr_nl** %6
store %struct.nlmsghdr** %1, %struct.nlmsghdr*** %7
store %struct.iovec** %2, %struct.iovec*** %8
store %struct.msghdr* %3, %struct.msghdr** %9
store i64 %4, i64* %10
%11 = load i64, i64* %10
%12 = call i32 @NLMSG_SPACE(i64 %11)
%13 = call i64 @malloc(i32 %12)
%14 = inttoptr i64 %13 to %struct.nlmsghdr*
%15 = load %struct.nlmsghdr**, %struct.nlmsghdr*** %7
store %struct.nlmsghdr* %14, %struct.nlmsghdr** %15
%16 = load %struct.nlmsghdr**, %struct.nlmsghdr*** %7
%17 = load %struct.nlmsghdr*, %struct.nlmsghdr** %16
%18 = load i64, i64* %10
%19 = call i32 @NLMSG_SPACE(i64 %18)
%20 = call i32 @memset(%struct.nlmsghdr* %17, i32 0, i32 %19)
%21 = load i64, i64* %10
%22 = call i32 @NLMSG_SPACE(i64 %21)
%23 = load %struct.nlmsghdr**, %struct.nlmsghdr*** %7
%24 = load %struct.nlmsghdr*, %struct.nlmsghdr** %23
%25 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %24, i32 0, i32 0
store i32 %22, i32* %25
%26 = call i32 (...) @getpid()
%27 = load %struct.nlmsghdr**, %struct.nlmsghdr*** %7
%28 = load %struct.nlmsghdr*, %struct.nlmsghdr** %27
%29 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %28, i32 0, i32 6
store i32 %26, i32* %29
%30 = load %struct.nlmsghdr**, %struct.nlmsghdr*** %7
%31 = load %struct.nlmsghdr*, %struct.nlmsghdr** %30
%32 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %31, i32 0, i32 5
store i64 0, i64* %32
%33 = load %struct.nlmsghdr**, %struct.nlmsghdr*** %7
%34 = load %struct.nlmsghdr*, %struct.nlmsghdr** %33
%35 = bitcast %struct.nlmsghdr* %34 to i8*
%36 = load %struct.iovec**, %struct.iovec*** %8
%37 = load %struct.iovec*, %struct.iovec** %36
%38 = getelementptr inbounds %struct.iovec, %struct.iovec* %37, i32 0, i32 1
store i8* %35, i8** %38
%39 = load %struct.nlmsghdr**, %struct.nlmsghdr*** %7
%40 = load %struct.nlmsghdr*, %struct.nlmsghdr** %39
%41 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %40, i32 0, i32 0
%42 = load i32, i32* %41
%43 = load %struct.iovec**, %struct.iovec*** %8
%44 = load %struct.iovec*, %struct.iovec** %43
%45 = getelementptr inbounds %struct.iovec, %struct.iovec* %44, i32 0, i32 0
store i32 %42, i32* %45
%46 = load %struct.msghdr*, %struct.msghdr** %9
%47 = bitcast %struct.msghdr* %46 to %struct.nlmsghdr*
%48 = call i32 @memset(%struct.nlmsghdr* %47, i32 0, i32 48)
%49 = load %struct.sockaddr_nl*, %struct.sockaddr_nl** %6
%50 = bitcast %struct.sockaddr_nl* %49 to i8*
%51 = load %struct.msghdr*, %struct.msghdr** %9
%52 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %51, i32 0, i32 4
store i8* %50, i8** %52
%53 = load %struct.msghdr*, %struct.msghdr** %9
%54 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %53, i32 0, i32 1
store i32 4, i32* %54
%55 = load %struct.iovec**, %struct.iovec*** %8
%56 = load %struct.iovec*, %struct.iovec** %55
%57 = load %struct.msghdr*, %struct.msghdr** %9
%58 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %57, i32 0, i32 3
store %struct.iovec* %56, %struct.iovec** %58
%59 = load %struct.msghdr*, %struct.msghdr** %9
%60 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %59, i32 0, i32 2
store i32 1, i32* %60
ret void
}

LLVM IR (-O2):
%struct.sockaddr_nl = type { i32 }
%struct.nlmsghdr = type { i32, i32, i32, %struct.iovec*, i8*, i64, i32 }
%struct.iovec = type { i32, i8* }
%struct.msghdr = type { i32, i32, i32, %struct.iovec*, i8*, i64, i32 }
define void @build_nl_msg(%struct.sockaddr_nl* %0, %struct.nlmsghdr** nocapture %1, %struct.iovec** nocapture %2, %struct.msghdr* %3, i64 %4) {
%6 = tail call i32 @NLMSG_SPACE(i64 %4)
%7 = tail call i64 @malloc(i32 %6)
%8 = inttoptr i64 %7 to %struct.nlmsghdr*
store %struct.nlmsghdr* %8, %struct.nlmsghdr** %1
%9 = tail call i32 @NLMSG_SPACE(i64 %4)
%10 = tail call i32 @memset(%struct.nlmsghdr* %8, i32 0, i32 %9)
%11 = tail call i32 @NLMSG_SPACE(i64 %4)
%12 = load %struct.nlmsghdr*, %struct.nlmsghdr** %1
%13 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %12, i64 0, i32 0
store i32 %11, i32* %13
%14 = tail call i32 (...) @getpid()
%15 = load %struct.nlmsghdr*, %struct.nlmsghdr** %1
%16 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %15, i64 0, i32 6
store i32 %14, i32* %16
%17 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %15, i64 0, i32 5
store i64 0, i64* %17
%18 = load %struct.iovec*, %struct.iovec** %2
%19 = getelementptr inbounds %struct.iovec, %struct.iovec* %18, i64 0, i32 1
%20 = bitcast i8** %19 to %struct.nlmsghdr**
store %struct.nlmsghdr* %15, %struct.nlmsghdr** %20
%21 = load %struct.nlmsghdr*, %struct.nlmsghdr** %1
%22 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %21, i64 0, i32 0
%23 = load i32, i32* %22
%24 = load %struct.iovec*, %struct.iovec** %2
%25 = getelementptr inbounds %struct.iovec, %struct.iovec* %24, i64 0, i32 0
store i32 %23, i32* %25
%26 = bitcast %struct.msghdr* %3 to %struct.nlmsghdr*
%27 = tail call i32 @memset(%struct.nlmsghdr* %26, i32 0, i32 48)
%28 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %3, i64 0, i32 4
%29 = bitcast i8** %28 to %struct.sockaddr_nl**
store %struct.sockaddr_nl* %0, %struct.sockaddr_nl** %29
%30 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %3, i64 0, i32 1
store i32 4, i32* %30
%31 = load %struct.iovec*, %struct.iovec** %2
%32 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %3, i64 0, i32 3
store %struct.iovec* %31, %struct.iovec** %32
%33 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %3, i64 0, i32 2
store i32 1, i32* %33
ret void
}