#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*/ free(char *);
scalar_t__ realloc(char *, unsigned int);
char *update_buffer(char c, bool do_i_free) {
  static unsigned int current_buffer_size;
  static unsigned int current_char_pos;
  static char *buffer;
  if (do_i_free) {
    free(buffer);
    current_char_pos = 0;
    current_buffer_size = 0;
    buffer = NULL;
    return NULL;
  }
  if (current_char_pos >= current_buffer_size) {
    char *new_buffer = (char *)realloc(buffer, current_buffer_size + 256);
    if (new_buffer) {
      current_buffer_size = current_buffer_size + 256;
      buffer = new_buffer;
    } else {
      return NULL;
    }
  }
  buffer[current_char_pos++] = c;
  return buffer;
}


x86 assembly (clang, -O0):
<update_buffer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %dil,%al
mov    %al,-0x9(%rbp)
mov    %esi,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
je     1173 <update_buffer+0x53>
mov    0x2efe(%rip),%rdi
call   1030 <free@plt>
movl   $0x0,0x2eeb(%rip)
movl   $0x0,0x2edd(%rip)
movq   $0x0,0x2eda(%rip)
movq   $0x0,-0x8(%rbp)
jmp    120a <update_buffer+0xea>
mov    0x2ec3(%rip),%eax
cmp    0x2eb9(%rip),%eax
jb     11df <update_buffer+0xbf>
mov    0x2eb4(%rip),%rdi
mov    0x2ea6(%rip),%esi
add    $0x100,%esi
call   1040 <realloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11cd <update_buffer+0xad>
mov    0x2e86(%rip),%eax
add    $0x100,%eax
mov    %eax,0x2e7b(%rip)
mov    -0x18(%rbp),%rax
mov    %rax,0x2e78(%rip)
jmp    11da <update_buffer+0xba>
movq   $0x0,-0x8(%rbp)
jmp    120a <update_buffer+0xea>
jmp    11df <update_buffer+0xbf>
mov    -0x9(%rbp),%dl
mov    0x2e57(%rip),%rax
mov    0x2e4d(%rip),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,0x2e42(%rip)
mov    %ecx,%ecx
mov    %dl,(%rax,%rcx,1)
mov    0x2e3a(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<update_buffer>:
push   %rbx
test   %esi,%esi
je     1154 <update_buffer+0x34>
mov    0x2f14(%rip),%rdi
call   1030 <free@plt>
movl   $0x0,0x2f01(%rip)
movl   $0x0,0x2ef3(%rip)
movq   $0x0,0x2ef0(%rip)
xor    %eax,%eax
pop    %rbx
ret
mov    %edi,%ebx
mov    0x2ee0(%rip),%ecx
mov    0x2ed6(%rip),%esi
mov    0x2ed7(%rip),%rax
cmp    %esi,%ecx
jb     1197 <update_buffer+0x77>
add    $0x100,%esi
mov    %rax,%rdi
call   1040 <realloc@plt>
test   %rax,%rax
je     1150 <update_buffer+0x30>
addl   $0x100,0x2eae(%rip)
mov    %rax,0x2eaf(%rip)
mov    0x2ea5(%rip),%ecx
lea    0x1(%rcx),%edx
mov    %edx,0x2e9c(%rip)
mov    %ecx,%ecx
mov    %bl,(%rax,%rcx,1)
pop    %rbx
ret

LLVM IR (-O0):
@update_buffer.current_buffer_size = internal global i32 0
@update_buffer.current_char_pos = internal global i32 0
@update_buffer.buffer = internal global i8* null
define i8* @update_buffer(i8 signext %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i8
%5 = alloca i32
%6 = alloca i8*
store i8 %0, i8* %4
store i32 %1, i32* %5
%7 = load i32, i32* %5
%8 = icmp ne i32 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %2
%10 = load i8*, i8** @update_buffer.buffer
%11 = call i32 @free(i8* %10)
store i32 0, i32* @update_buffer.current_char_pos
store i32 0, i32* @update_buffer.current_buffer_size
store i8* null, i8** @update_buffer.buffer
store i8* null, i8** %3
br label %38
12: ; preds = %2
%13 = load i32, i32* @update_buffer.current_char_pos
%14 = load i32, i32* @update_buffer.current_buffer_size
%15 = icmp uge i32 %13, %14
br i1 %15, label %16, label %30
16: ; preds = %12
%17 = load i8*, i8** @update_buffer.buffer
%18 = load i32, i32* @update_buffer.current_buffer_size
%19 = add i32 %18, 256
%20 = call i64 @realloc(i8* %17, i32 %19)
%21 = inttoptr i64 %20 to i8*
store i8* %21, i8** %6
%22 = load i8*, i8** %6
%23 = icmp ne i8* %22, null
br i1 %23, label %24, label %28
24: ; preds = %16
%25 = load i32, i32* @update_buffer.current_buffer_size
%26 = add i32 %25, 256
store i32 %26, i32* @update_buffer.current_buffer_size
%27 = load i8*, i8** %6
store i8* %27, i8** @update_buffer.buffer
br label %29
28: ; preds = %16
store i8* null, i8** %3
br label %38
29: ; preds = %24
br label %30
30: ; preds = %29, %12
%31 = load i8, i8* %4
%32 = load i8*, i8** @update_buffer.buffer
%33 = load i32, i32* @update_buffer.current_char_pos
%34 = add i32 %33, 1
store i32 %34, i32* @update_buffer.current_char_pos
%35 = zext i32 %33 to i64
%36 = getelementptr inbounds i8, i8* %32, i64 %35
store i8 %31, i8* %36
%37 = load i8*, i8** @update_buffer.buffer
store i8* %37, i8** %3
br label %38
38: ; preds = %30, %28, %9
%39 = load i8*, i8** %3
ret i8* %39
}

LLVM IR (-O2):
@update_buffer.current_buffer_size = internal unnamed_addr global i32 0
@update_buffer.current_char_pos = internal unnamed_addr global i32 0
@update_buffer.buffer = internal unnamed_addr global i8* null
define i8* @update_buffer(i8 signext %0, i32 %1) {
%3 = icmp eq i32 %1, 0
br i1 %3, label %7, label %4
4: ; preds = %2
%5 = load i8*, i8** @update_buffer.buffer
%6 = tail call i32 @free(i8* %5)
store i32 0, i32* @update_buffer.current_char_pos
store i32 0, i32* @update_buffer.current_buffer_size
store i8* null, i8** @update_buffer.buffer
br label %27
7: ; preds = %2
%8 = load i32, i32* @update_buffer.current_char_pos
%9 = load i32, i32* @update_buffer.current_buffer_size
%10 = icmp ult i32 %8, %9
%11 = load i8*, i8** @update_buffer.buffer
br i1 %10, label %21, label %12
12: ; preds = %7
%13 = add i32 %9, 256
%14 = tail call i64 @realloc(i8* %11, i32 %13)
%15 = icmp eq i64 %14, 0
br i1 %15, label %27, label %16
16: ; preds = %12
%17 = inttoptr i64 %14 to i8*
%18 = load i32, i32* @update_buffer.current_buffer_size
%19 = add i32 %18, 256
store i32 %19, i32* @update_buffer.current_buffer_size
store i8* %17, i8** @update_buffer.buffer
%20 = load i32, i32* @update_buffer.current_char_pos
br label %21
21: ; preds = %7, %16
%22 = phi i32 [ %20, %16 ], [ %8, %7 ]
%23 = phi i8* [ %17, %16 ], [ %11, %7 ]
%24 = add i32 %22, 1
store i32 %24, i32* @update_buffer.current_char_pos
%25 = zext i32 %22 to i64
%26 = getelementptr inbounds i8, i8* %23, i64 %25
store i8 %0, i8* %26
br label %27
27: ; preds = %12, %21, %4
%28 = phi i8* [ null, %4 ], [ %23, %21 ], [ null, %12 ]
ret i8* %28
}