#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 *xmalloc(size_t);
char *xrealloc(char *, size_t);
char *get_fmt_buf(size_t len) {
  static char *buf_set[32];
  static size_t buflen[32];
  static int next_buf = 0;
  char **bufp = &buf_set[next_buf];
  size_t *blenp = &buflen[next_buf];
  next_buf = (next_buf + 1) % 32;
  if (*bufp == NULL) {
    *bufp = xmalloc(len);
    *blenp = len;
  }
  while (len > *blenp) {
    *blenp *= 2;
    *bufp = xrealloc(*bufp, *blenp);
  }
  return *bufp;
}


x86 assembly (clang, -O0):
<get_fmt_buf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movslq 0x310d(%rip),%rcx
lea    0x2f06(%rip),%rax
shl    $0x3,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
movslq 0x30f4(%rip),%rcx
lea    0x2fed(%rip),%rax
shl    $0x3,%rcx
add    %rcx,%rax
mov    %rax,-0x18(%rbp)
mov    0x30dc(%rip),%eax
add    $0x1,%eax
mov    $0x20,%ecx
cltd
idiv   %ecx
mov    %edx,0x30cb(%rip)
mov    -0x10(%rbp),%rax
cmpq   $0x0,(%rax)
jne    11a1 <get_fmt_buf+0x81>
mov    -0x8(%rbp),%rdi
call   1040 <xmalloc@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x8(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
jmp    11a6 <get_fmt_buf+0x86>
mov    -0x8(%rbp),%rax
mov    -0x18(%rbp),%rcx
cmp    (%rcx),%rax
jbe    11e7 <get_fmt_buf+0xc7>
mov    -0x18(%rbp),%rax
mov    (%rax),%rcx
shl    $0x1,%rcx
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%rsi
call   1030 <xrealloc@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
jmp    11a6 <get_fmt_buf+0x86>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_fmt_buf>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
movslq 0x310e(%rip),%rbx
lea    0x2f07(%rip),%r15
lea    0x1(%rbx),%eax
lea    0x20(%rbx),%ecx
test   %eax,%eax
cmovns %eax,%ecx
and    $0xffffffe0,%ecx
neg    %ecx
lea    (%rbx,%rcx,1),%eax
add    $0x1,%eax
mov    %eax,0x30eb(%rip)
mov    (%r15,%rbx,8),%rax
test   %rax,%rax
je     118e <get_fmt_buf+0x6e>
lea    0x2fdb(%rip),%r12
cs nopw 0x0(%rax,%rax,1)
nop
mov    (%r12,%rbx,8),%rsi
cmp    %r14,%rsi
jae    11a5 <get_fmt_buf+0x85>
add    %rsi,%rsi
mov    %rsi,(%r12,%rbx,8)
mov    %rax,%rdi
call   1030 <xrealloc@plt>
mov    %rax,(%r15,%rbx,8)
jmp    1170 <get_fmt_buf+0x50>
mov    %r14,%rdi
call   1040 <xmalloc@plt>
mov    %rax,(%r15,%rbx,8)
lea    0x2f9f(%rip),%rcx
mov    %r14,(%rcx,%rbx,8)
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@get_fmt_buf.buf_set = internal global [32 x i8*] zeroinitializer
@get_fmt_buf.buflen = internal global [32 x i64] zeroinitializer
@get_fmt_buf.next_buf = internal global i32 0
define i8* @get_fmt_buf(i64 %0) {
%2 = alloca i64
%3 = alloca i8**
%4 = alloca i64*
store i64 %0, i64* %2
%5 = load i32, i32* @get_fmt_buf.next_buf
%6 = sext i32 %5 to i64
%7 = getelementptr inbounds [32 x i8*], [32 x i8*]* @get_fmt_buf.buf_set, i64 0, i64 %6
store i8** %7, i8*** %3
%8 = load i32, i32* @get_fmt_buf.next_buf
%9 = sext i32 %8 to i64
%10 = getelementptr inbounds [32 x i64], [32 x i64]* @get_fmt_buf.buflen, i64 0, i64 %9
store i64* %10, i64** %4
%11 = load i32, i32* @get_fmt_buf.next_buf
%12 = add nsw i32 %11, 1
%13 = srem i32 %12, 32
store i32 %13, i32* @get_fmt_buf.next_buf
%14 = load i8**, i8*** %3
%15 = load i8*, i8** %14
%16 = icmp eq i8* %15, null
br i1 %16, label %17, label %23
17: ; preds = %1
%18 = load i64, i64* %2
%19 = call i8* @xmalloc(i64 %18)
%20 = load i8**, i8*** %3
store i8* %19, i8** %20
%21 = load i64, i64* %2
%22 = load i64*, i64** %4
store i64 %21, i64* %22
br label %23
23: ; preds = %17, %1
br label %24
24: ; preds = %29, %23
%25 = load i64, i64* %2
%26 = load i64*, i64** %4
%27 = load i64, i64* %26
%28 = icmp ugt i64 %25, %27
br i1 %28, label %29, label %39
29: ; preds = %24
%30 = load i64*, i64** %4
%31 = load i64, i64* %30
%32 = mul i64 %31, 2
store i64 %32, i64* %30
%33 = load i8**, i8*** %3
%34 = load i8*, i8** %33
%35 = load i64*, i64** %4
%36 = load i64, i64* %35
%37 = call i8* @xrealloc(i8* %34, i64 %36)
%38 = load i8**, i8*** %3
store i8* %37, i8** %38
br label %24
39: ; preds = %24
%40 = load i8**, i8*** %3
%41 = load i8*, i8** %40
ret i8* %41
}

LLVM IR (-O2):
@get_fmt_buf.buf_set = internal unnamed_addr global [32 x i8*] zeroinitializer
@get_fmt_buf.buflen = internal unnamed_addr global [32 x i64] zeroinitializer
@get_fmt_buf.next_buf = internal unnamed_addr global i32 0
define i8* @get_fmt_buf(i64 %0) {
%2 = load i32, i32* @get_fmt_buf.next_buf
%3 = sext i32 %2 to i64
%4 = getelementptr inbounds [32 x i8*], [32 x i8*]* @get_fmt_buf.buf_set, i64 0, i64 %3
%5 = getelementptr inbounds [32 x i64], [32 x i64]* @get_fmt_buf.buflen, i64 0, i64 %3
%6 = add nsw i32 %2, 1
%7 = srem i32 %6, 32
store i32 %7, i32* @get_fmt_buf.next_buf
%8 = load i8*, i8** %4
%9 = icmp eq i8* %8, null
br i1 %9, label %10, label %12
10: ; preds = %1
%11 = tail call i8* @xmalloc(i64 %0)
store i8* %11, i8** %4
store i64 %0, i64* %5
br label %22
12: ; preds = %1
%13 = load i64, i64* %5
%14 = icmp ult i64 %13, %0
br i1 %14, label %15, label %22
15: ; preds = %12, %15
%16 = phi i8* [ %19, %15 ], [ %8, %12 ]
%17 = phi i64 [ %20, %15 ], [ %13, %12 ]
%18 = shl i64 %17, 1
store i64 %18, i64* %5
%19 = tail call i8* @xrealloc(i8* %16, i64 %18)
store i8* %19, i8** %4
%20 = load i64, i64* %5
%21 = icmp ult i64 %20, %0
br i1 %21, label %15, label %22
22: ; preds = %15, %10, %12
%23 = phi i8* [ %8, %12 ], [ %11, %10 ], [ %19, %15 ]
ret i8* %23
}