#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 int /*<<< orphan*/ dest;

/* Variables and functions */
int /*<<< orphan*/ check_char(char, char);
int /*<<< orphan*/ check_chars(char, char, char *);
int /*<<< orphan*/ check_ptrs(char *, char *, char *);
int /*<<< orphan*/ check_strs(char *, char const *, char *);
int /*<<< orphan*/ check_unsigned_int(int, int);
char *str_ncpy(char *, char const *, int);
char *strncpy(char *, char const *, int);
void test_str_ncpy() {
  const char *src = "foo";
  char dest[80];
  char *result, *expected_result;
  dest[60] = 'X';
  check_unsigned_int(sizeof(dest), 80);
  check_char(dest[60], 'X');
  result = str_ncpy(dest, src, sizeof(dest));
  check_strs(dest, src, "1");
  check_ptrs(result, dest, "2");
  check_chars(dest[60], '\0', "should null-out rest of buffer");
  expected_result = strncpy(dest, src, sizeof(dest));
  check_strs(dest, src, "3");
  check_ptrs(result, expected_result, "4");
}


x86 assembly (clang, -O0):
<test_str_ncpy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
lea    0xe81(%rip),%rax
mov    %rax,-0x8(%rbp)
movb   $0x58,-0x24(%rbp)
mov    $0x50,%esi
mov    %esi,%edi
call   1080 <check_unsigned_int@plt>
mov    $0x58,%esi
movsbl -0x24(%rbp),%edi
call   1040 <check_char@plt>
lea    -0x60(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    $0x50,%edx
call   1090 <str_ncpy@plt>
mov    %rax,-0x68(%rbp)
lea    -0x60(%rbp),%rdi
mov    -0x8(%rbp),%rsi
lea    0xe3e(%rip),%rdx
call   1070 <check_strs@plt>
mov    -0x68(%rbp),%rdi
lea    -0x60(%rbp),%rsi
lea    0xe2c(%rip),%rdx
call   1060 <check_ptrs@plt>
xor    %esi,%esi
lea    0xe20(%rip),%rdx
movsbl -0x24(%rbp),%edi
call   1050 <check_chars@plt>
lea    -0x60(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    $0x50,%edx
call   1030 <strncpy@plt>
mov    %rax,-0x70(%rbp)
lea    -0x60(%rbp),%rdi
mov    -0x8(%rbp),%rsi
lea    0xe11(%rip),%rdx
call   1070 <check_strs@plt>
mov    -0x68(%rbp),%rdi
mov    -0x70(%rbp),%rsi
lea    0xdff(%rip),%rdx
call   1060 <check_ptrs@plt>
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_str_ncpy>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x58,%rsp
movb   $0x58,0x3c(%rsp)
mov    $0x50,%edi
mov    $0x50,%esi
call   1080 <check_unsigned_int@plt>
mov    $0x58,%edi
mov    $0x58,%esi
call   1040 <check_char@plt>
lea    0xe5b(%rip),%r14
mov    %rsp,%r12
mov    %r12,%rdi
mov    %r14,%rsi
mov    $0x50,%edx
call   1090 <str_ncpy@plt>
mov    %rax,%r15
lea    0xe42(%rip),%rdx
mov    %r12,%rdi
mov    %r14,%rsi
call   1070 <check_strs@plt>
lea    0xe32(%rip),%rdx
mov    %r15,%rdi
mov    %r12,%rsi
call   1060 <check_ptrs@plt>
movsbl 0x3c(%rsp),%edi
lea    0xe1d(%rip),%rdx
xor    %esi,%esi
call   1050 <check_chars@plt>
mov    %r12,%rdi
mov    %r14,%rsi
mov    $0x50,%edx
call   1030 <strncpy@plt>
mov    %rax,%rbx
lea    0xe1b(%rip),%rdx
mov    %r12,%rdi
mov    %r14,%rsi
call   1070 <check_strs@plt>
lea    0xe0b(%rip),%rdx
mov    %r15,%rdi
mov    %rbx,%rsi
call   1060 <check_ptrs@plt>
add    $0x58,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [4 x i8] c"foo\00"
@.str.1 = [2 x i8] c"1\00"
@.str.2 = [2 x i8] c"2\00"
@.str.3 = [31 x i8] c"should null-out rest of buffer\00"
@.str.4 = [2 x i8] c"3\00"
@.str.5 = [2 x i8] c"4\00"
define void @test_str_ncpy() {
%1 = alloca i8*
%2 = alloca [80 x i8]
%3 = alloca i8*
%4 = alloca i8*
store i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8** %1
%5 = getelementptr inbounds [80 x i8], [80 x i8]* %2, i64 0, i64 60
store i8 88, i8* %5
%6 = call i32 @check_unsigned_int(i32 80, i32 80)
%7 = getelementptr inbounds [80 x i8], [80 x i8]* %2, i64 0, i64 60
%8 = load i8, i8* %7
%9 = call i32 @check_char(i8 signext %8, i8 signext 88)
%10 = getelementptr inbounds [80 x i8], [80 x i8]* %2, i64 0, i64 0
%11 = load i8*, i8** %1
%12 = call i8* @str_ncpy(i8* %10, i8* %11, i32 80)
store i8* %12, i8** %3
%13 = getelementptr inbounds [80 x i8], [80 x i8]* %2, i64 0, i64 0
%14 = load i8*, i8** %1
%15 = call i32 @check_strs(i8* %13, i8* %14, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%16 = load i8*, i8** %3
%17 = getelementptr inbounds [80 x i8], [80 x i8]* %2, i64 0, i64 0
%18 = call i32 @check_ptrs(i8* %16, i8* %17, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%19 = getelementptr inbounds [80 x i8], [80 x i8]* %2, i64 0, i64 60
%20 = load i8, i8* %19
%21 = call i32 @check_chars(i8 signext %20, i8 signext 0, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0))
%22 = getelementptr inbounds [80 x i8], [80 x i8]* %2, i64 0, i64 0
%23 = load i8*, i8** %1
%24 = call i8* @strncpy(i8* %22, i8* %23, i32 80)
store i8* %24, i8** %4
%25 = getelementptr inbounds [80 x i8], [80 x i8]* %2, i64 0, i64 0
%26 = load i8*, i8** %1
%27 = call i32 @check_strs(i8* %25, i8* %26, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%28 = load i8*, i8** %3
%29 = load i8*, i8** %4
%30 = call i32 @check_ptrs(i8* %28, i8* %29, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
ret void
}

LLVM IR (-O2):
@.str = [4 x i8] c"foo\00"
@.str.1 = [2 x i8] c"1\00"
@.str.2 = [2 x i8] c"2\00"
@.str.3 = [31 x i8] c"should null-out rest of buffer\00"
@.str.4 = [2 x i8] c"3\00"
@.str.5 = [2 x i8] c"4\00"
define void @test_str_ncpy() {
%1 = alloca [80 x i8]
%2 = getelementptr inbounds [80 x i8], [80 x i8]* %1, i64 0, i64 0
%3 = getelementptr inbounds [80 x i8], [80 x i8]* %1, i64 0, i64 60
store i8 88, i8* %3
%4 = tail call i32 @check_unsigned_int(i32 80, i32 80)
%5 = tail call i32 @check_char(i8 signext 88, i8 signext 88)
%6 = call i8* @str_ncpy(i8* %2, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 80)
%7 = call i32 @check_strs(i8* %2, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%8 = call i32 @check_ptrs(i8* %6, i8* %2, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%9 = load i8, i8* %3
%10 = call i32 @check_chars(i8 signext %9, i8 signext 0, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0))
%11 = call i8* @strncpy(i8* %2, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 80)
%12 = call i32 @check_strs(i8* %2, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%13 = call i32 @check_ptrs(i8* %6, i8* %11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
ret void
}