#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*/ assert(int);
int /*<<< orphan*/ memcpy(char *, char const *, size_t);
size_t strnlen(char const *, size_t);
size_t strlncpy(char *dst, size_t len, const char *src, size_t n) {
  size_t slen;
  size_t ncpy;
  slen = strnlen(src, n);
  if (len > 0) {
    ncpy = slen < len ? slen : (len - 1);
    memcpy(dst, src, ncpy);
    dst[ncpy] = '\0';
  }
  assert(len > slen);
  return slen;
}


x86 assembly (clang, -O0):
<strlncpy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1030 <strnlen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x10(%rbp)
jbe    11b0 <strlncpy+0x80>
mov    -0x28(%rbp),%rax
cmp    -0x10(%rbp),%rax
jae    117f <strlncpy+0x4f>
mov    -0x28(%rbp),%rax
mov    %rax,-0x38(%rbp)
jmp    118b <strlncpy+0x5b>
mov    -0x10(%rbp),%rax
sub    $0x1,%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x30(%rbp),%rdx
call   1050 <memcpy@plt>
mov    -0x8(%rbp),%rax
mov    -0x30(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
cmp    -0x28(%rbp),%rax
seta   %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    -0x28(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strlncpy>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdx,%r12
mov    %rsi,%r13
mov    %rdi,%r15
mov    %rdx,%rdi
mov    %rcx,%rsi
call   1030 <strnlen@plt>
mov    %rax,%r14
test   %r13,%r13
je     1173 <strlncpy+0x43>
lea    -0x1(%r13),%rbx
cmp    %r13,%r14
cmovb  %r14,%rbx
mov    %r15,%rdi
mov    %r12,%rsi
mov    %rbx,%rdx
call   1050 <memcpy@plt>
movb   $0x0,(%r15,%rbx,1)
xor    %edi,%edi
cmp    %r13,%r14
setb   %dil
call   1040 <assert@plt>
mov    %r14,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i64 @strlncpy(i8* %0, i64 %1, i8* %2, i64 %3) {
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i64
%10 = alloca i64
store i8* %0, i8** %5
store i64 %1, i64* %6
store i8* %2, i8** %7
store i64 %3, i64* %8
%11 = load i8*, i8** %7
%12 = load i64, i64* %8
%13 = call i64 @strnlen(i8* %11, i64 %12)
store i64 %13, i64* %9
%14 = load i64, i64* %6
%15 = icmp ugt i64 %14, 0
br i1 %15, label %16, label %34
16: ; preds = %4
%17 = load i64, i64* %9
%18 = load i64, i64* %6
%19 = icmp ult i64 %17, %18
br i1 %19, label %20, label %22
20: ; preds = %16
%21 = load i64, i64* %9
br label %25
22: ; preds = %16
%23 = load i64, i64* %6
%24 = sub i64 %23, 1
br label %25
25: ; preds = %22, %20
%26 = phi i64 [ %21, %20 ], [ %24, %22 ]
store i64 %26, i64* %10
%27 = load i8*, i8** %5
%28 = load i8*, i8** %7
%29 = load i64, i64* %10
%30 = call i32 @memcpy(i8* %27, i8* %28, i64 %29)
%31 = load i8*, i8** %5
%32 = load i64, i64* %10
%33 = getelementptr inbounds i8, i8* %31, i64 %32
store i8 0, i8* %33
br label %34
34: ; preds = %25, %4
%35 = load i64, i64* %6
%36 = load i64, i64* %9
%37 = icmp ugt i64 %35, %36
%38 = zext i1 %37 to i32
%39 = call i32 @assert(i32 %38)
%40 = load i64, i64* %9
ret i64 %40
}

LLVM IR (-O2):
define i64 @strlncpy(i8* %0, i64 %1, i8* %2, i64 %3) {
%5 = tail call i64 @strnlen(i8* %2, i64 %3)
%6 = icmp eq i64 %1, 0
br i1 %6, label %13, label %7
7: ; preds = %4
%8 = icmp ult i64 %5, %1
%9 = add i64 %1, -1
%10 = select i1 %8, i64 %5, i64 %9
%11 = tail call i32 @memcpy(i8* %0, i8* %2, i64 %10)
%12 = getelementptr inbounds i8, i8* %0, i64 %10
store i8 0, i8* %12
br label %13
13: ; preds = %7, %4
%14 = icmp ult i64 %5, %1
%15 = zext i1 %14 to i32
%16 = tail call i32 @assert(i32 %15)
ret i64 %5
}