#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*/ mstrncpy(char *, char *, int);
int /*<<< orphan*/ printf(char *, char *);
int strlen(char *);
int main(void) {
  char s[10];
  char t[10] = "abc";
  int lt = strlen(t);
  mstrncpy(s, t, lt - 1);
  printf("%s\n", s);
  mstrncpy(s, t, lt);
  printf("%s\n", s);
  mstrncpy(s, t, lt + 1);
  printf("%s\n", s);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0xeba(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    0xeb7(%rip),%ax
mov    %ax,-0x10(%rbp)
lea    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x1c(%rbp)
lea    -0xe(%rbp),%rdi
lea    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
sub    $0x1,%edx
call   1030 <mstrncpy@plt>
lea    -0xe(%rbp),%rsi
lea    0xe8b(%rip),%rdi
call   1050 <printf@plt>
lea    -0xe(%rbp),%rdi
lea    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1030 <mstrncpy@plt>
lea    -0xe(%rbp),%rsi
lea    0xe6b(%rip),%rdi
call   1050 <printf@plt>
lea    -0xe(%rbp),%rdi
lea    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
add    $0x1,%edx
call   1030 <mstrncpy@plt>
lea    -0xe(%rbp),%rsi
lea    0xe48(%rip),%rdi
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x18,%rsp
movw   $0x0,0x8(%rsp)
movq   $0x636261,(%rsp)
mov    %rsp,%r14
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebx
lea    -0x1(%rbx),%edx
lea    0xe(%rsp),%r15
mov    %r15,%rdi
mov    %r14,%rsi
call   1030 <mstrncpy@plt>
lea    0xe99(%rip),%r12
mov    %r12,%rdi
mov    %r15,%rsi
call   1050 <printf@plt>
mov    %r15,%rdi
mov    %r14,%rsi
mov    %ebx,%edx
call   1030 <mstrncpy@plt>
mov    %r12,%rdi
mov    %r15,%rsi
call   1050 <printf@plt>
add    $0x1,%ebx
mov    %r15,%rdi
mov    %r14,%rsi
mov    %ebx,%edx
call   1030 <mstrncpy@plt>
mov    %r12,%rdi
mov    %r15,%rsi
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@__const.main.t = [10 x i8] c"abc\00\00\00\00\00\00\00"
@.str = [4 x i8] c"%s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [10 x i8]
%3 = alloca [10 x i8]
%4 = alloca i32
store i32 0, i32* %1
%5 = bitcast [10 x i8]* %3 to i8*
%6 = getelementptr inbounds [10 x i8], [10 x i8]* %3, i64 0, i64 0
%7 = call i32 @strlen(i8* %6)
store i32 %7, i32* %4
%8 = getelementptr inbounds [10 x i8], [10 x i8]* %2, i64 0, i64 0
%9 = getelementptr inbounds [10 x i8], [10 x i8]* %3, i64 0, i64 0
%10 = load i32, i32* %4
%11 = sub nsw i32 %10, 1
%12 = call i32 @mstrncpy(i8* %8, i8* %9, i32 %11)
%13 = getelementptr inbounds [10 x i8], [10 x i8]* %2, i64 0, i64 0
%14 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %13)
%15 = getelementptr inbounds [10 x i8], [10 x i8]* %2, i64 0, i64 0
%16 = getelementptr inbounds [10 x i8], [10 x i8]* %3, i64 0, i64 0
%17 = load i32, i32* %4
%18 = call i32 @mstrncpy(i8* %15, i8* %16, i32 %17)
%19 = getelementptr inbounds [10 x i8], [10 x i8]* %2, i64 0, i64 0
%20 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %19)
%21 = getelementptr inbounds [10 x i8], [10 x i8]* %2, i64 0, i64 0
%22 = getelementptr inbounds [10 x i8], [10 x i8]* %3, i64 0, i64 0
%23 = load i32, i32* %4
%24 = add nsw i32 %23, 1
%25 = call i32 @mstrncpy(i8* %21, i8* %22, i32 %24)
%26 = getelementptr inbounds [10 x i8], [10 x i8]* %2, i64 0, i64 0
%27 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %26)
ret i32 0
}

LLVM IR (-O2):
@__const.main.t = [10 x i8] c"abc\00\00\00\00\00\00\00"
@.str = [4 x i8] c"%s\0A\00"
define i32 @main() {
%1 = alloca [10 x i8]
%2 = alloca [10 x i8]
%3 = getelementptr inbounds [10 x i8], [10 x i8]* %1, i64 0, i64 0
%4 = getelementptr inbounds [10 x i8], [10 x i8]* %2, i64 0, i64 0
%5 = call i32 @strlen(i8* %4)
%6 = add nsw i32 %5, -1
%7 = call i32 @mstrncpy(i8* %3, i8* %4, i32 %6)
%8 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %3)
%9 = call i32 @mstrncpy(i8* %3, i8* %4, i32 %5)
%10 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %3)
%11 = add nsw i32 %5, 1
%12 = call i32 @mstrncpy(i8* %3, i8* %4, i32 %11)
%13 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %3)
ret i32 0
}