int main() {
  char str[60 + 1], dest[60 + 1];
  int start, length, maxlength;
  printf("strncpy() Example 2\n\n");
  printf("Enter your test string: ");
  scanf("%s", str);
  printf("You entered: '%s'\n\n", str);
  do {
    printf("Enter the starting position (0 to %d): ", strlen(str) - 1);
    scanf("%d", &start);
    if (start >= 0 && start < strlen(str) - 1)
      break;
    printf("The value is out of range; try again.\n");
  } while (1);
  do {
    maxlength = strlen(str) - start;
    printf("Enter the length (1 to %d): ", maxlength);
    scanf("%d", &length);
    if (length >= 1 && length <= maxlength)
      break;
    printf("The value is out of range; try again.\n");
  } while (1);
  strncpy(dest, str + start, length);
  printf("Result: '%s'\n\n", dest);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xea7(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xeaf(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0x50(%rbp),%rsi
lea    0xeb6(%rip),%rdi
mov    $0x0,%al
call   1060 <scanf@plt>
lea    -0x50(%rbp),%rsi
lea    0xea7(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0x50(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,%rsi
sub    $0x1,%rsi
lea    0xe9d(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xeb7(%rip),%rdi
lea    -0x94(%rbp),%rsi
mov    $0x0,%al
call   1060 <scanf@plt>
cmpl   $0x0,-0x94(%rbp)
jl     1205 <main+0xc5>
movslq -0x94(%rbp),%rax
mov    %rax,-0xa8(%rbp)
lea    -0x50(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,%rcx
mov    -0xa8(%rbp),%rax
sub    $0x1,%rcx
cmp    %rcx,%rax
jae    1205 <main+0xc5>
jmp    1222 <main+0xe2>
lea    0xe65(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x1,%al
test   $0x1,%al
jne    1192 <main+0x52>
jmp    1222 <main+0xe2>
jmp    1227 <main+0xe7>
lea    -0x50(%rbp),%rdi
call   1040 <strlen@plt>
movslq -0x94(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,-0x9c(%rbp)
mov    -0x9c(%rbp),%esi
lea    0xe4b(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xe13(%rip),%rdi
lea    -0x98(%rbp),%rsi
mov    $0x0,%al
call   1060 <scanf@plt>
cmpl   $0x1,-0x98(%rbp)
jl     128d <main+0x14d>
mov    -0x98(%rbp),%eax
cmp    -0x9c(%rbp),%eax
jg     128d <main+0x14d>
jmp    12aa <main+0x16a>
lea    0xddd(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x1,%al
test   $0x1,%al
jne    1227 <main+0xe7>
jmp    12aa <main+0x16a>
lea    -0x90(%rbp),%rdi
lea    -0x50(%rbp),%rsi
movslq -0x94(%rbp),%rax
add    %rax,%rsi
movslq -0x98(%rbp),%rdx
call   1030 <strncpy@plt>
lea    -0x90(%rbp),%rsi
lea    0xddc(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
xor    %eax,%eax
add    $0xb0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x98,%rsp
lea    0xf1f(%rip),%rdi
call   1040 <puts@plt>
lea    0xe8c(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xe97(%rip),%rdi
lea    0x10(%rsp),%rbx
mov    %rbx,%rsi
xor    %eax,%eax
call   1070 <scanf@plt>
lea    0xe84(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xeaf(%rip),%r15
lea    0xc(%rsp),%r12
lea    0xee7(%rip),%r13
jmp    11c8 <main+0x78>
nopw   0x0(%rax,%rax,1)
mov    %r13,%rdi
call   1040 <puts@plt>
mov    %rbx,%rdi
call   1050 <strlen@plt>
lea    -0x1(%rax),%rsi
lea    0xe55(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r15,%rdi
mov    %r12,%rsi
xor    %eax,%eax
call   1070 <scanf@plt>
mov    0xc(%rsp),%r14d
test   %r14d,%r14d
js     11c0 <main+0x70>
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %rax,%rbp
add    $0xffffffffffffffff,%rax
cmp    %r14,%rax
jbe    11c0 <main+0x70>
sub    %r14d,%ebp
lea    0xe44(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xe31(%rip),%rdi
lea    0x8(%rsp),%rsi
xor    %eax,%eax
call   1070 <scanf@plt>
mov    0x8(%rsp),%eax
test   %eax,%eax
jle    1240 <main+0xf0>
cmp    %ebp,%eax
jle    128c <main+0x13c>
nop
lea    0xe55(%rip),%rdi
call   1040 <puts@plt>
lea    0x10(%rsp),%rdi
call   1050 <strlen@plt>
mov    %rax,%rbx
sub    0xc(%rsp),%ebx
lea    0xdf7(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xde4(%rip),%rdi
lea    0x8(%rsp),%rsi
xor    %eax,%eax
call   1070 <scanf@plt>
mov    0x8(%rsp),%eax
test   %eax,%eax
jle    1240 <main+0xf0>
cmp    %ebx,%eax
jg     1240 <main+0xf0>
movslq 0xc(%rsp),%rcx
lea    (%rsp,%rcx,1),%rsi
add    $0x10,%rsi
mov    %eax,%edx
lea    0x50(%rsp),%rbx
mov    %rbx,%rdi
call   1030 <strncpy@plt>
lea    0xdc9(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
xor    %eax,%eax
add    $0x98,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [22 x i8] c"strncpy() Example 2\0A\0A\00"
@.str.1 = [25 x i8] c"Enter your test string: \00"
@.str.2 = [3 x i8] c"%s\00"
@.str.3 = [20 x i8] c"You entered: '%s'\0A\0A\00"
@.str.4 = [40 x i8] c"Enter the starting position (0 to %d): \00"
@.str.5 = [3 x i8] c"%d\00"
@.str.6 = [39 x i8] c"The value is out of range; try again.\0A\00"
@.str.7 = [29 x i8] c"Enter the length (1 to %d): \00"
@.str.8 = [15 x i8] c"Result: '%s'\0A\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [61 x i8]
%3 = alloca [61 x i8]
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32 0, i32* %1
%7 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
%8 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
%9 = getelementptr inbounds [61 x i8], [61 x i8]* %2, i64 0, i64 0
%10 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %9)
%11 = getelementptr inbounds [61 x i8], [61 x i8]* %2, i64 0, i64 0
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i8* %11)
br label %13
13: ; preds = %31, %0
%14 = getelementptr inbounds [61 x i8], [61 x i8]* %2, i64 0, i64 0
%15 = call i64 @strlen(i8* %14)
%16 = sub i64 %15, 1
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.4, i64 0, i64 0), i64 %16)
%18 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i32* %4)
%19 = load i32, i32* %4
%20 = icmp sge i32 %19, 0
br i1 %20, label %21, label %29
21: ; preds = %13
%22 = load i32, i32* %4
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds [61 x i8], [61 x i8]* %2, i64 0, i64 0
%25 = call i64 @strlen(i8* %24)
%26 = sub i64 %25, 1
%27 = icmp ult i64 %23, %26
br i1 %27, label %28, label %29
28: ; preds = %21
br label %32
29: ; preds = %21, %13
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.6, i64 0, i64 0))
br label %31
31: ; preds = %29
br i1 true, label %13, label %32
32: ; preds = %31, %28
br label %33
33: ; preds = %52, %32
%34 = getelementptr inbounds [61 x i8], [61 x i8]* %2, i64 0, i64 0
%35 = call i64 @strlen(i8* %34)
%36 = load i32, i32* %4
%37 = sext i32 %36 to i64
%38 = sub i64 %35, %37
%39 = trunc i64 %38 to i32
store i32 %39, i32* %6
%40 = load i32, i32* %6
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.7, i64 0, i64 0), i32 %40)
%42 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i32* %5)
%43 = load i32, i32* %5
%44 = icmp sge i32 %43, 1
br i1 %44, label %45, label %50
45: ; preds = %33
%46 = load i32, i32* %5
%47 = load i32, i32* %6
%48 = icmp sle i32 %46, %47
br i1 %48, label %49, label %50
49: ; preds = %45
br label %53
50: ; preds = %45, %33
%51 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.6, i64 0, i64 0))
br label %52
52: ; preds = %50
br i1 true, label %33, label %53
53: ; preds = %52, %49
%54 = getelementptr inbounds [61 x i8], [61 x i8]* %3, i64 0, i64 0
%55 = getelementptr inbounds [61 x i8], [61 x i8]* %2, i64 0, i64 0
%56 = load i32, i32* %4
%57 = sext i32 %56 to i64
%58 = getelementptr inbounds i8, i8* %55, i64 %57
%59 = load i32, i32* %5
%60 = sext i32 %59 to i64
%61 = call i8* @strncpy(i8* %54, i8* %58, i64 %60)
%62 = getelementptr inbounds [61 x i8], [61 x i8]* %3, i64 0, i64 0
%63 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.8, i64 0, i64 0), i8* %62)
ret i32 0
}

LLVM IR (-O2):
@.str.1 = [25 x i8] c"Enter your test string: \00"
@.str.2 = [3 x i8] c"%s\00"
@.str.3 = [20 x i8] c"You entered: '%s'\0A\0A\00"
@.str.4 = [40 x i8] c"Enter the starting position (0 to %d): \00"
@.str.5 = [3 x i8] c"%d\00"
@.str.7 = [29 x i8] c"Enter the length (1 to %d): \00"
@.str.8 = [15 x i8] c"Result: '%s'\0A\0A\00"
@str = [21 x i8] c"strncpy() Example 2\0A\00"
@str.10 = [38 x i8] c"The value is out of range; try again.\00"
define i32 @main() {
%1 = alloca [61 x i8]
%2 = alloca [61 x i8]
%3 = alloca i32
%4 = alloca i32
%5 = getelementptr inbounds [61 x i8], [61 x i8]* %1, i64 0, i64 0
%6 = getelementptr inbounds [61 x i8], [61 x i8]* %2, i64 0, i64 0
%7 = bitcast i32* %3 to i8*
%8 = bitcast i32* %4 to i8*
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str, i64 0, i64 0))
%10 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
%11 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %5)
%12 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i8* %5)
br label %13
13: ; preds = %34, %0
%14 = call i64 @strlen(i8* %5)
%15 = add i64 %14, -1
%16 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @.str.4, i64 0, i64 0), i64 %15)
%17 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i32* %3)
%18 = load i32, i32* %3
%19 = icmp sgt i32 %18, -1
br i1 %19, label %20, label %34
20: ; preds = %13
%21 = zext i32 %18 to i64
%22 = call i64 @strlen(i8* %5)
%23 = add i64 %22, -1
%24 = icmp ugt i64 %23, %21
br i1 %24, label %25, label %34
25: ; preds = %20
%26 = trunc i64 %22 to i32
%27 = sub i32 %26, %18
%28 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.7, i64 0, i64 0), i32 %27)
%29 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i32* %4)
%30 = load i32, i32* %4
%31 = icmp slt i32 %30, 1
%32 = icmp sgt i32 %30, %27
%33 = or i1 %31, %32
br i1 %33, label %36, label %48
34: ; preds = %20, %13
%35 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @str.10, i64 0, i64 0))
br label %13
36: ; preds = %25, %36
%37 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @str.10, i64 0, i64 0))
%38 = call i64 @strlen(i8* %5)
%39 = load i32, i32* %3
%40 = trunc i64 %38 to i32
%41 = sub i32 %40, %39
%42 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.7, i64 0, i64 0), i32 %41)
%43 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i32* %4)
%44 = load i32, i32* %4
%45 = icmp slt i32 %44, 1
%46 = icmp sgt i32 %44, %41
%47 = or i1 %45, %46
br i1 %47, label %36, label %48
48: ; preds = %36, %25
%49 = phi i32 [ %30, %25 ], [ %44, %36 ]
%50 = load i32, i32* %3
%51 = sext i32 %50 to i64
%52 = getelementptr inbounds [61 x i8], [61 x i8]* %1, i64 0, i64 %51
%53 = zext i32 %49 to i64
%54 = call i8* @strncpy(i8* %6, i8* %52, i64 %53)
%55 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.8, i64 0, i64 0), i8* %6)
ret i32 0
}