#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 const);
size_t strlen(char const *);
size_t strlcpy(char *dst, const char *src, size_t size) {
  const size_t src_len = strlen(src);
  const size_t max_len = size - 1;
  const size_t len = src_len > max_len ? max_len : src_len;
  if (!size)
    return 0;
  memcpy(dst, src, len);
  dst[len] = 0;
  return src_len;
}


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)
mov    -0x20(%rbp),%rax
sub    $0x1,%rax
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x30(%rbp),%rax
jbe    1168 <strlcpy+0x48>
mov    -0x30(%rbp),%rax
mov    %rax,-0x40(%rbp)
jmp    1170 <strlcpy+0x50>
mov    -0x28(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rax
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1190 <strlcpy+0x70>
movq   $0x0,-0x8(%rbp)
jmp    11b5 <strlcpy+0x95>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x38(%rbp),%rdx
call   1040 <memcpy@plt>
mov    -0x10(%rbp),%rax
mov    -0x38(%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>
lea    -0x1(%r13),%rbx
cmp    %rbx,%rax
cmovbe %rax,%rbx
cmp    $0x1,%r13
jae    1150 <strlcpy+0x30>
xor    %r12d,%r12d
jmp    1166 <strlcpy+0x46>
mov    %rax,%r12
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
%10 = alloca i64
store i8* %0, i8** %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%11 = load i8*, i8** %6
%12 = call i64 @strlen(i8* %11)
store i64 %12, i64* %8
%13 = load i64, i64* %7
%14 = sub i64 %13, 1
store i64 %14, i64* %9
%15 = load i64, i64* %8
%16 = load i64, i64* %9
%17 = icmp ugt i64 %15, %16
br i1 %17, label %18, label %20
18: ; preds = %3
%19 = load i64, i64* %9
br label %22
20: ; preds = %3
%21 = load i64, i64* %8
br label %22
22: ; preds = %20, %18
%23 = phi i64 [ %19, %18 ], [ %21, %20 ]
store i64 %23, i64* %10
%24 = load i64, i64* %7
%25 = icmp ne i64 %24, 0
br i1 %25, label %27, label %26
26: ; preds = %22
store i64 0, i64* %4
br label %36
27: ; preds = %22
%28 = load i8*, i8** %5
%29 = load i8*, i8** %6
%30 = load i64, i64* %10
%31 = call i32 @memcpy(i8* %28, i8* %29, i64 %30)
%32 = load i8*, i8** %5
%33 = load i64, i64* %10
%34 = getelementptr inbounds i8, i8* %32, i64 %33
store i8 0, i8* %34
%35 = load i64, i64* %8
store i64 %35, i64* %4
br label %36
36: ; preds = %27, %26
%37 = load i64, i64* %4
ret i64 %37
}

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