#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 */
typedef char u_int32_t;

/* Variables and functions */
scalar_t__ memcpy(char *, char const *, size_t);
size_t u8_strlen(char *);
u_int32_t *u8_strncpy(u_int32_t *dest, const u_int32_t *src, size_t n) {
  int future_length = u8_strlen(dest) + n;
  if (NULL == (dest = (u_int32_t *)memcpy(dest, src, n * sizeof(u_int32_t)))) {
    return NULL;
  }
  dest[future_length] = '\0';
  return dest;
}


x86 assembly (clang, -O0):
<u8_strncpy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <u8_strlen@plt>
add    -0x20(%rbp),%rax
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
shl    $0x0,%rdx
call   1040 <memcpy@plt>
mov    %rax,%rcx
mov    %rcx,-0x10(%rbp)
xor    %eax,%eax
cmp    %rcx,%rax
jne    1178 <u8_strncpy+0x58>
movq   $0x0,-0x8(%rbp)
jmp    118c <u8_strncpy+0x6c>
mov    -0x10(%rbp),%rax
movslq -0x24(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<u8_strncpy>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%rbx
call   1030 <u8_strlen@plt>
mov    %rax,%r12
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
call   1040 <memcpy@plt>
test   %rax,%rax
je     1158 <u8_strncpy+0x38>
add    %r14d,%r12d
movslq %r12d,%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    115a <u8_strncpy+0x3a>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
define i8* @u8_strncpy(i8* %0, i8* %1, i64 %2) {
%4 = tail call i64 @u8_strlen(i8* %0)
%5 = tail call i64 @memcpy(i8* %0, i8* %1, i64 %2)
%6 = inttoptr i64 %5 to i8*
%7 = icmp eq i64 %5, 0
br i1 %7, label %13, label %8
8: ; preds = %3
%9 = add i64 %4, %2
%10 = shl i64 %9, 32
%11 = ashr exact i64 %10, 32
%12 = getelementptr inbounds i8, i8* %6, i64 %11
store i8 0, i8* %12
br label %13
13: ; preds = %3, %8
%14 = phi i8* [ %6, %8 ], [ null, %3 ]
ret i8* %14
}