#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*/ std_buf;
typedef int /*<<< orphan*/ liw_buf;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *);
int /*<<< orphan*/ liw_strncpy(char *, char const *, int);
scalar_t__ memcmp(char *, char *, int);
int /*<<< orphan*/ memset(char *, int, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strncpy(char *, char const *, int);
void test_ncpy(const char *str) {
  char std_buf[16];
  char liw_buf[16];
  memset(std_buf, 0x42, sizeof(std_buf));
  strncpy(std_buf, str, sizeof(std_buf));
  memset(liw_buf, 0x42, sizeof(liw_buf));
  liw_strncpy(liw_buf, str, sizeof(liw_buf));
  if (memcmp(std_buf, liw_buf, sizeof(std_buf)) != 0) {
    fprintf(stderr, "liw_strncpy failed for <%s>\n", str);
    exit(EXIT_FAILURE);
  }
}


x86 assembly (clang, -O0):
<test_ncpy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
lea    -0x20(%rbp),%rdi
mov    $0x42,%esi
mov    $0x10,%edx
call   1040 <memset@plt>
lea    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    $0x10,%edx
call   1030 <strncpy@plt>
lea    -0x30(%rbp),%rdi
mov    $0x42,%esi
mov    $0x10,%edx
call   1040 <memset@plt>
lea    -0x30(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    $0x10,%edx
call   1070 <liw_strncpy@plt>
lea    -0x20(%rbp),%rdi
lea    -0x30(%rbp),%rsi
mov    $0x10,%edx
call   1050 <memcmp@plt>
cmp    $0x0,%rax
je     11f9 <test_ncpy+0x99>
mov    0x2e07(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe1a(%rip),%rsi
call   1060 <fprintf@plt>
mov    0x2de6(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_ncpy>:
push   %r15
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %rdi,%r15
lea    0x10(%rsp),%r14
mov    %r14,%rdi
mov    $0x42,%esi
mov    $0x10,%edx
call   1040 <memset@plt>
mov    %r14,%rdi
mov    %r15,%rsi
mov    $0x10,%edx
call   1030 <strncpy@plt>
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x42,%esi
mov    $0x10,%edx
call   1040 <memset@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
mov    $0x10,%edx
call   1070 <liw_strncpy@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
mov    $0x10,%edx
call   1050 <memcmp@plt>
test   %rax,%rax
jne    11d7 <test_ncpy+0x77>
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
mov    0x2e02(%rip),%rax
mov    (%rax),%edi
lea    0xe19(%rip),%rsi
mov    %r15,%rdx
call   1060 <fprintf@plt>
mov    0x2de2(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [29 x i8] c"liw_strncpy failed for <%s>\0A\00"
@EXIT_FAILURE = global i32 0
define void @test_ncpy(i8* %0) {
%2 = alloca i8*
%3 = alloca [16 x i8]
%4 = alloca [16 x i8]
store i8* %0, i8** %2
%5 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 0
%6 = call i32 @memset(i8* %5, i32 66, i32 16)
%7 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 0
%8 = load i8*, i8** %2
%9 = call i32 @strncpy(i8* %7, i8* %8, i32 16)
%10 = getelementptr inbounds [16 x i8], [16 x i8]* %4, i64 0, i64 0
%11 = call i32 @memset(i8* %10, i32 66, i32 16)
%12 = getelementptr inbounds [16 x i8], [16 x i8]* %4, i64 0, i64 0
%13 = load i8*, i8** %2
%14 = call i32 @liw_strncpy(i8* %12, i8* %13, i32 16)
%15 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 0
%16 = getelementptr inbounds [16 x i8], [16 x i8]* %4, i64 0, i64 0
%17 = call i64 @memcmp(i8* %15, i8* %16, i32 16)
%18 = icmp ne i64 %17, 0
br i1 %18, label %19, label %25
19: ; preds = %1
%20 = load i32, i32* @stderr
%21 = load i8*, i8** %2
%22 = call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i8* %21)
%23 = load i32, i32* @EXIT_FAILURE
%24 = call i32 @exit(i32 %23)
unreachable
25: ; preds = %1
ret void
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [29 x i8] c"liw_strncpy failed for <%s>\0A\00"
@EXIT_FAILURE = global i32 0
define void @test_ncpy(i8* %0) {
%2 = alloca [16 x i8]
%3 = alloca [16 x i8]
%4 = getelementptr inbounds [16 x i8], [16 x i8]* %2, i64 0, i64 0
%5 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 0
%6 = call i32 @memset(i8* %4, i32 66, i32 16)
%7 = call i32 @strncpy(i8* %4, i8* %0, i32 16)
%8 = call i32 @memset(i8* %5, i32 66, i32 16)
%9 = call i32 @liw_strncpy(i8* %5, i8* %0, i32 16)
%10 = call i64 @memcmp(i8* %4, i8* %5, i32 16)
%11 = icmp eq i64 %10, 0
br i1 %11, label %17, label %12
12: ; preds = %1
%13 = load i32, i32* @stderr
%14 = call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i8* %0)
%15 = load i32, i32* @EXIT_FAILURE
%16 = call i32 @exit(i32 %15)
unreachable
17: ; preds = %1
ret void
}