#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 *pDestination, const char *pSource, size_t count) {
  char *pSaveDest = pDestination;
  while (count) {
    *pDestination = *pSource;
    if (*pSource == 0) {
      break;
    }
    pDestination++;
    pSource++;
    count--;
  }
  return pSaveDest;
}


x86 assembly (clang, -O0):
<strncpy>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     116d <strncpy+0x6d>
mov    -0x10(%rbp),%rax
mov    (%rax),%cl
mov    -0x8(%rbp),%rax
mov    %cl,(%rax)
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    1144 <strncpy+0x44>
jmp    116d <strncpy+0x6d>
mov    -0x8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
mov    -0x18(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x18(%rbp)
jmp    1118 <strncpy+0x18>
mov    -0x20(%rbp),%rax
pop    %rbp
ret

x86 assembly (clang, -O2):
<strncpy>:
mov    %rdi,%rax
test   %rdx,%rdx
je     1129 <strncpy+0x29>
add    $0xffffffffffffffff,%rdx
xor    %edi,%edi
xchg   %ax,%ax
movzbl (%rsi,%rdi,1),%ecx
mov    %cl,(%rax,%rdi,1)
cmpb   $0x0,(%rsi,%rdi,1)
je     1129 <strncpy+0x29>
lea    0x1(%rdi),%rcx
cmp    %rdi,%rdx
mov    %rcx,%rdi
jne    1110 <strncpy+0x10>
ret

LLVM IR (-O0):
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
br label %9
9: ; preds = %21, %3
%10 = load i64, i64* %6
%11 = icmp ne i64 %10, 0
br i1 %11, label %12, label %28
12: ; preds = %9
%13 = load i8*, i8** %5
%14 = load i8, i8* %13
%15 = load i8*, i8** %4
store i8 %14, i8* %15
%16 = load i8*, i8** %5
%17 = load i8, i8* %16
%18 = sext i8 %17 to i32
%19 = icmp eq i32 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %12
br label %28
21: ; preds = %12
%22 = load i8*, i8** %4
%23 = getelementptr inbounds i8, i8* %22, i32 1
store i8* %23, i8** %4
%24 = load i8*, i8** %5
%25 = getelementptr inbounds i8, i8* %24, i32 1
store i8* %25, i8** %5
%26 = load i64, i64* %6
%27 = add i64 %26, -1
store i64 %27, i64* %6
br label %9
28: ; preds = %20, %9
%29 = load i8*, i8** %7
ret i8* %29
}

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