#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 vec_append(char **, char *, int);
int vec_concat(char **, char *, int, int);
size_t vec_len(char *);
size_t vec_mem(char *);
int str_append(char **dst, char src) {
  int err = 0;
  size_t avail = 0;
  avail = vec_mem(*dst) - vec_len(*dst);
  if (avail < 3) {
    err = vec_concat(dst, "\0\0", 2, sizeof **dst);
    if (err)
      return err;
  }
  return vec_append(dst, &src, sizeof **dst);
}


x86 assembly (clang, -O0):
<str_append>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %sil,%al
mov    %rdi,-0x10(%rbp)
mov    %al,-0x11(%rbp)
movl   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1030 <vec_mem@plt>
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1060 <vec_len@plt>
mov    %rax,%rcx
mov    -0x28(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x20(%rbp)
cmpq   $0x3,-0x20(%rbp)
jae    11cd <str_append+0x8d>
mov    -0x10(%rbp),%rdi
lea    0xe5f(%rip),%rsi
mov    $0x2,%edx
mov    $0x1,%ecx
call   1040 <vec_concat@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
je     11c8 <str_append+0x88>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11e2 <str_append+0xa2>
jmp    11cd <str_append+0x8d>
mov    -0x10(%rbp),%rdi
lea    -0x11(%rbp),%rsi
mov    $0x1,%edx
call   1050 <vec_append@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str_append>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
mov    %sil,0x7(%rsp)
mov    (%rdi),%rdi
call   1030 <vec_mem@plt>
mov    %rax,%rbx
mov    (%r14),%rdi
call   1060 <vec_len@plt>
sub    %rax,%rbx
cmp    $0x2,%rbx
ja     1185 <str_append+0x45>
lea    0xe91(%rip),%rsi
mov    %r14,%rdi
mov    $0x2,%edx
mov    $0x1,%ecx
call   1040 <vec_concat@plt>
test   %eax,%eax
jne    1197 <str_append+0x57>
lea    0x7(%rsp),%rsi
mov    %r14,%rdi
mov    $0x1,%edx
call   1050 <vec_append@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [3 x i8] zeroinitializer
define i32 @str_append(i8** %0, i8 signext %1) {
%3 = alloca i32
%4 = alloca i8**
%5 = alloca i8
%6 = alloca i32
%7 = alloca i64
store i8** %0, i8*** %4
store i8 %1, i8* %5
store i32 0, i32* %6
store i64 0, i64* %7
%8 = load i8**, i8*** %4
%9 = load i8*, i8** %8
%10 = call i64 @vec_mem(i8* %9)
%11 = load i8**, i8*** %4
%12 = load i8*, i8** %11
%13 = call i64 @vec_len(i8* %12)
%14 = sub i64 %10, %13
store i64 %14, i64* %7
%15 = load i64, i64* %7
%16 = icmp ult i64 %15, 3
br i1 %16, label %17, label %25
17: ; preds = %2
%18 = load i8**, i8*** %4
%19 = call i32 @vec_concat(i8** %18, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 2, i32 1)
store i32 %19, i32* %6
%20 = load i32, i32* %6
%21 = icmp ne i32 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %17
%23 = load i32, i32* %6
store i32 %23, i32* %3
br label %28
24: ; preds = %17
br label %25
25: ; preds = %24, %2
%26 = load i8**, i8*** %4
%27 = call i32 @vec_append(i8** %26, i8* %5, i32 1)
store i32 %27, i32* %3
br label %28
28: ; preds = %25, %22
%29 = load i32, i32* %3
ret i32 %29
}

LLVM IR (-O2):
@.str = [3 x i8] zeroinitializer
define i32 @str_append(i8** %0, i8 signext %1) {
%3 = alloca i8
store i8 %1, i8* %3
%4 = load i8*, i8** %0
%5 = tail call i64 @vec_mem(i8* %4)
%6 = load i8*, i8** %0
%7 = tail call i64 @vec_len(i8* %6)
%8 = sub i64 %5, %7
%9 = icmp ult i64 %8, 3
br i1 %9, label %10, label %13
10: ; preds = %2
%11 = tail call i32 @vec_concat(i8** %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 2, i32 1)
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %10, %2
%14 = call i32 @vec_append(i8** %0, i8* %3, i32 1)
br label %15
15: ; preds = %10, %13
%16 = phi i32 [ %14, %13 ], [ %11, %10 ]
ret i32 %16
}