#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 */
char *strcpy(char *, char const *);
size_t strlen(char const *);
char *strncpy(char *, char const *, size_t);
char *safecopy(char *dest, const char *src, size_t maxlen) {
  dest[maxlen - 1] = 0;
  return strlen(src) < maxlen ? strcpy(dest, src)
                              : strncpy(dest, src, maxlen - 1);
}


x86 assembly (clang, -O0):
<safecopy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    -0x18(%rbp),%rcx
sub    $0x1,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
cmp    -0x18(%rbp),%rax
jae    117d <safecopy+0x4d>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <strcpy@plt>
mov    %rax,-0x20(%rbp)
jmp    1196 <safecopy+0x66>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
sub    $0x1,%rdx
call   1030 <strncpy@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<safecopy>:
push   %r15
push   %r14
push   %rbx
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %rdi,%r15
movb   $0x0,-0x1(%rdi,%rdx,1)
mov    %rsi,%rdi
call   1050 <strlen@plt>
cmp    %rbx,%rax
jae    115d <safecopy+0x2d>
mov    %r15,%rdi
mov    %r14,%rsi
call   1040 <strcpy@plt>
jmp    116f <safecopy+0x3f>
add    $0xffffffffffffffff,%rbx
mov    %r15,%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1030 <strncpy@plt>
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
define i8* @safecopy(i8* returned %0, i8* nocapture %1, i64 %2) {
%4 = add i64 %2, -1
%5 = getelementptr inbounds i8, i8* %0, i64 %4
store i8 0, i8* %5
%6 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%7 = icmp ult i64 %6, %2
br i1 %7, label %8, label %10
8: ; preds = %3
%9 = tail call i8* @strcpy(i8* dereferenceable(1) %0, i8* dereferenceable(1) %1)
br label %12
10: ; preds = %3
%11 = tail call i8* @strncpy(i8* dereferenceable(1) %0, i8* %1, i64 %4)
br label %12
12: ; preds = %10, %8
ret i8* %0
}