#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 *, size_t);
int /*<<< orphan*/ strcpy(char *, char const *);
size_t strlen(char const *);
bool safestrcpy(char *aDest, const char *aSrc, size_t aDestArraySize) {
  bool success = true;
  size_t srcLen = strlen(aSrc);
  if ((srcLen + 1) > aDestArraySize) {
    memcpy(aDest, aSrc, aDestArraySize - 1);
    success = false;
  } else {
    strcpy(aDest, aSrc);
  }
  return success;
}


x86 assembly (clang, -O0):
<safestrcpy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x1,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
add    $0x1,%rax
cmp    -0x18(%rbp),%rax
jbe    118b <safestrcpy+0x5b>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
sub    $0x1,%rdx
call   1050 <memcpy@plt>
movl   $0x0,-0x1c(%rbp)
jmp    1198 <safestrcpy+0x68>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x1c(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<safestrcpy>:
push   %r15
push   %r14
push   %rbx
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %rdi,%r15
mov    %rsi,%rdi
call   1040 <strlen@plt>
add    $0x1,%rax
cmp    %rbx,%rax
jbe    1165 <safestrcpy+0x35>
add    $0xffffffffffffffff,%rbx
mov    %r15,%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1050 <memcpy@plt>
xor    %eax,%eax
jmp    1175 <safestrcpy+0x45>
mov    %r15,%rdi
mov    %r14,%rsi
call   1030 <strcpy@plt>
mov    $0x1,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
define i32 @safestrcpy(i8* %0, i8* %1, i64 %2) {
%4 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%5 = add i64 %4, 1
%6 = icmp ugt i64 %5, %2
br i1 %6, label %7, label %10
7: ; preds = %3
%8 = add i64 %2, -1
%9 = tail call i32 @memcpy(i8* %0, i8* %1, i64 %8)
br label %12
10: ; preds = %3
%11 = tail call i32 @strcpy(i8* %0, i8* %1)
br label %12
12: ; preds = %10, %7
%13 = phi i32 [ 0, %7 ], [ 1, %10 ]
ret i32 %13
}