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


x86 assembly (clang, -O0):
<strncpy>:
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 <strncpy+0x6e>
cmpq   $0x0,-0x18(%rbp)
seta   %al
mov    %al,-0x21(%rbp)
mov    -0x21(%rbp),%al
test   $0x1,%al
jne    118e <strncpy+0x7e>
jmp    11c1 <strncpy+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 <strncpy+0x4e>
jmp    11c6 <strncpy+0xb6>
mov    -0x18(%rbp),%rax
mov    %rax,%rcx
add    $0xffffffffffffffff,%rcx
mov    %rcx,-0x18(%rbp)
cmp    $0x0,%rax
jbe    11f6 <strncpy+0xe6>
mov    -0x8(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x8(%rbp)
movb   $0x0,(%rax)
jmp    11c6 <strncpy+0xb6>
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strncpy>:
push   %r15
push   %r14
push   %rbx
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %rdi,%r15
xor    %edi,%edi
test   %rsi,%rsi
setne  %dil
lea    0xec2(%rip),%rsi
call   1040 <vassert@plt>
xor    %edi,%edi
test   %r15,%r15
setne  %dil
lea    0xeba(%rip),%rsi
call   1040 <vassert@plt>
mov    (%rbx),%al
test   %al,%al
je     1197 <strncpy+0x77>
test   %r14,%r14
je     1197 <strncpy+0x77>
lea    -0x1(%r14),%rcx
xor    %edx,%edx
nopl   0x0(%rax)
mov    %al,(%r15,%rdx,1)
movzbl 0x1(%rbx,%rdx,1),%eax
lea    0x1(%rdx),%rsi
test   %al,%al
je     1189 <strncpy+0x69>
cmp    %rdx,%rcx
mov    %rsi,%rdx
jne    1170 <strncpy+0x50>
lea    (%r15,%rsi,1),%rdi
sub    %rsi,%r14
test   %r14,%r14
jne    119f <strncpy+0x7f>
jmp    11a9 <strncpy+0x89>
mov    %r15,%rdi
test   %r14,%r14
je     11a9 <strncpy+0x89>
xor    %esi,%esi
mov    %r14,%rdx
call   1030 <memset@plt>
mov    %r15,%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 i8* @strncpy(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, 0
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
br label %36
36: ; preds = %40, %35
%37 = load i64, i64* %6
%38 = add i64 %37, -1
store i64 %38, i64* %6
%39 = icmp ugt i64 %37, 0
br i1 %39, label %40, label %43
40: ; preds = %36
%41 = load i8*, i8** %4
%42 = getelementptr inbounds i8, i8* %41, i32 1
store i8* %42, i8** %4
store i8 0, i8* %41
br label %36
43: ; preds = %36
%44 = load i8*, i8** %7
ret i8* %44
}

LLVM IR (-O2):
@.str = [13 x i8] c"from == NULL\00"
@.str.1 = [11 x i8] c"to == NULL\00"
define i8* @strncpy(i8* returned writeonly %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 ne i64 %2, 0
%13 = and i1 %11, %12
br i1 %13, label %19, label %14
14: ; preds = %19, %3
%15 = phi i64 [ %2, %3 ], [ %26, %19 ]
%16 = phi i8* [ %0, %3 ], [ %25, %19 ]
%17 = icmp eq i64 %15, 0
br i1 %17, label %31, label %18
18: ; preds = %14
br label %31
19: ; preds = %3, %19
%20 = phi i8 [ %27, %19 ], [ %10, %3 ]
%21 = phi i8* [ %25, %19 ], [ %0, %3 ]
%22 = phi i64 [ %26, %19 ], [ %2, %3 ]
%23 = phi i8* [ %24, %19 ], [ %1, %3 ]
%24 = getelementptr inbounds i8, i8* %23, i64 1
%25 = getelementptr inbounds i8, i8* %21, i64 1
store i8 %20, i8* %21
%26 = add i64 %22, -1
%27 = load i8, i8* %24
%28 = icmp ne i8 %27, 0
%29 = icmp ne i64 %26, 0
%30 = select i1 %28, i1 %29, i1 false
br i1 %30, label %19, label %14
31: ; preds = %18, %14
ret i8* %0
}