#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*/ printf(char *, char const *, size_t, char *);
int /*<<< orphan*/ x_strncpy(char *, char const *, size_t);
void strncpy_test(const char *str, size_t size) {
  if (size == 0)
    return;
  char buffer[size];
  x_strncpy(buffer, str, size);
  buffer[size - 1] = '\0';
  printf("strncpy (%s, %lu) = \"%s\"\n", str, size, buffer);
}


x86 assembly (clang, -O0):
<strncpy_test>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1140 <strncpy_test+0x20>
jmp    119f <strncpy_test+0x7f>
mov    -0x10(%rbp),%rax
mov    %rsp,%rcx
mov    %rcx,-0x18(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x28(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rsi
mov    -0x10(%rbp),%rdx
call   1030 <x_strncpy@plt>
mov    -0x28(%rbp),%rcx
mov    -0x10(%rbp),%rax
sub    $0x1,%rax
movb   $0x0,(%rcx,%rax,1)
mov    -0x8(%rbp),%rsi
mov    -0x10(%rbp),%rdx
lea    0xe6d(%rip),%rdi
call   1040 <printf@plt>
mov    -0x18(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strncpy_test>:
test   %rsi,%rsi
je     1182 <strncpy_test+0x62>
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%r15
mov    %rdi,%r14
mov    %rsp,%r12
mov    %rsp,%rbx
lea    0xf(%rsi),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
call   1030 <x_strncpy@plt>
movb   $0x0,-0x1(%r15,%rbx,1)
lea    0xe9b(%rip),%rdi
mov    %r14,%rsi
mov    %r15,%rdx
mov    %rbx,%rcx
call   1040 <printf@plt>
mov    %r12,%rsp
lea    -0x20(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [26 x i8] c"strncpy (%s, %lu) = \22%s\22\0A\00"
define void @strncpy_test(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i8*
%6 = alloca i64
store i8* %0, i8** %3
store i64 %1, i64* %4
%7 = load i64, i64* %4
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %10
9: ; preds = %2
br label %24
10: ; preds = %2
%11 = load i64, i64* %4
%12 = call i8* @llvm.stacksave()
store i8* %12, i8** %5
%13 = alloca i8, i64 %11
store i64 %11, i64* %6
%14 = load i8*, i8** %3
%15 = load i64, i64* %4
%16 = call i32 @x_strncpy(i8* %13, i8* %14, i64 %15)
%17 = load i64, i64* %4
%18 = sub i64 %17, 1
%19 = getelementptr inbounds i8, i8* %13, i64 %18
store i8 0, i8* %19
%20 = load i8*, i8** %3
%21 = load i64, i64* %4
%22 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i8* %20, i64 %21, i8* %13)
%23 = load i8*, i8** %5
br label %24
24: ; preds = %10, %9
ret void
}

LLVM IR (-O2):
@.str = [26 x i8] c"strncpy (%s, %lu) = \22%s\22\0A\00"
define void @strncpy_test(i8* %0, i64 %1) {
%3 = icmp eq i64 %1, 0
br i1 %3, label %11, label %4
4: ; preds = %2
%5 = tail call i8* @llvm.stacksave()
%6 = alloca i8, i64 %1
%7 = call i32 @x_strncpy(i8* %6, i8* %0, i64 %1)
%8 = add i64 %1, -1
%9 = getelementptr inbounds i8, i8* %6, i64 %8
store i8 0, i8* %9
%10 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i8* %0, i64 %1, i8* %6)
br label %11
11: ; preds = %2, %4
ret void
}