#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*/ strcpy(char *, char const *);
size_t strlen(char const *);
int /*<<< orphan*/ strncpy(char *, char const *, size_t);
size_t strlcpy(char *dst, const char *src, size_t dstsize) {
  if (strlen(src) < dstsize) {
    strcpy(dst, src);
  } else {
    strncpy(dst, src, dstsize - 1);
    dst[dstsize - 1] = '\0';
  }
  return strlen(src);
}


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   1050 <strlen@plt>
cmp    -0x18(%rbp),%rax
jae    1169 <strlcpy+0x39>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <strcpy@plt>
jmp    118e <strlcpy+0x5e>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
sub    $0x1,%rdx
call   1030 <strncpy@plt>
mov    -0x8(%rbp),%rax
mov    -0x18(%rbp),%rcx
sub    $0x1,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strlcpy>:
push   %r15
push   %r14
push   %rbx
mov    %rdx,%r15
mov    %rsi,%rbx
mov    %rdi,%r14
mov    %rsi,%rdi
call   1050 <strlen@plt>
cmp    %r15,%rax
jae    1158 <strlcpy+0x28>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1040 <strcpy@plt>
jmp    116d <strlcpy+0x3d>
lea    -0x1(%r15),%rdx
mov    %r14,%rdi
mov    %rbx,%rsi
call   1030 <strncpy@plt>
movb   $0x0,-0x1(%r14,%r15,1)
mov    %rbx,%rdi
pop    %rbx
pop    %r14
pop    %r15
jmp    1050 <strlen@plt>

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

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