#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 *StrCopy(char *Dest, size_t DestSize, const char *Source) {
  size_t Len = strlen(Source);
  if (Len >= DestSize) {
    memcpy(Dest, Source, DestSize - 1);
    Dest[DestSize - 1] = '\0';
  } else {
    memcpy(Dest, Source, Len + 1);
  }
  return Dest;
}


x86 assembly (clang, -O0):
<StrCopy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x10(%rbp),%rax
jb     1179 <StrCopy+0x59>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x10(%rbp),%rdx
sub    $0x1,%rdx
call   1040 <memcpy@plt>
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    $0x1,%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    118e <StrCopy+0x6e>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
add    $0x1,%rdx
call   1040 <memcpy@plt>
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<StrCopy>:
push   %r15
push   %r14
push   %rbx
mov    %rdx,%r15
mov    %rsi,%rbx
mov    %rdi,%r14
mov    %rdx,%rdi
call   1030 <strlen@plt>
cmp    %rbx,%rax
jae    114f <StrCopy+0x2f>
add    $0x1,%rax
mov    %r14,%rdi
mov    %r15,%rsi
mov    %rax,%rdx
call   1040 <memcpy@plt>
jmp    1164 <StrCopy+0x44>
lea    -0x1(%rbx),%rdx
mov    %r14,%rdi
mov    %r15,%rsi
call   1040 <memcpy@plt>
movb   $0x0,-0x1(%r14,%rbx,1)
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

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

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