#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*/ printf(char *, ...);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, int);
int main() {
  char str[30 + 1] = "This is a test.";
  char dest[30 + 1];
  printf("strncpy() Example 1\n\n");
  strncpy(dest, str, 4);
  printf("dest is: '%s'\n", dest);
  strncpy(dest, str + 5, 2);
  printf("dest is: '%s'\n", dest);
  strncpy(dest, str + 8, 6);
  printf("dest is: '%s'\n", dest);
  strncpy(dest, str + strlen(str) - 5, 5);
  printf("dest is: '%s'\n", dest);
  strcpy(dest, str + strlen(str) - 5);
  printf("dest is: '%s'\n\n", dest);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
movl   $0x0,-0x4(%rbp)
mov    0xeaa(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    0xea7(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    0xea4(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    0xea2(%rip),%eax
mov    %eax,-0x18(%rbp)
mov    0xe9c(%rip),%ax
mov    %ax,-0x14(%rbp)
mov    0xe94(%rip),%al
mov    %al,-0x12(%rbp)
lea    0xe8b(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    -0x50(%rbp),%rdi
lea    -0x30(%rbp),%rsi
mov    $0x4,%edx
call   1030 <strncpy@plt>
lea    -0x50(%rbp),%rsi
lea    0xe7d(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    -0x50(%rbp),%rdi
lea    -0x30(%rbp),%rsi
add    $0x5,%rsi
mov    $0x2,%edx
call   1030 <strncpy@plt>
lea    -0x50(%rbp),%rsi
lea    0xe55(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    -0x50(%rbp),%rdi
lea    -0x30(%rbp),%rsi
add    $0x8,%rsi
mov    $0x6,%edx
call   1030 <strncpy@plt>
lea    -0x50(%rbp),%rsi
lea    0xe2d(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    -0x50(%rbp),%rax
mov    %rax,-0x68(%rbp)
lea    -0x30(%rbp),%rax
mov    %rax,-0x70(%rbp)
lea    -0x30(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x70(%rbp),%rsi
mov    -0x68(%rbp),%rdi
cltq
add    %rax,%rsi
add    $0xfffffffffffffffb,%rsi
mov    $0x5,%edx
call   1030 <strncpy@plt>
lea    -0x50(%rbp),%rsi
lea    0xde7(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    -0x50(%rbp),%rax
mov    %rax,-0x58(%rbp)
lea    -0x30(%rbp),%rax
mov    %rax,-0x60(%rbp)
lea    -0x30(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x60(%rbp),%rsi
mov    -0x58(%rbp),%rdi
cltq
add    %rax,%rsi
add    $0xfffffffffffffffb,%rsi
call   1040 <strcpy@plt>
lea    -0x50(%rbp),%rsi
lea    0xdb5(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
xor    %eax,%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x40,%rsp
movups 0xeaf(%rip),%xmm0
movups %xmm0,0xf(%rsp)
movaps 0xe94(%rip),%xmm0
movaps %xmm0,(%rsp)
lea    0xec7(%rip),%rdi
call   1050 <puts@plt>
lea    0x20(%rsp),%rbx
mov    %rsp,%r14
mov    %rbx,%rdi
mov    %r14,%rsi
mov    $0x4,%edx
call   1030 <strncpy@plt>
lea    0xe84(%rip),%r15
mov    %r15,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
lea    0x5(%rsp),%rsi
mov    %rbx,%rdi
mov    $0x2,%edx
call   1030 <strncpy@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
mov    $0x6,%edx
call   1030 <strncpy@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
mov    %r14,%rdi
call   1060 <strlen@plt>
cltq
lea    (%rsp,%rax,1),%rsi
add    $0xfffffffffffffffb,%rsi
mov    %rbx,%rdi
mov    $0x5,%edx
call   1030 <strncpy@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
mov    %r14,%rdi
call   1060 <strlen@plt>
cltq
lea    (%rsp,%rax,1),%rsi
add    $0xfffffffffffffffb,%rsi
mov    %rbx,%rdi
call   1040 <strcpy@plt>
lea    0xdfb(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@__const.main.str = [31 x i8] c"This is a test.\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00"
@.str = [22 x i8] c"strncpy() Example 1\0A\0A\00"
@.str.1 = [15 x i8] c"dest is: '%s'\0A\00"
@.str.2 = [16 x i8] c"dest is: '%s'\0A\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [31 x i8]
%3 = alloca [31 x i8]
store i32 0, i32* %1
%4 = bitcast [31 x i8]* %2 to i8*
%5 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
%6 = getelementptr inbounds [31 x i8], [31 x i8]* %3, i64 0, i64 0
%7 = getelementptr inbounds [31 x i8], [31 x i8]* %2, i64 0, i64 0
%8 = call i32 @strncpy(i8* %6, i8* %7, i32 4)
%9 = getelementptr inbounds [31 x i8], [31 x i8]* %3, i64 0, i64 0
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8* %9)
%11 = getelementptr inbounds [31 x i8], [31 x i8]* %3, i64 0, i64 0
%12 = getelementptr inbounds [31 x i8], [31 x i8]* %2, i64 0, i64 0
%13 = getelementptr inbounds i8, i8* %12, i64 5
%14 = call i32 @strncpy(i8* %11, i8* %13, i32 2)
%15 = getelementptr inbounds [31 x i8], [31 x i8]* %3, i64 0, i64 0
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8* %15)
%17 = getelementptr inbounds [31 x i8], [31 x i8]* %3, i64 0, i64 0
%18 = getelementptr inbounds [31 x i8], [31 x i8]* %2, i64 0, i64 0
%19 = getelementptr inbounds i8, i8* %18, i64 8
%20 = call i32 @strncpy(i8* %17, i8* %19, i32 6)
%21 = getelementptr inbounds [31 x i8], [31 x i8]* %3, i64 0, i64 0
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8* %21)
%23 = getelementptr inbounds [31 x i8], [31 x i8]* %3, i64 0, i64 0
%24 = getelementptr inbounds [31 x i8], [31 x i8]* %2, i64 0, i64 0
%25 = getelementptr inbounds [31 x i8], [31 x i8]* %2, i64 0, i64 0
%26 = call i32 @strlen(i8* %25)
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds i8, i8* %24, i64 %27
%29 = getelementptr inbounds i8, i8* %28, i64 -5
%30 = call i32 @strncpy(i8* %23, i8* %29, i32 5)
%31 = getelementptr inbounds [31 x i8], [31 x i8]* %3, i64 0, i64 0
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8* %31)
%33 = getelementptr inbounds [31 x i8], [31 x i8]* %3, i64 0, i64 0
%34 = getelementptr inbounds [31 x i8], [31 x i8]* %2, i64 0, i64 0
%35 = getelementptr inbounds [31 x i8], [31 x i8]* %2, i64 0, i64 0
%36 = call i32 @strlen(i8* %35)
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds i8, i8* %34, i64 %37
%39 = getelementptr inbounds i8, i8* %38, i64 -5
%40 = call i32 @strcpy(i8* %33, i8* %39)
%41 = getelementptr inbounds [31 x i8], [31 x i8]* %3, i64 0, i64 0
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* %41)
ret i32 0
}

LLVM IR (-O2):
@__const.main.str = [31 x i8] c"This is a test.\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00"
@.str.1 = [15 x i8] c"dest is: '%s'\0A\00"
@.str.2 = [16 x i8] c"dest is: '%s'\0A\0A\00"
@str = [21 x i8] c"strncpy() Example 1\0A\00"
define i32 @main() {
%1 = alloca [31 x i8]
%2 = alloca [31 x i8]
%3 = getelementptr inbounds [31 x i8], [31 x i8]* %1, i64 0, i64 0
%4 = getelementptr inbounds [31 x i8], [31 x i8]* %2, i64 0, i64 0
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str, i64 0, i64 0))
%6 = call i32 @strncpy(i8* %4, i8* %3, i32 4)
%7 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8* %4)
%8 = getelementptr inbounds [31 x i8], [31 x i8]* %1, i64 0, i64 5
%9 = call i32 @strncpy(i8* %4, i8* %8, i32 2)
%10 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8* %4)
%11 = getelementptr inbounds [31 x i8], [31 x i8]* %1, i64 0, i64 8
%12 = call i32 @strncpy(i8* %4, i8* %11, i32 6)
%13 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8* %4)
%14 = call i32 @strlen(i8* %3)
%15 = sext i32 %14 to i64
%16 = getelementptr inbounds [31 x i8], [31 x i8]* %1, i64 0, i64 %15
%17 = getelementptr inbounds i8, i8* %16, i64 -5
%18 = call i32 @strncpy(i8* %4, i8* %17, i32 5)
%19 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8* %4)
%20 = call i32 @strlen(i8* %3)
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds [31 x i8], [31 x i8]* %1, i64 0, i64 %21
%23 = getelementptr inbounds i8, i8* %22, i64 -5
%24 = call i32 @strcpy(i8* %4, i8* %23)
%25 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* %4)
ret i32 0
}