#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 */
char *memchr(char const *, int, size_t);
int /*<<< orphan*/ memcpy(char *, char const *, size_t);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, size_t);
size_t strnlen(char const *, size_t);
char *strcncpy(char *restrict whither, const char *restrict whence, int c,
               size_t maxlen) {
  const char *stop = memchr(whence, c, maxlen);
  size_t n = stop == NULL ? strnlen(whence, maxlen) : (size_t)(stop - whence);
  char *r = stop == NULL ? NULL : (whither + n);
  memcpy(whither, whence, n);
  memset(whither + n, 0, maxlen - n);
  return r;
}


x86 assembly (clang, -O0):
<strcncpy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x20(%rbp),%rdx
call   1050 <memchr@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    118c <strcncpy+0x4c>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1040 <strnlen@plt>
mov    %rax,-0x40(%rbp)
jmp    119b <strcncpy+0x5b>
mov    -0x28(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rax
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11b9 <strcncpy+0x79>
xor    %eax,%eax
mov    %rax,-0x48(%rbp)
jmp    11c5 <strcncpy+0x85>
mov    -0x8(%rbp),%rax
add    -0x30(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x30(%rbp),%rdx
call   1060 <memcpy@plt>
mov    -0x8(%rbp),%rdi
add    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%rdx
sub    -0x30(%rbp),%rdx
xor    %esi,%esi
call   1030 <memset@plt>
mov    -0x38(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strcncpy>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r14
mov    %rsi,%r15
mov    %rdi,%r12
mov    %rsi,%rdi
mov    %edx,%esi
mov    %rcx,%rdx
call   1050 <memchr@plt>
mov    %rax,%r13
test   %rax,%rax
je     1171 <strcncpy+0x31>
mov    %r13,%rbx
sub    %r15,%rbx
jmp    117f <strcncpy+0x3f>
mov    %r15,%rdi
mov    %r14,%rsi
call   1040 <strnlen@plt>
mov    %rax,%rbx
lea    (%r12,%rbx,1),%rbp
test   %r13,%r13
cmovne %rbp,%r13
mov    %r12,%rdi
mov    %r15,%rsi
mov    %rbx,%rdx
call   1060 <memcpy@plt>
sub    %rbx,%r14
mov    %rbp,%rdi
xor    %esi,%esi
mov    %r14,%rdx
call   1030 <memset@plt>
mov    %r13,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i8* @strcncpy(i8* noalias %0, i8* noalias %1, i32 %2, i64 %3) {
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i64
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i8*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i64 %3, i64* %8
%12 = load i8*, i8** %6
%13 = load i32, i32* %7
%14 = load i64, i64* %8
%15 = call i8* @memchr(i8* %12, i32 %13, i64 %14)
store i8* %15, i8** %9
%16 = load i8*, i8** %9
%17 = icmp eq i8* %16, null
br i1 %17, label %18, label %22
18: ; preds = %4
%19 = load i8*, i8** %6
%20 = load i64, i64* %8
%21 = call i64 @strnlen(i8* %19, i64 %20)
br label %28
22: ; preds = %4
%23 = load i8*, i8** %9
%24 = load i8*, i8** %6
%25 = ptrtoint i8* %23 to i64
%26 = ptrtoint i8* %24 to i64
%27 = sub i64 %25, %26
br label %28
28: ; preds = %22, %18
%29 = phi i64 [ %21, %18 ], [ %27, %22 ]
store i64 %29, i64* %10
%30 = load i8*, i8** %9
%31 = icmp eq i8* %30, null
br i1 %31, label %32, label %33
32: ; preds = %28
br label %37
33: ; preds = %28
%34 = load i8*, i8** %5
%35 = load i64, i64* %10
%36 = getelementptr inbounds i8, i8* %34, i64 %35
br label %37
37: ; preds = %33, %32
%38 = phi i8* [ null, %32 ], [ %36, %33 ]
store i8* %38, i8** %11
%39 = load i8*, i8** %5
%40 = load i8*, i8** %6
%41 = load i64, i64* %10
%42 = call i32 @memcpy(i8* %39, i8* %40, i64 %41)
%43 = load i8*, i8** %5
%44 = load i64, i64* %10
%45 = getelementptr inbounds i8, i8* %43, i64 %44
%46 = load i64, i64* %8
%47 = load i64, i64* %10
%48 = sub i64 %46, %47
%49 = call i32 @memset(i8* %45, i32 0, i64 %48)
%50 = load i8*, i8** %11
ret i8* %50
}

LLVM IR (-O2):
define i8* @strcncpy(i8* noalias %0, i8* noalias %1, i32 %2, i64 %3) {
%5 = tail call i8* @memchr(i8* %1, i32 %2, i64 %3)
%6 = icmp eq i8* %5, null
br i1 %6, label %7, label %9
7: ; preds = %4
%8 = tail call i64 @strnlen(i8* %1, i64 %3)
br label %13
9: ; preds = %4
%10 = ptrtoint i8* %5 to i64
%11 = ptrtoint i8* %1 to i64
%12 = sub i64 %10, %11
br label %13
13: ; preds = %9, %7
%14 = phi i64 [ %8, %7 ], [ %12, %9 ]
%15 = getelementptr inbounds i8, i8* %0, i64 %14
%16 = select i1 %6, i8* null, i8* %15
%17 = tail call i32 @memcpy(i8* %0, i8* %1, i64 %14)
%18 = sub i64 %3, %14
%19 = tail call i32 @memset(i8* %15, i32 0, i64 %18)
ret i8* %16
}