#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*/ assert(char *);
char *realloc(char *, size_t);
size_t strlen(char *);
char *String_append_char(char *dst, const char src, size_t *capacity) {
  size_t length_dst = strlen(dst);
  if (length_dst == *capacity - 1) {
    *capacity *= 2;
    dst = realloc(dst, *capacity * sizeof(char));
    assert(dst);
  }
  dst[length_dst] = src;
  dst[length_dst + 1] = '\0';
  return dst;
}


x86 assembly (clang, -O0):
<String_append_char>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %sil,%al
mov    %rdi,-0x8(%rbp)
mov    %al,-0x9(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    -0x18(%rbp),%rcx
mov    (%rcx),%rcx
sub    $0x1,%rcx
cmp    %rcx,%rax
jne    119a <String_append_char+0x6a>
mov    -0x18(%rbp),%rax
mov    (%rax),%rcx
shl    $0x1,%rcx
mov    %rcx,(%rax)
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%rsi
shl    $0x0,%rsi
call   1050 <realloc@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <assert@plt>
mov    -0x9(%rbp),%dl
mov    -0x8(%rbp),%rax
mov    -0x20(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x8(%rbp),%rax
mov    -0x20(%rbp),%rcx
movb   $0x0,0x1(%rax,%rcx,1)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<String_append_char>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r15
mov    %esi,%r14d
mov    %rdi,%rbx
call   1030 <strlen@plt>
mov    %rax,%rbp
mov    (%r15),%rsi
lea    -0x1(%rsi),%rax
cmp    %rax,%rbp
jne    116d <String_append_char+0x3d>
add    %rsi,%rsi
mov    %rsi,(%r15)
mov    %rbx,%rdi
call   1050 <realloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1040 <assert@plt>
mov    %r14b,(%rbx,%rbp,1)
movb   $0x0,0x1(%rbx,%rbp,1)
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i8* @String_append_char(i8* %0, i8 signext %1, i64* nocapture %2) {
%4 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%5 = load i64, i64* %2
%6 = add i64 %5, -1
%7 = icmp eq i64 %4, %6
br i1 %7, label %8, label %12
8: ; preds = %3
%9 = shl i64 %5, 1
store i64 %9, i64* %2
%10 = tail call i8* @realloc(i8* %0, i64 %9)
%11 = tail call i32 @assert(i8* %10)
br label %12
12: ; preds = %8, %3
%13 = phi i8* [ %10, %8 ], [ %0, %3 ]
%14 = getelementptr inbounds i8, i8* %13, i64 %4
store i8 %1, i8* %14
%15 = add i64 %4, 1
%16 = getelementptr inbounds i8, i8* %13, i64 %15
store i8 0, i8* %16
ret i8* %13
}