#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 s1, const char *restrict s2, size_t n) {
  char *rc = s1;
  while ((n > 0) && (*s1++ = *s2++)) {
    --n;
  }
  while (n-- > 1) {
    *s1++ = '\0';
  }
  return rc;
}


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    1156 <strncpy+0x56>
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
setne  %al
mov    %al,-0x21(%rbp)
mov    -0x21(%rbp),%al
test   $0x1,%al
jne    1166 <strncpy+0x66>
jmp    1177 <strncpy+0x77>
mov    -0x18(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x18(%rbp)
jmp    1118 <strncpy+0x18>
jmp    117c <strncpy+0x7c>
mov    -0x18(%rbp),%rax
mov    %rax,%rcx
add    $0xffffffffffffffff,%rcx
mov    %rcx,-0x18(%rbp)
cmp    $0x1,%rax
jbe    11ac <strncpy+0xac>
mov    -0x8(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x8(%rbp)
movb   $0x0,(%rax)
jmp    117c <strncpy+0x7c>
mov    -0x20(%rbp),%rax
pop    %rbp
ret

x86 assembly (clang, -O2):
<strncpy>:
push   %rbx
mov    %rdi,%rbx
test   %rdx,%rdx
je     115a <strncpy+0x4a>
xor    %eax,%eax
nopl   0x0(%rax,%rax,1)
movzbl (%rsi,%rax,1),%ecx
mov    %cl,(%rbx,%rax,1)
test   %cl,%cl
je     1136 <strncpy+0x26>
add    $0x1,%rax
cmp    %rax,%rdx
jne    1120 <strncpy+0x10>
jmp    115a <strncpy+0x4a>
mov    %rdx,%rcx
sub    %rax,%rcx
cmp    $0x2,%rcx
jb     115a <strncpy+0x4a>
lea    (%rbx,%rax,1),%rdi
add    $0x1,%rdi
not    %rax
add    %rdx,%rax
xor    %esi,%esi
mov    %rax,%rdx
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 = %22, %3
%10 = load i64, i64* %6
%11 = icmp ugt i64 %10, 0
br i1 %11, label %12, label %20
12: ; preds = %9
%13 = load i8*, i8** %5
%14 = getelementptr inbounds i8, i8* %13, i32 1
store i8* %14, i8** %5
%15 = load i8, i8* %13
%16 = load i8*, i8** %4
%17 = getelementptr inbounds i8, i8* %16, i32 1
store i8* %17, i8** %4
store i8 %15, i8* %16
%18 = sext i8 %15 to i32
%19 = icmp ne i32 %18, 0
br label %20
20: ; preds = %12, %9
%21 = phi i1 [ false, %9 ], [ %19, %12 ]
br i1 %21, label %22, label %25
22: ; preds = %20
%23 = load i64, i64* %6
%24 = add i64 %23, -1
store i64 %24, i64* %6
br label %9
25: ; preds = %20
br label %26
26: ; preds = %30, %25
%27 = load i64, i64* %6
%28 = add i64 %27, -1
store i64 %28, i64* %6
%29 = icmp ugt i64 %27, 1
br i1 %29, label %30, label %33
30: ; preds = %26
%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 %26
33: ; preds = %26
%34 = load i8*, i8** %7
ret i8* %34
}

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