#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*/ EXIT_FAILURE;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, size_t);
char *realloc(char *, size_t);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcpy(char *, char *);
char *strdup(char *);
void add_char_to_strval(char **strval_ptr, size_t *strval_bufsize_ptr,
                        size_t *strval_len_ptr, int ch) {
  if (*strval_len_ptr + 1 < *strval_bufsize_ptr - 1) {
    (*strval_ptr)[*strval_len_ptr] = ch;
    (*strval_len_ptr)++;
  } else {
    (*strval_bufsize_ptr) *= 2;
    char *curr_strval = strdup(*strval_ptr);
    if (curr_strval == NULL) {
      fprintf(stderr, "add_char_to_strval(): Out of memory\n");
      exit(EXIT_FAILURE);
    }
    *strval_ptr = realloc(*strval_ptr, *strval_bufsize_ptr);
    if (*strval_ptr == NULL) {
      fprintf(stderr, "add_char_to_strval(): Out of memory\n");
      exit(EXIT_FAILURE);
    }
    memset(*strval_ptr, 0, *strval_bufsize_ptr);
    strcpy(*strval_ptr, curr_strval);
    free(curr_strval);
    curr_strval = NULL;
    (*strval_ptr)[*strval_len_ptr] = ch;
    (*strval_len_ptr)++;
  }
}


x86 assembly (clang, -O0):
<add_char_to_strval>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
add    $0x1,%rax
mov    -0x10(%rbp),%rcx
mov    (%rcx),%rcx
sub    $0x1,%rcx
cmp    %rcx,%rax
jae    11cf <add_char_to_strval+0x5f>
mov    -0x1c(%rbp),%eax
mov    %al,%dl
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    -0x18(%rbp),%rcx
mov    (%rcx),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
mov    (%rax),%rcx
add    $0x1,%rcx
mov    %rcx,(%rax)
jmp    12c3 <add_char_to_strval+0x153>
mov    -0x10(%rbp),%rax
mov    (%rax),%rcx
shl    $0x1,%rcx
mov    %rcx,(%rax)
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
call   1090 <strdup@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    121b <add_char_to_strval+0xab>
mov    0x2de1(%rip),%rax
mov    (%rax),%edi
lea    0xdf8(%rip),%rsi
call   1060 <fprintf@plt>
mov    0x2dc4(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
call   1070 <realloc@plt>
mov    %rax,%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x8(%rbp),%rax
cmpq   $0x0,(%rax)
jne    1269 <add_char_to_strval+0xf9>
mov    0x2d93(%rip),%rax
mov    (%rax),%edi
lea    0xdaa(%rip),%rsi
call   1060 <fprintf@plt>
mov    0x2d76(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
xor    %esi,%esi
call   1050 <memset@plt>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    -0x28(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x28(%rbp)
mov    -0x1c(%rbp),%eax
mov    %al,%dl
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    -0x18(%rbp),%rcx
mov    (%rcx),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
mov    (%rax),%rcx
add    $0x1,%rcx
mov    %rcx,(%rax)
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<add_char_to_strval>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r15
mov    %rsi,%rbx
mov    %rdi,%r13
mov    (%rdx),%rax
lea    0x1(%rax),%rsi
mov    (%rbx),%rbp
lea    -0x1(%rbp),%rdx
cmp    %rdx,%rsi
jae    11a0 <add_char_to_strval+0x30>
mov    0x0(%r13),%rdx
mov    %cl,(%rdx,%rax,1)
jmp    1201 <add_char_to_strval+0x91>
mov    %ecx,0x4(%rsp)
add    %rbp,%rbp
mov    %rbp,(%rbx)
mov    0x0(%r13),%r14
mov    %r14,%rdi
call   1090 <strdup@plt>
test   %rax,%rax
je     1214 <add_char_to_strval+0xa4>
mov    %rax,%r12
mov    %r14,%rdi
mov    %rbp,%rsi
call   1070 <realloc@plt>
mov    %rax,0x0(%r13)
test   %rax,%rax
je     1214 <add_char_to_strval+0xa4>
mov    (%rbx),%rdx
mov    %rax,%rdi
xor    %esi,%esi
call   1050 <memset@plt>
mov    0x0(%r13),%rdi
mov    %r12,%rsi
call   1040 <strcpy@plt>
mov    %r12,%rdi
call   1030 <free@plt>
mov    0x0(%r13),%rax
mov    (%r15),%rcx
mov    0x4(%rsp),%edx
mov    %dl,(%rax,%rcx,1)
addq   $0x1,(%r15)
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2dc5(%rip),%rax
mov    (%rax),%edi
lea    0xddc(%rip),%rsi
call   1060 <fprintf@plt>
mov    0x2da8(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [37 x i8] c"add_char_to_strval(): Out of memory\0A\00"
@EXIT_FAILURE = global i32 0
define void @add_char_to_strval(i8** %0, i64* %1, i64* %2, i32 %3) {
%5 = alloca i8**
%6 = alloca i64*
%7 = alloca i64*
%8 = alloca i32
%9 = alloca i8*
store i8** %0, i8*** %5
store i64* %1, i64** %6
store i64* %2, i64** %7
store i32 %3, i32* %8
%10 = load i64*, i64** %7
%11 = load i64, i64* %10
%12 = add i64 %11, 1
%13 = load i64*, i64** %6
%14 = load i64, i64* %13
%15 = sub i64 %14, 1
%16 = icmp ult i64 %12, %15
br i1 %16, label %17, label %28
17: ; preds = %4
%18 = load i32, i32* %8
%19 = trunc i32 %18 to i8
%20 = load i8**, i8*** %5
%21 = load i8*, i8** %20
%22 = load i64*, i64** %7
%23 = load i64, i64* %22
%24 = getelementptr inbounds i8, i8* %21, i64 %23
store i8 %19, i8* %24
%25 = load i64*, i64** %7
%26 = load i64, i64* %25
%27 = add i64 %26, 1
store i64 %27, i64* %25
br label %79
28: ; preds = %4
%29 = load i64*, i64** %6
%30 = load i64, i64* %29
%31 = mul i64 %30, 2
store i64 %31, i64* %29
%32 = load i8**, i8*** %5
%33 = load i8*, i8** %32
%34 = call i8* @strdup(i8* %33)
store i8* %34, i8** %9
%35 = load i8*, i8** %9
%36 = icmp eq i8* %35, null
br i1 %36, label %37, label %42
37: ; preds = %28
%38 = load i32, i32* @stderr
%39 = call i32 @fprintf(i32 %38, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0))
%40 = load i32, i32* @EXIT_FAILURE
%41 = call i32 @exit(i32 %40)
unreachable
42: ; preds = %28
%43 = load i8**, i8*** %5
%44 = load i8*, i8** %43
%45 = load i64*, i64** %6
%46 = load i64, i64* %45
%47 = call i8* @realloc(i8* %44, i64 %46)
%48 = load i8**, i8*** %5
store i8* %47, i8** %48
%49 = load i8**, i8*** %5
%50 = load i8*, i8** %49
%51 = icmp eq i8* %50, null
br i1 %51, label %52, label %57
52: ; preds = %42
%53 = load i32, i32* @stderr
%54 = call i32 @fprintf(i32 %53, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0))
%55 = load i32, i32* @EXIT_FAILURE
%56 = call i32 @exit(i32 %55)
unreachable
57: ; preds = %42
%58 = load i8**, i8*** %5
%59 = load i8*, i8** %58
%60 = load i64*, i64** %6
%61 = load i64, i64* %60
%62 = call i32 @memset(i8* %59, i32 0, i64 %61)
%63 = load i8**, i8*** %5
%64 = load i8*, i8** %63
%65 = load i8*, i8** %9
%66 = call i32 @strcpy(i8* %64, i8* %65)
%67 = load i8*, i8** %9
%68 = call i32 @free(i8* %67)
store i8* null, i8** %9
%69 = load i32, i32* %8
%70 = trunc i32 %69 to i8
%71 = load i8**, i8*** %5
%72 = load i8*, i8** %71
%73 = load i64*, i64** %7
%74 = load i64, i64* %73
%75 = getelementptr inbounds i8, i8* %72, i64 %74
store i8 %70, i8* %75
%76 = load i64*, i64** %7
%77 = load i64, i64* %76
%78 = add i64 %77, 1
store i64 %78, i64* %76
br label %79
79: ; preds = %57, %17
ret void
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [37 x i8] c"add_char_to_strval(): Out of memory\0A\00"
@EXIT_FAILURE = global i32 0
define void @add_char_to_strval(i8** nocapture %0, i64* nocapture %1, i64* nocapture %2, i32 %3) {
%5 = load i64, i64* %2
%6 = add i64 %5, 1
%7 = load i64, i64* %1
%8 = add i64 %7, -1
%9 = icmp ult i64 %6, %8
br i1 %9, label %10, label %14
10: ; preds = %4
%11 = trunc i32 %3 to i8
%12 = load i8*, i8** %0
%13 = getelementptr inbounds i8, i8* %12, i64 %5
store i8 %11, i8* %13
br label %42
14: ; preds = %4
%15 = shl i64 %7, 1
store i64 %15, i64* %1
%16 = load i8*, i8** %0
%17 = tail call i8* @strdup(i8* %16)
%18 = icmp eq i8* %17, null
br i1 %18, label %19, label %24
19: ; preds = %14
%20 = load i32, i32* @stderr
%21 = tail call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0))
%22 = load i32, i32* @EXIT_FAILURE
%23 = tail call i32 @exit(i32 %22)
unreachable
24: ; preds = %14
%25 = tail call i8* @realloc(i8* %16, i64 %15)
store i8* %25, i8** %0
%26 = icmp eq i8* %25, null
br i1 %26, label %27, label %32
27: ; preds = %24
%28 = load i32, i32* @stderr
%29 = tail call i32 @fprintf(i32 %28, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0))
%30 = load i32, i32* @EXIT_FAILURE
%31 = tail call i32 @exit(i32 %30)
unreachable
32: ; preds = %24
%33 = load i64, i64* %1
%34 = tail call i32 @memset(i8* %25, i32 0, i64 %33)
%35 = load i8*, i8** %0
%36 = tail call i32 @strcpy(i8* %35, i8* %17)
%37 = tail call i32 @free(i8* %17)
%38 = trunc i32 %3 to i8
%39 = load i8*, i8** %0
%40 = load i64, i64* %2
%41 = getelementptr inbounds i8, i8* %39, i64 %40
store i8 %38, i8* %41
br label %42
42: ; preds = %32, %10
%43 = load i64, i64* %2
%44 = add i64 %43, 1
store i64 %44, i64* %2
ret void
}