#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);
size_t strlen(char const *);
char *safe_strcpy(char *dest, const char *src, size_t maxlength) {
  size_t len;
  if (!dest) {
    ;
    ;
    return NULL;
  }
  if (!src) {
    *dest = 0;
    return dest;
  }
  len = strlen(src);
  if (len > maxlength) {
    ;
    ;
    len = maxlength;
  }
  memcpy(dest, src, len);
  dest[len] = 0;
  return dest;
}


x86 assembly (clang, -O0):
<safe_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)
jne    114c <safe_strcpy+0x2c>
movq   $0x0,-0x8(%rbp)
jmp    11b3 <safe_strcpy+0x93>
cmpq   $0x0,-0x18(%rbp)
jne    116b <safe_strcpy+0x4b>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11b3 <safe_strcpy+0x93>
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x20(%rbp),%rax
jbe    118e <safe_strcpy+0x6e>
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1040 <memcpy@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):
<safe_strcpy>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r12
test   %rdi,%rdi
je     1164 <safe_strcpy+0x44>
mov    %rsi,%r14
mov    %r12,%rbx
test   %rsi,%rsi
je     1161 <safe_strcpy+0x41>
mov    %rdx,%r15
mov    %r14,%rdi
call   1030 <strlen@plt>
mov    %rax,%rbx
cmp    %r15,%rax
cmova  %r15,%rbx
mov    %r12,%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1040 <memcpy@plt>
add    %r12,%rbx
movb   $0x0,(%rbx)
mov    %r12,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
define i8* @safe_strcpy(i8* %0, i8* %1, i64 %2) {
%4 = icmp eq i8* %0, null
br i1 %4, label %15, label %5
5: ; preds = %3
%6 = icmp eq i8* %1, null
br i1 %6, label %13, label %7
7: ; preds = %5
%8 = tail call i64 @strlen(i8* %1)
%9 = icmp ugt i64 %8, %2
%10 = select i1 %9, i64 %2, i64 %8
%11 = tail call i32 @memcpy(i8* %0, i8* %1, i64 %10)
%12 = getelementptr inbounds i8, i8* %0, i64 %10
br label %13
13: ; preds = %5, %7
%14 = phi i8* [ %12, %7 ], [ %0, %5 ]
store i8 0, i8* %14
br label %15
15: ; preds = %13, %3
ret i8* %0
}