#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 /*<<< orphan*/ RTA_DATA(struct rtattr *);
int RTA_LENGTH(int);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/, void const *, int);
int addattr_len(struct nlmsghdr *n, int maxlen, int type, const void *data,
                int alen) {
  int len = RTA_LENGTH(alen);
  struct rtattr *rta;
  if ((NLMSG_ALIGN(n->nlmsg_len) + len) > maxlen) {
    return -1;
  }
  rta = ((struct rtattr *)(((void *)(n)) + NLMSG_ALIGN((n)->nlmsg_len)));
  rta->rta_type = type;
  rta->rta_len = len;
  memcpy(RTA_DATA(rta), data, len);
  n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + len;
  return 0;
}


x86 assembly (clang, -O0):
<addattr_len>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%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   1060 <RTA_LENGTH@plt>
mov    %eax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
call   1030 <NLMSG_ALIGN@plt>
add    -0x28(%rbp),%eax
cmp    -0x14(%rbp),%eax
jle    1188 <addattr_len+0x48>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f2 <addattr_len+0xb2>
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
call   1030 <NLMSG_ALIGN@plt>
mov    %eax,%ecx
mov    -0x38(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%ecx
mov    -0x30(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x28(%rbp),%ecx
mov    -0x30(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    -0x30(%rbp),%rdi
call   1050 <RTA_DATA@plt>
mov    %eax,%edi
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%edx
call   1040 <memcpy@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
call   1030 <NLMSG_ALIGN@plt>
mov    %eax,%ecx
add    -0x28(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<addattr_len>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rcx,%r14
mov    %edx,%r15d
mov    %esi,%r12d
mov    %rdi,%rbx
mov    %r8d,%edi
call   1060 <RTA_LENGTH@plt>
mov    %eax,%ebp
mov    (%rbx),%edi
call   1030 <NLMSG_ALIGN@plt>
mov    %eax,%ecx
add    %ebp,%ecx
mov    $0xffffffff,%eax
cmp    %r12d,%ecx
jg     11a6 <addattr_len+0x66>
mov    (%rbx),%edi
call   1030 <NLMSG_ALIGN@plt>
cltq
lea    (%rbx,%rax,1),%rdi
mov    %r15d,(%rbx,%rax,1)
mov    %ebp,0x4(%rbx,%rax,1)
call   1050 <RTA_DATA@plt>
mov    %eax,%edi
mov    %r14,%rsi
mov    %ebp,%edx
call   1040 <memcpy@plt>
mov    (%rbx),%edi
call   1030 <NLMSG_ALIGN@plt>
add    %ebp,%eax
mov    %eax,(%rbx)
xor    %eax,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

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