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


x86 assembly (clang, -O0):
<strlcpy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%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)
cmpq   $0x0,-0x18(%rbp)
je     1194 <strlcpy+0x74>
mov    -0x18(%rbp),%rax
sub    $0x1,%rax
cmp    -0x20(%rbp),%rax
jae    116f <strlcpy+0x4f>
mov    -0x18(%rbp),%rax
sub    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    1177 <strlcpy+0x57>
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1040 <memcpy@plt>
mov    -0x8(%rbp),%rax
mov    -0x28(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
add    $0x30,%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   1030 <strlen@plt>
mov    %rax,%r15
test   %rbx,%rbx
je     115f <strlcpy+0x3f>
add    $0xffffffffffffffff,%rbx
cmp    %r15,%rbx
cmovae %r15,%rbx
mov    %r14,%rdi
mov    %r12,%rsi
mov    %rbx,%rdx
call   1040 <memcpy@plt>
movb   $0x0,(%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 i64
%8 = alloca i64
store i8* %0, i8** %4
store i8* %1, i8** %5
store i64 %2, i64* %6
%9 = load i8*, i8** %5
%10 = call i64 @strlen(i8* %9)
store i64 %10, i64* %7
%11 = load i64, i64* %6
%12 = icmp ne i64 %11, 0
br i1 %12, label %13, label %31
13: ; preds = %3
%14 = load i64, i64* %6
%15 = sub i64 %14, 1
%16 = load i64, i64* %7
%17 = icmp ult i64 %15, %16
br i1 %17, label %18, label %21
18: ; preds = %13
%19 = load i64, i64* %6
%20 = sub i64 %19, 1
store i64 %20, i64* %8
br label %23
21: ; preds = %13
%22 = load i64, i64* %7
store i64 %22, i64* %8
br label %23
23: ; preds = %21, %18
%24 = load i8*, i8** %4
%25 = load i8*, i8** %5
%26 = load i64, i64* %8
%27 = call i32 @memcpy(i8* %24, i8* %25, i64 %26)
%28 = load i8*, i8** %4
%29 = load i64, i64* %8
%30 = getelementptr inbounds i8, i8* %28, i64 %29
store i8 0, i8* %30
br label %31
31: ; preds = %23, %3
%32 = load i64, i64* %7
ret i64 %32
}

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