#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__ malloc(size_t);
scalar_t__ realloc(char *, size_t);
size_t strlen(char const *);
char *strncat(char *, char const *, size_t);
char *c_wcappendn(char *dst, const char *src, size_t src_limit) {
  size_t src_len = 0;
  size_t dst_len = 0;
  if (src == NULL)
    return (dst);
  if ((src_len = strlen(src)) < 1)
    return (dst);
  if ((src_len > src_limit) && (src_limit > 0))
    src_len = src_limit;
  if (dst != NULL) {
    dst_len = strlen(dst);
    if ((dst = (char *)realloc((dst), ((dst_len + src_len + 1)) *
                                          (sizeof(char)))) == NULL) {
      return (NULL);
    }
  } else {
    if ((dst = (char *)malloc(((dst_len + src_len + 1)) * (sizeof(char)))) ==
        NULL) {
      return (NULL);
    }
    *dst = '\0';
  }
  return (strncat(dst, src, src_len));
}


x86 assembly (clang, -O0):
<c_wcappendn>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    117c <c_wcappendn+0x3c>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    125e <c_wcappendn+0x11e>
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x1,%rax
jae    11a0 <c_wcappendn+0x60>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    125e <c_wcappendn+0x11e>
mov    -0x28(%rbp),%rax
cmp    -0x20(%rbp),%rax
jbe    11c1 <c_wcappendn+0x81>
cmpq   $0x0,-0x20(%rbp)
jbe    11c1 <c_wcappendn+0x81>
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1212 <c_wcappendn+0xd2>
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x30(%rbp),%rsi
add    -0x28(%rbp),%rsi
add    $0x1,%rsi
shl    $0x0,%rsi
call   1060 <realloc@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    120d <c_wcappendn+0xcd>
movq   $0x0,-0x8(%rbp)
jmp    125e <c_wcappendn+0x11e>
jmp    1249 <c_wcappendn+0x109>
mov    -0x30(%rbp),%rdi
add    -0x28(%rbp),%rdi
add    $0x1,%rdi
shl    $0x0,%rdi
call   1050 <malloc@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    1242 <c_wcappendn+0x102>
movq   $0x0,-0x8(%rbp)
jmp    125e <c_wcappendn+0x11e>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1040 <strncat@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<c_wcappendn>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r12
test   %rsi,%rsi
je     11c2 <c_wcappendn+0x82>
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %rsi,%rdi
call   1030 <strlen@plt>
test   %rax,%rax
je     11c2 <c_wcappendn+0x82>
cmp    %rbx,%rax
mov    %rax,%r15
cmova  %rbx,%r15
test   %rbx,%rbx
cmove  %rax,%r15
test   %r12,%r12
je     119b <c_wcappendn+0x5b>
mov    %r12,%rdi
call   1030 <strlen@plt>
lea    (%r15,%rax,1),%rsi
add    $0x1,%rsi
mov    %r12,%rdi
call   1060 <realloc@plt>
mov    %rax,%r12
test   %rax,%rax
jne    11af <c_wcappendn+0x6f>
jmp    11bf <c_wcappendn+0x7f>
lea    0x1(%r15),%rdi
call   1050 <malloc@plt>
test   %rax,%rax
je     11bf <c_wcappendn+0x7f>
mov    %rax,%r12
movb   $0x0,(%rax)
mov    %r12,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
call   1040 <strncat@plt>
jmp    11c2 <c_wcappendn+0x82>
xor    %r12d,%r12d
mov    %r12,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @c_wcappendn(i8* %0, i8* %1, i64 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i64
store i8* %0, i8** %5
store i8* %1, i8** %6
store i64 %2, i64* %7
store i64 0, i64* %8
store i64 0, i64* %9
%10 = load i8*, i8** %6
%11 = icmp eq i8* %10, null
br i1 %11, label %12, label %14
12: ; preds = %3
%13 = load i8*, i8** %5
store i8* %13, i8** %4
br label %63
14: ; preds = %3
%15 = load i8*, i8** %6
%16 = call i64 @strlen(i8* %15)
store i64 %16, i64* %8
%17 = icmp ult i64 %16, 1
br i1 %17, label %18, label %20
18: ; preds = %14
%19 = load i8*, i8** %5
store i8* %19, i8** %4
br label %63
20: ; preds = %14
%21 = load i64, i64* %8
%22 = load i64, i64* %7
%23 = icmp ugt i64 %21, %22
br i1 %23, label %24, label %29
24: ; preds = %20
%25 = load i64, i64* %7
%26 = icmp ugt i64 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %24
%28 = load i64, i64* %7
store i64 %28, i64* %8
br label %29
29: ; preds = %27, %24, %20
%30 = load i8*, i8** %5
%31 = icmp ne i8* %30, null
br i1 %31, label %32, label %46
32: ; preds = %29
%33 = load i8*, i8** %5
%34 = call i64 @strlen(i8* %33)
store i64 %34, i64* %9
%35 = load i8*, i8** %5
%36 = load i64, i64* %9
%37 = load i64, i64* %8
%38 = add i64 %36, %37
%39 = add i64 %38, 1
%40 = mul i64 %39, 1
%41 = call i64 @realloc(i8* %35, i64 %40)
%42 = inttoptr i64 %41 to i8*
store i8* %42, i8** %5
%43 = icmp eq i8* %42, null
br i1 %43, label %44, label %45
44: ; preds = %32
store i8* null, i8** %4
br label %63
45: ; preds = %32
br label %58
46: ; preds = %29
%47 = load i64, i64* %9
%48 = load i64, i64* %8
%49 = add i64 %47, %48
%50 = add i64 %49, 1
%51 = mul i64 %50, 1
%52 = call i64 @malloc(i64 %51)
%53 = inttoptr i64 %52 to i8*
store i8* %53, i8** %5
%54 = icmp eq i8* %53, null
br i1 %54, label %55, label %56
55: ; preds = %46
store i8* null, i8** %4
br label %63
56: ; preds = %46
%57 = load i8*, i8** %5
store i8 0, i8* %57
br label %58
58: ; preds = %56, %45
%59 = load i8*, i8** %5
%60 = load i8*, i8** %6
%61 = load i64, i64* %8
%62 = call i8* @strncat(i8* %59, i8* %60, i64 %61)
store i8* %62, i8** %4
br label %63
63: ; preds = %58, %55, %44, %18, %12
%64 = load i8*, i8** %4
ret i8* %64
}

LLVM IR (-O2):
define i8* @c_wcappendn(i8* %0, i8* %1, i64 %2) {
%4 = icmp eq i8* %1, null
br i1 %4, label %30, label %5
5: ; preds = %3
%6 = tail call i64 @strlen(i8* %1)
%7 = icmp eq i64 %6, 0
br i1 %7, label %30, label %8
8: ; preds = %5
%9 = icmp ugt i64 %6, %2
%10 = icmp ne i64 %2, 0
%11 = and i1 %10, %9
%12 = select i1 %11, i64 %2, i64 %6
%13 = icmp eq i8* %0, null
br i1 %13, label %21, label %14
14: ; preds = %8
%15 = tail call i64 @strlen(i8* %0)
%16 = add i64 %12, 1
%17 = add i64 %16, %15
%18 = tail call i64 @realloc(i8* %0, i64 %17)
%19 = inttoptr i64 %18 to i8*
%20 = icmp eq i64 %18, 0
br i1 %20, label %30, label %27
21: ; preds = %8
%22 = add i64 %12, 1
%23 = tail call i64 @malloc(i64 %22)
%24 = inttoptr i64 %23 to i8*
%25 = icmp eq i64 %23, 0
br i1 %25, label %30, label %26
26: ; preds = %21
store i8 0, i8* %24
br label %27
27: ; preds = %14, %26
%28 = phi i8* [ %19, %14 ], [ %24, %26 ]
%29 = tail call i8* @strncat(i8* dereferenceable(1) %28, i8* %1, i64 %12)
br label %30
30: ; preds = %21, %14, %5, %3, %27
%31 = phi i8* [ %28, %27 ], [ %0, %3 ], [ %0, %5 ], [ null, %14 ], [ null, %21 ]
ret i8* %31
}