#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(int);
int /*<<< orphan*/ printf(char *, char *);
char *realloc(char *, int);
void read_by_char(const int c, char **buffer, int *charCount, int *bytes) {
  if (*bytes <= *charCount + 1) {
    *bytes = *charCount + 2;
    *buffer = realloc(*buffer, sizeof(char) * *bytes);
    if ((*buffer != NULL) == 0) {
      printf("Assertion: '%s' failed.\n", "(*buffer != NULL)");
      exit(9);
    };
    ;
  }
  (*buffer)[(*charCount)++] = c;
  (*buffer)[(*charCount)] = '\0';
}


x86 assembly (clang, -O0):
<read_by_char>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%eax
mov    -0x18(%rbp),%rcx
mov    (%rcx),%ecx
add    $0x1,%ecx
cmp    %ecx,%eax
jg     11cb <read_by_char+0x9b>
mov    -0x18(%rbp),%rax
mov    (%rax),%ecx
add    $0x2,%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x20(%rbp),%rax
movslq (%rax),%rax
shl    $0x0,%rax
mov    %eax,%esi
call   1040 <realloc@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
cmpq   $0x0,(%rax)
setne  %al
and    $0x1,%al
movzbl %al,%eax
cmp    $0x0,%eax
jne    11c6 <read_by_char+0x96>
lea    0xe50(%rip),%rdi
lea    0xe62(%rip),%rsi
call   1030 <printf@plt>
mov    $0x9,%edi
call   1050 <exit@plt>
jmp    11cb <read_by_char+0x9b>
mov    -0x4(%rbp),%eax
mov    %al,%dl
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    -0x18(%rbp),%rsi
mov    (%rsi),%ecx
mov    %ecx,%edi
add    $0x1,%edi
mov    %edi,(%rsi)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    -0x18(%rbp),%rcx
movslq (%rcx),%rcx
movb   $0x0,(%rax,%rcx,1)
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_by_char>:
push   %rbp
push   %r14
push   %rbx
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %edi,%ebp
mov    (%rdx),%esi
lea    0x1(%rsi),%edx
cmp    %edx,(%rcx)
jle    114a <read_by_char+0x1a>
mov    (%r14),%rax
jmp    1164 <read_by_char+0x34>
add    $0x2,%esi
mov    %esi,(%rcx)
mov    (%r14),%rdi
call   1040 <realloc@plt>
mov    %rax,(%r14)
test   %rax,%rax
je     117c <read_by_char+0x4c>
mov    (%rbx),%esi
lea    0x1(%rsi),%edx
mov    %edx,(%rbx)
movslq %esi,%rcx
mov    %bpl,(%rax,%rcx,1)
mov    (%r14),%rax
movslq (%rbx),%rcx
movb   $0x0,(%rax,%rcx,1)
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe7d(%rip),%rdi
lea    0xe8f(%rip),%rsi
call   1030 <printf@plt>
mov    $0x9,%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@.str = [25 x i8] c"Assertion: '%s' failed.\0A\00"
@.str.1 = [18 x i8] c"(*buffer != NULL)\00"
define void @read_by_char(i32 %0, i8** %1, i32* %2, i32* %3) {
%5 = alloca i32
%6 = alloca i8**
%7 = alloca i32*
%8 = alloca i32*
store i32 %0, i32* %5
store i8** %1, i8*** %6
store i32* %2, i32** %7
store i32* %3, i32** %8
%9 = load i32*, i32** %8
%10 = load i32, i32* %9
%11 = load i32*, i32** %7
%12 = load i32, i32* %11
%13 = add nsw i32 %12, 1
%14 = icmp sle i32 %10, %13
br i1 %14, label %15, label %38
15: ; preds = %4
%16 = load i32*, i32** %7
%17 = load i32, i32* %16
%18 = add nsw i32 %17, 2
%19 = load i32*, i32** %8
store i32 %18, i32* %19
%20 = load i8**, i8*** %6
%21 = load i8*, i8** %20
%22 = load i32*, i32** %8
%23 = load i32, i32* %22
%24 = sext i32 %23 to i64
%25 = mul i64 1, %24
%26 = trunc i64 %25 to i32
%27 = call i8* @realloc(i8* %21, i32 %26)
%28 = load i8**, i8*** %6
store i8* %27, i8** %28
%29 = load i8**, i8*** %6
%30 = load i8*, i8** %29
%31 = icmp ne i8* %30, null
%32 = zext i1 %31 to i32
%33 = icmp eq i32 %32, 0
br i1 %33, label %34, label %37
34: ; preds = %15
%35 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
%36 = call i32 @exit(i32 9)
unreachable
37: ; preds = %15
br label %38
38: ; preds = %37, %4
%39 = load i32, i32* %5
%40 = trunc i32 %39 to i8
%41 = load i8**, i8*** %6
%42 = load i8*, i8** %41
%43 = load i32*, i32** %7
%44 = load i32, i32* %43
%45 = add nsw i32 %44, 1
store i32 %45, i32* %43
%46 = sext i32 %44 to i64
%47 = getelementptr inbounds i8, i8* %42, i64 %46
store i8 %40, i8* %47
%48 = load i8**, i8*** %6
%49 = load i8*, i8** %48
%50 = load i32*, i32** %7
%51 = load i32, i32* %50
%52 = sext i32 %51 to i64
%53 = getelementptr inbounds i8, i8* %49, i64 %52
store i8 0, i8* %53
ret void
}

LLVM IR (-O2):
@.str = [25 x i8] c"Assertion: '%s' failed.\0A\00"
@.str.1 = [18 x i8] c"(*buffer != NULL)\00"
define void @read_by_char(i32 %0, i8** nocapture %1, i32* nocapture %2, i32* nocapture %3) {
%5 = load i32, i32* %3
%6 = load i32, i32* %2
%7 = add nsw i32 %6, 1
%8 = icmp sgt i32 %5, %7
br i1 %8, label %9, label %11
9: ; preds = %4
%10 = load i8*, i8** %1
br label %22
11: ; preds = %4
%12 = add nsw i32 %6, 2
store i32 %12, i32* %3
%13 = load i8*, i8** %1
%14 = tail call i8* @realloc(i8* %13, i32 %12)
store i8* %14, i8** %1
%15 = icmp eq i8* %14, null
br i1 %15, label %19, label %16
16: ; preds = %11
%17 = load i32, i32* %2
%18 = add nsw i32 %17, 1
br label %22
19: ; preds = %11
%20 = tail call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
%21 = tail call i32 @exit(i32 9)
unreachable
22: ; preds = %16, %9
%23 = phi i32 [ %18, %16 ], [ %7, %9 ]
%24 = phi i32 [ %17, %16 ], [ %6, %9 ]
%25 = phi i8* [ %14, %16 ], [ %10, %9 ]
%26 = trunc i32 %0 to i8
store i32 %23, i32* %2
%27 = sext i32 %24 to i64
%28 = getelementptr inbounds i8, i8* %25, i64 %27
store i8 %26, i8* %28
%29 = load i8*, i8** %1
%30 = load i32, i32* %2
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i8, i8* %29, i64 %31
store i8 0, i8* %32
ret void
}