#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 */
int /*<<< orphan*/ memcpy(char *, char const *, size_t);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, size_t);
int strlen(char const *);
char *strncpy(char *d, const char *s, size_t n) {
  size_t copy = strlen(s) + 1;
  size_t fill = 0;
  if (copy > n) {
    copy = n;
  } else {
    fill = n - copy;
  }
  memcpy(d, s, copy);
  memset(d + copy, 0, fill);
  return d;
}


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>
add    $0x1,%eax
cltq
mov    %rax,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
jbe    1179 <strncpy+0x49>
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
jmp    1185 <strncpy+0x55>
mov    -0x18(%rbp),%rax
sub    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1050 <memcpy@plt>
mov    -0x8(%rbp),%rdi
add    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rdx
xor    %esi,%esi
call   1040 <memset@plt>
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strncpy>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdx,%r12
mov    %rsi,%r15
mov    %rdi,%r14
mov    %rsi,%rdi
call   1030 <strlen@plt>
add    $0x1,%eax
cltq
cmp    %r12,%rax
mov    %rax,%rbx
cmova  %r12,%rbx
xor    %r13d,%r13d
sub    %rax,%r12
cmovae %r12,%r13
mov    %r14,%rdi
mov    %r15,%rsi
mov    %rbx,%rdx
call   1050 <memcpy@plt>
add    %r14,%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    %r13,%rdx
call   1040 <memset@plt>
mov    %r14,%rax
pop    %rbx
pop    %r12
pop    %r13
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 i64
%8 = alloca i64
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)
%11 = add nsw i32 %10, 1
%12 = sext i32 %11 to i64
store i64 %12, i64* %7
store i64 0, i64* %8
%13 = load i64, i64* %7
%14 = load i64, i64* %6
%15 = icmp ugt i64 %13, %14
br i1 %15, label %16, label %18
16: ; preds = %3
%17 = load i64, i64* %6
store i64 %17, i64* %7
br label %22
18: ; preds = %3
%19 = load i64, i64* %6
%20 = load i64, i64* %7
%21 = sub i64 %19, %20
store i64 %21, i64* %8
br label %22
22: ; preds = %18, %16
%23 = load i8*, i8** %4
%24 = load i8*, i8** %5
%25 = load i64, i64* %7
%26 = call i32 @memcpy(i8* %23, i8* %24, i64 %25)
%27 = load i8*, i8** %4
%28 = load i64, i64* %7
%29 = getelementptr inbounds i8, i8* %27, i64 %28
%30 = load i64, i64* %8
%31 = call i32 @memset(i8* %29, i32 0, i64 %30)
%32 = load i8*, i8** %4
ret i8* %32
}

LLVM IR (-O2):
define i8* @strncpy(i8* returned %0, i8* %1, i64 %2) {
%4 = tail call i32 @strlen(i8* %1)
%5 = add nsw i32 %4, 1
%6 = sext i32 %5 to i64
%7 = icmp ugt i64 %6, %2
%8 = select i1 %7, i64 %2, i64 %6
%9 = tail call i64 @llvm.usub.sat.i64(i64 %2, i64 %6)
%10 = tail call i32 @memcpy(i8* %0, i8* %1, i64 %8)
%11 = getelementptr inbounds i8, i8* %0, i64 %8
%12 = tail call i32 @memset(i8* %11, i32 0, i64 %9)
ret i8* %0
}