#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 */
typedef int /*<<< orphan*/ uint8_t;
struct rtattr {
  int rta_type;
  size_t rta_len;
};
struct nlmsghdr {
  size_t nlmsg_len;
};

/* Variables and functions */
int E2BIG;
size_t NLMSG_ALIGN(size_t);
size_t RTA_ALIGN(size_t);
int /*<<< orphan*/ RTA_DATA(struct rtattr *);
size_t RTA_LENGTH(size_t);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/, void const *, size_t);
int nl_add_rtattr(struct nlmsghdr *n, size_t max_length, int type,
                  const void *data, size_t data_length) {
  size_t length = RTA_LENGTH(data_length);
  if (NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(length) > max_length)
    return -E2BIG;
  struct rtattr *rta =
      ((struct rtattr *)(((uint8_t *)(n)) + NLMSG_ALIGN((n)->nlmsg_len)));
  rta->rta_type = type;
  rta->rta_len = length;
  memcpy(RTA_DATA(rta), data, data_length);
  n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(length);
  return 0;
}


x86 assembly (clang, -O0):
<nl_add_rtattr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%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    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1040 <NLMSG_ALIGN@plt>
mov    %rax,-0x48(%rbp)
mov    -0x38(%rbp),%rdi
call   1030 <RTA_ALIGN@plt>
mov    %rax,%rcx
mov    -0x48(%rbp),%rax
add    %rcx,%rax
cmp    -0x18(%rbp),%rax
jbe    11b8 <nl_add_rtattr+0x68>
xor    %eax,%eax
mov    0x2e3a(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1239 <nl_add_rtattr+0xe9>
mov    -0x10(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1040 <NLMSG_ALIGN@plt>
mov    %rax,%rcx
mov    -0x58(%rbp),%rax
shl    $0x2,%rcx
add    %rcx,%rax
mov    %rax,-0x40(%rbp)
mov    -0x1c(%rbp),%ecx
mov    -0x40(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x38(%rbp),%rcx
mov    -0x40(%rbp),%rax
mov    %rcx,0x8(%rax)
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),%rdi
call   1040 <NLMSG_ALIGN@plt>
mov    %rax,-0x50(%rbp)
mov    -0x38(%rbp),%rdi
call   1030 <RTA_ALIGN@plt>
mov    -0x50(%rbp),%rcx
add    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<nl_add_rtattr>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r8,%r15
mov    %rcx,(%rsp)
mov    %edx,%r13d
mov    %rsi,%rbp
mov    %rdi,%rbx
mov    %r8,%rdi
call   1070 <RTA_LENGTH@plt>
mov    %rax,%r14
mov    (%rbx),%rdi
call   1040 <NLMSG_ALIGN@plt>
mov    %rax,%r12
mov    %r14,%rdi
call   1030 <RTA_ALIGN@plt>
add    %r12,%rax
cmp    %rbp,%rax
jbe    119e <nl_add_rtattr+0x4e>
mov    0x2e50(%rip),%rcx
xor    %eax,%eax
sub    (%rcx),%eax
jmp    11e1 <nl_add_rtattr+0x91>
mov    (%rbx),%rdi
call   1040 <NLMSG_ALIGN@plt>
lea    (%rbx,%rax,4),%rdi
mov    %r13d,(%rbx,%rax,4)
mov    %r14,0x8(%rbx,%rax,4)
call   1060 <RTA_DATA@plt>
mov    %eax,%edi
mov    (%rsp),%rsi
mov    %r15,%rdx
call   1050 <memcpy@plt>
mov    (%rbx),%rdi
call   1040 <NLMSG_ALIGN@plt>
mov    %rax,%rbp
mov    %r14,%rdi
call   1030 <RTA_ALIGN@plt>
add    %rbp,%rax
mov    %rax,(%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 { i64 }
%struct.rtattr = type { i32, i64 }
@E2BIG = global i32 0
define i32 @nl_add_rtattr(%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 i64
%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 i64 @RTA_LENGTH(i64 %14)
store i64 %15, i64* %12
%16 = load %struct.nlmsghdr*, %struct.nlmsghdr** %7
%17 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %16, i32 0, i32 0
%18 = load i64, i64* %17
%19 = call i64 @NLMSG_ALIGN(i64 %18)
%20 = load i64, i64* %12
%21 = call i64 @RTA_ALIGN(i64 %20)
%22 = add i64 %19, %21
%23 = load i64, i64* %8
%24 = icmp ugt i64 %22, %23
br i1 %24, label %25, label %28
25: ; preds = %5
%26 = load i32, i32* @E2BIG
%27 = sub nsw i32 0, %26
store i32 %27, i32* %6
br label %57
28: ; preds = %5
%29 = load %struct.nlmsghdr*, %struct.nlmsghdr** %7
%30 = bitcast %struct.nlmsghdr* %29 to i32*
%31 = load %struct.nlmsghdr*, %struct.nlmsghdr** %7
%32 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %31, i32 0, i32 0
%33 = load i64, i64* %32
%34 = call i64 @NLMSG_ALIGN(i64 %33)
%35 = getelementptr inbounds i32, i32* %30, i64 %34
%36 = bitcast i32* %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 i64, i64* %12
%41 = load %struct.rtattr*, %struct.rtattr** %13
%42 = getelementptr inbounds %struct.rtattr, %struct.rtattr* %41, i32 0, i32 1
store i64 %40, i64* %42
%43 = load %struct.rtattr*, %struct.rtattr** %13
%44 = call i32 @RTA_DATA(%struct.rtattr* %43)
%45 = load i8*, i8** %10
%46 = load i64, i64* %11
%47 = call i32 @memcpy(i32 %44, i8* %45, i64 %46)
%48 = load %struct.nlmsghdr*, %struct.nlmsghdr** %7
%49 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %48, i32 0, i32 0
%50 = load i64, i64* %49
%51 = call i64 @NLMSG_ALIGN(i64 %50)
%52 = load i64, i64* %12
%53 = call i64 @RTA_ALIGN(i64 %52)
%54 = add i64 %51, %53
%55 = load %struct.nlmsghdr*, %struct.nlmsghdr** %7
%56 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %55, i32 0, i32 0
store i64 %54, i64* %56
store i32 0, i32* %6
br label %57
57: ; preds = %28, %25
%58 = load i32, i32* %6
ret i32 %58
}

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