#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 */
scalar_t__ cha_malloc(int);
char *cha_malloc_char(int size) {
  static int idx = (1024 * 64);
  static char *ptr;
  if (idx + size >= (1024 * 64)) {
    ptr = (char *)cha_malloc((1024 * 64));
    idx = 0;
  }
  idx += size;
  return ptr + idx - size;
}


x86 assembly (clang, -O0):
<cha_malloc_char>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    0x2f07(%rip),%eax
add    -0x4(%rbp),%eax
cmp    $0x10000,%eax
jl     114a <cha_malloc_char+0x3a>
mov    $0x10000,%edi
call   1030 <cha_malloc@plt>
mov    %rax,0x2ef8(%rip)
movl   $0x0,0x2ede(%rip)
mov    -0x4(%rbp),%eax
add    0x2ed5(%rip),%eax
mov    %eax,0x2ecf(%rip)
mov    0x2ed8(%rip),%rax
movslq 0x2ec1(%rip),%rcx
add    %rcx,%rax
movslq -0x4(%rbp),%rdx
xor    %ecx,%ecx
sub    %rdx,%rcx
add    %rcx,%rax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cha_malloc_char>:
push   %rbx
mov    %edi,%ebx
mov    0x2f0f(%rip),%ecx
lea    (%rcx,%rbx,1),%eax
cmp    $0xffff,%eax
jle    1138 <cha_malloc_char+0x28>
mov    $0x10000,%edi
call   1030 <cha_malloc@plt>
mov    %rax,0x2f04(%rip)
xor    %ecx,%ecx
jmp    113f <cha_malloc_char+0x2f>
mov    0x2ef9(%rip),%rax
add    %ebx,%ecx
mov    %ecx,0x2ee1(%rip)
movslq %ecx,%rcx
add    %rax,%rcx
movslq %ebx,%rax
sub    %rax,%rcx
mov    %rcx,%rax
pop    %rbx
ret

LLVM IR (-O0):
@cha_malloc_char.idx = internal global i32 65536
@cha_malloc_char.ptr = internal global i8* null
define i8* @cha_malloc_char(i32 %0) {
%2 = alloca i32
store i32 %0, i32* %2
%3 = load i32, i32* @cha_malloc_char.idx
%4 = load i32, i32* %2
%5 = add nsw i32 %3, %4
%6 = icmp sge i32 %5, 65536
br i1 %6, label %7, label %10
7: ; preds = %1
%8 = call i64 @cha_malloc(i32 65536)
%9 = inttoptr i64 %8 to i8*
store i8* %9, i8** @cha_malloc_char.ptr
store i32 0, i32* @cha_malloc_char.idx
br label %10
10: ; preds = %7, %1
%11 = load i32, i32* %2
%12 = load i32, i32* @cha_malloc_char.idx
%13 = add nsw i32 %12, %11
store i32 %13, i32* @cha_malloc_char.idx
%14 = load i8*, i8** @cha_malloc_char.ptr
%15 = load i32, i32* @cha_malloc_char.idx
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds i8, i8* %14, i64 %16
%18 = load i32, i32* %2
%19 = sext i32 %18 to i64
%20 = sub i64 0, %19
%21 = getelementptr inbounds i8, i8* %17, i64 %20
ret i8* %21
}

LLVM IR (-O2):
@cha_malloc_char.idx = internal unnamed_addr global i32 65536
@cha_malloc_char.ptr = internal unnamed_addr global i8* null
define i8* @cha_malloc_char(i32 %0) {
%2 = load i32, i32* @cha_malloc_char.idx
%3 = add nsw i32 %2, %0
%4 = icmp sgt i32 %3, 65535
br i1 %4, label %7, label %5
5: ; preds = %1
%6 = load i8*, i8** @cha_malloc_char.ptr
br label %10
7: ; preds = %1
%8 = tail call i64 @cha_malloc(i32 65536)
%9 = inttoptr i64 %8 to i8*
store i8* %9, i8** @cha_malloc_char.ptr
br label %10
10: ; preds = %5, %7
%11 = phi i8* [ %9, %7 ], [ %6, %5 ]
%12 = phi i32 [ 0, %7 ], [ %2, %5 ]
%13 = add nsw i32 %12, %0
store i32 %13, i32* @cha_malloc_char.idx
%14 = sext i32 %13 to i64
%15 = getelementptr inbounds i8, i8* %11, i64 %14
%16 = sext i32 %0 to i64
%17 = sub nsw i64 0, %16
%18 = getelementptr inbounds i8, i8* %15, i64 %17
ret i8* %18
}