#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;
};
typedef scalar_t__ nh;

/* Variables and functions */
int NLMSG_ALIGN(int);
int /*<<< orphan*/ RTA_DATA(struct rtattr *);
int RTA_LENGTH(int);
int RTA_SPACE(int);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/, void const *, int);
unsigned int rnl_addattr(struct nlmsghdr *nh, int maxlen, int type,
                         const void *data, int alen) {
  struct rtattr *attr;
  int len = RTA_LENGTH(alen);
  int nlen = NLMSG_ALIGN(nh->nlmsg_len) + RTA_SPACE(alen);
  if (nlen > maxlen)
    return nlen;
  attr = ((void *)((char *)(nh) + NLMSG_ALIGN((nh)->nlmsg_len)));
  attr->rta_type = type;
  attr->rta_len = len;
  if (data)
    memcpy(RTA_DATA(attr), data, alen);
  nh->nlmsg_len = nlen;
  return nlen;
}


x86 assembly (clang, -O0):
<rnl_addattr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8d,-0x24(%rbp)
mov    -0x24(%rbp),%edi
call   1070 <RTA_LENGTH@plt>
mov    %eax,-0x34(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
call   1030 <NLMSG_ALIGN@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x24(%rbp),%edi
call   1040 <RTA_SPACE@plt>
mov    %eax,%ecx
mov    -0x3c(%rbp),%eax
add    %ecx,%eax
mov    %eax,-0x38(%rbp)
mov    -0x38(%rbp),%eax
cmp    -0x14(%rbp),%eax
jle    11ac <rnl_addattr+0x5c>
mov    -0x38(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1213 <rnl_addattr+0xc3>
mov    -0x10(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
call   1030 <NLMSG_ALIGN@plt>
mov    %eax,%ecx
mov    -0x48(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%ecx
mov    -0x30(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x34(%rbp),%ecx
mov    -0x30(%rbp),%rax
mov    %ecx,0x4(%rax)
cmpq   $0x0,-0x20(%rbp)
je     1204 <rnl_addattr+0xb4>
mov    -0x30(%rbp),%rdi
call   1060 <RTA_DATA@plt>
mov    %eax,%edi
mov    -0x20(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1050 <memcpy@plt>
mov    -0x38(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x38(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rnl_addattr>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %r8d,%r15d
mov    %rcx,0x10(%rsp)
mov    %edx,0xc(%rsp)
mov    %esi,%r14d
mov    %rdi,%rbx
mov    %r8d,%edi
call   1070 <RTA_LENGTH@plt>
mov    %eax,%r13d
mov    (%rbx),%edi
call   1030 <NLMSG_ALIGN@plt>
mov    %eax,%r12d
mov    %r15d,%edi
call   1040 <RTA_SPACE@plt>
mov    %eax,%ebp
add    %r12d,%ebp
cmp    %r14d,%ebp
jg     11ce <rnl_addattr+0x7e>
mov    (%rbx),%edi
call   1030 <NLMSG_ALIGN@plt>
movslq %eax,%rdi
mov    0xc(%rsp),%eax
mov    %eax,(%rbx,%rdi,1)
mov    %r13d,0x4(%rbx,%rdi,1)
mov    0x10(%rsp),%r14
test   %r14,%r14
je     11cc <rnl_addattr+0x7c>
add    %rbx,%rdi
call   1060 <RTA_DATA@plt>
mov    %eax,%edi
mov    %r14,%rsi
mov    %r15d,%edx
call   1050 <memcpy@plt>
mov    %ebp,(%rbx)
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.nlmsghdr = type { i32 }
%struct.rtattr = type { i32, i32 }
define i32 @rnl_addattr(%struct.nlmsghdr* %0, i32 %1, i32 %2, i8* %3, i32 %4) {
%6 = alloca i32
%7 = alloca %struct.nlmsghdr*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i32
%12 = alloca %struct.rtattr*
%13 = alloca i32
%14 = alloca i32
store %struct.nlmsghdr* %0, %struct.nlmsghdr** %7
store i32 %1, i32* %8
store i32 %2, i32* %9
store i8* %3, i8** %10
store i32 %4, i32* %11
%15 = load i32, i32* %11
%16 = call i32 @RTA_LENGTH(i32 %15)
store i32 %16, i32* %13
%17 = load %struct.nlmsghdr*, %struct.nlmsghdr** %7
%18 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %17, i32 0, i32 0
%19 = load i32, i32* %18
%20 = call i32 @NLMSG_ALIGN(i32 %19)
%21 = load i32, i32* %11
%22 = call i32 @RTA_SPACE(i32 %21)
%23 = add nsw i32 %20, %22
store i32 %23, i32* %14
%24 = load i32, i32* %14
%25 = load i32, i32* %8
%26 = icmp sgt i32 %24, %25
br i1 %26, label %27, label %29
27: ; preds = %5
%28 = load i32, i32* %14
store i32 %28, i32* %6
br label %58
29: ; preds = %5
%30 = load %struct.nlmsghdr*, %struct.nlmsghdr** %7
%31 = bitcast %struct.nlmsghdr* %30 to i8*
%32 = load %struct.nlmsghdr*, %struct.nlmsghdr** %7
%33 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %32, i32 0, i32 0
%34 = load i32, i32* %33
%35 = call i32 @NLMSG_ALIGN(i32 %34)
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds i8, i8* %31, i64 %36
%38 = bitcast i8* %37 to %struct.rtattr*
store %struct.rtattr* %38, %struct.rtattr** %12
%39 = load i32, i32* %9
%40 = load %struct.rtattr*, %struct.rtattr** %12
%41 = getelementptr inbounds %struct.rtattr, %struct.rtattr* %40, i32 0, i32 0
store i32 %39, i32* %41
%42 = load i32, i32* %13
%43 = load %struct.rtattr*, %struct.rtattr** %12
%44 = getelementptr inbounds %struct.rtattr, %struct.rtattr* %43, i32 0, i32 1
store i32 %42, i32* %44
%45 = load i8*, i8** %10
%46 = icmp ne i8* %45, null
br i1 %46, label %47, label %53
47: ; preds = %29
%48 = load %struct.rtattr*, %struct.rtattr** %12
%49 = call i32 @RTA_DATA(%struct.rtattr* %48)
%50 = load i8*, i8** %10
%51 = load i32, i32* %11
%52 = call i32 @memcpy(i32 %49, i8* %50, i32 %51)
br label %53
53: ; preds = %47, %29
%54 = load i32, i32* %14
%55 = load %struct.nlmsghdr*, %struct.nlmsghdr** %7
%56 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %55, i32 0, i32 0
store i32 %54, i32* %56
%57 = load i32, i32* %14
store i32 %57, i32* %6
br label %58
58: ; preds = %53, %27
%59 = load i32, i32* %6
ret i32 %59
}

LLVM IR (-O2):
%struct.nlmsghdr = type { i32 }
%struct.rtattr = type { i32, i32 }
define i32 @rnl_addattr(%struct.nlmsghdr* %0, i32 %1, i32 %2, i8* %3, i32 %4) {
%6 = tail call i32 @RTA_LENGTH(i32 %4)
%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 = tail call i32 @RTA_SPACE(i32 %4)
%11 = add nsw i32 %10, %9
%12 = icmp sgt i32 %11, %1
br i1 %12, label %28, label %13
13: ; preds = %5
%14 = bitcast %struct.nlmsghdr* %0 to i8*
%15 = load i32, i32* %7
%16 = tail call i32 @NLMSG_ALIGN(i32 %15)
%17 = sext i32 %16 to i64
%18 = getelementptr inbounds i8, i8* %14, i64 %17
%19 = bitcast i8* %18 to i32*
store i32 %2, i32* %19
%20 = getelementptr inbounds i8, i8* %18, i64 4
%21 = bitcast i8* %20 to i32*
store i32 %6, i32* %21
%22 = icmp eq i8* %3, null
br i1 %22, label %27, label %23
23: ; preds = %13
%24 = bitcast i8* %18 to %struct.rtattr*
%25 = tail call i32 @RTA_DATA(%struct.rtattr* %24)
%26 = tail call i32 @memcpy(i32 %25, i8* %3, i32 %4)
br label %27
27: ; preds = %23, %13
store i32 %11, i32* %7
br label %28
28: ; preds = %5, %27
ret i32 %11
}