#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*/ exit(int);
int /*<<< orphan*/ printf(char *, int, int);
size_t strlen(char const *);
int /*<<< orphan*/ strncpy(char *, char const *, size_t);
size_t linux_strlcpy(char *dst, const char *src, size_t dstsize) {
  size_t n = strlen(src);
  if (n >= dstsize) {
    printf("error: strlcpy: src too big: %d >= %d\n", (int)n, (int)dstsize);
    exit(1);
  }
  strncpy(dst, src, dstsize);
  return n;
}


x86 assembly (clang, -O0):
<linux_strlcpy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
jb     1191 <linux_strlcpy+0x51>
mov    -0x20(%rbp),%rax
mov    %eax,%esi
mov    -0x18(%rbp),%rax
mov    %eax,%edx
lea    0xe7e(%rip),%rdi
call   1050 <printf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <strncpy@plt>
mov    -0x20(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<linux_strlcpy>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %rdi,%r12
mov    %rsi,%rdi
call   1040 <strlen@plt>
mov    %rax,%r15
cmp    %r14,%rax
jae    117e <linux_strlcpy+0x3e>
mov    %r12,%rdi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1030 <strncpy@plt>
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0xe7b(%rip),%rdi
mov    %r15d,%esi
mov    %r14d,%edx
call   1050 <printf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@.str = [39 x i8] c"error: strlcpy: src too big: %d >= %d\0A\00"
define i64 @linux_strlcpy(i8* %0, i8* %1, i64 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
store i8* %0, i8** %4
store i8* %1, i8** %5
store i64 %2, i64* %6
%8 = load i8*, i8** %5
%9 = call i64 @strlen(i8* %8)
store i64 %9, i64* %7
%10 = load i64, i64* %7
%11 = load i64, i64* %6
%12 = icmp uge i64 %10, %11
br i1 %12, label %13, label %20
13: ; preds = %3
%14 = load i64, i64* %7
%15 = trunc i64 %14 to i32
%16 = load i64, i64* %6
%17 = trunc i64 %16 to i32
%18 = call i32 @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0), i32 %15, i32 %17)
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %3
%21 = load i8*, i8** %4
%22 = load i8*, i8** %5
%23 = load i64, i64* %6
%24 = call i32 @strncpy(i8* %21, i8* %22, i64 %23)
%25 = load i64, i64* %7
ret i64 %25
}

LLVM IR (-O2):
@.str = [39 x i8] c"error: strlcpy: src too big: %d >= %d\0A\00"
define i64 @linux_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 %11, label %6
6: ; preds = %3
%7 = trunc i64 %4 to i32
%8 = trunc i64 %2 to i32
%9 = tail call i32 @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0), i32 %7, i32 %8)
%10 = tail call i32 @exit(i32 1)
unreachable
11: ; preds = %3
%12 = tail call i32 @strncpy(i8* %0, i8* %1, i64 %2)
ret i64 %4
}