#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 */
size_t cpo_strlen(char const *);
int /*<<< orphan*/ strcpy(char *, char const *);
int /*<<< orphan*/ strncpy(char *, char const *, size_t);
size_t cpo_strlcpy(char *dest, const char *src, size_t len) {
  size_t ret = cpo_strlen(src);
  if (len != 0) {
    if (ret < len)
      strcpy(dest, src);
    else {
      strncpy(dest, src, len - 1);
      dest[len - 1] = 0;
    }
  }
  return ret;
}


x86 assembly (clang, -O0):
<cpo_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 <cpo_strlen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11a6 <cpo_strlcpy+0x76>
mov    -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    117c <cpo_strlcpy+0x4c>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <strcpy@plt>
jmp    11a1 <cpo_strlcpy+0x71>
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    11a6 <cpo_strlcpy+0x76>
mov    -0x20(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cpo_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 <cpo_strlen@plt>
mov    %rax,%r15
test   %rbx,%rbx
je     1178 <cpo_strlcpy+0x48>
cmp    %rbx,%r15
jae    1163 <cpo_strlcpy+0x33>
mov    %r14,%rdi
mov    %r12,%rsi
call   1040 <strcpy@plt>
jmp    1178 <cpo_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 @cpo_strlcpy(i8* %0, i8* %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 @cpo_strlen(i8* %8)
store i64 %9, i64* %7
%10 = load i64, i64* %6
%11 = icmp ne i64 %10, 0
br i1 %11, label %12, label %31
12: ; preds = %3
%13 = load i64, i64* %7
%14 = load i64, i64* %6
%15 = icmp ult i64 %13, %14
br i1 %15, label %16, label %20
16: ; preds = %12
%17 = load i8*, i8** %4
%18 = load i8*, i8** %5
%19 = call i32 @strcpy(i8* %17, i8* %18)
br label %30
20: ; preds = %12
%21 = load i8*, i8** %4
%22 = load i8*, i8** %5
%23 = load i64, i64* %6
%24 = sub i64 %23, 1
%25 = call i32 @strncpy(i8* %21, i8* %22, i64 %24)
%26 = load i8*, i8** %4
%27 = load i64, i64* %6
%28 = sub i64 %27, 1
%29 = getelementptr inbounds i8, i8* %26, i64 %28
store i8 0, i8* %29
br label %30
30: ; preds = %20, %16
br label %31
31: ; preds = %30, %3
%32 = load i64, i64* %7
ret i64 %32
}

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