#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 */
void _strncpy(char *dest, const char *src, size_t n) {
  char *end = dest + n;
  while ((dest < end) && ((*dest = *src) != '\0')) {
    dest++;
    src++;
  }
  *(end - 1) = '\0';
}


x86 assembly (clang, -O0):
<_strncpy>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rax
add    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rcx
xor    %eax,%eax
cmp    -0x20(%rbp),%rcx
mov    %al,-0x21(%rbp)
jae    1147 <_strncpy+0x47>
mov    -0x10(%rbp),%rax
mov    (%rax),%al
mov    -0x8(%rbp),%rcx
mov    %al,(%rcx)
movsbl %al,%eax
cmp    $0x0,%eax
setne  %al
mov    %al,-0x21(%rbp)
mov    -0x21(%rbp),%al
test   $0x1,%al
jne    1157 <_strncpy+0x57>
jmp    1174 <_strncpy+0x74>
mov    -0x8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    111c <_strncpy+0x1c>
mov    -0x20(%rbp),%rax
movb   $0x0,-0x1(%rax)
pop    %rbp
ret

x86 assembly (clang, -O2):
<_strncpy>:
lea    (%rdi,%rdx,1),%rax
test   %rdx,%rdx
jle    112a <_strncpy+0x2a>
add    $0x1,%rdi
nopl   (%rax)
movzbl (%rsi),%ecx
mov    %cl,-0x1(%rdi)
test   %cl,%cl
je     112a <_strncpy+0x2a>
add    $0x1,%rsi
lea    0x1(%rdi),%rcx
cmp    %rax,%rdi
mov    %rcx,%rdi
jb     1110 <_strncpy+0x10>
movb   $0x0,-0x1(%rax)
ret

LLVM IR (-O0):
define void @_strncpy(i8* %0, i8* %1, i64 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
store i64 %2, i64* %6
%8 = load i8*, i8** %4
%9 = load i64, i64* %6
%10 = getelementptr inbounds i8, i8* %8, i64 %9
store i8* %10, i8** %7
br label %11
11: ; preds = %23, %3
%12 = load i8*, i8** %4
%13 = load i8*, i8** %7
%14 = icmp ult i8* %12, %13
br i1 %14, label %15, label %21
15: ; preds = %11
%16 = load i8*, i8** %5
%17 = load i8, i8* %16
%18 = load i8*, i8** %4
store i8 %17, i8* %18
%19 = sext i8 %17 to i32
%20 = icmp ne i32 %19, 0
br label %21
21: ; preds = %15, %11
%22 = phi i1 [ false, %11 ], [ %20, %15 ]
br i1 %22, label %23, label %28
23: ; preds = %21
%24 = load i8*, i8** %4
%25 = getelementptr inbounds i8, i8* %24, i32 1
store i8* %25, i8** %4
%26 = load i8*, i8** %5
%27 = getelementptr inbounds i8, i8* %26, i32 1
store i8* %27, i8** %5
br label %11
28: ; preds = %21
%29 = load i8*, i8** %7
%30 = getelementptr inbounds i8, i8* %29, i64 -1
store i8 0, i8* %30
ret void
}

LLVM IR (-O2):
define void @_strncpy(i8* writeonly %0, i8* nocapture %1, i64 %2) {
%4 = getelementptr inbounds i8, i8* %0, i64 %2
%5 = icmp sgt i64 %2, 0
br i1 %5, label %6, label %15
6: ; preds = %3, %6
%7 = phi i8* [ %11, %6 ], [ %0, %3 ]
%8 = phi i8* [ %12, %6 ], [ %1, %3 ]
%9 = load i8, i8* %8
store i8 %9, i8* %7
%10 = icmp ne i8 %9, 0
%11 = getelementptr inbounds i8, i8* %7, i64 1
%12 = getelementptr inbounds i8, i8* %8, i64 1
%13 = icmp ult i8* %11, %4
%14 = select i1 %10, i1 %13, i1 false
br i1 %14, label %6, label %15
15: ; preds = %6, %3
%16 = getelementptr inbounds i8, i8* %4, i64 -1
store i8 0, i8* %16
ret void
}