#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 rtattr {
  int rta_type;
  int rta_len;
};
struct nlmsghdr {
  int nlmsg_len;
};

/* Variables and functions */
int NLMSG_ALIGN(int);
int RTA_ALIGN(int);
int /*<<< orphan*/ RTA_DATA(struct rtattr *);
int RTA_LENGTH(int);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/, void const *, int);
void add_netlink_msg(struct nlmsghdr *nh, int type, const void *data,
                     int raw_data_length) {
  struct rtattr *rta;
  int rta_length = RTA_LENGTH(raw_data_length);
  rta = (struct rtattr *)((char *)nh + NLMSG_ALIGN(nh->nlmsg_len));
  rta->rta_type = type;
  rta->rta_len = rta_length;
  nh->nlmsg_len = NLMSG_ALIGN(nh->nlmsg_len) + RTA_ALIGN(rta_length);
  memcpy(RTA_DATA(rta), data, raw_data_length);
}


x86 assembly (clang, -O0):
<add_netlink_msg>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    -0x1c(%rbp),%edi
call   1070 <RTA_LENGTH@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%edi
call   1040 <NLMSG_ALIGN@plt>
mov    %eax,%ecx
mov    -0x38(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0xc(%rbp),%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x2c(%rbp),%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    -0x8(%rbp),%rax
mov    (%rax),%edi
call   1040 <NLMSG_ALIGN@plt>
mov    %eax,-0x30(%rbp)
mov    -0x2c(%rbp),%edi
call   1030 <RTA_ALIGN@plt>
mov    -0x30(%rbp),%ecx
add    %eax,%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x28(%rbp),%rdi
call   1060 <RTA_DATA@plt>
mov    %eax,%edi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1050 <memcpy@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<add_netlink_msg>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %ecx,%r15d
mov    %rdx,%r14
mov    %esi,%r12d
mov    %rdi,%rbx
mov    %ecx,%edi
call   1070 <RTA_LENGTH@plt>
mov    %eax,%ebp
mov    (%rbx),%edi
call   1040 <NLMSG_ALIGN@plt>
cltq
lea    (%rbx,%rax,1),%r13
mov    %r12d,(%rbx,%rax,1)
mov    %ebp,0x4(%rbx,%rax,1)
mov    (%rbx),%edi
call   1040 <NLMSG_ALIGN@plt>
mov    %eax,%r12d
mov    %ebp,%edi
call   1030 <RTA_ALIGN@plt>
add    %r12d,%eax
mov    %eax,(%rbx)
mov    %r13,%rdi
call   1060 <RTA_DATA@plt>
mov    %eax,%edi
mov    %r14,%rsi
mov    %r15d,%edx
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <memcpy@plt>

LLVM IR (-O0):
%struct.nlmsghdr = type { i32 }
%struct.rtattr = type { i32, i32 }
define void @add_netlink_msg(%struct.nlmsghdr* %0, i32 %1, i8* %2, i32 %3) {
%5 = alloca %struct.nlmsghdr*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca %struct.rtattr*
%10 = alloca i32
store %struct.nlmsghdr* %0, %struct.nlmsghdr** %5
store i32 %1, i32* %6
store i8* %2, i8** %7
store i32 %3, i32* %8
%11 = load i32, i32* %8
%12 = call i32 @RTA_LENGTH(i32 %11)
store i32 %12, i32* %10
%13 = load %struct.nlmsghdr*, %struct.nlmsghdr** %5
%14 = bitcast %struct.nlmsghdr* %13 to i8*
%15 = load %struct.nlmsghdr*, %struct.nlmsghdr** %5
%16 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %15, i32 0, i32 0
%17 = load i32, i32* %16
%18 = call i32 @NLMSG_ALIGN(i32 %17)
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds i8, i8* %14, i64 %19
%21 = bitcast i8* %20 to %struct.rtattr*
store %struct.rtattr* %21, %struct.rtattr** %9
%22 = load i32, i32* %6
%23 = load %struct.rtattr*, %struct.rtattr** %9
%24 = getelementptr inbounds %struct.rtattr, %struct.rtattr* %23, i32 0, i32 0
store i32 %22, i32* %24
%25 = load i32, i32* %10
%26 = load %struct.rtattr*, %struct.rtattr** %9
%27 = getelementptr inbounds %struct.rtattr, %struct.rtattr* %26, i32 0, i32 1
store i32 %25, i32* %27
%28 = load %struct.nlmsghdr*, %struct.nlmsghdr** %5
%29 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %28, i32 0, i32 0
%30 = load i32, i32* %29
%31 = call i32 @NLMSG_ALIGN(i32 %30)
%32 = load i32, i32* %10
%33 = call i32 @RTA_ALIGN(i32 %32)
%34 = add nsw i32 %31, %33
%35 = load %struct.nlmsghdr*, %struct.nlmsghdr** %5
%36 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %35, i32 0, i32 0
store i32 %34, i32* %36
%37 = load %struct.rtattr*, %struct.rtattr** %9
%38 = call i32 @RTA_DATA(%struct.rtattr* %37)
%39 = load i8*, i8** %7
%40 = load i32, i32* %8
%41 = call i32 @memcpy(i32 %38, i8* %39, i32 %40)
ret void
}

LLVM IR (-O2):
%struct.nlmsghdr = type { i32 }
%struct.rtattr = type { i32, i32 }
define void @add_netlink_msg(%struct.nlmsghdr* %0, i32 %1, i8* %2, i32 %3) {
%5 = tail call i32 @RTA_LENGTH(i32 %3)
%6 = bitcast %struct.nlmsghdr* %0 to i8*
%7 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %0, i64 0, i32 0
%8 = load i32, i32* %7
%9 = tail call i32 @NLMSG_ALIGN(i32 %8)
%10 = sext i32 %9 to i64
%11 = getelementptr inbounds i8, i8* %6, i64 %10
%12 = bitcast i8* %11 to %struct.rtattr*
%13 = bitcast i8* %11 to i32*
store i32 %1, i32* %13
%14 = getelementptr inbounds i8, i8* %11, i64 4
%15 = bitcast i8* %14 to i32*
store i32 %5, i32* %15
%16 = load i32, i32* %7
%17 = tail call i32 @NLMSG_ALIGN(i32 %16)
%18 = tail call i32 @RTA_ALIGN(i32 %5)
%19 = add nsw i32 %18, %17
store i32 %19, i32* %7
%20 = tail call i32 @RTA_DATA(%struct.rtattr* %12)
%21 = tail call i32 @memcpy(i32 %20, i8* %2, i32 %3)
ret void
}