#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*/ memcpy(char *, char const *, size_t const);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, size_t);
size_t strnlen(char const *, size_t);
char *strncpy(char *__restrict dst, const char *__restrict src, size_t maxlen) {
  const size_t srclen = strnlen(src, maxlen);
  if (srclen < maxlen) {
    memcpy(dst, src, srclen);
    memset(dst + srclen, 0, maxlen - srclen);
  } else {
    memcpy(dst, src, maxlen);
  }
  return dst;
}


x86 assembly (clang, -O0):
<strncpy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <strnlen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    1190 <strncpy+0x60>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1050 <memcpy@plt>
mov    -0x8(%rbp),%rdi
add    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rdx
sub    -0x20(%rbp),%rdx
xor    %esi,%esi
call   1030 <memset@plt>
jmp    11a1 <strncpy+0x71>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1050 <memcpy@plt>
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strncpy>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r12
mov    %rsi,%r15
mov    %rdi,%r14
mov    %rsi,%rdi
mov    %rdx,%rsi
call   1040 <strnlen@plt>
cmp    %r12,%rax
jae    1175 <strncpy+0x45>
mov    %rax,%rbx
mov    %r14,%rdi
mov    %r15,%rsi
mov    %rax,%rdx
call   1050 <memcpy@plt>
lea    (%r14,%rbx,1),%rdi
sub    %rbx,%r12
xor    %esi,%esi
mov    %r12,%rdx
call   1030 <memset@plt>
jmp    1183 <strncpy+0x53>
mov    %r14,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
call   1050 <memcpy@plt>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @strncpy(i8* noalias %0, i8* noalias %1, i64 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
store i8* %0, i8** %4
store i8* %1, i8** %5
store i64 %2, i64* %6
%8 = load i8*, i8** %5
%9 = load i64, i64* %6
%10 = call i64 @strnlen(i8* %8, i64 %9)
store i64 %10, i64* %7
%11 = load i64, i64* %7
%12 = load i64, i64* %6
%13 = icmp ult i64 %11, %12
br i1 %13, label %14, label %26
14: ; preds = %3
%15 = load i8*, i8** %4
%16 = load i8*, i8** %5
%17 = load i64, i64* %7
%18 = call i32 @memcpy(i8* %15, i8* %16, i64 %17)
%19 = load i8*, i8** %4
%20 = load i64, i64* %7
%21 = getelementptr inbounds i8, i8* %19, i64 %20
%22 = load i64, i64* %6
%23 = load i64, i64* %7
%24 = sub i64 %22, %23
%25 = call i32 @memset(i8* %21, i32 0, i64 %24)
br label %31
26: ; preds = %3
%27 = load i8*, i8** %4
%28 = load i8*, i8** %5
%29 = load i64, i64* %6
%30 = call i32 @memcpy(i8* %27, i8* %28, i64 %29)
br label %31
31: ; preds = %26, %14
%32 = load i8*, i8** %4
ret i8* %32
}

LLVM IR (-O2):
define i8* @strncpy(i8* noalias returned %0, i8* noalias %1, i64 %2) {
%4 = tail call i64 @strnlen(i8* %1, i64 %2)
%5 = icmp ult i64 %4, %2
br i1 %5, label %6, label %11
6: ; preds = %3
%7 = tail call i32 @memcpy(i8* %0, i8* %1, i64 %4)
%8 = getelementptr inbounds i8, i8* %0, i64 %4
%9 = sub i64 %2, %4
%10 = tail call i32 @memset(i8* %8, i32 0, i64 %9)
br label %13
11: ; preds = %3
%12 = tail call i32 @memcpy(i8* %0, i8* %1, i64 %2)
br label %13
13: ; preds = %11, %6
ret i8* %0
}