#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 *strncpy(char *to, const char *from, int count) {
  register char *ret = to;
  register char data;
  while (count > 0) {
    count--;
    data = *from++;
    *to++ = data;
    if (data == '\0')
      break;
  }
  while (count > 0) {
    count--;
    *to++ = '\0';
  }
  return ret;
}


x86 assembly (clang, -O0):
<strncpy>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
cmpl   $0x0,-0x14(%rbp)
jle    1169 <strncpy+0x69>
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x10(%rbp)
mov    (%rax),%al
mov    %al,-0x21(%rbp)
mov    -0x21(%rbp),%cl
mov    -0x8(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x8(%rbp)
mov    %cl,(%rax)
movsbl -0x21(%rbp),%eax
cmp    $0x0,%eax
jne    1164 <strncpy+0x64>
jmp    1169 <strncpy+0x69>
jmp    1117 <strncpy+0x17>
jmp    116e <strncpy+0x6e>
cmpl   $0x0,-0x14(%rbp)
jle    1198 <strncpy+0x98>
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x8(%rbp)
movb   $0x0,(%rax)
jmp    116e <strncpy+0x6e>
mov    -0x20(%rbp),%rax
pop    %rbp
ret

x86 assembly (clang, -O2):
<strncpy>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
test   %edx,%edx
jle    1157 <strncpy+0x47>
xor    %eax,%eax
mov    %edx,%edi
nop
lea    -0x1(%rdi),%ecx
movzbl (%rsi,%rax,1),%ebx
mov    %bl,(%r14,%rax,1)
add    $0x1,%rax
test   %bl,%bl
je     113a <strncpy+0x2a>
cmp    $0x1,%edi
mov    %ecx,%edi
ja     1120 <strncpy+0x10>
add    $0x1,%ecx
cmp    $0x2,%ecx
jl     1157 <strncpy+0x47>
lea    (%r14,%rax,1),%rdi
add    $0xffffffff,%edx
sub    %rax,%rdx
add    $0x1,%rdx
xor    %esi,%esi
call   1030 <memset@plt>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define i8* @strncpy(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%9 = load i8*, i8** %4
store i8* %9, i8** %7
br label %10
10: ; preds = %26, %3
%11 = load i32, i32* %6
%12 = icmp sgt i32 %11, 0
br i1 %12, label %13, label %27
13: ; preds = %10
%14 = load i32, i32* %6
%15 = add nsw i32 %14, -1
store i32 %15, i32* %6
%16 = load i8*, i8** %5
%17 = getelementptr inbounds i8, i8* %16, i32 1
store i8* %17, i8** %5
%18 = load i8, i8* %16
store i8 %18, i8* %8
%19 = load i8, i8* %8
%20 = load i8*, i8** %4
%21 = getelementptr inbounds i8, i8* %20, i32 1
store i8* %21, i8** %4
store i8 %19, i8* %20
%22 = load i8, i8* %8
%23 = sext i8 %22 to i32
%24 = icmp eq i32 %23, 0
br i1 %24, label %25, label %26
25: ; preds = %13
br label %27
26: ; preds = %13
br label %10
27: ; preds = %25, %10
br label %28
28: ; preds = %31, %27
%29 = load i32, i32* %6
%30 = icmp sgt i32 %29, 0
br i1 %30, label %31, label %36
31: ; preds = %28
%32 = load i32, i32* %6
%33 = add nsw i32 %32, -1
store i32 %33, i32* %6
%34 = load i8*, i8** %4
%35 = getelementptr inbounds i8, i8* %34, i32 1
store i8* %35, i8** %4
store i8 0, i8* %34
br label %28
36: ; preds = %28
%37 = load i8*, i8** %7
ret i8* %37
}

LLVM IR (-O2):
define i8* @strncpy(i8* returned writeonly %0, i8* nocapture %1, i32 %2) {
%4 = icmp sgt i32 %2, 0
br i1 %4, label %5, label %20
5: ; preds = %3, %5
%6 = phi i32 [ %9, %5 ], [ %2, %3 ]
%7 = phi i8* [ %10, %5 ], [ %1, %3 ]
%8 = phi i8* [ %12, %5 ], [ %0, %3 ]
%9 = add nsw i32 %6, -1
%10 = getelementptr inbounds i8, i8* %7, i64 1
%11 = load i8, i8* %7
%12 = getelementptr inbounds i8, i8* %8, i64 1
store i8 %11, i8* %8
%13 = icmp ne i8 %11, 0
%14 = icmp ugt i32 %6, 1
%15 = select i1 %13, i1 %14, i1 false
br i1 %15, label %5, label %16
16: ; preds = %5
%17 = icmp sgt i32 %6, 1
br i1 %17, label %18, label %20
18: ; preds = %16
%19 = zext i32 %9 to i64
br label %20
20: ; preds = %3, %18, %16
ret i8* %0
}