#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_c(char *, char const *, size_t);
size_t strlen(char const *);
void strlcpy(char *dest, const char *src, size_t size) {
  size_t len = strlen(src);
  if (len < size) {
    memcpy_c(dest, src, len + 1);
  } else if (size != 0) {
    size_t size1 = size - 1;
    memcpy_c(dest, src, size1);
    dest[size1] = '\0';
  }
}


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)
mov    -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    1169 <strlcpy+0x49>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rdx
add    $0x1,%rdx
call   1040 <memcpy_c@plt>
jmp    11a2 <strlcpy+0x82>
cmpq   $0x0,-0x18(%rbp)
je     119d <strlcpy+0x7d>
mov    -0x18(%rbp),%rax
sub    $0x1,%rax
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1040 <memcpy_c@plt>
mov    -0x8(%rbp),%rax
mov    -0x28(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    11a2 <strlcpy+0x82>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strlcpy>:
push   %r15
push   %r14
push   %rbx
mov    %rdx,%rbx
mov    %rsi,%r15
mov    %rdi,%r14
mov    %rsi,%rdi
call   1030 <strlen@plt>
cmp    %rbx,%rax
jae    1152 <strlcpy+0x32>
add    $0x1,%rax
mov    %r14,%rdi
mov    %r15,%rsi
mov    %rax,%rdx
pop    %rbx
pop    %r14
pop    %r15
jmp    1040 <memcpy_c@plt>
test   %rbx,%rbx
je     116c <strlcpy+0x4c>
lea    -0x1(%rbx),%rdx
mov    %r14,%rdi
mov    %r15,%rsi
call   1040 <memcpy_c@plt>
movb   $0x0,-0x1(%r14,%rbx,1)
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define void @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* %7
%12 = load i64, i64* %6
%13 = icmp ult i64 %11, %12
br i1 %13, label %14, label %20
14: ; preds = %3
%15 = load i8*, i8** %4
%16 = load i8*, i8** %5
%17 = load i64, i64* %7
%18 = add i64 %17, 1
%19 = call i32 @memcpy_c(i8* %15, i8* %16, i64 %18)
br label %34
20: ; preds = %3
%21 = load i64, i64* %6
%22 = icmp ne i64 %21, 0
br i1 %22, label %23, label %33
23: ; preds = %20
%24 = load i64, i64* %6
%25 = sub i64 %24, 1
store i64 %25, i64* %8
%26 = load i8*, i8** %4
%27 = load i8*, i8** %5
%28 = load i64, i64* %8
%29 = call i32 @memcpy_c(i8* %26, i8* %27, i64 %28)
%30 = load i8*, i8** %4
%31 = load i64, i64* %8
%32 = getelementptr inbounds i8, i8* %30, i64 %31
store i8 0, i8* %32
br label %33
33: ; preds = %23, %20
br label %34
34: ; preds = %33, %14
ret void
}

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