#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 */
char *memchr(void *, char, size_t);
int /*<<< orphan*/ memcpy(char *, char const *, int);
int strlcpy(char *dst, const char *src, size_t size) {
  char *src_end = memchr((void *)src, '\0', size);
  if (src_end == NULL)
    return 0;
  memcpy(dst, src, src_end - src);
  return (src_end - src);
}


x86 assembly (clang, -O0):
<strlcpy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rdx
xor    %esi,%esi
call   1030 <memchr@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    115e <strlcpy+0x3e>
movl   $0x0,-0x4(%rbp)
jmp    1186 <strlcpy+0x66>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x28(%rbp),%rax
mov    -0x18(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%edx
call   1040 <memcpy@plt>
mov    -0x28(%rbp),%rax
mov    -0x18(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strlcpy>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r15
mov    %rdi,%r14
xor    %ebp,%ebp
mov    %rsi,%rdi
xor    %esi,%esi
call   1030 <memchr@plt>
test   %rax,%rax
je     1153 <strlcpy+0x33>
mov    %rax,%rbx
sub    %r15d,%ebx
mov    %r14,%rdi
mov    %r15,%rsi
mov    %ebx,%edx
call   1040 <memcpy@plt>
mov    %ebx,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @strlcpy(i8* %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i8*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%9 = load i8*, i8** %6
%10 = load i64, i64* %7
%11 = call i8* @memchr(i8* %9, i8 signext 0, i64 %10)
store i8* %11, i8** %8
%12 = load i8*, i8** %8
%13 = icmp eq i8* %12, null
br i1 %13, label %14, label %15
14: ; preds = %3
store i32 0, i32* %4
br label %31
15: ; preds = %3
%16 = load i8*, i8** %5
%17 = load i8*, i8** %6
%18 = load i8*, i8** %8
%19 = load i8*, i8** %6
%20 = ptrtoint i8* %18 to i64
%21 = ptrtoint i8* %19 to i64
%22 = sub i64 %20, %21
%23 = trunc i64 %22 to i32
%24 = call i32 @memcpy(i8* %16, i8* %17, i32 %23)
%25 = load i8*, i8** %8
%26 = load i8*, i8** %6
%27 = ptrtoint i8* %25 to i64
%28 = ptrtoint i8* %26 to i64
%29 = sub i64 %27, %28
%30 = trunc i64 %29 to i32
store i32 %30, i32* %4
br label %31
31: ; preds = %15, %14
%32 = load i32, i32* %4
ret i32 %32
}

LLVM IR (-O2):
define i32 @strlcpy(i8* %0, i8* %1, i64 %2) {
%4 = tail call i8* @memchr(i8* %1, i8 signext 0, i64 %2)
%5 = icmp eq i8* %4, null
br i1 %5, label %12, label %6
6: ; preds = %3
%7 = ptrtoint i8* %4 to i64
%8 = ptrtoint i8* %1 to i64
%9 = sub i64 %7, %8
%10 = trunc i64 %9 to i32
%11 = tail call i32 @memcpy(i8* %0, i8* %1, i32 %10)
br label %12
12: ; preds = %3, %6
%13 = phi i32 [ %10, %6 ], [ 0, %3 ]
ret i32 %13
}