#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, const char *src, size_t n) {
  size_t len = strlen(src);
  memcpy(dest, src, len < n ? len : n);
  if (len < n)
    memset(dest + len, ' ', n - 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    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    117c <strncpy_pad+0x4c>
mov    -0x20(%rbp),%rax
mov    %rax,-0x38(%rbp)
jmp    1184 <strncpy_pad+0x54>
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%rsi
mov    -0x30(%rbp),%rdi
mov    -0x38(%rbp),%rdx
call   1050 <memcpy@plt>
mov    -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    11bd <strncpy_pad+0x8d>
mov    -0x8(%rbp),%rdi
add    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rdx
sub    -0x20(%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   %r12
push   %rbx
push   %rax
mov    %rdx,%r15
mov    %rsi,%r12
mov    %rdi,%r14
mov    %rsi,%rdi
call   1030 <strlen@plt>
mov    %rax,%rbx
cmp    %r15,%rax
mov    %r15,%rdx
cmovb  %rax,%rdx
mov    %r14,%rdi
mov    %r12,%rsi
call   1050 <memcpy@plt>
cmp    %r15,%rbx
jae    1187 <strncpy_pad+0x57>
add    %rbx,%r14
sub    %rbx,%r15
mov    %r14,%rdi
mov    $0x20,%esi
mov    %r15,%rdx
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1040 <memset@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define void @strncpy_pad(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 i8*, i8** %4
%11 = load i8*, i8** %5
%12 = load i64, i64* %7
%13 = load i64, i64* %6
%14 = icmp ult i64 %12, %13
br i1 %14, label %15, label %17
15: ; preds = %3
%16 = load i64, i64* %7
br label %19
17: ; preds = %3
%18 = load i64, i64* %6
br label %19
19: ; preds = %17, %15
%20 = phi i64 [ %16, %15 ], [ %18, %17 ]
%21 = call i32 @memcpy(i8* %10, i8* %11, i64 %20)
%22 = load i64, i64* %7
%23 = load i64, i64* %6
%24 = icmp ult i64 %22, %23
br i1 %24, label %25, label %33
25: ; preds = %19
%26 = load i8*, i8** %4
%27 = load i64, i64* %7
%28 = getelementptr inbounds i8, i8* %26, i64 %27
%29 = load i64, i64* %6
%30 = load i64, i64* %7
%31 = sub i64 %29, %30
%32 = call i32 @memset(i8* %28, i8 signext 32, i64 %31)
br label %33
33: ; preds = %25, %19
ret void
}

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