#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, size_t n) {
  char *p1;
  const char *p2;
  p1 = dest;
  p2 = src;
  while (n > 0) {
    if (*p2 == '\0')
      break;
    *p1++ = *p2++;
    n--;
  }
  while (n > 0) {
    *p1++ = '\0';
    n--;
  }
  return (dest);
}


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)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x18(%rbp)
jbe    1173 <strncpy+0x73>
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    1140 <strncpy+0x40>
jmp    1173 <strncpy+0x73>
mov    -0x28(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x28(%rbp)
mov    (%rax),%cl
mov    -0x20(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x20(%rbp)
mov    %cl,(%rax)
mov    -0x18(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x18(%rbp)
jmp    1120 <strncpy+0x20>
jmp    1178 <strncpy+0x78>
cmpq   $0x0,-0x18(%rbp)
jbe    11a6 <strncpy+0xa6>
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x20(%rbp)
movb   $0x0,(%rax)
mov    -0x18(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x18(%rbp)
jmp    1178 <strncpy+0x78>
mov    -0x8(%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* %0, i8* %1, i64 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
store i64 %2, i64* %6
%9 = load i8*, i8** %4
store i8* %9, i8** %7
%10 = load i8*, i8** %5
store i8* %10, i8** %8
br label %11
11: ; preds = %20, %3
%12 = load i64, i64* %6
%13 = icmp ugt i64 %12, 0
br i1 %13, label %14, label %28
14: ; preds = %11
%15 = load i8*, i8** %8
%16 = load i8, i8* %15
%17 = sext i8 %16 to i32
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %20
19: ; preds = %14
br label %28
20: ; preds = %14
%21 = load i8*, i8** %8
%22 = getelementptr inbounds i8, i8* %21, i32 1
store i8* %22, i8** %8
%23 = load i8, i8* %21
%24 = load i8*, i8** %7
%25 = getelementptr inbounds i8, i8* %24, i32 1
store i8* %25, i8** %7
store i8 %23, i8* %24
%26 = load i64, i64* %6
%27 = add i64 %26, -1
store i64 %27, i64* %6
br label %11
28: ; preds = %19, %11
br label %29
29: ; preds = %32, %28
%30 = load i64, i64* %6
%31 = icmp ugt i64 %30, 0
br i1 %31, label %32, label %37
32: ; preds = %29
%33 = load i8*, i8** %7
%34 = getelementptr inbounds i8, i8* %33, i32 1
store i8* %34, i8** %7
store i8 0, i8* %33
%35 = load i64, i64* %6
%36 = add i64 %35, -1
store i64 %36, i64* %6
br label %29
37: ; preds = %29
%38 = load i8*, i8** %4
ret i8* %38
}

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