#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 *, int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int strlen(char *);
void safecpy(char *to, char *from, int tolen) {
  int fromlen;
  memset(to, 0, tolen);
  fromlen = from ? strlen(from) : 0;
  if (fromlen > tolen)
    fromlen = tolen;
  memcpy(to, from, fromlen);
}


x86 assembly (clang, -O0):
<safecpy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x14(%rbp),%edx
xor    %esi,%esi
call   1040 <memset@plt>
cmpq   $0x0,-0x10(%rbp)
je     116d <safecpy+0x3d>
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x1c(%rbp)
jmp    1177 <safecpy+0x47>
xor    %eax,%eax
mov    %eax,-0x1c(%rbp)
jmp    1177 <safecpy+0x47>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jle    118f <safecpy+0x5f>
mov    -0x14(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%edx
call   1050 <memcpy@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<safecpy>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%ebp
mov    %rsi,%r15
mov    %rdi,%r14
xor    %ebx,%ebx
xor    %esi,%esi
call   1040 <memset@plt>
test   %r15,%r15
je     1157 <safecpy+0x27>
mov    %r15,%rdi
call   1030 <strlen@plt>
mov    %eax,%ebx
cmp    %ebp,%ebx
cmovg  %ebp,%ebx
mov    %r14,%rdi
mov    %r15,%rsi
mov    %ebx,%edx
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <memcpy@plt>

LLVM IR (-O0):
define void @safecpy(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%8 = load i8*, i8** %4
%9 = load i32, i32* %6
%10 = call i32 @memset(i8* %8, i32 0, i32 %9)
%11 = load i8*, i8** %5
%12 = icmp ne i8* %11, null
br i1 %12, label %13, label %16
13: ; preds = %3
%14 = load i8*, i8** %5
%15 = call i32 @strlen(i8* %14)
br label %17
16: ; preds = %3
br label %17
17: ; preds = %16, %13
%18 = phi i32 [ %15, %13 ], [ 0, %16 ]
store i32 %18, i32* %7
%19 = load i32, i32* %7
%20 = load i32, i32* %6
%21 = icmp sgt i32 %19, %20
br i1 %21, label %22, label %24
22: ; preds = %17
%23 = load i32, i32* %6
store i32 %23, i32* %7
br label %24
24: ; preds = %22, %17
%25 = load i8*, i8** %4
%26 = load i8*, i8** %5
%27 = load i32, i32* %7
%28 = call i32 @memcpy(i8* %25, i8* %26, i32 %27)
ret void
}

LLVM IR (-O2):
define void @safecpy(i8* %0, i8* %1, i32 %2) {
%4 = tail call i32 @memset(i8* %0, i32 0, i32 %2)
%5 = icmp eq i8* %1, null
br i1 %5, label %8, label %6
6: ; preds = %3
%7 = tail call i32 @strlen(i8* %1)
br label %8
8: ; preds = %3, %6
%9 = phi i32 [ %7, %6 ], [ 0, %3 ]
%10 = icmp sgt i32 %9, %2
%11 = select i1 %10, i32 %2, i32 %9
%12 = tail call i32 @memcpy(i8* %0, i8* %1, i32 %11)
ret void
}