#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 ncopy(char *dst, size_t dlen, char const *src, size_t slen) {
  assert(dlen > 0);
  size_t len = (dlen - 1 < slen) ? dlen - 1 : slen;
  memcpy(dst, src, len);
  dst[len] = '\0';
  return len;
}


x86 assembly (clang, -O0):
<ncopy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
cmpq   $0x0,-0x10(%rbp)
seta   %al
and    $0x1,%al
movzbl %al,%edi
call   1030 <assert@plt>
mov    -0x10(%rbp),%rax
sub    $0x1,%rax
cmp    -0x20(%rbp),%rax
jae    116d <ncopy+0x4d>
mov    -0x10(%rbp),%rax
sub    $0x1,%rax
mov    %rax,-0x30(%rbp)
jmp    1175 <ncopy+0x55>
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1040 <memcpy@plt>
mov    -0x8(%rbp),%rax
mov    -0x28(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ncopy>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r15
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %rdi,%r12
xor    %edi,%edi
add    $0xffffffffffffffff,%rbx
setb   %dil
call   1030 <assert@plt>
cmp    %r15,%rbx
cmovae %r15,%rbx
mov    %r12,%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1040 <memcpy@plt>
movb   $0x0,(%r12,%rbx,1)
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
define i64 @ncopy(i8* %0, i64 %1, i8* %2, i64 %3) {
%5 = icmp ne i64 %1, 0
%6 = zext i1 %5 to i32
%7 = tail call i32 @assert(i32 %6)
%8 = add i64 %1, -1
%9 = icmp ult i64 %8, %3
%10 = select i1 %9, i64 %8, i64 %3
%11 = tail call i32 @memcpy(i8* %0, i8* %2, i64 %10)
%12 = getelementptr inbounds i8, i8* %0, i64 %10
store i8 0, i8* %12
ret i64 %10
}