#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 const);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, size_t);
size_t strnlen(char const *, size_t);
char *stpncpy(char *restrict dst, const char *restrict src, size_t maxlen) {
  const size_t srclen = strnlen(src, maxlen);
  if (srclen < maxlen) {
    memcpy(dst, src, srclen);
    memset(dst + srclen, 0, maxlen - srclen);
    return dst + srclen;
  } else {
    memcpy(dst, src, maxlen);
    return dst + maxlen;
  }
}


x86 assembly (clang, -O0):
<stpncpy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1040 <strnlen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x20(%rbp),%rax
jae    119c <stpncpy+0x6c>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1050 <memcpy@plt>
mov    -0x10(%rbp),%rdi
add    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rdx
sub    -0x28(%rbp),%rdx
xor    %esi,%esi
call   1030 <memset@plt>
mov    -0x10(%rbp),%rax
add    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11b9 <stpncpy+0x89>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1050 <memcpy@plt>
mov    -0x10(%rbp),%rax
add    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<stpncpy>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%rbx
mov    %rsi,%r15
mov    %rdi,%r14
mov    %rsi,%rdi
mov    %rdx,%rsi
call   1040 <strnlen@plt>
cmp    %rbx,%rax
jae    1177 <stpncpy+0x47>
mov    %rax,%r12
mov    %r14,%rdi
mov    %r15,%rsi
mov    %rax,%rdx
call   1050 <memcpy@plt>
add    %r12,%r14
sub    %r12,%rbx
mov    %r14,%rdi
xor    %esi,%esi
mov    %rbx,%rdx
call   1030 <memset@plt>
jmp    1188 <stpncpy+0x58>
mov    %r14,%rdi
mov    %r15,%rsi
mov    %rbx,%rdx
call   1050 <memcpy@plt>
add    %rbx,%r14
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @stpncpy(i8* noalias %0, i8* noalias %1, i64 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
store i8* %0, i8** %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%9 = load i8*, i8** %6
%10 = load i64, i64* %7
%11 = call i64 @strnlen(i8* %9, i64 %10)
store i64 %11, i64* %8
%12 = load i64, i64* %8
%13 = load i64, i64* %7
%14 = icmp ult i64 %12, %13
br i1 %14, label %15, label %30
15: ; preds = %3
%16 = load i8*, i8** %5
%17 = load i8*, i8** %6
%18 = load i64, i64* %8
%19 = call i32 @memcpy(i8* %16, i8* %17, i64 %18)
%20 = load i8*, i8** %5
%21 = load i64, i64* %8
%22 = getelementptr inbounds i8, i8* %20, i64 %21
%23 = load i64, i64* %7
%24 = load i64, i64* %8
%25 = sub i64 %23, %24
%26 = call i32 @memset(i8* %22, i32 0, i64 %25)
%27 = load i8*, i8** %5
%28 = load i64, i64* %8
%29 = getelementptr inbounds i8, i8* %27, i64 %28
store i8* %29, i8** %4
br label %38
30: ; preds = %3
%31 = load i8*, i8** %5
%32 = load i8*, i8** %6
%33 = load i64, i64* %7
%34 = call i32 @memcpy(i8* %31, i8* %32, i64 %33)
%35 = load i8*, i8** %5
%36 = load i64, i64* %7
%37 = getelementptr inbounds i8, i8* %35, i64 %36
store i8* %37, i8** %4
br label %38
38: ; preds = %30, %15
%39 = load i8*, i8** %4
ret i8* %39
}

LLVM IR (-O2):
define i8* @stpncpy(i8* noalias %0, i8* noalias %1, i64 %2) {
%4 = tail call i64 @strnlen(i8* %1, i64 %2)
%5 = icmp ult i64 %4, %2
br i1 %5, label %6, label %11
6: ; preds = %3
%7 = tail call i32 @memcpy(i8* %0, i8* %1, i64 %4)
%8 = getelementptr inbounds i8, i8* %0, i64 %4
%9 = sub i64 %2, %4
%10 = tail call i32 @memset(i8* %8, i32 0, i64 %9)
br label %14
11: ; preds = %3
%12 = tail call i32 @memcpy(i8* %0, i8* %1, i64 %2)
%13 = getelementptr inbounds i8, i8* %0, i64 %2
br label %14
14: ; preds = %11, %6
%15 = phi i8* [ %8, %6 ], [ %13, %11 ]
ret i8* %15
}