#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 nlattr {
  int nla_len;
  int nla_type;
};

/* Variables and functions */
int NLA_ALIGN(int);
int NLA_HDRLEN;
int /*<<< orphan*/ memcpy(int /*<<< orphan*/, char const *, int);
int /*<<< orphan*/ nla_data(struct nlattr *);
int strlen(char const *);
int nla_put_string(struct nlattr *nla, int type, const char *str) {
  int attrlen = strlen(str) + 1;
  nla->nla_len = NLA_HDRLEN + attrlen;
  nla->nla_type = type;
  memcpy(nla_data(nla), str, attrlen);
  return NLA_HDRLEN + NLA_ALIGN(attrlen);
}


x86 assembly (clang, -O0):
<nla_put_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
mov    0x2e77(%rip),%rax
mov    (%rax),%ecx
add    -0x1c(%rbp),%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,(%rax)
mov    -0xc(%rbp),%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    -0x8(%rbp),%rdi
call   1040 <nla_data@plt>
mov    %eax,%edi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1050 <memcpy@plt>
mov    0x2e44(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%edi
call   1060 <NLA_ALIGN@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%eax
add    %ecx,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<nla_put_string>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdx,%r15
mov    %esi,%r14d
mov    %rdi,%rbp
mov    %rdx,%rdi
call   1030 <strlen@plt>
lea    0x1(%rax),%ebx
mov    0x2e7d(%rip),%r12
mov    (%r12),%eax
add    %ebx,%eax
mov    %eax,0x0(%rbp)
mov    %r14d,0x4(%rbp)
mov    %rbp,%rdi
call   1040 <nla_data@plt>
mov    %eax,%edi
mov    %r15,%rsi
mov    %ebx,%edx
call   1050 <memcpy@plt>
mov    (%r12),%ebp
mov    %ebx,%edi
call   1060 <NLA_ALIGN@plt>
add    %ebp,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.nlattr = type { i32, i32 }
@NLA_HDRLEN = global i32 0
define i32 @nla_put_string(%struct.nlattr* %0, i32 %1, i8* %2) {
%4 = alloca %struct.nlattr*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
store %struct.nlattr* %0, %struct.nlattr** %4
store i32 %1, i32* %5
store i8* %2, i8** %6
%8 = load i8*, i8** %6
%9 = call i32 @strlen(i8* %8)
%10 = add nsw i32 %9, 1
store i32 %10, i32* %7
%11 = load i32, i32* @NLA_HDRLEN
%12 = load i32, i32* %7
%13 = add nsw i32 %11, %12
%14 = load %struct.nlattr*, %struct.nlattr** %4
%15 = getelementptr inbounds %struct.nlattr, %struct.nlattr* %14, i32 0, i32 0
store i32 %13, i32* %15
%16 = load i32, i32* %5
%17 = load %struct.nlattr*, %struct.nlattr** %4
%18 = getelementptr inbounds %struct.nlattr, %struct.nlattr* %17, i32 0, i32 1
store i32 %16, i32* %18
%19 = load %struct.nlattr*, %struct.nlattr** %4
%20 = call i32 @nla_data(%struct.nlattr* %19)
%21 = load i8*, i8** %6
%22 = load i32, i32* %7
%23 = call i32 @memcpy(i32 %20, i8* %21, i32 %22)
%24 = load i32, i32* @NLA_HDRLEN
%25 = load i32, i32* %7
%26 = call i32 @NLA_ALIGN(i32 %25)
%27 = add nsw i32 %24, %26
ret i32 %27
}

LLVM IR (-O2):
%struct.nlattr = type { i32, i32 }
@NLA_HDRLEN = global i32 0
define i32 @nla_put_string(%struct.nlattr* %0, i32 %1, i8* %2) {
%4 = tail call i32 @strlen(i8* %2)
%5 = add nsw i32 %4, 1
%6 = load i32, i32* @NLA_HDRLEN
%7 = add nsw i32 %6, %5
%8 = getelementptr inbounds %struct.nlattr, %struct.nlattr* %0, i64 0, i32 0
store i32 %7, i32* %8
%9 = getelementptr inbounds %struct.nlattr, %struct.nlattr* %0, i64 0, i32 1
store i32 %1, i32* %9
%10 = tail call i32 @nla_data(%struct.nlattr* %0)
%11 = tail call i32 @memcpy(i32 %10, i8* %2, i32 %5)
%12 = load i32, i32* @NLA_HDRLEN
%13 = tail call i32 @NLA_ALIGN(i32 %5)
%14 = add nsw i32 %13, %12
ret i32 %14
}