#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 asprintf(char **, char *, char *, int, char const *);
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ free(char *);
void vc4_strncat(char **dest, const char *src, int len) {
  char *new_dest;
  int r;
  r = asprintf(&new_dest, "%s%.*s", *dest, len, src);
  assert(new_dest != NULL);
  assert(r >= 0);
  free(*dest);
  *dest = new_dest;
}


x86 assembly (clang, -O0):
<vc4_strncat>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rdx
mov    -0x14(%rbp),%ecx
mov    -0x10(%rbp),%r8
lea    -0x20(%rbp),%rdi
lea    0xea4(%rip),%rsi
call   1050 <asprintf@plt>
mov    %eax,-0x24(%rbp)
cmpq   $0x0,-0x20(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
cmpl   $0x0,-0x24(%rbp)
setge  %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<vc4_strncat>:
push   %rbp
push   %rbx
push   %rax
mov    %edx,%ecx
mov    %rsi,%r8
mov    %rdi,%rbx
mov    (%rdi),%rdx
lea    0xebb(%rip),%rsi
mov    %rsp,%rdi
call   1050 <asprintf@plt>
mov    %eax,%ebp
xor    %edi,%edi
cmpq   $0x0,(%rsp)
setne  %dil
call   1040 <assert@plt>
not    %ebp
shr    $0x1f,%ebp
mov    %ebp,%edi
call   1040 <assert@plt>
mov    (%rbx),%rdi
call   1030 <free@plt>
mov    (%rsp),%rax
mov    %rax,(%rbx)
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [7 x i8] c"%s%.*s\00"
define void @vc4_strncat(i8** %0, i8* %1, i32 %2) {
%4 = alloca i8**
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
store i8** %0, i8*** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%9 = load i8**, i8*** %4
%10 = load i8*, i8** %9
%11 = load i32, i32* %6
%12 = load i8*, i8** %5
%13 = call i32 @asprintf(i8** %7, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* %10, i32 %11, i8* %12)
store i32 %13, i32* %8
%14 = load i8*, i8** %7
%15 = icmp ne i8* %14, null
%16 = zext i1 %15 to i32
%17 = call i32 @assert(i32 %16)
%18 = load i32, i32* %8
%19 = icmp sge i32 %18, 0
%20 = zext i1 %19 to i32
%21 = call i32 @assert(i32 %20)
%22 = load i8**, i8*** %4
%23 = load i8*, i8** %22
%24 = call i32 @free(i8* %23)
%25 = load i8*, i8** %7
%26 = load i8**, i8*** %4
store i8* %25, i8** %26
ret void
}

LLVM IR (-O2):
@.str = [7 x i8] c"%s%.*s\00"
define void @vc4_strncat(i8** nocapture %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = bitcast i8** %4 to i8*
%6 = load i8*, i8** %0
%7 = call i32 @asprintf(i8** %4, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* %6, i32 %2, i8* %1)
%8 = load i8*, i8** %4
%9 = icmp ne i8* %8, null
%10 = zext i1 %9 to i32
%11 = call i32 @assert(i32 %10)
%12 = xor i32 %7, -1
%13 = lshr i32 %12, 31
%14 = call i32 @assert(i32 %13)
%15 = load i8*, i8** %0
%16 = call i32 @free(i8* %15)
%17 = load i8*, i8** %4
store i8* %17, i8** %0
ret void
}