#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 *, char, size_t);
size_t strlen(char const *);
void strncpy_pad(char *dest, size_t dest_len, const char *src, size_t n) {
  size_t src_len = strlen(src);
  if (src_len > n)
    src_len = n;
  memcpy(dest, src, src_len < dest_len ? src_len : dest_len);
  if (src_len < dest_len)
    memset(dest + src_len, ' ', dest_len - src_len);
}


x86 assembly (clang, -O0):
<strncpy_pad>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x20(%rbp),%rax
jbe    116b <strncpy_pad+0x3b>
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x10(%rbp),%rax
jae    1196 <strncpy_pad+0x66>
mov    -0x28(%rbp),%rax
mov    %rax,-0x40(%rbp)
jmp    119e <strncpy_pad+0x6e>
mov    -0x10(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x30(%rbp),%rsi
mov    -0x38(%rbp),%rdi
mov    -0x40(%rbp),%rdx
call   1050 <memcpy@plt>
mov    -0x28(%rbp),%rax
cmp    -0x10(%rbp),%rax
jae    11d7 <strncpy_pad+0xa7>
mov    -0x8(%rbp),%rdi
add    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rdx
sub    -0x28(%rbp),%rdx
mov    $0x20,%esi
call   1040 <memset@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strncpy_pad>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rcx,%r12
mov    %rdx,%r13
mov    %rsi,%r15
mov    %rdi,%r14
mov    %rdx,%rdi
call   1030 <strlen@plt>
mov    %rax,%rbx
cmp    %r12,%rax
cmova  %r12,%rbx
cmp    %r15,%rbx
mov    %r15,%rdx
cmovb  %rbx,%rdx
mov    %r14,%rdi
mov    %r13,%rsi
call   1050 <memcpy@plt>
cmp    %r15,%rbx
jae    1190 <strncpy_pad+0x60>
add    %rbx,%r14
sub    %rbx,%r15
mov    %r14,%rdi
mov    $0x20,%esi
mov    %r15,%rdx
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
jmp    1040 <memset@plt>
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

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

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