#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__ __mempcpy(char *, char const *, size_t const);
int /*<<< orphan*/ free(char *);
char *realloc(char *, size_t);
void str_append(char **to, size_t *to_len, const char *buf,
                const size_t buf_len) {
  size_t new_len = *to_len + buf_len;
  char *new_to = realloc(*to, new_len + 1);
  if (new_to) {
    *((char *)__mempcpy(new_to + *to_len, buf, buf_len)) = '\0';
    *to = new_to;
    *to_len = new_len;
  } else {
    free(*to);
    *to = 0;
  }
}


x86 assembly (clang, -O0):
<str_append>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
add    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    -0x28(%rbp),%rsi
add    $0x1,%rsi
call   1050 <realloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     11b0 <str_append+0x80>
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rax
add    (%rax),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1040 <__mempcpy@plt>
movb   $0x0,(%rax)
mov    -0x30(%rbp),%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x28(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
jmp    11c7 <str_append+0x97>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
call   1030 <free@plt>
mov    -0x8(%rbp),%rax
movq   $0x0,(%rax)
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str_append>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r13
mov    %rdx,%r12
mov    %rsi,%r15
mov    %rdi,%r14
mov    (%rsi),%rbp
mov    (%rdi),%rdi
lea    (%rcx,%rbp,1),%rsi
add    $0x1,%rsi
call   1050 <realloc@plt>
test   %rax,%rax
je     1181 <str_append+0x51>
mov    %rax,%rbx
add    %r13,%rbp
mov    (%r15),%rdi
add    %rax,%rdi
mov    %r12,%rsi
mov    %r13,%rdx
call   1040 <__mempcpy@plt>
movb   $0x0,(%rax)
mov    %rbx,(%r14)
mov    %rbp,(%r15)
jmp    1190 <str_append+0x60>
mov    (%r14),%rdi
call   1030 <free@plt>
movq   $0x0,(%r14)
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @str_append(i8** %0, i64* %1, i8* %2, i64 %3) {
%5 = alloca i8**
%6 = alloca i64*
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i64
%10 = alloca i8*
store i8** %0, i8*** %5
store i64* %1, i64** %6
store i8* %2, i8** %7
store i64 %3, i64* %8
%11 = load i64*, i64** %6
%12 = load i64, i64* %11
%13 = load i64, i64* %8
%14 = add i64 %12, %13
store i64 %14, i64* %9
%15 = load i8**, i8*** %5
%16 = load i8*, i8** %15
%17 = load i64, i64* %9
%18 = add i64 %17, 1
%19 = call i8* @realloc(i8* %16, i64 %18)
store i8* %19, i8** %10
%20 = load i8*, i8** %10
%21 = icmp ne i8* %20, null
br i1 %21, label %22, label %35
22: ; preds = %4
%23 = load i8*, i8** %10
%24 = load i64*, i64** %6
%25 = load i64, i64* %24
%26 = getelementptr inbounds i8, i8* %23, i64 %25
%27 = load i8*, i8** %7
%28 = load i64, i64* %8
%29 = call i64 @__mempcpy(i8* %26, i8* %27, i64 %28)
%30 = inttoptr i64 %29 to i8*
store i8 0, i8* %30
%31 = load i8*, i8** %10
%32 = load i8**, i8*** %5
store i8* %31, i8** %32
%33 = load i64, i64* %9
%34 = load i64*, i64** %6
store i64 %33, i64* %34
br label %40
35: ; preds = %4
%36 = load i8**, i8*** %5
%37 = load i8*, i8** %36
%38 = call i32 @free(i8* %37)
%39 = load i8**, i8*** %5
store i8* null, i8** %39
br label %40
40: ; preds = %35, %22
ret void
}

LLVM IR (-O2):
define void @str_append(i8** nocapture %0, i64* nocapture %1, i8* %2, i64 %3) {
%5 = load i64, i64* %1
%6 = add i64 %5, %3
%7 = load i8*, i8** %0
%8 = add i64 %6, 1
%9 = tail call i8* @realloc(i8* %7, i64 %8)
%10 = icmp eq i8* %9, null
br i1 %10, label %16, label %11
11: ; preds = %4
%12 = load i64, i64* %1
%13 = getelementptr inbounds i8, i8* %9, i64 %12
%14 = tail call i64 @__mempcpy(i8* %13, i8* %2, i64 %3)
%15 = inttoptr i64 %14 to i8*
store i8 0, i8* %15
store i8* %9, i8** %0
store i64 %6, i64* %1
br label %19
16: ; preds = %4
%17 = load i8*, i8** %0
%18 = tail call i32 @free(i8* %17)
store i8* null, i8** %0
br label %19
19: ; preds = %16, %11
ret void
}