#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);
void KheBuffAddChar(char **buff, int *curr_len, int *max_len, char ch) {
  if (curr_len >= max_len) {
    *max_len = *max_len + 16;
    *buff = realloc(*buff, *max_len);
  }
  (*buff)[(*curr_len)++] = ch;
}


x86 assembly (clang, -O0):
<KheBuffAddChar>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %cl,%al
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %al,-0x19(%rbp)
mov    -0x10(%rbp),%rax
cmp    -0x18(%rbp),%rax
jb     1162 <KheBuffAddChar+0x52>
mov    -0x18(%rbp),%rax
mov    (%rax),%ecx
add    $0x10,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%esi
call   1030 <realloc@plt>
mov    %rax,%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x19(%rbp),%dl
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    -0x10(%rbp),%rsi
mov    (%rsi),%ecx
mov    %ecx,%edi
add    $0x1,%edi
mov    %edi,(%rsi)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<KheBuffAddChar>:
push   %rbp
push   %r14
push   %rbx
mov    %ecx,%r14d
mov    %rsi,%rbx
mov    %rdi,%rbp
cmp    %rdx,%rsi
jae    1128 <KheBuffAddChar+0x18>
mov    0x0(%rbp),%rax
jmp    113c <KheBuffAddChar+0x2c>
mov    (%rdx),%esi
add    $0x10,%esi
mov    %esi,(%rdx)
mov    0x0(%rbp),%rdi
call   1030 <realloc@plt>
mov    %rax,0x0(%rbp)
movslq (%rbx),%rcx
lea    0x1(%rcx),%edx
mov    %edx,(%rbx)
mov    %r14b,(%rax,%rcx,1)
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
define void @KheBuffAddChar(i8** nocapture %0, i32* %1, i32* %2, i8 signext %3) {
%5 = icmp ult i32* %1, %2
br i1 %5, label %6, label %8
6: ; preds = %4
%7 = load i8*, i8** %0
br label %13
8: ; preds = %4
%9 = load i32, i32* %2
%10 = add nsw i32 %9, 16
store i32 %10, i32* %2
%11 = load i8*, i8** %0
%12 = tail call i8* @realloc(i8* %11, i32 %10)
store i8* %12, i8** %0
br label %13
13: ; preds = %6, %8
%14 = phi i8* [ %7, %6 ], [ %12, %8 ]
%15 = load i32, i32* %1
%16 = add nsw i32 %15, 1
store i32 %16, i32* %1
%17 = sext i32 %15 to i64
%18 = getelementptr inbounds i8, i8* %14, i64 %17
store i8 %3, i8* %18
ret void
}