#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 *);
int strlen(char const *);
int /*<<< orphan*/ strncpy(char *, char const *, size_t);
size_t strlcpy(char *dst, const char *src, size_t dstlen) {
  int srclen = strlen(src);
  if (dstlen > 0) {
    if (dstlen > srclen)
      strcpy(dst, src);
    else {
      strncpy(dst, src, dstlen - 1);
      dst[dstlen - 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   1050 <strlen@plt>
mov    %eax,-0x1c(%rbp)
cmpq   $0x0,-0x18(%rbp)
jbe    11a8 <strlcpy+0x78>
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
cmp    %rcx,%rax
jbe    117e <strlcpy+0x4e>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <strcpy@plt>
jmp    11a3 <strlcpy+0x73>
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)
jmp    11a8 <strlcpy+0x78>
movslq -0x1c(%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,%rbx
mov    %rsi,%r12
mov    %rdi,%r14
mov    %rsi,%rdi
call   1050 <strlen@plt>
movslq %eax,%r15
test   %rbx,%rbx
je     1178 <strlcpy+0x48>
cmp    %rbx,%r15
jae    1163 <strlcpy+0x33>
mov    %r14,%rdi
mov    %r12,%rsi
call   1040 <strcpy@plt>
jmp    1178 <strlcpy+0x48>
lea    -0x1(%rbx),%rdx
mov    %r14,%rdi
mov    %r12,%rsi
call   1030 <strncpy@plt>
movb   $0x0,-0x1(%r14,%rbx,1)
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

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