#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*/ fprintf(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/, void const *, int);
int /*<<< orphan*/ stderr;
int addattr_l(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) + RTA_ALIGN(len) > maxlen) {
    fprintf(stderr, "addattr_l: message exceeded bound of %d\n", 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, alen);
  n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(len);
  return 0;
}


x86 assembly (clang, -O0):
<addattr_l>:
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   1080 <RTA_LENGTH@plt>
mov    %eax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
call   1040 <NLMSG_ALIGN@plt>
mov    %eax,-0x34(%rbp)
mov    -0x28(%rbp),%edi
call   1030 <RTA_ALIGN@plt>
mov    %eax,%ecx
mov    -0x34(%rbp),%eax
add    %ecx,%eax
cmp    -0x14(%rbp),%eax
jle    11cf <addattr_l+0x6f>
mov    0x2e2e(%rip),%rax
mov    (%rax),%edi
mov    -0x14(%rbp),%edx
lea    0xe42(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1244 <addattr_l+0xe4>
mov    -0x10(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
call   1040 <NLMSG_ALIGN@plt>
mov    %eax,%ecx
mov    -0x40(%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   1070 <RTA_DATA@plt>
mov    %eax,%edi
mov    -0x20(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1060 <memcpy@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
call   1040 <NLMSG_ALIGN@plt>
mov    %eax,-0x38(%rbp)
mov    -0x28(%rbp),%edi
call   1030 <RTA_ALIGN@plt>
mov    -0x38(%rbp),%ecx
add    %eax,%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_l>:
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   1080 <RTA_LENGTH@plt>
mov    %eax,%r13d
mov    (%rbx),%edi
call   1040 <NLMSG_ALIGN@plt>
mov    %eax,%r15d
mov    %r13d,%edi
call   1030 <RTA_ALIGN@plt>
add    %r15d,%eax
cmp    %ebp,%eax
jle    11bc <addattr_l+0x5c>
mov    0x2e3b(%rip),%rax
mov    (%rax),%edi
lea    0xe52(%rip),%rsi
mov    %ebp,%edx
call   1050 <fprintf@plt>
mov    $0xffffffff,%eax
jmp    11fc <addattr_l+0x9c>
mov    (%rbx),%edi
call   1040 <NLMSG_ALIGN@plt>
cltq
lea    (%rbx,%rax,1),%rdi
mov    %r12d,(%rbx,%rax,1)
mov    %r13d,0x4(%rbx,%rax,1)
call   1070 <RTA_DATA@plt>
mov    %eax,%edi
mov    (%rsp),%rsi
mov    %r14d,%edx
call   1060 <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.nlmsghdr = type { i32 }
%struct.rtattr = type { i32, i32 }
@stderr = global i32 0
@.str = [41 x i8] c"addattr_l: message exceeded bound of %d\0A\00"
define i32 @addattr_l(%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 = 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 %29
25: ; preds = %5
%26 = load i32, i32* @stderr
%27 = load i32, i32* %8
%28 = call i32 @fprintf(i32 %26, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i32 %27)
store i32 -1, i32* %6
br label %59
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 i8, i8* %31, i64 %36
%38 = bitcast i8* %37 to %struct.rtattr*
store %struct.rtattr* %38, %struct.rtattr** %13
%39 = load i32, i32* %9
%40 = load %struct.rtattr*, %struct.rtattr** %13
%41 = getelementptr inbounds %struct.rtattr, %struct.rtattr* %40, i32 0, i32 0
store i32 %39, i32* %41
%42 = load i32, i32* %12
%43 = load %struct.rtattr*, %struct.rtattr** %13
%44 = getelementptr inbounds %struct.rtattr, %struct.rtattr* %43, i32 0, i32 1
store i32 %42, i32* %44
%45 = load %struct.rtattr*, %struct.rtattr** %13
%46 = call i32 @RTA_DATA(%struct.rtattr* %45)
%47 = load i8*, i8** %10
%48 = load i32, i32* %11
%49 = call i32 @memcpy(i32 %46, i8* %47, i32 %48)
%50 = load %struct.nlmsghdr*, %struct.nlmsghdr** %7
%51 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %50, i32 0, i32 0
%52 = load i32, i32* %51
%53 = call i32 @NLMSG_ALIGN(i32 %52)
%54 = load i32, i32* %12
%55 = call i32 @RTA_ALIGN(i32 %54)
%56 = add nsw i32 %53, %55
%57 = load %struct.nlmsghdr*, %struct.nlmsghdr** %7
%58 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %57, i32 0, i32 0
store i32 %56, i32* %58
store i32 0, i32* %6
br label %59
59: ; preds = %29, %25
%60 = load i32, i32* %6
ret i32 %60
}

LLVM IR (-O2):
%struct.nlmsghdr = type { i32 }
%struct.rtattr = type { i32, i32 }
@stderr = global i32 0
@.str = [41 x i8] c"addattr_l: message exceeded bound of %d\0A\00"
define i32 @addattr_l(%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_ALIGN(i32 %6)
%11 = add nsw i32 %10, %9
%12 = icmp sgt i32 %11, %1
br i1 %12, label %13, label %16
13: ; preds = %5
%14 = load i32, i32* @stderr
%15 = tail call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i32 %1)
br label %32
16: ; preds = %5
%17 = bitcast %struct.nlmsghdr* %0 to i8*
%18 = load i32, i32* %7
%19 = tail call i32 @NLMSG_ALIGN(i32 %18)
%20 = sext i32 %19 to i64
%21 = getelementptr i8, i8* %17, i64 %20
%22 = bitcast i8* %21 to %struct.rtattr*
%23 = bitcast i8* %21 to i32*
store i32 %2, i32* %23
%24 = getelementptr inbounds i8, i8* %21, i64 4
%25 = bitcast i8* %24 to i32*
store i32 %6, i32* %25
%26 = tail call i32 @RTA_DATA(%struct.rtattr* %22)
%27 = tail call i32 @memcpy(i32 %26, i8* %3, i32 %4)
%28 = load i32, i32* %7
%29 = tail call i32 @NLMSG_ALIGN(i32 %28)
%30 = tail call i32 @RTA_ALIGN(i32 %6)
%31 = add nsw i32 %30, %29
store i32 %31, i32* %7
br label %32
32: ; preds = %16, %13
%33 = phi i32 [ -1, %13 ], [ 0, %16 ]
ret i32 %33
}