#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 *dest, const char *src, unsigned int n) {
  char *s = dest;
  while (n > 0 && *src != '\0') {
    *s++ = *src++;
    --n;
  }
  while (n > 0) {
    *s++ = '\0';
    --n;
  }
  return dest;
}


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)
xor    %eax,%eax
cmpl   $0x0,-0x14(%rbp)
mov    %al,-0x21(%rbp)
jbe    1136 <strncpy+0x36>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
setne  %al
mov    %al,-0x21(%rbp)
mov    -0x21(%rbp),%al
test   $0x1,%al
jne    1146 <strncpy+0x46>
jmp    1176 <strncpy+0x76>
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x10(%rbp)
mov    (%rax),%cl
mov    -0x20(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x20(%rbp)
mov    %cl,(%rax)
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
jmp    1117 <strncpy+0x17>
jmp    117b <strncpy+0x7b>
cmpl   $0x0,-0x14(%rbp)
jbe    11a5 <strncpy+0xa5>
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x20(%rbp)
movb   $0x0,(%rax)
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
jmp    117b <strncpy+0x7b>
mov    -0x8(%rbp),%rax
pop    %rbp
ret

x86 assembly (clang, -O2):
<strncpy>:
push   %rbx
mov    %rdi,%rbx
test   %edx,%edx
je     1143 <strncpy+0x33>
mov    %edx,%edx
xor    %eax,%eax
nopl   0x0(%rax)
movzbl (%rsi,%rax,1),%ecx
test   %cl,%cl
je     1135 <strncpy+0x25>
mov    %cl,(%rbx,%rax,1)
add    $0x1,%rax
cmp    %eax,%edx
jne    1120 <strncpy+0x10>
jmp    1143 <strncpy+0x33>
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* %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 = %19, %3
%10 = load i32, i32* %6
%11 = icmp ugt i32 %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 %27
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** %7
%24 = getelementptr inbounds i8, i8* %23, i32 1
store i8* %24, i8** %7
store i8 %22, i8* %23
%25 = load i32, i32* %6
%26 = add i32 %25, -1
store i32 %26, i32* %6
br label %9
27: ; preds = %17
br label %28
28: ; preds = %31, %27
%29 = load i32, i32* %6
%30 = icmp ugt i32 %29, 0
br i1 %30, label %31, label %36
31: ; preds = %28
%32 = load i8*, i8** %7
%33 = getelementptr inbounds i8, i8* %32, i32 1
store i8* %33, i8** %7
store i8 0, i8* %32
%34 = load i32, i32* %6
%35 = add i32 %34, -1
store i32 %35, i32* %6
br label %28
36: ; preds = %28
%37 = load i8*, i8** %4
ret i8* %37
}

LLVM IR (-O2):
define i8* @strncpy(i8* returned writeonly %0, i8* nocapture %1, i32 %2) {
%4 = icmp eq i32 %2, 0
br i1 %4, label %18, label %5
5: ; preds = %3, %11
%6 = phi i8* [ %13, %11 ], [ %0, %3 ]
%7 = phi i32 [ %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 i32 %7, -1
%15 = icmp eq i32 %14, 0
br i1 %15, label %18, label %5
16: ; preds = %5
%17 = zext i32 %7 to i64
br label %18
18: ; preds = %11, %3, %16
ret i8* %0
}