#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 {
  scalar_t__ nla_len;
};

/* Variables and functions */
int /*<<< orphan*/ memcpy(void *, void *, size_t);
scalar_t__ nla_attr_size(int /*<<< orphan*/);
void *nla_data(struct nlattr const *);
int nla_get_mem(const struct nlattr *nla, void *ptr, size_t size) {
  void *src = nla_data(nla);
  if ((size_t)(nla->nla_len - nla_attr_size(0)) > size) {
    memcpy(ptr, src, size);
    return 0;
  }
  return -1;
}


x86 assembly (clang, -O0):
<nla_get_mem>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <nla_data@plt>
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
xor    %edi,%edi
call   1050 <nla_attr_size@plt>
mov    %rax,%rcx
mov    -0x30(%rbp),%rax
sub    %rcx,%rax
cmp    -0x20(%rbp),%rax
jbe    1194 <nla_get_mem+0x64>
mov    -0x18(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1040 <memcpy@plt>
movl   $0x0,-0x4(%rbp)
jmp    119b <nla_get_mem+0x6b>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<nla_get_mem>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r12
mov    %rsi,%r14
mov    %rdi,%rbx
call   1030 <nla_data@plt>
mov    %rax,%r15
mov    (%rbx),%rbx
xor    %edi,%edi
call   1050 <nla_attr_size@plt>
sub    %rax,%rbx
mov    $0xffffffff,%eax
cmp    %r12,%rbx
jbe    1170 <nla_get_mem+0x40>
mov    %r14,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
call   1040 <memcpy@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.nlattr = type { i64 }
define i32 @nla_get_mem(%struct.nlattr* %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca %struct.nlattr*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i8*
store %struct.nlattr* %0, %struct.nlattr** %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%9 = load %struct.nlattr*, %struct.nlattr** %5
%10 = call i8* @nla_data(%struct.nlattr* %9)
store i8* %10, i8** %8
%11 = load %struct.nlattr*, %struct.nlattr** %5
%12 = getelementptr inbounds %struct.nlattr, %struct.nlattr* %11, i32 0, i32 0
%13 = load i64, i64* %12
%14 = call i64 @nla_attr_size(i32 0)
%15 = sub nsw i64 %13, %14
%16 = load i64, i64* %7
%17 = icmp ugt i64 %15, %16
br i1 %17, label %18, label %23
18: ; preds = %3
%19 = load i8*, i8** %6
%20 = load i8*, i8** %8
%21 = load i64, i64* %7
%22 = call i32 @memcpy(i8* %19, i8* %20, i64 %21)
store i32 0, i32* %4
br label %24
23: ; preds = %3
store i32 -1, i32* %4
br label %24
24: ; preds = %23, %18
%25 = load i32, i32* %4
ret i32 %25
}

LLVM IR (-O2):
%struct.nlattr = type { i64 }
define i32 @nla_get_mem(%struct.nlattr* %0, i8* %1, i64 %2) {
%4 = tail call i8* @nla_data(%struct.nlattr* %0)
%5 = getelementptr inbounds %struct.nlattr, %struct.nlattr* %0, i64 0, i32 0
%6 = load i64, i64* %5
%7 = tail call i64 @nla_attr_size(i32 0)
%8 = sub nsw i64 %6, %7
%9 = icmp ugt i64 %8, %2
br i1 %9, label %10, label %12
10: ; preds = %3
%11 = tail call i32 @memcpy(i8* %1, i8* %4, i64 %2)
br label %12
12: ; preds = %3, %10
%13 = phi i32 [ 0, %10 ], [ -1, %3 ]
ret i32 %13
}