#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*/ fprintf(int /*<<< orphan*/, char *, unsigned int);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/, void const *, int);
int /*<<< orphan*/ stderr;
int addattr_l(struct nlmsghdr *n, unsigned 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) {
    fprintf(stderr, "addattr_l ERROR: message exceeded bound of %d\n", maxlen);
    return -1;
  }
  rta = (struct rtattr *)(((char *)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) + 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   1070 <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
jbe    11b0 <addattr_l+0x60>
mov    0x2e4d(%rip),%rax
mov    (%rax),%edi
mov    -0x14(%rbp),%edx
lea    0xe61(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    121a <addattr_l+0xca>
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   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   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_l>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r8d,%r14d
mov    %rcx,%r15
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 <NLMSG_ALIGN@plt>
add    %r13d,%eax
cmp    %ebp,%eax
jbe    11a0 <addattr_l+0x50>
mov    0x2e57(%rip),%rax
mov    (%rax),%edi
lea    0xe6e(%rip),%rsi
mov    %ebp,%edx
call   1040 <fprintf@plt>
mov    $0xffffffff,%eax
jmp    11d6 <addattr_l+0x86>
mov    (%rbx),%edi
call   1030 <NLMSG_ALIGN@plt>
cltq
lea    (%rbx,%rax,1),%rdi
mov    %r12d,(%rbx,%rax,1)
mov    %r13d,0x4(%rbx,%rax,1)
call   1060 <RTA_DATA@plt>
mov    %eax,%edi
mov    %r15,%rsi
mov    %r14d,%edx
call   1050 <memcpy@plt>
mov    (%rbx),%edi
call   1030 <NLMSG_ALIGN@plt>
add    %r13d,%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 = [47 x i8] c"addattr_l ERROR: 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 = add nsw i32 %19, %20
%22 = load i32, i32* %8
%23 = icmp ugt i32 %21, %22
br i1 %23, label %24, label %28
24: ; preds = %5
%25 = load i32, i32* @stderr
%26 = load i32, i32* %8
%27 = call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str, i64 0, i64 0), i32 %26)
store i32 -1, i32* %6
br label %57
28: ; preds = %5
%29 = load %struct.nlmsghdr*, %struct.nlmsghdr** %7
%30 = bitcast %struct.nlmsghdr* %29 to i8*
%31 = load %struct.nlmsghdr*, %struct.nlmsghdr** %7
%32 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %31, i32 0, i32 0
%33 = load i32, i32* %32
%34 = call i32 @NLMSG_ALIGN(i32 %33)
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds i8, i8* %30, i64 %35
%37 = bitcast i8* %36 to %struct.rtattr*
store %struct.rtattr* %37, %struct.rtattr** %13
%38 = load i32, i32* %9
%39 = load %struct.rtattr*, %struct.rtattr** %13
%40 = getelementptr inbounds %struct.rtattr, %struct.rtattr* %39, i32 0, i32 0
store i32 %38, i32* %40
%41 = load i32, i32* %12
%42 = load %struct.rtattr*, %struct.rtattr** %13
%43 = getelementptr inbounds %struct.rtattr, %struct.rtattr* %42, i32 0, i32 1
store i32 %41, i32* %43
%44 = load %struct.rtattr*, %struct.rtattr** %13
%45 = call i32 @RTA_DATA(%struct.rtattr* %44)
%46 = load i8*, i8** %10
%47 = load i32, i32* %11
%48 = call i32 @memcpy(i32 %45, i8* %46, i32 %47)
%49 = load %struct.nlmsghdr*, %struct.nlmsghdr** %7
%50 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %49, i32 0, i32 0
%51 = load i32, i32* %50
%52 = call i32 @NLMSG_ALIGN(i32 %51)
%53 = load i32, i32* %12
%54 = add nsw i32 %52, %53
%55 = load %struct.nlmsghdr*, %struct.nlmsghdr** %7
%56 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %55, i32 0, i32 0
store i32 %54, i32* %56
store i32 0, i32* %6
br label %57
57: ; preds = %28, %24
%58 = load i32, i32* %6
ret i32 %58
}

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