#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;
  while (count > 0) {
    count--;
    if ((*to++ = *from++) == '\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    1162 <strncpy+0x62>
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    -0x8(%rbp),%rcx
mov    %rcx,%rdx
add    $0x1,%rdx
mov    %rdx,-0x8(%rbp)
mov    %al,(%rcx)
movsbl %al,%eax
cmp    $0x0,%eax
jne    115d <strncpy+0x5d>
jmp    1162 <strncpy+0x62>
jmp    1117 <strncpy+0x17>
jmp    1167 <strncpy+0x67>
cmpl   $0x0,-0x14(%rbp)
jle    1191 <strncpy+0x91>
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    1167 <strncpy+0x67>
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    115b <strncpy+0x4b>
xor    %eax,%eax
mov    %edx,%ecx
nop
movzbl (%rsi,%rax,1),%ebx
mov    %bl,(%r14,%rax,1)
add    $0x1,%rax
test   %bl,%bl
je     113a <strncpy+0x2a>
lea    -0x1(%rcx),%edi
cmp    $0x1,%ecx
mov    %edi,%ecx
ja     1120 <strncpy+0x10>
mov    %edx,%ecx
sub    %eax,%ecx
add    $0x1,%ecx
cmp    $0x2,%ecx
jl     115b <strncpy+0x4b>
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*
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%8 = load i8*, i8** %4
store i8* %8, i8** %7
br label %9
9: ; preds = %23, %3
%10 = load i32, i32* %6
%11 = icmp sgt i32 %10, 0
br i1 %11, label %12, label %24
12: ; preds = %9
%13 = load i32, i32* %6
%14 = add nsw i32 %13, -1
store i32 %14, i32* %6
%15 = load i8*, i8** %5
%16 = getelementptr inbounds i8, i8* %15, i32 1
store i8* %16, i8** %5
%17 = load i8, i8* %15
%18 = load i8*, i8** %4
%19 = getelementptr inbounds i8, i8* %18, i32 1
store i8* %19, i8** %4
store i8 %17, i8* %18
%20 = sext i8 %17 to i32
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %23
22: ; preds = %12
br label %24
23: ; preds = %12
br label %9
24: ; preds = %22, %9
br label %25
25: ; preds = %28, %24
%26 = load i32, i32* %6
%27 = icmp sgt i32 %26, 0
br i1 %27, label %28, label %33
28: ; preds = %25
%29 = load i32, i32* %6
%30 = add nsw i32 %29, -1
store i32 %30, i32* %6
%31 = load i8*, i8** %4
%32 = getelementptr inbounds i8, i8* %31, i32 1
store i8* %32, i8** %4
store i8 0, i8* %31
br label %25
33: ; preds = %25
%34 = load i8*, i8** %7
ret i8* %34
}

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 i8* [ %10, %5 ], [ %1, %3 ]
%7 = phi i32 [ %9, %5 ], [ %2, %3 ]
%8 = phi i8* [ %12, %5 ], [ %0, %3 ]
%9 = add nsw i32 %7, -1
%10 = getelementptr inbounds i8, i8* %6, i64 1
%11 = load i8, i8* %6
%12 = getelementptr inbounds i8, i8* %8, i64 1
store i8 %11, i8* %8
%13 = icmp ne i8 %11, 0
%14 = icmp ugt i32 %7, 1
%15 = select i1 %13, i1 %14, i1 false
br i1 %15, label %5, label %16
16: ; preds = %5
%17 = icmp sgt i32 %7, 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
}