int main() {
  char str1[10] = {"gwr22"}, str2[30] = {"sBhteahne"};
  int index;
  printf("Please give the index from which you'd like to copy:\n");
  scanf("%d", &index);
  printf("original str2=%s\n", str2);
  mstrcpy(str1, str2, index);
  printf("After:\nstr1=%s\nstr2=%s\n", str1, str2);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    0xeba(%rip),%rax
mov    %rax,-0xe(%rbp)
mov    0xeb7(%rip),%ax
mov    %ax,-0x6(%rbp)
mov    0xeb4(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    0xeb1(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    0xeae(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    0xeac(%rip),%eax
mov    %eax,-0x18(%rbp)
mov    0xea6(%rip),%ax
mov    %ax,-0x14(%rbp)
lea    0xe9d(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xec5(%rip),%rdi
lea    -0x34(%rbp),%rsi
mov    $0x0,%al
call   1050 <scanf@plt>
lea    -0x30(%rbp),%rsi
lea    0xeb2(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    -0xe(%rbp),%rdi
lea    -0x30(%rbp),%rsi
mov    -0x34(%rbp),%edx
mov    $0x0,%al
call   1040 <mstrcpy@plt>
lea    -0xe(%rbp),%rsi
lea    -0x30(%rbp),%rdx
lea    0xe9c(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x48,%rsp
movabs $0x3232727767,%rax
mov    %rax,0x10(%rsp)
movw   $0x0,0x18(%rsp)
movups 0xeba(%rip),%xmm0
movups %xmm0,0x2e(%rsp)
movaps 0xea0(%rip),%xmm0
movaps %xmm0,0x20(%rsp)
lea    0xedf(%rip),%rdi
call   1030 <puts@plt>
lea    0xea6(%rip),%rdi
lea    0xc(%rsp),%rsi
xor    %eax,%eax
call   1060 <scanf@plt>
lea    0xe96(%rip),%rdi
lea    0x20(%rsp),%rbx
mov    %rbx,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0xc(%rsp),%edx
lea    0x10(%rsp),%r14
mov    %r14,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <mstrcpy@plt>
lea    0xe7c(%rip),%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x48,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@__const.main.str1 = [10 x i8] c"gwr22\00\00\00\00\00"
@__const.main.str2 = [30 x i8] c"sBhteahne\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00"
@.str = [54 x i8] c"Please give the index from which you'd like to copy:\0A\00"
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [18 x i8] c"original str2=%s\0A\00"
@.str.3 = [24 x i8] c"After:\0Astr1=%s\0Astr2=%s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [10 x i8]
%3 = alloca [30 x i8]
%4 = alloca i32
store i32 0, i32* %1
%5 = bitcast [10 x i8]* %2 to i8*
%6 = bitcast [30 x i8]* %3 to i8*
%7 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str, i64 0, i64 0))
%8 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %4)
%9 = getelementptr inbounds [30 x i8], [30 x i8]* %3, i64 0, i64 0
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i8* %9)
%11 = getelementptr inbounds [10 x i8], [10 x i8]* %2, i64 0, i64 0
%12 = getelementptr inbounds [30 x i8], [30 x i8]* %3, i64 0, i64 0
%13 = load i32, i32* %4
%14 = call i32 (i8*, i8*, i32, ...) bitcast (i32 (...)* @mstrcpy to i32 (i8*, i8*, i32, ...)*)(i8* %11, i8* %12, i32 %13)
%15 = getelementptr inbounds [10 x i8], [10 x i8]* %2, i64 0, i64 0
%16 = getelementptr inbounds [30 x i8], [30 x i8]* %3, i64 0, i64 0
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i8* %15, i8* %16)
ret i32 0
}

LLVM IR (-O2):
@__const.main.str1 = [10 x i8] c"gwr22\00\00\00\00\00"
@__const.main.str2 = [30 x i8] c"sBhteahne\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00"
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [18 x i8] c"original str2=%s\0A\00"
@.str.3 = [24 x i8] c"After:\0Astr1=%s\0Astr2=%s\0A\00"
@str = [53 x i8] c"Please give the index from which you'd like to copy:\00"
define i32 @main() {
%1 = alloca [10 x i8]
%2 = alloca [30 x i8]
%3 = alloca i32
%4 = getelementptr inbounds [10 x i8], [10 x i8]* %1, i64 0, i64 0
%5 = getelementptr inbounds [30 x i8], [30 x i8]* %2, i64 0, i64 0
%6 = bitcast i32* %3 to i8*
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([53 x i8], [53 x i8]* @str, i64 0, i64 0))
%8 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %3)
%9 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i8* %5)
%10 = load i32, i32* %3
%11 = call i32 (i8*, i8*, i32, ...) bitcast (i32 (...)* @mstrcpy to i32 (i8*, i8*, i32, ...)*)(i8* %4, i8* %5, i32 %10)
%12 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i8* %4, i8* %5)
ret i32 0
}