#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*/ printf(char *, ...);
size_t strlen(char const *);
char *safe_strcpy(char *dest, const char *src, size_t maxlength) {
  size_t len;
  if (!dest) {
    do {
      printf("ERROR: NULL dest in safe_strcpy\n");
    } while (0);
    return NULL;
  }
  if (!src) {
    *dest = 0;
    return dest;
  }
  len = strlen(src);
  if (len > maxlength) {
    do {
      printf("ERROR: string overflow by %d in safe_strcpy [%.50s]\n",
             (int)(len - maxlength), src);
    } while (0);
    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    116f <safe_strcpy+0x3f>
jmp    1154 <safe_strcpy+0x24>
lea    0xea5(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    11f7 <safe_strcpy+0xc7>
cmpq   $0x0,-0x18(%rbp)
jne    118e <safe_strcpy+0x5e>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11f7 <safe_strcpy+0xc7>
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x20(%rbp),%rax
jbe    11d2 <safe_strcpy+0xa2>
jmp    11ae <safe_strcpy+0x7e>
mov    -0x28(%rbp),%rax
sub    -0x20(%rbp),%rax
mov    %eax,%esi
mov    -0x18(%rbp),%rdx
lea    0xe5e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1050 <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   %rbx
mov    %rdi,%rbx
test   %rdi,%rdi
je     117d <safe_strcpy+0x3d>
mov    %rsi,%r15
test   %rsi,%rsi
je     118b <safe_strcpy+0x4b>
mov    %rdx,%r14
mov    %r15,%rdi
call   1040 <strlen@plt>
cmp    %r14,%rax
jbe    1190 <safe_strcpy+0x50>
sub    %r14d,%eax
lea    0xe91(%rip),%rdi
mov    %eax,%esi
mov    %r15,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
jmp    1193 <safe_strcpy+0x53>
lea    0xeb1(%rip),%rdi
call   1030 <puts@plt>
jmp    11a6 <safe_strcpy+0x66>
movb   $0x0,(%rbx)
jmp    11a6 <safe_strcpy+0x66>
mov    %rax,%r14
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
call   1060 <memcpy@plt>
movb   $0x0,(%rbx,%r14,1)
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [33 x i8] c"ERROR: NULL dest in safe_strcpy\0A\00"
@.str.1 = [53 x i8] c"ERROR: string overflow by %d in safe_strcpy [%.50s]\0A\00"
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 %15, label %11
11: ; preds = %3
br label %12
12: ; preds = %11
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
br label %14
14: ; preds = %12
store i8* null, i8** %4
br label %46
15: ; preds = %3
%16 = load i8*, i8** %6
%17 = icmp ne i8* %16, null
br i1 %17, label %21, label %18
18: ; preds = %15
%19 = load i8*, i8** %5
store i8 0, i8* %19
%20 = load i8*, i8** %5
store i8* %20, i8** %4
br label %46
21: ; preds = %15
%22 = load i8*, i8** %6
%23 = call i64 @strlen(i8* %22)
store i64 %23, i64* %8
%24 = load i64, i64* %8
%25 = load i64, i64* %7
%26 = icmp ugt i64 %24, %25
br i1 %26, label %27, label %37
27: ; preds = %21
br label %28
28: ; preds = %27
%29 = load i64, i64* %8
%30 = load i64, i64* %7
%31 = sub i64 %29, %30
%32 = trunc i64 %31 to i32
%33 = load i8*, i8** %6
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.1, i64 0, i64 0), i32 %32, i8* %33)
br label %35
35: ; preds = %28
%36 = load i64, i64* %7
store i64 %36, i64* %8
br label %37
37: ; preds = %35, %21
%38 = load i8*, i8** %5
%39 = load i8*, i8** %6
%40 = load i64, i64* %8
%41 = call i32 @memcpy(i8* %38, i8* %39, i64 %40)
%42 = load i8*, i8** %5
%43 = load i64, i64* %8
%44 = getelementptr inbounds i8, i8* %42, i64 %43
store i8 0, i8* %44
%45 = load i8*, i8** %5
store i8* %45, i8** %4
br label %46
46: ; preds = %37, %18, %14
%47 = load i8*, i8** %4
ret i8* %47
}

LLVM IR (-O2):
@.str.1 = [53 x i8] c"ERROR: string overflow by %d in safe_strcpy [%.50s]\0A\00"
@str = [32 x i8] c"ERROR: NULL dest in safe_strcpy\00"
define i8* @safe_strcpy(i8* %0, i8* %1, i64 %2) {
%4 = icmp eq i8* %0, null
br i1 %4, label %5, label %7
5: ; preds = %3
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @str, i64 0, i64 0))
br label %21
7: ; preds = %3
%8 = icmp eq i8* %1, null
br i1 %8, label %9, label %10
9: ; preds = %7
store i8 0, i8* %0
br label %21
10: ; preds = %7
%11 = tail call i64 @strlen(i8* %1)
%12 = icmp ugt i64 %11, %2
br i1 %12, label %13, label %17
13: ; preds = %10
%14 = sub i64 %11, %2
%15 = trunc i64 %14 to i32
%16 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([53 x i8], [53 x i8]* @.str.1, i64 0, i64 0), i32 %15, i8* %1)
br label %17
17: ; preds = %13, %10
%18 = phi i64 [ %2, %13 ], [ %11, %10 ]
%19 = tail call i32 @memcpy(i8* %0, i8* %1, i64 %18)
%20 = getelementptr inbounds i8, i8* %0, i64 %18
store i8 0, i8* %20
br label %21
21: ; preds = %17, %9, %5
ret i8* %0
}