#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*/ strcpy(char *, char const *);
int strlen(char const *);
int xstrncat(char *dest, const char *src, size_t destsize) {
  int destlen = strlen(dest);
  int srclen = strlen(src);
  if (srclen + destlen + 1 > destsize)
    return -1;
  strcpy(dest + destlen, src);
  return destlen + srclen;
}


x86 assembly (clang, -O0):
<xstrncat>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x24(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%eax
add    -0x24(%rbp),%eax
add    $0x1,%eax
cltq
cmp    -0x20(%rbp),%rax
jbe    116d <xstrncat+0x4d>
movl   $0xffffffff,-0x4(%rbp)
jmp    118a <xstrncat+0x6a>
mov    -0x10(%rbp),%rdi
movslq -0x24(%rbp),%rax
add    %rax,%rdi
mov    -0x18(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x24(%rbp),%eax
add    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xstrncat>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdx,%r13
mov    %rsi,%r14
mov    %rdi,%r15
call   1040 <strlen@plt>
mov    %eax,%r12d
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebx
lea    (%rbx,%r12,1),%eax
add    $0x1,%eax
movslq %eax,%rcx
mov    $0xffffffff,%eax
cmp    %r13,%rcx
ja     116e <xstrncat+0x4e>
add    %r12d,%ebx
movslq %r12d,%rax
add    %rax,%r15
mov    %r15,%rdi
mov    %r14,%rsi
call   1030 <strcpy@plt>
mov    %ebx,%eax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i32 @xstrncat(i8* %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%10 = load i8*, i8** %5
%11 = call i32 @strlen(i8* %10)
store i32 %11, i32* %8
%12 = load i8*, i8** %6
%13 = call i32 @strlen(i8* %12)
store i32 %13, i32* %9
%14 = load i32, i32* %9
%15 = load i32, i32* %8
%16 = add nsw i32 %14, %15
%17 = add nsw i32 %16, 1
%18 = sext i32 %17 to i64
%19 = load i64, i64* %7
%20 = icmp ugt i64 %18, %19
br i1 %20, label %21, label %22
21: ; preds = %3
store i32 -1, i32* %4
br label %32
22: ; preds = %3
%23 = load i8*, i8** %5
%24 = load i32, i32* %8
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i8, i8* %23, i64 %25
%27 = load i8*, i8** %6
%28 = call i32 @strcpy(i8* %26, i8* %27)
%29 = load i32, i32* %8
%30 = load i32, i32* %9
%31 = add nsw i32 %29, %30
store i32 %31, i32* %4
br label %32
32: ; preds = %22, %21
%33 = load i32, i32* %4
ret i32 %33
}

LLVM IR (-O2):
define i32 @xstrncat(i8* %0, i8* %1, i64 %2) {
%4 = tail call i32 @strlen(i8* %0)
%5 = tail call i32 @strlen(i8* %1)
%6 = add nsw i32 %5, %4
%7 = add nsw i32 %6, 1
%8 = sext i32 %7 to i64
%9 = icmp ugt i64 %8, %2
br i1 %9, label %14, label %10
10: ; preds = %3
%11 = sext i32 %4 to i64
%12 = getelementptr inbounds i8, i8* %0, i64 %11
%13 = tail call i32 @strcpy(i8* %12, i8* %1)
br label %14
14: ; preds = %3, %10
%15 = phi i32 [ %6, %10 ], [ -1, %3 ]
ret i32 %15
}