#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);
unsigned int strlen(char const *);
char *strncpy(char *dest, const char *src, size_t n) {
  unsigned int len = strlen(src);
  char *ret = memcpy(dest, src, len);
  if (n > len) {
    memcpy(dest + len, 0, n - len);
  }
  return ret;
}


x86 assembly (clang, -O0):
<strncpy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x1c(%rbp),%eax
mov    %eax,%edx
call   1040 <memcpy@plt>
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rax
mov    -0x1c(%rbp),%ecx
cmp    %rcx,%rax
jbe    1183 <strncpy+0x63>
mov    -0x8(%rbp),%rdi
mov    -0x1c(%rbp),%eax
add    %rax,%rdi
mov    -0x18(%rbp),%rdx
mov    -0x1c(%rbp),%eax
sub    %rax,%rdx
xor    %eax,%eax
mov    %eax,%esi
call   1040 <memcpy@plt>
mov    -0x28(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strncpy>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r15
mov    %rsi,%r12
mov    %rdi,%r14
mov    %rsi,%rdi
call   1030 <strlen@plt>
mov    %eax,%ebx
mov    %r14,%rdi
mov    %r12,%rsi
mov    %rbx,%rdx
call   1040 <memcpy@plt>
cmp    %r15,%rbx
jae    115f <strncpy+0x3f>
lea    (%r14,%rbx,1),%rdi
sub    %rbx,%r15
xor    %esi,%esi
mov    %r15,%rdx
call   1040 <memcpy@plt>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

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