#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*/ memcpy(char *, char const *, unsigned int);
unsigned int strlen(char const *);
int bh_strcpy(char *dest, const char *src, unsigned maxdestlen) {
  int ret = 0;
  unsigned numtocopy = strlen(src);
  if (numtocopy > maxdestlen - 1) {
    ret = numtocopy - (maxdestlen - 1);
    numtocopy = maxdestlen - 1;
  }
  memcpy(dest, src, numtocopy);
  dest[numtocopy] = '\0';
  return ret;
}


x86 assembly (clang, -O0):
<bh_strcpy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jbe    116e <bh_strcpy+0x4e>
mov    -0x1c(%rbp),%eax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
sub    %ecx,%eax
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1040 <memcpy@plt>
mov    -0x8(%rbp),%rax
mov    -0x1c(%rbp),%ecx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bh_strcpy>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%ebx
mov    %rsi,%r14
mov    %rdi,%r15
mov    %rsi,%rdi
call   1030 <strlen@plt>
add    $0xffffffff,%ebx
xor    %ecx,%ecx
mov    %eax,%ebp
sub    %ebx,%ebp
cmovb  %ecx,%ebp
cmovbe %eax,%ebx
mov    %r15,%rdi
mov    %r14,%rsi
mov    %ebx,%edx
call   1040 <memcpy@plt>
movb   $0x0,(%r15,%rbx,1)
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @bh_strcpy(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
store i32 0, i32* %7
%9 = load i8*, i8** %5
%10 = call i32 @strlen(i8* %9)
store i32 %10, i32* %8
%11 = load i32, i32* %8
%12 = load i32, i32* %6
%13 = sub i32 %12, 1
%14 = icmp ugt i32 %11, %13
br i1 %14, label %15, label %22
15: ; preds = %3
%16 = load i32, i32* %8
%17 = load i32, i32* %6
%18 = sub i32 %17, 1
%19 = sub i32 %16, %18
store i32 %19, i32* %7
%20 = load i32, i32* %6
%21 = sub i32 %20, 1
store i32 %21, i32* %8
br label %22
22: ; preds = %15, %3
%23 = load i8*, i8** %4
%24 = load i8*, i8** %5
%25 = load i32, i32* %8
%26 = call i32 @memcpy(i8* %23, i8* %24, i32 %25)
%27 = load i8*, i8** %4
%28 = load i32, i32* %8
%29 = zext i32 %28 to i64
%30 = getelementptr inbounds i8, i8* %27, i64 %29
store i8 0, i8* %30
%31 = load i32, i32* %7
ret i32 %31
}

LLVM IR (-O2):
define i32 @bh_strcpy(i8* %0, i8* %1, i32 %2) {
%4 = tail call i32 @strlen(i8* %1)
%5 = add i32 %2, -1
%6 = icmp ugt i32 %4, %5
%7 = tail call i32 @llvm.usub.sat.i32(i32 %4, i32 %5)
%8 = select i1 %6, i32 %5, i32 %4
%9 = tail call i32 @memcpy(i8* %0, i8* %1, i32 %8)
%10 = zext i32 %8 to i64
%11 = getelementptr inbounds i8, i8* %0, i64 %10
store i8 0, i8* %11
ret i32 %7
}