#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*/ lua_error(char *);
scalar_t__ malloc(int);
scalar_t__ realloc(char *, int);
char *luaI_addchar(int c) {
  static char *buff = NULL;
  static int max = 0;
  static int n = 0;
  if (n >= max) {
    if (max == 0) {
      max = 100;
      buff = (char *)malloc(max);
    } else {
      max *= 2;
      buff = (char *)realloc(buff, max);
    }
    if (buff == NULL)
      lua_error("memory overflow");
  }
  buff[n++] = c;
  if (c == 0)
    n = 0;
  return buff;
}


x86 assembly (clang, -O0):
<luaI_addchar>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    0x2f0b(%rip),%eax
cmp    0x2f01(%rip),%eax
jl     11c2 <luaI_addchar+0x92>
cmpl   $0x0,0x2ef4(%rip)
jne    117b <luaI_addchar+0x4b>
movl   $0x64,0x2ee4(%rip)
mov    0x2ede(%rip),%edi
call   1030 <malloc@plt>
mov    %rax,0x2eca(%rip)
jmp    11a3 <luaI_addchar+0x73>
mov    0x2ec7(%rip),%eax
shl    $0x1,%eax
mov    %eax,0x2ebe(%rip)
mov    0x2eaf(%rip),%rdi
mov    0x2eb1(%rip),%esi
call   1040 <realloc@plt>
mov    %rax,0x2e9d(%rip)
cmpq   $0x0,0x2e95(%rip)
jne    11bd <luaI_addchar+0x8d>
lea    0xe48(%rip),%rdi
call   1050 <lua_error@plt>
jmp    11c2 <luaI_addchar+0x92>
mov    -0x4(%rbp),%eax
mov    %al,%dl
mov    0x2e72(%rip),%rax
mov    0x2e78(%rip),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,0x2e6d(%rip)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
cmpl   $0x0,-0x4(%rbp)
jne    11f9 <luaI_addchar+0xc9>
movl   $0x0,0x2e53(%rip)
mov    0x2e40(%rip),%rax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<luaI_addchar>:
push   %rbx
mov    %edi,%ebx
mov    0x2f0f(%rip),%esi
cmp    %esi,0x2f0d(%rip)
jl     1187 <luaI_addchar+0x57>
test   %esi,%esi
je     115b <luaI_addchar+0x2b>
add    %esi,%esi
mov    %esi,0x2efb(%rip)
mov    0x2eec(%rip),%rdi
call   1040 <realloc@plt>
jmp    116f <luaI_addchar+0x3f>
movl   $0x64,0x2ee3(%rip)
mov    $0x64,%edi
call   1030 <malloc@plt>
mov    %rax,0x2eca(%rip)
test   %rax,%rax
jne    1187 <luaI_addchar+0x57>
lea    0xe7e(%rip),%rdi
call   1050 <lua_error@plt>
mov    0x2eb2(%rip),%rax
movslq 0x2eb7(%rip),%rcx
lea    0x1(%rcx),%edx
mov    %edx,0x2eae(%rip)
mov    %bl,(%rax,%rcx,1)
test   %ebx,%ebx
je     11a7 <luaI_addchar+0x77>
pop    %rbx
ret
movl   $0x0,0x2e9b(%rip)
pop    %rbx
ret

LLVM IR (-O0):
@luaI_addchar.buff = internal global i8* null
@luaI_addchar.max = internal global i32 0
@luaI_addchar.n = internal global i32 0
@.str = [16 x i8] c"memory overflow\00"
define i8* @luaI_addchar(i32 %0) {
%2 = alloca i32
store i32 %0, i32* %2
%3 = load i32, i32* @luaI_addchar.n
%4 = load i32, i32* @luaI_addchar.max
%5 = icmp sge i32 %3, %4
br i1 %5, label %6, label %26
6: ; preds = %1
%7 = load i32, i32* @luaI_addchar.max
%8 = icmp eq i32 %7, 0
br i1 %8, label %9, label %13
9: ; preds = %6
store i32 100, i32* @luaI_addchar.max
%10 = load i32, i32* @luaI_addchar.max
%11 = call i64 @malloc(i32 %10)
%12 = inttoptr i64 %11 to i8*
store i8* %12, i8** @luaI_addchar.buff
br label %20
13: ; preds = %6
%14 = load i32, i32* @luaI_addchar.max
%15 = mul nsw i32 %14, 2
store i32 %15, i32* @luaI_addchar.max
%16 = load i8*, i8** @luaI_addchar.buff
%17 = load i32, i32* @luaI_addchar.max
%18 = call i64 @realloc(i8* %16, i32 %17)
%19 = inttoptr i64 %18 to i8*
store i8* %19, i8** @luaI_addchar.buff
br label %20
20: ; preds = %13, %9
%21 = load i8*, i8** @luaI_addchar.buff
%22 = icmp eq i8* %21, null
br i1 %22, label %23, label %25
23: ; preds = %20
%24 = call i32 @lua_error(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
br label %25
25: ; preds = %23, %20
br label %26
26: ; preds = %25, %1
%27 = load i32, i32* %2
%28 = trunc i32 %27 to i8
%29 = load i8*, i8** @luaI_addchar.buff
%30 = load i32, i32* @luaI_addchar.n
%31 = add nsw i32 %30, 1
store i32 %31, i32* @luaI_addchar.n
%32 = sext i32 %30 to i64
%33 = getelementptr inbounds i8, i8* %29, i64 %32
store i8 %28, i8* %33
%34 = load i32, i32* %2
%35 = icmp eq i32 %34, 0
br i1 %35, label %36, label %37
36: ; preds = %26
store i32 0, i32* @luaI_addchar.n
br label %37
37: ; preds = %36, %26
%38 = load i8*, i8** @luaI_addchar.buff
ret i8* %38
}

LLVM IR (-O2):
@luaI_addchar.buff = internal unnamed_addr global i8* null
@luaI_addchar.max = internal unnamed_addr global i32 0
@luaI_addchar.n = internal unnamed_addr global i32 0
@.str = [16 x i8] c"memory overflow\00"
define i8* @luaI_addchar(i32 %0) {
%2 = load i32, i32* @luaI_addchar.n
%3 = load i32, i32* @luaI_addchar.max
%4 = icmp slt i32 %2, %3
br i1 %4, label %19, label %5
5: ; preds = %1
%6 = icmp eq i32 %3, 0
br i1 %6, label %7, label %9
7: ; preds = %5
store i32 100, i32* @luaI_addchar.max
%8 = tail call i64 @malloc(i32 100)
br label %13
9: ; preds = %5
%10 = shl nsw i32 %3, 1
store i32 %10, i32* @luaI_addchar.max
%11 = load i8*, i8** @luaI_addchar.buff
%12 = tail call i64 @realloc(i8* %11, i32 %10)
br label %13
13: ; preds = %9, %7
%14 = phi i64 [ %12, %9 ], [ %8, %7 ]
%15 = inttoptr i64 %14 to i8*
store i8* %15, i8** @luaI_addchar.buff
%16 = icmp eq i64 %14, 0
br i1 %16, label %17, label %19
17: ; preds = %13
%18 = tail call i32 @lua_error(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
br label %19
19: ; preds = %13, %17, %1
%20 = trunc i32 %0 to i8
%21 = load i8*, i8** @luaI_addchar.buff
%22 = load i32, i32* @luaI_addchar.n
%23 = add nsw i32 %22, 1
store i32 %23, i32* @luaI_addchar.n
%24 = sext i32 %22 to i64
%25 = getelementptr inbounds i8, i8* %21, i64 %24
store i8 %20, i8* %25
%26 = icmp eq i32 %0, 0
br i1 %26, label %27, label %28
27: ; preds = %19
store i32 0, i32* @luaI_addchar.n
br label %28
28: ; preds = %27, %19
ret i8* %21
}