#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 */
char *realloc(char *, int const);
int strlen(char const *);
int /*<<< orphan*/ strncpy(char *, char const *, int const);
void append_to_string_buf(char **buf, const char *src) {
  const int srclen = strlen(src);
  const int buflen = strlen(*buf);
  *buf = realloc(*buf, srclen + buflen + 1);
  strncpy(*buf + buflen, src, srclen);
  (*buf)[buflen + srclen] = '\0';
}


x86 assembly (clang, -O0):
<append_to_string_buf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    -0x14(%rbp),%esi
add    -0x18(%rbp),%esi
add    $0x1,%esi
call   1050 <realloc@plt>
mov    %rax,%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
movslq -0x18(%rbp),%rax
add    %rax,%rdi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <strncpy@plt>
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    -0x18(%rbp),%ecx
add    -0x14(%rbp),%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<append_to_string_buf>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%r14
mov    %rdi,%r12
mov    %rsi,%rdi
call   1040 <strlen@plt>
mov    %eax,%r15d
mov    (%r12),%rdi
call   1040 <strlen@plt>
mov    %eax,%ebx
mov    (%r12),%rdi
lea    (%rbx,%r15,1),%ebp
lea    (%rbx,%r15,1),%esi
add    $0x1,%esi
call   1050 <realloc@plt>
mov    %rax,(%r12)
movslq %ebx,%rdi
add    %rax,%rdi
mov    %r14,%rsi
mov    %r15d,%edx
call   1030 <strncpy@plt>
mov    (%r12),%rax
movslq %ebp,%rcx
movb   $0x0,(%rax,%rcx,1)
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define void @append_to_string_buf(i8** nocapture %0, i8* %1) {
%3 = tail call i32 @strlen(i8* %1)
%4 = load i8*, i8** %0
%5 = tail call i32 @strlen(i8* %4)
%6 = load i8*, i8** %0
%7 = add nsw i32 %5, %3
%8 = add nsw i32 %7, 1
%9 = tail call i8* @realloc(i8* %6, i32 %8)
store i8* %9, i8** %0
%10 = sext i32 %5 to i64
%11 = getelementptr inbounds i8, i8* %9, i64 %10
%12 = tail call i32 @strncpy(i8* %11, i8* %1, i32 %3)
%13 = load i8*, i8** %0
%14 = sext i32 %7 to i64
%15 = getelementptr inbounds i8, i8* %13, i64 %14
store i8 0, i8* %15
ret void
}