#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 *restrict dst, const char *restrict src, size_t n) {
  char *ret = dst;
  for (; n > 0 && *src; --n)
    *dst++ = *src++;
  while (n-- > 0)
    *dst++ = '\0';
  return ret;
}


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)
xor    %eax,%eax
cmpq   $0x0,-0x18(%rbp)
mov    %al,-0x21(%rbp)
jbe    1138 <strncpy+0x38>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
setne  %al
mov    %al,-0x21(%rbp)
mov    -0x21(%rbp),%al
test   $0x1,%al
jne    1148 <strncpy+0x48>
jmp    117b <strncpy+0x7b>
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    1118 <strncpy+0x18>
jmp    1180 <strncpy+0x80>
mov    -0x18(%rbp),%rax
mov    %rax,%rcx
add    $0xffffffffffffffff,%rcx
mov    %rcx,-0x18(%rbp)
cmp    $0x0,%rax
jbe    11b0 <strncpy+0xb0>
mov    -0x8(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x8(%rbp)
movb   $0x0,(%rax)
jmp    1180 <strncpy+0x80>
mov    -0x20(%rbp),%rax
pop    %rbp
ret

x86 assembly (clang, -O2):
<strncpy>:
push   %rbx
mov    %rdi,%rbx
test   %rdx,%rdx
je     1144 <strncpy+0x34>
xor    %eax,%eax
nopl   0x0(%rax,%rax,1)
movzbl (%rsi,%rax,1),%ecx
test   %cl,%cl
je     1136 <strncpy+0x26>
mov    %cl,(%rbx,%rax,1)
add    $0x1,%rax
cmp    %rax,%rdx
jne    1120 <strncpy+0x10>
jmp    1144 <strncpy+0x34>
lea    (%rbx,%rax,1),%rdi
sub    %rax,%rdx
xor    %esi,%esi
call   1030 <memset@plt>
mov    %rbx,%rax
pop    %rbx
ret

LLVM IR (-O0):
define i8* @strncpy(i8* noalias %0, i8* noalias %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 = %25, %3
%10 = load i64, i64* %6
%11 = icmp ugt i64 %10, 0
br i1 %11, label %12, label %17
12: ; preds = %9
%13 = load i8*, i8** %5
%14 = load i8, i8* %13
%15 = sext i8 %14 to i32
%16 = icmp ne i32 %15, 0
br label %17
17: ; preds = %12, %9
%18 = phi i1 [ false, %9 ], [ %16, %12 ]
br i1 %18, label %19, label %28
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
br label %25
25: ; preds = %19
%26 = load i64, i64* %6
%27 = add i64 %26, -1
store i64 %27, i64* %6
br label %9
28: ; preds = %17
br label %29
29: ; preds = %33, %28
%30 = load i64, i64* %6
%31 = add i64 %30, -1
store i64 %31, i64* %6
%32 = icmp ugt i64 %30, 0
br i1 %32, label %33, label %36
33: ; preds = %29
%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 %29
36: ; preds = %29
%37 = load i8*, i8** %7
ret i8* %37
}

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