#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*/ memcpy(char *, char const *, int);
int strlen(char const *);
char *zstrncat(char *dest, const char *src, int len) {
  int llen, rlen;
  llen = strlen(dest);
  rlen = strlen(src);
  if (rlen < len) {
    len = rlen;
  }
  if (len > 0) {
    memcpy(dest + llen, src, len);
  }
  dest[llen + len] = 0;
  return dest;
}


x86 assembly (clang, -O0):
<zstrncat>:
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   1030 <strlen@plt>
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    115d <zstrncat+0x3d>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jle    117e <zstrncat+0x5e>
mov    -0x8(%rbp),%rdi
movslq -0x18(%rbp),%rax
add    %rax,%rdi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1040 <memcpy@plt>
mov    -0x8(%rbp),%rax
mov    -0x18(%rbp),%ecx
add    -0x14(%rbp),%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<zstrncat>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%ebp
mov    %rsi,%r14
mov    %rdi,%rbx
call   1030 <strlen@plt>
mov    %eax,%r15d
mov    %r14,%rdi
call   1030 <strlen@plt>
cmp    %ebp,%eax
cmovl  %eax,%ebp
test   %ebp,%ebp
jle    1158 <zstrncat+0x38>
movslq %r15d,%rdi
add    %rbx,%rdi
mov    %r14,%rsi
mov    %ebp,%edx
call   1040 <memcpy@plt>
add    %r15d,%ebp
movslq %ebp,%rax
movb   $0x0,(%rbx,%rax,1)
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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