#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 */
typedef int /*<<< orphan*/ string;

/* Variables and functions */
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, char *, ...);
int strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, int);
int main(void) {
  char dest1[20];
  char src1[] = "abc";
  int n1 = 3;
  strncpy(dest1, src1, n1);
  printf("dest1 = %s\n", dest1);
  char dest2[20] = "********************";
  char src2[] = "abcxyz";
  int n2 = (int)strlen(src2) + 1;
  strncpy(dest2, src2, n2);
  printf("dest2 = %s, dest2[15]= %c \n", dest2, dest2[10]);
  char dest3[100] = "http://man7.org/tlpi/";
  char src3[6] = "abcxyz";
  int n3 = 20;
  strncpy(dest3, src3, n3);
  printf("dest3 = %s\n", dest3);
  char string[10] = {0};
  char *string2 = "Hello World";
  strncpy(string, string2, 2);
  printf("%s\n", string);
  memset(string, 0, sizeof(string));
  strncpy(string, string2, strlen(string2));
  printf("%s\n", string);
  memset(string, 0, sizeof(string));
  strncpy(string, string2, sizeof(string) - 1);
  printf("%s\n", string);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xe0,%rsp
mov    0xe9f(%rip),%eax
mov    %eax,-0x24(%rbp)
movl   $0x3,-0x28(%rbp)
lea    -0x20(%rbp),%rdi
lea    -0x24(%rbp),%rsi
mov    -0x28(%rbp),%edx
call   1030 <strncpy@plt>
lea    -0x20(%rbp),%rsi
lea    0xe7e(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0xebc(%rip),%rax
mov    %rax,-0x40(%rbp)
mov    0xeb9(%rip),%rax
mov    %rax,-0x38(%rbp)
mov    0xeb7(%rip),%eax
mov    %eax,-0x30(%rbp)
mov    0xe5e(%rip),%eax
mov    %eax,-0x47(%rbp)
mov    0xe58(%rip),%ax
mov    %ax,-0x43(%rbp)
mov    0xe50(%rip),%al
mov    %al,-0x41(%rbp)
lea    -0x47(%rbp),%rdi
call   1040 <strlen@plt>
add    $0x1,%eax
mov    %eax,-0x4c(%rbp)
lea    -0x40(%rbp),%rdi
lea    -0x47(%rbp),%rsi
mov    -0x4c(%rbp),%edx
call   1030 <strncpy@plt>
lea    -0x40(%rbp),%rsi
movsbl -0x36(%rbp),%edx
lea    0xe20(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0xb0(%rbp),%rdi
lea    0xe64(%rip),%rsi
mov    $0x64,%edx
call   1070 <memcpy@plt>
mov    0xeb8(%rip),%eax
mov    %eax,-0xb6(%rbp)
mov    0xeaf(%rip),%ax
mov    %ax,-0xb2(%rbp)
movl   $0x14,-0xbc(%rbp)
lea    -0xb0(%rbp),%rdi
lea    -0xb6(%rbp),%rsi
mov    -0xbc(%rbp),%edx
call   1030 <strncpy@plt>
lea    -0xb0(%rbp),%rsi
lea    0xdd2(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0xc6(%rbp),%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   1060 <memset@plt>
lea    0xdbd(%rip),%rax
mov    %rax,-0xd0(%rbp)
lea    -0xc6(%rbp),%rdi
mov    -0xd0(%rbp),%rsi
mov    $0x2,%edx
call   1030 <strncpy@plt>
lea    -0xc6(%rbp),%rsi
lea    0xd5d(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0xc6(%rbp),%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   1060 <memset@plt>
lea    -0xc6(%rbp),%rax
mov    %rax,-0xe0(%rbp)
mov    -0xd0(%rbp),%rax
mov    %rax,-0xd8(%rbp)
mov    -0xd0(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0xe0(%rbp),%rdi
mov    -0xd8(%rbp),%rsi
mov    %eax,%edx
call   1030 <strncpy@plt>
lea    -0xc6(%rbp),%rsi
lea    0xcf8(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0xc6(%rbp),%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   1060 <memset@plt>
lea    -0xc6(%rbp),%rdi
mov    -0xd0(%rbp),%rsi
mov    $0x9,%edx
call   1030 <strncpy@plt>
lea    -0xc6(%rbp),%rsi
lea    0xcb8(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
xor    %eax,%eax
add    $0xe0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0xe0,%rsp
movl   $0x636261,0x14(%rsp)
lea    0xc0(%rsp),%rbx
lea    0x14(%rsp),%rsi
mov    %rbx,%rdi
mov    $0x3,%edx
call   1030 <strncpy@plt>
lea    0xe9b(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
movaps 0xe7a(%rip),%xmm0
movaps %xmm0,0x30(%rsp)
movl   $0x2a2a2a2a,0x40(%rsp)
2a
movl   $0x7a7978,0x1b(%rsp)
movl   $0x78636261,0x18(%rsp)
78
lea    0x18(%rsp),%rbx
mov    %rbx,%rdi
call   1040 <strlen@plt>
lea    0x1(%rax),%edx
lea    0x30(%rsp),%r14
mov    %r14,%rdi
mov    %rbx,%rsi
call   1030 <strncpy@plt>
movsbl 0x3a(%rsp),%edx
lea    0xe54(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
movaps 0xed0(%rip),%xmm0
movaps %xmm0,0xa0(%rsp)
movaps 0xeb1(%rip),%xmm0
movaps %xmm0,0x90(%rsp)
movaps 0xe92(%rip),%xmm0
movaps %xmm0,0x80(%rsp)
movaps 0xe73(%rip),%xmm0
movaps %xmm0,0x70(%rsp)
movaps 0xe57(%rip),%xmm0
movaps %xmm0,0x60(%rsp)
movaps 0xe3b(%rip),%xmm0
movaps %xmm0,0x50(%rsp)
movl   $0x0,0xb0(%rsp)
movw   $0x7a79,0x10(%rsp)
movl   $0x78636261,0xc(%rsp)
78
lea    0x50(%rsp),%rbx
lea    0xc(%rsp),%rsi
mov    %rbx,%rdi
mov    $0x14,%edx
call   1030 <strncpy@plt>
lea    0xddd(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
movw   $0x0,0x28(%rsp)
movq   $0x0,0x20(%rsp)
lea    0xdc8(%rip),%r14
lea    0x20(%rsp),%rbx
mov    %rbx,%rdi
mov    %r14,%rsi
mov    $0x2,%edx
call   1030 <strncpy@plt>
lea    0xd79(%rip),%r15
mov    %r15,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   1060 <memset@plt>
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %eax,%edx
call   1030 <strncpy@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   1060 <memset@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    $0x9,%edx
call   1030 <strncpy@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
add    $0xe0,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@__const.main.src1 = [4 x i8] c"abc\00"
@.str = [12 x i8] c"dest1 = %s\0A\00"
@__const.main.dest2 = [20 x i8] c"********************"
@__const.main.src2 = [7 x i8] c"abcxyz\00"
@.str.1 = [28 x i8] c"dest2 = %s, dest2[15]= %c \0A\00"
@__const.main.dest3 = [100 x i8] c"http://man7.org/tlpi/\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00"
@__const.main.src3 = [6 x i8] c"abcxyz"
@.str.2 = [12 x i8] c"dest3 = %s\0A\00"
@.str.3 = [12 x i8] c"Hello World\00"
@.str.4 = [4 x i8] c"%s\0A\00"
define i32 @main() {
%1 = alloca [20 x i8]
%2 = alloca [4 x i8]
%3 = alloca i32
%4 = alloca [20 x i8]
%5 = alloca [7 x i8]
%6 = alloca i32
%7 = alloca [100 x i8]
%8 = alloca [6 x i8]
%9 = alloca i32
%10 = alloca [10 x i8]
%11 = alloca i8*
%12 = bitcast [4 x i8]* %2 to i8*
store i32 3, i32* %3
%13 = getelementptr inbounds [20 x i8], [20 x i8]* %1, i64 0, i64 0
%14 = getelementptr inbounds [4 x i8], [4 x i8]* %2, i64 0, i64 0
%15 = load i32, i32* %3
%16 = call i32 @strncpy(i8* %13, i8* %14, i32 %15)
%17 = getelementptr inbounds [20 x i8], [20 x i8]* %1, i64 0, i64 0
%18 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* %17)
%19 = bitcast [20 x i8]* %4 to i8*
%20 = bitcast [7 x i8]* %5 to i8*
%21 = getelementptr inbounds [7 x i8], [7 x i8]* %5, i64 0, i64 0
%22 = call i32 @strlen(i8* %21)
%23 = add nsw i32 %22, 1
store i32 %23, i32* %6
%24 = getelementptr inbounds [20 x i8], [20 x i8]* %4, i64 0, i64 0
%25 = getelementptr inbounds [7 x i8], [7 x i8]* %5, i64 0, i64 0
%26 = load i32, i32* %6
%27 = call i32 @strncpy(i8* %24, i8* %25, i32 %26)
%28 = getelementptr inbounds [20 x i8], [20 x i8]* %4, i64 0, i64 0
%29 = getelementptr inbounds [20 x i8], [20 x i8]* %4, i64 0, i64 10
%30 = load i8, i8* %29
%31 = sext i8 %30 to i32
%32 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i8* %28, i32 %31)
%33 = bitcast [100 x i8]* %7 to i8*
%34 = bitcast [6 x i8]* %8 to i8*
store i32 20, i32* %9
%35 = getelementptr inbounds [100 x i8], [100 x i8]* %7, i64 0, i64 0
%36 = getelementptr inbounds [6 x i8], [6 x i8]* %8, i64 0, i64 0
%37 = load i32, i32* %9
%38 = call i32 @strncpy(i8* %35, i8* %36, i32 %37)
%39 = getelementptr inbounds [100 x i8], [100 x i8]* %7, i64 0, i64 0
%40 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i8* %39)
%41 = bitcast [10 x i8]* %10 to i8*
store i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i8** %11
%42 = getelementptr inbounds [10 x i8], [10 x i8]* %10, i64 0, i64 0
%43 = load i8*, i8** %11
%44 = call i32 @strncpy(i8* %42, i8* %43, i32 2)
%45 = getelementptr inbounds [10 x i8], [10 x i8]* %10, i64 0, i64 0
%46 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i8* %45)
%47 = getelementptr inbounds [10 x i8], [10 x i8]* %10, i64 0, i64 0
%48 = call i32 @memset(i8* %47, i32 0, i32 10)
%49 = getelementptr inbounds [10 x i8], [10 x i8]* %10, i64 0, i64 0
%50 = load i8*, i8** %11
%51 = load i8*, i8** %11
%52 = call i32 @strlen(i8* %51)
%53 = call i32 @strncpy(i8* %49, i8* %50, i32 %52)
%54 = getelementptr inbounds [10 x i8], [10 x i8]* %10, i64 0, i64 0
%55 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i8* %54)
%56 = getelementptr inbounds [10 x i8], [10 x i8]* %10, i64 0, i64 0
%57 = call i32 @memset(i8* %56, i32 0, i32 10)
%58 = getelementptr inbounds [10 x i8], [10 x i8]* %10, i64 0, i64 0
%59 = load i8*, i8** %11
%60 = call i32 @strncpy(i8* %58, i8* %59, i32 9)
%61 = getelementptr inbounds [10 x i8], [10 x i8]* %10, i64 0, i64 0
%62 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i8* %61)
ret i32 0
}

LLVM IR (-O2):
@.str = [12 x i8] c"dest1 = %s\0A\00"
@__const.main.src2 = [7 x i8] c"abcxyz\00"
@.str.1 = [28 x i8] c"dest2 = %s, dest2[15]= %c \0A\00"
@__const.main.dest3 = [100 x i8] c"http://man7.org/tlpi/\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00"
@__const.main.src3 = [6 x i8] c"abcxyz"
@.str.2 = [12 x i8] c"dest3 = %s\0A\00"
@.str.3 = [12 x i8] c"Hello World\00"
@.str.4 = [4 x i8] c"%s\0A\00"
define i32 @main() {
%1 = alloca [20 x i8]
%2 = alloca i32
%3 = alloca [20 x i8]
%4 = alloca [7 x i8]
%5 = alloca [100 x i8]
%6 = alloca [6 x i8]
%7 = alloca [10 x i8]
%8 = getelementptr inbounds [20 x i8], [20 x i8]* %1, i64 0, i64 0
%9 = bitcast i32* %2 to i8*
store i32 6513249, i32* %2
%10 = call i32 @strncpy(i8* %8, i8* %9, i32 3)
%11 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* %8)
%12 = getelementptr inbounds [20 x i8], [20 x i8]* %3, i64 0, i64 0
%13 = getelementptr inbounds [7 x i8], [7 x i8]* %4, i64 0, i64 0
%14 = call i32 @strlen(i8* %13)
%15 = add nsw i32 %14, 1
%16 = call i32 @strncpy(i8* %12, i8* %13, i32 %15)
%17 = getelementptr inbounds [20 x i8], [20 x i8]* %3, i64 0, i64 10
%18 = load i8, i8* %17
%19 = sext i8 %18 to i32
%20 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i8* %12, i32 %19)
%21 = getelementptr inbounds [100 x i8], [100 x i8]* %5, i64 0, i64 0
%22 = getelementptr inbounds [6 x i8], [6 x i8]* %6, i64 0, i64 0
%23 = call i32 @strncpy(i8* %21, i8* %22, i32 20)
%24 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i8* %21)
%25 = getelementptr inbounds [10 x i8], [10 x i8]* %7, i64 0, i64 0
%26 = call i32 @strncpy(i8* %25, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i32 2)
%27 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i8* %25)
%28 = call i32 @memset(i8* %25, i32 0, i32 10)
%29 = call i32 @strlen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
%30 = call i32 @strncpy(i8* %25, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i32 %29)
%31 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i8* %25)
%32 = call i32 @memset(i8* %25, i32 0, i32 10)
%33 = call i32 @strncpy(i8* %25, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i32 9)
%34 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i8* %25)
ret i32 0
}