#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 {
  unsigned short rta_type;
  unsigned short rta_len;
};
struct nlmsghdr {
  short nlmsg_len;
};

/* Variables and functions */
scalar_t__ E2BIG;
short NLMSG_ALIGN(short);
short RTA_ALIGN(unsigned short);
int /*<<< orphan*/ RTA_DATA(struct rtattr *);
scalar_t__ RTA_LENGTH(unsigned short);
scalar_t__ errno;
int /*<<< orphan*/ memcpy(int /*<<< orphan*/, void *, unsigned short);
int /*<<< orphan*/ printf(char *);
struct rtattr *nlmsg_addattr(struct nlmsghdr *nlmsg, unsigned short maxlen,
                             unsigned short type, void *data,
                             unsigned short size) {
  struct rtattr *attr;
  unsigned short len = (unsigned short)RTA_LENGTH(size);
  errno = 0;
  if (nlmsg == NULL)
    return NULL;
  if (NLMSG_ALIGN(nlmsg->nlmsg_len) + RTA_ALIGN(len) > maxlen) {
    errno = E2BIG;
    printf("nlmsg_len too long\n");
    return NULL;
  }
  attr =
      ((struct rtattr *)(((char *)(nlmsg)) + NLMSG_ALIGN((nlmsg)->nlmsg_len)));
  attr->rta_type = type;
  attr->rta_len = len;
  if (data && size)
    memcpy(RTA_DATA(attr), data, size);
  nlmsg->nlmsg_len = NLMSG_ALIGN(nlmsg->nlmsg_len) + RTA_ALIGN(len);
  return attr;
}


x86 assembly (clang, -O0):
<nlmsg_addattr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %r8w,%ax
mov    %rdi,-0x10(%rbp)
mov    %si,-0x12(%rbp)
mov    %dx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %ax,-0x22(%rbp)
movzwl -0x22(%rbp),%edi
call   1080 <RTA_LENGTH@plt>
mov    %ax,-0x32(%rbp)
mov    0x2e44(%rip),%rax
movq   $0x0,(%rax)
cmpq   $0x0,-0x10(%rbp)
jne    11b3 <nlmsg_addattr+0x53>
movq   $0x0,-0x8(%rbp)
jmp    12b8 <nlmsg_addattr+0x158>
mov    -0x10(%rbp),%rax
movswl (%rax),%edi
call   1040 <NLMSG_ALIGN@plt>
cwtl
mov    %eax,-0x38(%rbp)
movzwl -0x32(%rbp),%edi
call   1030 <RTA_ALIGN@plt>
mov    %ax,%cx
mov    -0x38(%rbp),%eax
movswl %cx,%ecx
add    %ecx,%eax
movzwl -0x12(%rbp),%ecx
cmp    %ecx,%eax
jle    1210 <nlmsg_addattr+0xb0>
mov    0x2dfe(%rip),%rax
mov    (%rax),%rcx
mov    0x2de4(%rip),%rax
mov    %rcx,(%rax)
lea    0xe02(%rip),%rdi
call   1050 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    12b8 <nlmsg_addattr+0x158>
mov    -0x10(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x10(%rbp),%rax
movswl (%rax),%edi
call   1040 <NLMSG_ALIGN@plt>
mov    %ax,%cx
mov    -0x40(%rbp),%rax
movswl %cx,%ecx
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x30(%rbp)
mov    -0x14(%rbp),%cx
mov    -0x30(%rbp),%rax
mov    %cx,(%rax)
mov    -0x32(%rbp),%cx
mov    -0x30(%rbp),%rax
mov    %cx,0x2(%rax)
cmpq   $0x0,-0x20(%rbp)
je     1282 <nlmsg_addattr+0x122>
movzwl -0x22(%rbp),%eax
cmp    $0x0,%eax
je     1282 <nlmsg_addattr+0x122>
mov    -0x30(%rbp),%rdi
call   1070 <RTA_DATA@plt>
mov    %eax,%edi
mov    -0x20(%rbp),%rsi
mov    -0x22(%rbp),%ax
movzwl %ax,%edx
call   1060 <memcpy@plt>
mov    -0x10(%rbp),%rax
movswl (%rax),%edi
call   1040 <NLMSG_ALIGN@plt>
cwtl
mov    %eax,-0x44(%rbp)
movzwl -0x32(%rbp),%edi
call   1030 <RTA_ALIGN@plt>
mov    %ax,%cx
mov    -0x44(%rbp),%eax
movswl %cx,%ecx
add    %ecx,%eax
mov    %ax,%cx
mov    -0x10(%rbp),%rax
mov    %cx,(%rax)
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<nlmsg_addattr>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %r8d,%ebp
mov    %rcx,%r15
mov    %edx,%ebx
mov    %esi,%r13d
mov    %rdi,%r12
mov    %r8d,%edi
call   1080 <RTA_LENGTH@plt>
mov    %rax,%r14
mov    0x2e4a(%rip),%rax
movq   $0x0,(%rax)
test   %r12,%r12
je     11e8 <nlmsg_addattr+0x88>
mov    %ebp,0xc(%rsp)
mov    %r15,0x10(%rsp)
movswl (%r12),%edi
call   1050 <NLMSG_ALIGN@plt>
movswl %ax,%r15d
movzwl %r14w,%ebp
mov    %ebp,%edi
call   1030 <RTA_ALIGN@plt>
cwtl
add    %r15d,%eax
movzwl %r13w,%ecx
cmp    %ecx,%eax
jle    11ec <nlmsg_addattr+0x8c>
mov    0x2e19(%rip),%rax
mov    (%rax),%rax
mov    0x2dff(%rip),%rcx
mov    %rax,(%rcx)
lea    0xe1d(%rip),%rdi
call   1040 <puts@plt>
xor    %ebp,%ebp
jmp    1252 <nlmsg_addattr+0xf2>
mov    %ebp,%r15d
movswl (%r12),%edi
call   1050 <NLMSG_ALIGN@plt>
movswq %ax,%rax
lea    (%r12,%rax,1),%rbp
mov    %bx,(%r12,%rax,1)
mov    %r14w,0x2(%r12,%rax,1)
mov    0x10(%rsp),%r14
test   %r14,%r14
je     1235 <nlmsg_addattr+0xd5>
cmpw   $0x0,0xc(%rsp)
je     1235 <nlmsg_addattr+0xd5>
mov    %rbp,%rdi
call   1070 <RTA_DATA@plt>
movzwl 0xc(%rsp),%edx
mov    %eax,%edi
mov    %r14,%rsi
call   1060 <memcpy@plt>
movswl (%r12),%edi
call   1050 <NLMSG_ALIGN@plt>
mov    %eax,%r14d
mov    %r15d,%edi
call   1030 <RTA_ALIGN@plt>
add    %r14d,%eax
mov    %ax,(%r12)
mov    %rbp,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.rtattr = type { i16, i16 }
%struct.nlmsghdr = type { i16 }
@errno = global i64 0
@E2BIG = global i64 0
@.str = [20 x i8] c"nlmsg_len too long\0A\00"
define %struct.rtattr* @nlmsg_addattr(%struct.nlmsghdr* %0, i16 zeroext %1, i16 zeroext %2, i8* %3, i16 zeroext %4) {
%6 = alloca %struct.rtattr*
%7 = alloca %struct.nlmsghdr*
%8 = alloca i16
%9 = alloca i16
%10 = alloca i8*
%11 = alloca i16
%12 = alloca %struct.rtattr*
%13 = alloca i16
store %struct.nlmsghdr* %0, %struct.nlmsghdr** %7
store i16 %1, i16* %8
store i16 %2, i16* %9
store i8* %3, i8** %10
store i16 %4, i16* %11
%14 = load i16, i16* %11
%15 = call i64 @RTA_LENGTH(i16 zeroext %14)
%16 = trunc i64 %15 to i16
store i16 %16, i16* %13
store i64 0, i64* @errno
%17 = load %struct.nlmsghdr*, %struct.nlmsghdr** %7
%18 = icmp eq %struct.nlmsghdr* %17, null
br i1 %18, label %19, label %20
19: ; preds = %5
store %struct.rtattr* null, %struct.rtattr** %6
br label %79
20: ; preds = %5
%21 = load %struct.nlmsghdr*, %struct.nlmsghdr** %7
%22 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %21, i32 0, i32 0
%23 = load i16, i16* %22
%24 = call signext i16 @NLMSG_ALIGN(i16 signext %23)
%25 = sext i16 %24 to i32
%26 = load i16, i16* %13
%27 = call signext i16 @RTA_ALIGN(i16 zeroext %26)
%28 = sext i16 %27 to i32
%29 = add nsw i32 %25, %28
%30 = load i16, i16* %8
%31 = zext i16 %30 to i32
%32 = icmp sgt i32 %29, %31
br i1 %32, label %33, label %36
33: ; preds = %20
%34 = load i64, i64* @E2BIG
store i64 %34, i64* @errno
%35 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
store %struct.rtattr* null, %struct.rtattr** %6
br label %79
36: ; preds = %20
%37 = load %struct.nlmsghdr*, %struct.nlmsghdr** %7
%38 = bitcast %struct.nlmsghdr* %37 to i8*
%39 = load %struct.nlmsghdr*, %struct.nlmsghdr** %7
%40 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %39, i32 0, i32 0
%41 = load i16, i16* %40
%42 = call signext i16 @NLMSG_ALIGN(i16 signext %41)
%43 = sext i16 %42 to i32
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds i8, i8* %38, i64 %44
%46 = bitcast i8* %45 to %struct.rtattr*
store %struct.rtattr* %46, %struct.rtattr** %12
%47 = load i16, i16* %9
%48 = load %struct.rtattr*, %struct.rtattr** %12
%49 = getelementptr inbounds %struct.rtattr, %struct.rtattr* %48, i32 0, i32 0
store i16 %47, i16* %49
%50 = load i16, i16* %13
%51 = load %struct.rtattr*, %struct.rtattr** %12
%52 = getelementptr inbounds %struct.rtattr, %struct.rtattr* %51, i32 0, i32 1
store i16 %50, i16* %52
%53 = load i8*, i8** %10
%54 = icmp ne i8* %53, null
br i1 %54, label %55, label %65
55: ; preds = %36
%56 = load i16, i16* %11
%57 = zext i16 %56 to i32
%58 = icmp ne i32 %57, 0
br i1 %58, label %59, label %65
59: ; preds = %55
%60 = load %struct.rtattr*, %struct.rtattr** %12
%61 = call i32 @RTA_DATA(%struct.rtattr* %60)
%62 = load i8*, i8** %10
%63 = load i16, i16* %11
%64 = call i32 @memcpy(i32 %61, i8* %62, i16 zeroext %63)
br label %65
65: ; preds = %59, %55, %36
%66 = load %struct.nlmsghdr*, %struct.nlmsghdr** %7
%67 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %66, i32 0, i32 0
%68 = load i16, i16* %67
%69 = call signext i16 @NLMSG_ALIGN(i16 signext %68)
%70 = sext i16 %69 to i32
%71 = load i16, i16* %13
%72 = call signext i16 @RTA_ALIGN(i16 zeroext %71)
%73 = sext i16 %72 to i32
%74 = add nsw i32 %70, %73
%75 = trunc i32 %74 to i16
%76 = load %struct.nlmsghdr*, %struct.nlmsghdr** %7
%77 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %76, i32 0, i32 0
store i16 %75, i16* %77
%78 = load %struct.rtattr*, %struct.rtattr** %12
store %struct.rtattr* %78, %struct.rtattr** %6
br label %79
79: ; preds = %65, %33, %19
%80 = load %struct.rtattr*, %struct.rtattr** %6
ret %struct.rtattr* %80
}

LLVM IR (-O2):
%struct.rtattr = type { i16, i16 }
%struct.nlmsghdr = type { i16 }
@errno = global i64 0
@E2BIG = global i64 0
@str = [19 x i8] c"nlmsg_len too long\00"
define %struct.rtattr* @nlmsg_addattr(%struct.nlmsghdr* %0, i16 zeroext %1, i16 zeroext %2, i8* %3, i16 zeroext %4) {
%6 = tail call i64 @RTA_LENGTH(i16 zeroext %4)
%7 = trunc i64 %6 to i16
store i64 0, i64* @errno
%8 = icmp eq %struct.nlmsghdr* %0, null
br i1 %8, label %43, label %9
9: ; preds = %5
%10 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %0, i64 0, i32 0
%11 = load i16, i16* %10
%12 = tail call signext i16 @NLMSG_ALIGN(i16 signext %11)
%13 = sext i16 %12 to i32
%14 = tail call signext i16 @RTA_ALIGN(i16 zeroext %7)
%15 = sext i16 %14 to i32
%16 = add nsw i32 %15, %13
%17 = zext i16 %1 to i32
%18 = icmp sgt i32 %16, %17
br i1 %18, label %19, label %22
19: ; preds = %9
%20 = load i64, i64* @E2BIG
store i64 %20, i64* @errno
%21 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str, i64 0, i64 0))
br label %43
22: ; preds = %9
%23 = bitcast %struct.nlmsghdr* %0 to i8*
%24 = load i16, i16* %10
%25 = tail call signext i16 @NLMSG_ALIGN(i16 signext %24)
%26 = sext i16 %25 to i64
%27 = getelementptr inbounds i8, i8* %23, i64 %26
%28 = bitcast i8* %27 to %struct.rtattr*
%29 = bitcast i8* %27 to i16*
store i16 %2, i16* %29
%30 = getelementptr inbounds i8, i8* %27, i64 2
%31 = bitcast i8* %30 to i16*
store i16 %7, i16* %31
%32 = icmp ne i8* %3, null
%33 = icmp ne i16 %4, 0
%34 = and i1 %32, %33
br i1 %34, label %35, label %38
35: ; preds = %22
%36 = tail call i32 @RTA_DATA(%struct.rtattr* %28)
%37 = tail call i32 @memcpy(i32 %36, i8* %3, i16 zeroext %4)
br label %38
38: ; preds = %35, %22
%39 = load i16, i16* %10
%40 = tail call signext i16 @NLMSG_ALIGN(i16 signext %39)
%41 = tail call signext i16 @RTA_ALIGN(i16 zeroext %7)
%42 = add i16 %41, %40
store i16 %42, i16* %10
br label %43
43: ; preds = %5, %38, %19
%44 = phi %struct.rtattr* [ null, %19 ], [ %28, %38 ], [ null, %5 ]
ret %struct.rtattr* %44
}