#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*/ vassert(int /*<<< orphan*/, char *);
size_t strnzcpy(char *to, const char *from, size_t size) {
  char *res = to;
  vassert(from != NULL, "from == NULL");
  vassert(to != NULL, "to == NULL");
  while (*from && size > 1) {
    *to++ = *from++;
    size--;
  }
  *to = '\0';
  return to - res;
}


x86 assembly (clang, -O0):
<strnzcpy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x10(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
lea    0xec0(%rip),%rsi
call   1030 <vassert@plt>
cmpq   $0x0,-0x8(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
lea    0xeb4(%rip),%rsi
call   1030 <vassert@plt>
mov    -0x10(%rbp),%rax
movsbl (%rax),%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x21(%rbp)
je     117e <strnzcpy+0x6e>
cmpq   $0x1,-0x18(%rbp)
seta   %al
mov    %al,-0x21(%rbp)
mov    -0x21(%rbp),%al
test   $0x1,%al
jne    118e <strnzcpy+0x7e>
jmp    11c1 <strnzcpy+0xb1>
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x10(%rbp)
mov    (%rax),%cl
mov    -0x8(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x8(%rbp)
mov    %cl,(%rax)
mov    -0x18(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x18(%rbp)
jmp    115e <strnzcpy+0x4e>
mov    -0x8(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x8(%rbp),%rax
mov    -0x20(%rbp),%rcx
sub    %rcx,%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strnzcpy>:
push   %r15
push   %r14
push   %rbx
mov    %rdx,%r15
mov    %rsi,%rbx
mov    %rdi,%r14
xor    %edi,%edi
test   %rsi,%rsi
setne  %dil
lea    0xed2(%rip),%rsi
call   1030 <vassert@plt>
xor    %edi,%edi
test   %r14,%r14
setne  %dil
lea    0xeca(%rip),%rsi
call   1030 <vassert@plt>
mov    (%rbx),%cl
mov    %r14,%rax
test   %cl,%cl
je     118e <strnzcpy+0x7e>
mov    %r14,%rax
cmp    $0x2,%r15
jb     118e <strnzcpy+0x7e>
add    $0xffffffffffffffff,%r15
add    $0x1,%rbx
mov    %r14,%rax
cs nopw 0x0(%rax,%rax,1)
nop
mov    %cl,(%rax)
add    $0x1,%rax
movzbl (%rbx),%ecx
test   %cl,%cl
je     118e <strnzcpy+0x7e>
lea    -0x1(%r15),%rdx
add    $0x1,%rbx
cmp    $0x1,%r15
mov    %rdx,%r15
ja     1170 <strnzcpy+0x60>
movb   $0x0,(%rax)
sub    %r14,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [13 x i8] c"from == NULL\00"
@.str.1 = [11 x i8] c"to == NULL\00"
define i64 @strnzcpy(i8* %0, i8* %1, i64 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
store i64 %2, i64* %6
%8 = load i8*, i8** %4
store i8* %8, i8** %7
%9 = load i8*, i8** %5
%10 = icmp ne i8* %9, null
%11 = zext i1 %10 to i32
%12 = call i32 @vassert(i32 %11, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%13 = load i8*, i8** %4
%14 = icmp ne i8* %13, null
%15 = zext i1 %14 to i32
%16 = call i32 @vassert(i32 %15, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
br label %17
17: ; preds = %27, %3
%18 = load i8*, i8** %5
%19 = load i8, i8* %18
%20 = sext i8 %19 to i32
%21 = icmp ne i32 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %17
%23 = load i64, i64* %6
%24 = icmp ugt i64 %23, 1
br label %25
25: ; preds = %22, %17
%26 = phi i1 [ false, %17 ], [ %24, %22 ]
br i1 %26, label %27, label %35
27: ; preds = %25
%28 = load i8*, i8** %5
%29 = getelementptr inbounds i8, i8* %28, i32 1
store i8* %29, i8** %5
%30 = load i8, i8* %28
%31 = load i8*, i8** %4
%32 = getelementptr inbounds i8, i8* %31, i32 1
store i8* %32, i8** %4
store i8 %30, i8* %31
%33 = load i64, i64* %6
%34 = add i64 %33, -1
store i64 %34, i64* %6
br label %17
35: ; preds = %25
%36 = load i8*, i8** %4
store i8 0, i8* %36
%37 = load i8*, i8** %4
%38 = load i8*, i8** %7
%39 = ptrtoint i8* %37 to i64
%40 = ptrtoint i8* %38 to i64
%41 = sub i64 %39, %40
ret i64 %41
}

LLVM IR (-O2):
@.str = [13 x i8] c"from == NULL\00"
@.str.1 = [11 x i8] c"to == NULL\00"
define i64 @strnzcpy(i8* %0, i8* %1, i64 %2) {
%4 = icmp ne i8* %1, null
%5 = zext i1 %4 to i32
%6 = tail call i32 @vassert(i32 %5, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%7 = icmp ne i8* %0, null
%8 = zext i1 %7 to i32
%9 = tail call i32 @vassert(i32 %8, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%10 = load i8, i8* %1
%11 = icmp ne i8 %10, 0
%12 = icmp ugt i64 %2, 1
%13 = and i1 %11, %12
br i1 %13, label %14, label %26
14: ; preds = %3, %14
%15 = phi i8 [ %22, %14 ], [ %10, %3 ]
%16 = phi i8* [ %20, %14 ], [ %0, %3 ]
%17 = phi i64 [ %21, %14 ], [ %2, %3 ]
%18 = phi i8* [ %19, %14 ], [ %1, %3 ]
%19 = getelementptr inbounds i8, i8* %18, i64 1
%20 = getelementptr inbounds i8, i8* %16, i64 1
store i8 %15, i8* %16
%21 = add i64 %17, -1
%22 = load i8, i8* %19
%23 = icmp ne i8 %22, 0
%24 = icmp ugt i64 %21, 1
%25 = select i1 %23, i1 %24, i1 false
br i1 %25, label %14, label %26
26: ; preds = %14, %3
%27 = phi i8* [ %0, %3 ], [ %20, %14 ]
store i8 0, i8* %27
%28 = ptrtoint i8* %27 to i64
%29 = ptrtoint i8* %0 to i64
%30 = sub i64 %28, %29
ret i64 %30
}