#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 */
char *rtxStrncpy(char *, size_t, char const *, size_t);
size_t strlen(char *);
char *rtxStrncat(char *dest, size_t bufsiz, const char *src, size_t nchars) {
  size_t i;
  if (0 == dest || bufsiz <= nchars || 0 == src || 0 == nchars)
    return (char *)0;
  i = strlen(dest);
  if ((bufsiz - i) <= nchars || i >= bufsiz)
    return (char *)0;
  return rtxStrncpy(&dest[i], bufsiz - i, src, nchars);
}


x86 assembly (clang, -O0):
<rtxStrncat>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
xor    %eax,%eax
cmp    -0x10(%rbp),%rax
je     116a <rtxStrncat+0x4a>
mov    -0x18(%rbp),%rax
cmp    -0x28(%rbp),%rax
jbe    116a <rtxStrncat+0x4a>
xor    %eax,%eax
cmp    -0x20(%rbp),%rax
je     116a <rtxStrncat+0x4a>
xor    %eax,%eax
cmp    -0x28(%rbp),%rax
jne    1177 <rtxStrncat+0x57>
movq   $0x0,-0x8(%rbp)
jmp    11d2 <rtxStrncat+0xb2>
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rax
sub    -0x30(%rbp),%rax
cmp    -0x28(%rbp),%rax
jbe    11a4 <rtxStrncat+0x84>
mov    -0x30(%rbp),%rax
cmp    -0x18(%rbp),%rax
jb     11b1 <rtxStrncat+0x91>
movq   $0x0,-0x8(%rbp)
jmp    11d2 <rtxStrncat+0xb2>
mov    -0x10(%rbp),%rdi
add    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%rsi
sub    -0x30(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    -0x28(%rbp),%rcx
call   1040 <rtxStrncpy@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rtxStrncat>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
test   %rdi,%rdi
je     1176 <rtxStrncat+0x56>
mov    %rcx,%r15
test   %rcx,%rcx
je     1176 <rtxStrncat+0x56>
mov    %rdx,%r14
test   %rdx,%rdx
je     1176 <rtxStrncat+0x56>
mov    %rsi,%r12
cmp    %r15,%rsi
jbe    1176 <rtxStrncat+0x56>
mov    %rdi,%rbx
call   1030 <strlen@plt>
sub    %rax,%r12
jbe    1176 <rtxStrncat+0x56>
cmp    %r15,%r12
jbe    1176 <rtxStrncat+0x56>
add    %rax,%rbx
mov    %rbx,%rdi
mov    %r12,%rsi
mov    %r14,%rdx
mov    %r15,%rcx
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1040 <rtxStrncpy@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @rtxStrncat(i8* %0, i64 %1, i8* %2, i64 %3) {
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i64
store i8* %0, i8** %6
store i64 %1, i64* %7
store i8* %2, i8** %8
store i64 %3, i64* %9
%11 = load i8*, i8** %6
%12 = icmp eq i8* null, %11
br i1 %12, label %23, label %13
13: ; preds = %4
%14 = load i64, i64* %7
%15 = load i64, i64* %9
%16 = icmp ule i64 %14, %15
br i1 %16, label %23, label %17
17: ; preds = %13
%18 = load i8*, i8** %8
%19 = icmp eq i8* null, %18
br i1 %19, label %23, label %20
20: ; preds = %17
%21 = load i64, i64* %9
%22 = icmp eq i64 0, %21
br i1 %22, label %23, label %24
23: ; preds = %20, %17, %13, %4
store i8* null, i8** %5
br label %47
24: ; preds = %20
%25 = load i8*, i8** %6
%26 = call i64 @strlen(i8* %25)
store i64 %26, i64* %10
%27 = load i64, i64* %7
%28 = load i64, i64* %10
%29 = sub i64 %27, %28
%30 = load i64, i64* %9
%31 = icmp ule i64 %29, %30
br i1 %31, label %36, label %32
32: ; preds = %24
%33 = load i64, i64* %10
%34 = load i64, i64* %7
%35 = icmp uge i64 %33, %34
br i1 %35, label %36, label %37
36: ; preds = %32, %24
store i8* null, i8** %5
br label %47
37: ; preds = %32
%38 = load i8*, i8** %6
%39 = load i64, i64* %10
%40 = getelementptr inbounds i8, i8* %38, i64 %39
%41 = load i64, i64* %7
%42 = load i64, i64* %10
%43 = sub i64 %41, %42
%44 = load i8*, i8** %8
%45 = load i64, i64* %9
%46 = call i8* @rtxStrncpy(i8* %40, i64 %43, i8* %44, i64 %45)
store i8* %46, i8** %5
br label %47
47: ; preds = %37, %36, %23
%48 = load i8*, i8** %5
ret i8* %48
}

LLVM IR (-O2):
define i8* @rtxStrncat(i8* %0, i64 %1, i8* %2, i64 %3) {
%5 = icmp eq i8* %0, null
br i1 %5, label %21, label %6
6: ; preds = %4
%7 = icmp ule i64 %1, %3
%8 = icmp eq i8* %2, null
%9 = or i1 %8, %7
%10 = icmp eq i64 %3, 0
%11 = or i1 %10, %9
br i1 %11, label %21, label %12
12: ; preds = %6
%13 = tail call i64 @strlen(i8* %0)
%14 = sub i64 %1, %13
%15 = icmp ugt i64 %14, %3
%16 = icmp ult i64 %13, %1
%17 = and i1 %16, %15
br i1 %17, label %18, label %21
18: ; preds = %12
%19 = getelementptr inbounds i8, i8* %0, i64 %13
%20 = tail call i8* @rtxStrncpy(i8* %19, i64 %14, i8* %2, i64 %3)
br label %21
21: ; preds = %12, %4, %6, %18
%22 = phi i8* [ %20, %18 ], [ null, %6 ], [ null, %4 ], [ null, %12 ]
ret i8* %22
}