#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_len;
  int rta_type;
};

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


x86 assembly (clang, -O0):
<rta_addattr_len>:
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 <RTA_ALIGN@plt>
mov    %eax,-0x38(%rbp)
mov    -0x34(%rbp),%edi
call   1030 <RTA_ALIGN@plt>
mov    %eax,%ecx
mov    -0x38(%rbp),%eax
add    %ecx,%eax
cmp    -0x14(%rbp),%eax
jle    11a7 <rta_addattr_len+0x57>
movl   $0xffffffff,-0x4(%rbp)
jmp    121c <rta_addattr_len+0xcc>
mov    -0x10(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
call   1030 <RTA_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,0x4(%rax)
mov    -0x34(%rbp),%ecx
mov    -0x30(%rbp),%rax
mov    %ecx,(%rax)
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    -0x10(%rbp),%rax
mov    (%rax),%edi
call   1040 <NLMSG_ALIGN@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x34(%rbp),%edi
call   1030 <RTA_ALIGN@plt>
mov    -0x3c(%rbp),%ecx
add    %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):
<rta_addattr_len>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r8d,%r14d
mov    %rcx,(%rsp)
mov    %edx,%r12d
mov    %esi,%ebp
mov    %rdi,%rbx
mov    %r8d,%edi
call   1070 <RTA_LENGTH@plt>
mov    %eax,%r13d
mov    (%rbx),%edi
call   1030 <RTA_ALIGN@plt>
mov    %eax,%r15d
mov    %r13d,%edi
call   1030 <RTA_ALIGN@plt>
mov    %eax,%ecx
add    %r15d,%ecx
mov    $0xffffffff,%eax
cmp    %ebp,%ecx
jg     11d5 <rta_addattr_len+0x85>
mov    (%rbx),%edi
call   1030 <RTA_ALIGN@plt>
cltq
lea    (%rbx,%rax,1),%rdi
mov    %r12d,0x4(%rbx,%rax,1)
mov    %r13d,(%rbx,%rax,1)
call   1060 <RTA_DATA@plt>
mov    %eax,%edi
mov    (%rsp),%rsi
mov    %r14d,%edx
call   1050 <memcpy@plt>
mov    (%rbx),%edi
call   1040 <NLMSG_ALIGN@plt>
mov    %eax,%ebp
mov    %r13d,%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.rtattr = type { i32, i32 }
define i32 @rta_addattr_len(%struct.rtattr* %0, i32 %1, i32 %2, i8* %3, i32 %4) {
%6 = alloca i32
%7 = alloca %struct.rtattr*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i32
%12 = alloca %struct.rtattr*
%13 = alloca i32
store %struct.rtattr* %0, %struct.rtattr** %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* %13
%16 = load %struct.rtattr*, %struct.rtattr** %7
%17 = getelementptr inbounds %struct.rtattr, %struct.rtattr* %16, i32 0, i32 0
%18 = load i32, i32* %17
%19 = call i32 @RTA_ALIGN(i32 %18)
%20 = load i32, i32* %13
%21 = call i32 @RTA_ALIGN(i32 %20)
%22 = add nsw i32 %19, %21
%23 = load i32, i32* %8
%24 = icmp sgt i32 %22, %23
br i1 %24, label %25, label %26
25: ; preds = %5
store i32 -1, i32* %6
br label %56
26: ; preds = %5
%27 = load %struct.rtattr*, %struct.rtattr** %7
%28 = bitcast %struct.rtattr* %27 to i8*
%29 = load %struct.rtattr*, %struct.rtattr** %7
%30 = getelementptr inbounds %struct.rtattr, %struct.rtattr* %29, i32 0, i32 0
%31 = load i32, i32* %30
%32 = call i32 @RTA_ALIGN(i32 %31)
%33 = sext i32 %32 to i64
%34 = getelementptr i8, i8* %28, i64 %33
%35 = bitcast i8* %34 to %struct.rtattr*
store %struct.rtattr* %35, %struct.rtattr** %12
%36 = load i32, i32* %9
%37 = load %struct.rtattr*, %struct.rtattr** %12
%38 = getelementptr inbounds %struct.rtattr, %struct.rtattr* %37, i32 0, i32 1
store i32 %36, i32* %38
%39 = load i32, i32* %13
%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 %struct.rtattr*, %struct.rtattr** %12
%43 = call i32 @RTA_DATA(%struct.rtattr* %42)
%44 = load i8*, i8** %10
%45 = load i32, i32* %11
%46 = call i32 @memcpy(i32 %43, i8* %44, i32 %45)
%47 = load %struct.rtattr*, %struct.rtattr** %7
%48 = getelementptr inbounds %struct.rtattr, %struct.rtattr* %47, i32 0, i32 0
%49 = load i32, i32* %48
%50 = call i32 @NLMSG_ALIGN(i32 %49)
%51 = load i32, i32* %13
%52 = call i32 @RTA_ALIGN(i32 %51)
%53 = add nsw i32 %50, %52
%54 = load %struct.rtattr*, %struct.rtattr** %7
%55 = getelementptr inbounds %struct.rtattr, %struct.rtattr* %54, i32 0, i32 0
store i32 %53, i32* %55
store i32 0, i32* %6
br label %56
56: ; preds = %26, %25
%57 = load i32, i32* %6
ret i32 %57
}

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