#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 *dst, const char *src, size_t dstsize) {
  size_t srclen = strlen(src);
  size_t tocopy;
  if (dstsize == 0)
    return srclen;
  tocopy = (srclen < dstsize ? srclen : dstsize - 1);
  if (tocopy > 0)
    memcpy(dst, src, tocopy);
  dst[tocopy] = 0;
  return srclen;
}


x86 assembly (clang, -O0):
<strlcpy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1159 <strlcpy+0x39>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11b8 <strlcpy+0x98>
mov    -0x28(%rbp),%rax
cmp    -0x20(%rbp),%rax
jae    1174 <strlcpy+0x54>
mov    -0x28(%rbp),%rax
mov    %rax,-0x38(%rbp)
jmp    1180 <strlcpy+0x60>
mov    -0x20(%rbp),%rax
sub    $0x1,%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jbe    11a4 <strlcpy+0x84>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x30(%rbp),%rdx
call   1040 <memcpy@plt>
mov    -0x10(%rbp),%rax
mov    -0x30(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strlcpy>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdx,%r13
mov    %rsi,%r15
mov    %rdi,%r14
mov    %rsi,%rdi
call   1030 <strlen@plt>
mov    %rax,%r12
test   %r13,%r13
je     1165 <strlcpy+0x45>
lea    -0x1(%r13),%rbx
cmp    %r13,%r12
cmovb  %r12,%rbx
test   %rbx,%rbx
je     1160 <strlcpy+0x40>
mov    %r14,%rdi
mov    %r15,%rsi
mov    %rbx,%rdx
call   1040 <memcpy@plt>
movb   $0x0,(%r14,%rbx,1)
mov    %r12,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

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

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 %15, label %6
6: ; preds = %3
%7 = icmp ult i64 %4, %2
%8 = add i64 %2, -1
%9 = select i1 %7, i64 %4, i64 %8
%10 = icmp eq i64 %9, 0
br i1 %10, label %13, label %11
11: ; preds = %6
%12 = tail call i32 @memcpy(i8* %0, i8* %1, i64 %9)
br label %13
13: ; preds = %11, %6
%14 = getelementptr inbounds i8, i8* %0, i64 %9
store i8 0, i8* %14
br label %15
15: ; preds = %3, %13
ret i64 %4
}