#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);
size_t strlen(char const *);
size_t strlcpy(char *restrict dst, const char *restrict src, size_t maxlen) {
  const size_t srclen = strlen(src);
  if (srclen < maxlen) {
    memcpy(dst, src, srclen + 1);
  } else if (maxlen != 0) {
    memcpy(dst, src, maxlen - 1);
    dst[maxlen - 1] = '\0';
  }
  return srclen;
}


x86 assembly (clang, -O0):
<strlcpy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    1169 <strlcpy+0x49>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rdx
add    $0x1,%rdx
call   1040 <memcpy@plt>
jmp    119e <strlcpy+0x7e>
cmpq   $0x0,-0x18(%rbp)
je     1199 <strlcpy+0x79>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
sub    $0x1,%rdx
call   1040 <memcpy@plt>
mov    -0x8(%rbp),%rax
mov    -0x18(%rbp),%rcx
sub    $0x1,%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    119e <strlcpy+0x7e>
mov    -0x20(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strlcpy>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r12
mov    %rsi,%r15
mov    %rdi,%r14
mov    %rsi,%rdi
call   1030 <strlen@plt>
mov    %rax,%rbx
cmp    %r12,%rax
jae    1155 <strlcpy+0x35>
mov    %rbx,%rdx
add    $0x1,%rdx
mov    %r14,%rdi
mov    %r15,%rsi
call   1040 <memcpy@plt>
jmp    1170 <strlcpy+0x50>
test   %r12,%r12
je     1170 <strlcpy+0x50>
lea    -0x1(%r12),%rdx
mov    %r14,%rdi
mov    %r15,%rsi
call   1040 <memcpy@plt>
movb   $0x0,-0x1(%r14,%r12,1)
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i64 @strlcpy(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 = call i64 @strlen(i8* %8)
store i64 %9, i64* %7
%10 = load i64, i64* %7
%11 = load i64, i64* %6
%12 = icmp ult i64 %10, %11
br i1 %12, label %13, label %19
13: ; preds = %3
%14 = load i8*, i8** %4
%15 = load i8*, i8** %5
%16 = load i64, i64* %7
%17 = add i64 %16, 1
%18 = call i32 @memcpy(i8* %14, i8* %15, i64 %17)
br label %33
19: ; preds = %3
%20 = load i64, i64* %6
%21 = icmp ne i64 %20, 0
br i1 %21, label %22, label %32
22: ; preds = %19
%23 = load i8*, i8** %4
%24 = load i8*, i8** %5
%25 = load i64, i64* %6
%26 = sub i64 %25, 1
%27 = call i32 @memcpy(i8* %23, i8* %24, i64 %26)
%28 = load i8*, i8** %4
%29 = load i64, i64* %6
%30 = sub i64 %29, 1
%31 = getelementptr inbounds i8, i8* %28, i64 %30
store i8 0, i8* %31
br label %32
32: ; preds = %22, %19
br label %33
33: ; preds = %32, %13
%34 = load i64, i64* %7
ret i64 %34
}

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