#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 *dst, const char *src, int count) {
  int c = 1;
  char *dstSave = dst;
  while (count-- > 0 && c)
    c = *dst++ = *src++;
  *dst = 0;
  return dstSave;
}


x86 assembly (clang, -O0):
<strncpy>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x1,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x14(%rbp),%ecx
mov    %ecx,%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x21(%rbp)
jle    1141 <strncpy+0x41>
cmpl   $0x0,-0x18(%rbp)
setne  %al
mov    %al,-0x21(%rbp)
mov    -0x21(%rbp),%al
test   $0x1,%al
jne    1151 <strncpy+0x51>
jmp    117e <strncpy+0x7e>
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
mov    %eax,-0x18(%rbp)
jmp    111e <strncpy+0x1e>
mov    -0x8(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x20(%rbp),%rax
pop    %rbp
ret

x86 assembly (clang, -O2):
<strncpy>:
mov    %rdi,%rax
test   %edx,%edx
jle    1129 <strncpy+0x29>
mov    %rax,%rdi
nopw   0x0(%rax,%rax,1)
movzbl (%rsi),%ecx
mov    %cl,(%rdi)
add    $0x1,%rdi
cmp    $0x2,%edx
jb     1129 <strncpy+0x29>
add    $0xffffffff,%edx
add    $0x1,%rsi
test   %cl,%cl
jne    1110 <strncpy+0x10>
movb   $0x0,(%rdi)
ret

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

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