#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 polyad {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ EINVAL;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ memcpy(void *, int /*<<< orphan*/, size_t);
int /*<<< orphan*/ polyad_data(struct polyad const *);
size_t polyad_size(struct polyad const *);
size_t polyad_copy(const struct polyad *src, void *dst, size_t len) {
  const size_t size = polyad_size(src);
  if (len >= size) {
    memcpy(dst, polyad_data(src), polyad_size(src));
    return polyad_size(src);
  } else {
    errno = EINVAL;
    return 0;
  }
}


x86 assembly (clang, -O0):
<polyad_copy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <polyad_size@plt>
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x28(%rbp),%rax
jb     119d <polyad_copy+0x6d>
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <polyad_data@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <polyad_size@plt>
mov    -0x38(%rbp),%rdi
mov    -0x2c(%rbp),%esi
mov    %rax,%rdx
call   1030 <memcpy@plt>
mov    -0x10(%rbp),%rdi
call   1040 <polyad_size@plt>
mov    %rax,-0x8(%rbp)
jmp    11b7 <polyad_copy+0x87>
mov    0x2e34(%rip),%rax
mov    (%rax),%ecx
mov    0x2e33(%rip),%rax
mov    %ecx,(%rax)
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<polyad_copy>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r15
mov    %rsi,%r14
mov    %rdi,%rbx
call   1040 <polyad_size@plt>
cmp    %r15,%rax
jbe    1169 <polyad_copy+0x39>
mov    0x2e87(%rip),%rax
mov    (%rax),%eax
mov    0x2e86(%rip),%rcx
mov    %eax,(%rcx)
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %rbx,%rdi
call   1050 <polyad_data@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1040 <polyad_size@plt>
mov    %r14,%rdi
mov    %ebp,%esi
mov    %rax,%rdx
call   1030 <memcpy@plt>
mov    %rbx,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <polyad_size@plt>

LLVM IR (-O0):
%struct.polyad = type { i32 }
@EINVAL = global i32 0
@errno = global i32 0
define i64 @polyad_copy(%struct.polyad* %0, i8* %1, i64 %2) {
%4 = alloca i64
%5 = alloca %struct.polyad*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
store %struct.polyad* %0, %struct.polyad** %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%9 = load %struct.polyad*, %struct.polyad** %5
%10 = call i64 @polyad_size(%struct.polyad* %9)
store i64 %10, i64* %8
%11 = load i64, i64* %7
%12 = load i64, i64* %8
%13 = icmp uge i64 %11, %12
br i1 %13, label %14, label %23
14: ; preds = %3
%15 = load i8*, i8** %6
%16 = load %struct.polyad*, %struct.polyad** %5
%17 = call i32 @polyad_data(%struct.polyad* %16)
%18 = load %struct.polyad*, %struct.polyad** %5
%19 = call i64 @polyad_size(%struct.polyad* %18)
%20 = call i32 @memcpy(i8* %15, i32 %17, i64 %19)
%21 = load %struct.polyad*, %struct.polyad** %5
%22 = call i64 @polyad_size(%struct.polyad* %21)
store i64 %22, i64* %4
br label %25
23: ; preds = %3
%24 = load i32, i32* @EINVAL
store i32 %24, i32* @errno
store i64 0, i64* %4
br label %25
25: ; preds = %23, %14
%26 = load i64, i64* %4
ret i64 %26
}

LLVM IR (-O2):
%struct.polyad = type { i32 }
@EINVAL = global i32 0
@errno = global i32 0
define i64 @polyad_copy(%struct.polyad* %0, i8* %1, i64 %2) {
%4 = tail call i64 @polyad_size(%struct.polyad* %0)
%5 = icmp ugt i64 %4, %2
br i1 %5, label %11, label %6
6: ; preds = %3
%7 = tail call i32 @polyad_data(%struct.polyad* %0)
%8 = tail call i64 @polyad_size(%struct.polyad* %0)
%9 = tail call i32 @memcpy(i8* %1, i32 %7, i64 %8)
%10 = tail call i64 @polyad_size(%struct.polyad* %0)
br label %13
11: ; preds = %3
%12 = load i32, i32* @EINVAL
store i32 %12, i32* @errno
br label %13
13: ; preds = %11, %6
%14 = phi i64 [ %10, %6 ], [ 0, %11 ]
ret i64 %14
}