#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 */

/* Variables and functions */
int /*<<< orphan*/ memmove(void *, void *, size_t);
size_t strlen(char const *);
char *_q_strcpy(char *dst, size_t size, const char *src) {
  if (dst == NULL || size == 0 || src == NULL)
    return dst;
  size_t copylen = strlen(src);
  if (copylen >= size)
    copylen = size - 1;
  memmove((void *)dst, (void *)src, copylen);
  dst[copylen] = '\0';
  return dst;
}


x86 assembly (clang, -O0):
<_q_strcpy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1155 <_q_strcpy+0x35>
cmpq   $0x0,-0x18(%rbp)
je     1155 <_q_strcpy+0x35>
cmpq   $0x0,-0x20(%rbp)
jne    1162 <_q_strcpy+0x42>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11ae <_q_strcpy+0x8e>
mov    -0x20(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x18(%rbp),%rax
jb     1189 <_q_strcpy+0x69>
mov    -0x18(%rbp),%rax
sub    $0x1,%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1040 <memmove@plt>
mov    -0x10(%rbp),%rax
mov    -0x28(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_q_strcpy>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%rbx
test   %rdi,%rdi
je     1166 <_q_strcpy+0x46>
mov    %rsi,%r15
test   %rsi,%rsi
je     1166 <_q_strcpy+0x46>
mov    %rdx,%r14
test   %rdx,%rdx
je     1166 <_q_strcpy+0x46>
mov    %r14,%rdi
call   1030 <strlen@plt>
lea    -0x1(%r15),%r12
cmp    %r15,%rax
cmovb  %rax,%r12
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r12,%rdx
call   1040 <memmove@plt>
movb   $0x0,(%rbx,%r12,1)
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @_q_strcpy(i8* %0, i64 %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i64
store i8* %0, i8** %5
store i64 %1, i64* %6
store i8* %2, i8** %7
%9 = load i8*, i8** %5
%10 = icmp eq i8* %9, null
br i1 %10, label %17, label %11
11: ; preds = %3
%12 = load i64, i64* %6
%13 = icmp eq i64 %12, 0
br i1 %13, label %17, label %14
14: ; preds = %11
%15 = load i8*, i8** %7
%16 = icmp eq i8* %15, null
br i1 %16, label %17, label %19
17: ; preds = %14, %11, %3
%18 = load i8*, i8** %5
store i8* %18, i8** %4
br label %37
19: ; preds = %14
%20 = load i8*, i8** %7
%21 = call i64 @strlen(i8* %20)
store i64 %21, i64* %8
%22 = load i64, i64* %8
%23 = load i64, i64* %6
%24 = icmp uge i64 %22, %23
br i1 %24, label %25, label %28
25: ; preds = %19
%26 = load i64, i64* %6
%27 = sub i64 %26, 1
store i64 %27, i64* %8
br label %28
28: ; preds = %25, %19
%29 = load i8*, i8** %5
%30 = load i8*, i8** %7
%31 = load i64, i64* %8
%32 = call i32 @memmove(i8* %29, i8* %30, i64 %31)
%33 = load i8*, i8** %5
%34 = load i64, i64* %8
%35 = getelementptr inbounds i8, i8* %33, i64 %34
store i8 0, i8* %35
%36 = load i8*, i8** %5
store i8* %36, i8** %4
br label %37
37: ; preds = %28, %17
%38 = load i8*, i8** %4
ret i8* %38
}

LLVM IR (-O2):
define i8* @_q_strcpy(i8* returned %0, i64 %1, i8* %2) {
%4 = icmp eq i8* %0, null
%5 = icmp eq i64 %1, 0
%6 = or i1 %4, %5
%7 = icmp eq i8* %2, null
%8 = or i1 %6, %7
br i1 %8, label %16, label %9
9: ; preds = %3
%10 = tail call i64 @strlen(i8* dereferenceable(1) %2)
%11 = icmp ult i64 %10, %1
%12 = add i64 %1, -1
%13 = select i1 %11, i64 %10, i64 %12
%14 = tail call i32 @memmove(i8* %0, i8* %2, i64 %13)
%15 = getelementptr inbounds i8, i8* %0, i64 %13
store i8 0, i8* %15
br label %16
16: ; preds = %3, %9
ret i8* %0
}