#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 *, int);
int strlen(char const *);
void IcsStrCpy(char *dest, char const *src, int len) {
  if (dest != src) {
    int nchar = strlen(src);
    nchar = (nchar > len - 1) ? len - 1 : nchar;
    memcpy(dest, src, nchar);
    dest[nchar] = 0;
  }
}


x86 assembly (clang, -O0):
<IcsStrCpy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%rax
cmp    -0x10(%rbp),%rax
je     1194 <IcsStrCpy+0x74>
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jle    116c <IcsStrCpy+0x4c>
mov    -0x14(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1172 <IcsStrCpy+0x52>
mov    -0x18(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%edx
call   1040 <memcpy@plt>
mov    -0x8(%rbp),%rax
movslq -0x18(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<IcsStrCpy>:
cmp    %rsi,%rdi
je     115c <IcsStrCpy+0x3c>
push   %r15
push   %r14
push   %rbx
mov    %edx,%ebx
mov    %rsi,%r15
mov    %rdi,%r14
mov    %rsi,%rdi
call   1030 <strlen@plt>
add    $0xffffffff,%ebx
cmp    %ebx,%eax
cmovle %eax,%ebx
mov    %r14,%rdi
mov    %r15,%rsi
mov    %ebx,%edx
call   1040 <memcpy@plt>
movslq %ebx,%rax
movb   $0x0,(%r14,%rax,1)
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define void @IcsStrCpy(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 i8*, i8** %5
%10 = icmp ne i8* %8, %9
br i1 %10, label %11, label %33
11: ; preds = %3
%12 = load i8*, i8** %5
%13 = call i32 @strlen(i8* %12)
store i32 %13, i32* %7
%14 = load i32, i32* %7
%15 = load i32, i32* %6
%16 = sub nsw i32 %15, 1
%17 = icmp sgt i32 %14, %16
br i1 %17, label %18, label %21
18: ; preds = %11
%19 = load i32, i32* %6
%20 = sub nsw i32 %19, 1
br label %23
21: ; preds = %11
%22 = load i32, i32* %7
br label %23
23: ; preds = %21, %18
%24 = phi i32 [ %20, %18 ], [ %22, %21 ]
store i32 %24, i32* %7
%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)
%29 = load i8*, i8** %4
%30 = load i32, i32* %7
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i8, i8* %29, i64 %31
store i8 0, i8* %32
br label %33
33: ; preds = %23, %3
ret void
}

LLVM IR (-O2):
define void @IcsStrCpy(i8* %0, i8* %1, i32 %2) {
%4 = icmp eq i8* %0, %1
br i1 %4, label %13, label %5
5: ; preds = %3
%6 = tail call i32 @strlen(i8* %1)
%7 = add nsw i32 %2, -1
%8 = icmp sgt i32 %6, %7
%9 = select i1 %8, i32 %7, i32 %6
%10 = tail call i32 @memcpy(i8* %0, i8* %1, i32 %9)
%11 = sext i32 %9 to i64
%12 = getelementptr inbounds i8, i8* %0, i64 %11
store i8 0, i8* %12
br label %13
13: ; preds = %5, %3
ret void
}