#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 *,
                           char const *);
int /*<<< orphan*/ liw_strncat(char *, char const *, int);
scalar_t__ memcmp(char *, char *, int);
int /*<<< orphan*/ memset(char *, int, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcpy(char *, char const *);
int strlen(char *);
int /*<<< orphan*/ strncat(char *, char const *, int);
void test_ncat(const char *first, const char *second) {
  char std_buf[16];
  char liw_buf[16];
  memset(std_buf, 0x69, sizeof(std_buf));
  strcpy(std_buf, first);
  strncat(std_buf, second, sizeof(std_buf) - strlen(std_buf) - 1);
  memset(liw_buf, 0x69, sizeof(liw_buf));
  strcpy(liw_buf, first);
  liw_strncat(liw_buf, second, sizeof(liw_buf) - strlen(liw_buf) - 1);
  if (memcmp(std_buf, liw_buf, sizeof(std_buf)) != 0) {
    fprintf(stderr, "liw_strncat failed, <%s> and <%s>\n", first, second);
    exit(EXIT_FAILURE);
  }
}


x86 assembly (clang, -O0):
<test_ncat>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x20(%rbp),%rdi
mov    $0x69,%esi
mov    $0x10,%edx
call   1050 <memset@plt>
lea    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1030 <strcpy@plt>
lea    -0x20(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x48(%rbp)
lea    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x50(%rbp),%rdi
mov    -0x48(%rbp),%rsi
movslq %eax,%rcx
mov    $0x10,%eax
sub    %rcx,%rax
sub    $0x1,%rax
mov    %eax,%edx
call   1060 <strncat@plt>
lea    -0x30(%rbp),%rdi
mov    $0x69,%esi
mov    $0x10,%edx
call   1050 <memset@plt>
lea    -0x30(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1030 <strcpy@plt>
lea    -0x30(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
lea    -0x30(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x40(%rbp),%rdi
mov    -0x38(%rbp),%rsi
movslq %eax,%rcx
mov    $0x10,%eax
sub    %rcx,%rax
sub    $0x1,%rax
mov    %eax,%edx
call   1090 <liw_strncat@plt>
lea    -0x20(%rbp),%rdi
lea    -0x30(%rbp),%rsi
mov    $0x10,%edx
call   1070 <memcmp@plt>
cmp    $0x0,%rax
je     1285 <test_ncat+0x105>
mov    0x2d7f(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
mov    -0x10(%rbp),%rcx
lea    0xd8e(%rip),%rsi
call   1080 <fprintf@plt>
mov    0x2d5a(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_ncat>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %rsi,%r14
mov    %rdi,%r15
lea    0x10(%rsp),%r12
mov    %r12,%rdi
mov    $0x69,%esi
mov    $0x10,%edx
call   1050 <memset@plt>
mov    %r12,%rdi
mov    %r15,%rsi
call   1030 <strcpy@plt>
mov    %r12,%rdi
call   1040 <strlen@plt>
mov    $0xf,%ebp
mov    $0xf,%edx
sub    %eax,%edx
mov    %r12,%rdi
mov    %r14,%rsi
call   1060 <strncat@plt>
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x69,%esi
mov    $0x10,%edx
call   1050 <memset@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
call   1030 <strcpy@plt>
mov    %rbx,%rdi
call   1040 <strlen@plt>
sub    %eax,%ebp
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %ebp,%edx
call   1090 <liw_strncat@plt>
mov    %r12,%rdi
mov    %rbx,%rsi
mov    $0x10,%edx
call   1070 <memcmp@plt>
test   %rax,%rax
jne    122c <test_ncat+0xac>
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2dad(%rip),%rax
mov    (%rax),%edi
lea    0xdc4(%rip),%rsi
mov    %r15,%rdx
mov    %r14,%rcx
call   1080 <fprintf@plt>
mov    0x2d8a(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [35 x i8] c"liw_strncat failed, <%s> and <%s>\0A\00"
@EXIT_FAILURE = global i32 0
define void @test_ncat(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca [16 x i8]
%6 = alloca [16 x i8]
store i8* %0, i8** %3
store i8* %1, i8** %4
%7 = getelementptr inbounds [16 x i8], [16 x i8]* %5, i64 0, i64 0
%8 = call i32 @memset(i8* %7, i32 105, i32 16)
%9 = getelementptr inbounds [16 x i8], [16 x i8]* %5, i64 0, i64 0
%10 = load i8*, i8** %3
%11 = call i32 @strcpy(i8* %9, i8* %10)
%12 = getelementptr inbounds [16 x i8], [16 x i8]* %5, i64 0, i64 0
%13 = load i8*, i8** %4
%14 = getelementptr inbounds [16 x i8], [16 x i8]* %5, i64 0, i64 0
%15 = call i32 @strlen(i8* %14)
%16 = sext i32 %15 to i64
%17 = sub i64 16, %16
%18 = sub i64 %17, 1
%19 = trunc i64 %18 to i32
%20 = call i32 @strncat(i8* %12, i8* %13, i32 %19)
%21 = getelementptr inbounds [16 x i8], [16 x i8]* %6, i64 0, i64 0
%22 = call i32 @memset(i8* %21, i32 105, i32 16)
%23 = getelementptr inbounds [16 x i8], [16 x i8]* %6, i64 0, i64 0
%24 = load i8*, i8** %3
%25 = call i32 @strcpy(i8* %23, i8* %24)
%26 = getelementptr inbounds [16 x i8], [16 x i8]* %6, i64 0, i64 0
%27 = load i8*, i8** %4
%28 = getelementptr inbounds [16 x i8], [16 x i8]* %6, i64 0, i64 0
%29 = call i32 @strlen(i8* %28)
%30 = sext i32 %29 to i64
%31 = sub i64 16, %30
%32 = sub i64 %31, 1
%33 = trunc i64 %32 to i32
%34 = call i32 @liw_strncat(i8* %26, i8* %27, i32 %33)
%35 = getelementptr inbounds [16 x i8], [16 x i8]* %5, i64 0, i64 0
%36 = getelementptr inbounds [16 x i8], [16 x i8]* %6, i64 0, i64 0
%37 = call i64 @memcmp(i8* %35, i8* %36, i32 16)
%38 = icmp ne i64 %37, 0
br i1 %38, label %39, label %46
39: ; preds = %2
%40 = load i32, i32* @stderr
%41 = load i8*, i8** %3
%42 = load i8*, i8** %4
%43 = call i32 @fprintf(i32 %40, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i8* %41, i8* %42)
%44 = load i32, i32* @EXIT_FAILURE
%45 = call i32 @exit(i32 %44)
unreachable
46: ; preds = %2
ret void
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [35 x i8] c"liw_strncat failed, <%s> and <%s>\0A\00"
@EXIT_FAILURE = global i32 0
define void @test_ncat(i8* %0, i8* %1) {
%3 = alloca [16 x i8]
%4 = alloca [16 x i8]
%5 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 0
%6 = getelementptr inbounds [16 x i8], [16 x i8]* %4, i64 0, i64 0
%7 = call i32 @memset(i8* %5, i32 105, i32 16)
%8 = call i32 @strcpy(i8* %5, i8* %0)
%9 = call i32 @strlen(i8* %5)
%10 = sub i32 15, %9
%11 = call i32 @strncat(i8* %5, i8* %1, i32 %10)
%12 = call i32 @memset(i8* %6, i32 105, i32 16)
%13 = call i32 @strcpy(i8* %6, i8* %0)
%14 = call i32 @strlen(i8* %6)
%15 = sub i32 15, %14
%16 = call i32 @liw_strncat(i8* %6, i8* %1, i32 %15)
%17 = call i64 @memcmp(i8* %5, i8* %6, i32 16)
%18 = icmp eq i64 %17, 0
br i1 %18, label %24, label %19
19: ; preds = %2
%20 = load i32, i32* @stderr
%21 = call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %1)
%22 = load i32, i32* @EXIT_FAILURE
%23 = call i32 @exit(i32 %22)
unreachable
24: ; preds = %2
ret void
}