#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 */
scalar_t__ strlen(char *);
int /*<<< orphan*/ strncpy(char *, char const *, int);
char *StrNCat(char *pszDest, char const *pszSrc, int iMaxSize) {
  int iDestLength = (int)strlen(pszDest);
  if (iDestLength < iMaxSize)
    do {
      strncpy(pszDest + iDestLength, pszSrc, iMaxSize - iDestLength);
      (pszDest + iDestLength)[(iMaxSize - iDestLength) - 1] = '\0';
    } while (0);
  return pszDest;
}


x86 assembly (clang, -O0):
<StrNCat>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    118a <StrNCat+0x6a>
jmp    1150 <StrNCat+0x30>
mov    -0x8(%rbp),%rdi
movslq -0x18(%rbp),%rax
add    %rax,%rdi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
sub    -0x18(%rbp),%edx
call   1030 <strncpy@plt>
mov    -0x8(%rbp),%rax
movslq -0x18(%rbp),%rcx
add    %rcx,%rax
mov    -0x14(%rbp),%ecx
sub    -0x18(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    118a <StrNCat+0x6a>
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<StrNCat>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%ebp
mov    %rsi,%r14
mov    %rdi,%r15
call   1040 <strlen@plt>
cmp    %ebp,%eax
jge    1155 <StrNCat+0x35>
movslq %eax,%rbx
add    %r15,%rbx
sub    %eax,%ebp
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %ebp,%edx
call   1030 <strncpy@plt>
movslq %ebp,%rax
movb   $0x0,-0x1(%rax,%rbx,1)
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i8* @StrNCat(i8* returned %0, i8* %1, i32 %2) {
%4 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%5 = trunc i64 %4 to i32
%6 = icmp slt i32 %5, %2
br i1 %6, label %7, label %16
7: ; preds = %3
%8 = shl i64 %4, 32
%9 = ashr exact i64 %8, 32
%10 = getelementptr inbounds i8, i8* %0, i64 %9
%11 = sub nsw i32 %2, %5
%12 = tail call i32 @strncpy(i8* %10, i8* %1, i32 %11)
%13 = add nsw i32 %11, -1
%14 = sext i32 %13 to i64
%15 = getelementptr inbounds i8, i8* %10, i64 %14
store i8 0, i8* %15
br label %16
16: ; preds = %7, %3
ret i8* %0
}