#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 *memcpy(char *, char const *, size_t);
int /*<<< orphan*/ memset(char *, char, size_t);
size_t strlen(char const *);
char *strncpy(char *destination, const char *source, size_t n) {
  size_t size = strlen(source);
  if (size > n) {
    size = n;
  }
  if (size != n) {
    memset(destination + size, '\0', n - size);
  }
  return memcpy(destination, source, size);
}


x86 assembly (clang, -O0):
<strncpy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
jbe    1167 <strncpy+0x37>
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
je     118c <strncpy+0x5c>
mov    -0x8(%rbp),%rdi
add    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rdx
sub    -0x20(%rbp),%rdx
xor    %esi,%esi
call   1040 <memset@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1050 <memcpy@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strncpy>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r12
mov    %rsi,%r15
mov    %rdi,%r14
mov    %rsi,%rdi
call   1030 <strlen@plt>
mov    %rax,%rbx
cmp    %r12,%rax
cmova  %r12,%rbx
jae    1166 <strncpy+0x36>
lea    (%r14,%rbx,1),%rdi
sub    %rbx,%r12
xor    %esi,%esi
mov    %r12,%rdx
call   1040 <memset@plt>
mov    %r14,%rdi
mov    %r15,%rsi
mov    %rbx,%rdx
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1050 <memcpy@plt>

LLVM IR (-O0):
define i8* @strncpy(i8* %0, i8* %1, i64 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
store i8* %0, i8** %4
store i8* %1, i8** %5
store i64 %2, i64* %6
%8 = load i8*, i8** %5
%9 = call i64 @strlen(i8* %8)
store i64 %9, i64* %7
%10 = load i64, i64* %7
%11 = load i64, i64* %6
%12 = icmp ugt i64 %10, %11
br i1 %12, label %13, label %15
13: ; preds = %3
%14 = load i64, i64* %6
store i64 %14, i64* %7
br label %15
15: ; preds = %13, %3
%16 = load i64, i64* %7
%17 = load i64, i64* %6
%18 = icmp ne i64 %16, %17
br i1 %18, label %19, label %27
19: ; preds = %15
%20 = load i8*, i8** %4
%21 = load i64, i64* %7
%22 = getelementptr inbounds i8, i8* %20, i64 %21
%23 = load i64, i64* %6
%24 = load i64, i64* %7
%25 = sub i64 %23, %24
%26 = call i32 @memset(i8* %22, i8 signext 0, i64 %25)
br label %27
27: ; preds = %19, %15
%28 = load i8*, i8** %4
%29 = load i8*, i8** %5
%30 = load i64, i64* %7
%31 = call i8* @memcpy(i8* %28, i8* %29, i64 %30)
ret i8* %31
}

LLVM IR (-O2):
define i8* @strncpy(i8* returned %0, i8* nocapture %1, i64 %2) {
%4 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%5 = icmp ugt i64 %4, %2
%6 = select i1 %5, i64 %2, i64 %4
%7 = icmp ult i64 %4, %2
br i1 %7, label %8, label %12
8: ; preds = %3
%9 = getelementptr inbounds i8, i8* %0, i64 %6
%10 = sub i64 %2, %6
%11 = tail call i32 @memset(i8* %9, i8 signext 0, i64 %10)
br label %12
12: ; preds = %8, %3
tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 1 %0, i8* align 1 %1, i64 %6, i1 false)
ret i8* %0
}