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


x86 assembly (clang, -O0):
<addattr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
call   1070 <RTA_LENGTH@plt>
mov    %eax,-0x34(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
call   1040 <NLMSG_ALIGN@plt>
mov    %rax,-0x48(%rbp)
mov    -0x34(%rbp),%edi
call   1030 <RTA_ALIGN@plt>
mov    %rax,%rcx
mov    -0x48(%rbp),%rax
add    %rcx,%rax
cmp    -0x18(%rbp),%rax
jbe    11ae <addattr+0x5e>
movl   $0xffffffff,-0x4(%rbp)
jmp    122e <addattr+0xde>
mov    -0x10(%rbp),%rax
mov    -0x10(%rbp),%rcx
mov    (%rcx),%ecx
add    $0x3,%ecx
and    $0xfffffffc,%ecx
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x40(%rbp)
mov    -0x1c(%rbp),%ecx
mov    -0x40(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x34(%rbp),%ecx
mov    -0x40(%rbp),%rax
mov    %ecx,0x4(%rax)
cmpq   $0x0,-0x30(%rbp)
je     11fe <addattr+0xae>
mov    -0x40(%rbp),%rdi
call   1060 <RTA_DATA@plt>
mov    %eax,%edi
mov    -0x28(%rbp),%rsi
mov    -0x30(%rbp),%rdx
call   1050 <memcpy@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
call   1040 <NLMSG_ALIGN@plt>
mov    %rax,-0x50(%rbp)
mov    -0x34(%rbp),%edi
call   1030 <RTA_ALIGN@plt>
mov    %rax,%rcx
mov    -0x50(%rbp),%rax
add    %rcx,%rax
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<addattr>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r8,%r13
mov    %rcx,(%rsp)
mov    %edx,%r12d
mov    %rsi,%rbp
mov    %rdi,%rbx
mov    %r8,%rdi
call   1070 <RTA_LENGTH@plt>
mov    %eax,%r14d
mov    (%rbx),%edi
call   1040 <NLMSG_ALIGN@plt>
mov    %rax,%r15
mov    %r14d,%edi
call   1030 <RTA_ALIGN@plt>
mov    %rax,%rcx
add    %r15,%rcx
mov    $0xffffffff,%eax
cmp    %rbp,%rcx
ja     11e2 <addattr+0x92>
movslq (%rbx),%rdi
lea    0x3(%rdi),%rax
and    $0xfffffffffffffffc,%rax
mov    %r12d,(%rbx,%rax,1)
mov    %r14d,0x4(%rbx,%rax,1)
test   %r13,%r13
je     11cc <addattr+0x7c>
add    %rbx,%rax
mov    %rax,%rdi
call   1060 <RTA_DATA@plt>
mov    %eax,%edi
mov    (%rsp),%rsi
mov    %r13,%rdx
call   1050 <memcpy@plt>
mov    (%rbx),%edi
call   1040 <NLMSG_ALIGN@plt>
mov    %rax,%rbp
mov    %r14d,%edi
call   1030 <RTA_ALIGN@plt>
add    %ebp,%eax
mov    %eax,(%rbx)
xor    %eax,%eax
add    $0x8,%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 @addattr(%struct.nlmsghdr* %0, i64 %1, i32 %2, i8* %3, i64 %4) {
%6 = alloca i32
%7 = alloca %struct.nlmsghdr*
%8 = alloca i64
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i64
%12 = alloca i32
%13 = alloca %struct.rtattr*
store %struct.nlmsghdr* %0, %struct.nlmsghdr** %7
store i64 %1, i64* %8
store i32 %2, i32* %9
store i8* %3, i8** %10
store i64 %4, i64* %11
%14 = load i64, i64* %11
%15 = call i32 @RTA_LENGTH(i64 %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 i64 @NLMSG_ALIGN(i32 %18)
%20 = load i32, i32* %12
%21 = call i64 @RTA_ALIGN(i32 %20)
%22 = add i64 %19, %21
%23 = load i64, i64* %8
%24 = icmp ugt i64 %22, %23
br i1 %24, label %25, label %26
25: ; preds = %5
store i32 -1, i32* %6
br label %62
26: ; preds = %5
%27 = load %struct.nlmsghdr*, %struct.nlmsghdr** %7
%28 = bitcast %struct.nlmsghdr* %27 to i8*
%29 = load %struct.nlmsghdr*, %struct.nlmsghdr** %7
%30 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %29, i32 0, i32 0
%31 = load i32, i32* %30
%32 = add nsw i32 %31, 3
%33 = and i32 %32, -4
%34 = sext i32 %33 to i64
%35 = getelementptr i8, i8* %28, i64 %34
%36 = bitcast i8* %35 to %struct.rtattr*
store %struct.rtattr* %36, %struct.rtattr** %13
%37 = load i32, i32* %9
%38 = load %struct.rtattr*, %struct.rtattr** %13
%39 = getelementptr inbounds %struct.rtattr, %struct.rtattr* %38, i32 0, i32 0
store i32 %37, i32* %39
%40 = load i32, i32* %12
%41 = load %struct.rtattr*, %struct.rtattr** %13
%42 = getelementptr inbounds %struct.rtattr, %struct.rtattr* %41, i32 0, i32 1
store i32 %40, i32* %42
%43 = load i64, i64* %11
%44 = icmp ne i64 %43, 0
br i1 %44, label %45, label %51
45: ; preds = %26
%46 = load %struct.rtattr*, %struct.rtattr** %13
%47 = call i32 @RTA_DATA(%struct.rtattr* %46)
%48 = load i8*, i8** %10
%49 = load i64, i64* %11
%50 = call i32 @memcpy(i32 %47, i8* %48, i64 %49)
br label %51
51: ; preds = %45, %26
%52 = load %struct.nlmsghdr*, %struct.nlmsghdr** %7
%53 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %52, i32 0, i32 0
%54 = load i32, i32* %53
%55 = call i64 @NLMSG_ALIGN(i32 %54)
%56 = load i32, i32* %12
%57 = call i64 @RTA_ALIGN(i32 %56)
%58 = add i64 %55, %57
%59 = trunc i64 %58 to i32
%60 = load %struct.nlmsghdr*, %struct.nlmsghdr** %7
%61 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %60, i32 0, i32 0
store i32 %59, i32* %61
store i32 0, i32* %6
br label %62
62: ; preds = %51, %25
%63 = load i32, i32* %6
ret i32 %63
}

LLVM IR (-O2):
%struct.nlmsghdr = type { i32 }
%struct.rtattr = type { i32, i32 }
define i32 @addattr(%struct.nlmsghdr* %0, i64 %1, i32 %2, i8* %3, i64 %4) {
%6 = tail call i32 @RTA_LENGTH(i64 %4)
%7 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %0, i64 0, i32 0
%8 = load i32, i32* %7
%9 = tail call i64 @NLMSG_ALIGN(i32 %8)
%10 = tail call i64 @RTA_ALIGN(i32 %6)
%11 = add i64 %10, %9
%12 = icmp ugt i64 %11, %1
br i1 %12, label %35, label %13
13: ; preds = %5
%14 = bitcast %struct.nlmsghdr* %0 to i8*
%15 = load i32, i32* %7
%16 = add nsw i32 %15, 3
%17 = and i32 %16, -4
%18 = sext i32 %17 to i64
%19 = getelementptr i8, i8* %14, i64 %18
%20 = bitcast i8* %19 to i32*
store i32 %2, i32* %20
%21 = getelementptr inbounds i8, i8* %19, i64 4
%22 = bitcast i8* %21 to i32*
store i32 %6, i32* %22
%23 = icmp eq i64 %4, 0
br i1 %23, label %29, label %24
24: ; preds = %13
%25 = bitcast i8* %19 to %struct.rtattr*
%26 = tail call i32 @RTA_DATA(%struct.rtattr* %25)
%27 = tail call i32 @memcpy(i32 %26, i8* %3, i64 %4)
%28 = load i32, i32* %7
br label %29
29: ; preds = %24, %13
%30 = phi i32 [ %28, %24 ], [ %15, %13 ]
%31 = tail call i64 @NLMSG_ALIGN(i32 %30)
%32 = tail call i64 @RTA_ALIGN(i32 %6)
%33 = add i64 %32, %31
%34 = trunc i64 %33 to i32
store i32 %34, i32* %7
br label %35
35: ; preds = %5, %29
%36 = phi i32 [ 0, %29 ], [ -1, %5 ]
ret i32 %36
}