#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 */
typedef int /*<<< orphan*/ retarray;

/* Variables and functions */
int /*<<< orphan*/ snprintf(char *, int, char *, long long, char);
char *format_b(long long amt) {
  static char retarray[8][16];
  static int index = 0;
  register char *ret;
  register char tag = 'B';
  ret = retarray[index];
  index = (index + 1) % 8;
  if (amt >= 10000) {
    amt = (amt + 512) / 1024;
    tag = 'K';
    if (amt >= 10000) {
      amt = (amt + 512) / 1024;
      tag = 'B';
      if (amt >= 10000) {
        amt = (amt + 512) / 1024;
        tag = 'G';
      }
    }
  }
  snprintf(ret, sizeof(retarray[index]) - 1, "%lld%c", amt, tag);
  return (ret);
}


x86 assembly (clang, -O0):
<format_b>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movb   $0x42,-0x11(%rbp)
movslq 0x2f99(%rip),%rcx
lea    0x2f12(%rip),%rax
shl    $0x4,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
mov    0x2f81(%rip),%eax
add    $0x1,%eax
mov    $0x8,%ecx
cltd
idiv   %ecx
mov    %edx,0x2f70(%rip)
cmpq   $0x2710,-0x8(%rbp)
jl     11d8 <format_b+0xc8>
mov    -0x8(%rbp),%rax
add    $0x200,%rax
mov    $0x400,%ecx
cqto
idiv   %rcx
mov    %rax,-0x8(%rbp)
movb   $0x4b,-0x11(%rbp)
cmpq   $0x2710,-0x8(%rbp)
jl     11d3 <format_b+0xc3>
mov    -0x8(%rbp),%rax
add    $0x200,%rax
mov    $0x400,%ecx
cqto
idiv   %rcx
mov    %rax,-0x8(%rbp)
movb   $0x42,-0x11(%rbp)
cmpq   $0x2710,-0x8(%rbp)
jl     11ce <format_b+0xbe>
mov    -0x8(%rbp),%rax
add    $0x200,%rax
mov    $0x400,%ecx
cqto
idiv   %rcx
mov    %rax,-0x8(%rbp)
movb   $0x47,-0x11(%rbp)
jmp    11d3 <format_b+0xc3>
jmp    11d8 <format_b+0xc8>
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%rcx
mov    $0xf,%esi
lea    0xe14(%rip),%rdx
movsbl -0x11(%rbp),%r8d
call   1030 <snprintf@plt>
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<format_b>:
push   %rbx
mov    %rdi,%rcx
mov    0x2fa6(%rip),%eax
mov    %rax,%rdx
shl    $0x4,%rdx
lea    0x2f18(%rip),%rbx
add    %rdx,%rbx
lea    0x1(%rax),%edx
lea    0x8(%rax),%esi
test   %edx,%edx
cmovns %edx,%esi
and    $0xfffffff8,%esi
neg    %esi
add    %esi,%eax
add    $0x1,%eax
mov    %eax,0x2f7a(%rip)
mov    $0x42,%al
cmp    $0x2710,%rdi
jl     1194 <format_b+0x84>
lea    0x200(%rcx),%rdx
shr    $0xa,%rdx
cmp    $0x9c3e00,%rcx
jb     118f <format_b+0x7f>
add    $0x200,%rdx
shr    $0xa,%rdx
shr    $0x9,%rcx
cmp    $0x1387bff,%rcx
jb     118a <format_b+0x7a>
add    $0x200,%rdx
shr    $0xa,%rdx
mov    $0x47,%al
mov    %rdx,%rcx
jmp    1194 <format_b+0x84>
mov    %rdx,%rcx
mov    $0x4b,%al
movzbl %al,%r8d
lea    0xe61(%rip),%rdx
mov    %rbx,%rdi
mov    $0xf,%esi
call   1030 <snprintf@plt>
mov    %rbx,%rax
pop    %rbx
ret

LLVM IR (-O0):
@format_b.retarray = internal global [8 x [16 x i8]] zeroinitializer
@format_b.index = internal global i32 0
@.str = [7 x i8] c"%lld%c\00"
define i8* @format_b(i64 %0) {
%2 = alloca i64
%3 = alloca i8*
%4 = alloca i8
store i64 %0, i64* %2
store i8 66, i8* %4
%5 = load i32, i32* @format_b.index
%6 = sext i32 %5 to i64
%7 = getelementptr inbounds [8 x [16 x i8]], [8 x [16 x i8]]* @format_b.retarray, i64 0, i64 %6
%8 = getelementptr inbounds [16 x i8], [16 x i8]* %7, i64 0, i64 0
store i8* %8, i8** %3
%9 = load i32, i32* @format_b.index
%10 = add nsw i32 %9, 1
%11 = srem i32 %10, 8
store i32 %11, i32* @format_b.index
%12 = load i64, i64* %2
%13 = icmp sge i64 %12, 10000
br i1 %13, label %14, label %32
14: ; preds = %1
%15 = load i64, i64* %2
%16 = add nsw i64 %15, 512
%17 = sdiv i64 %16, 1024
store i64 %17, i64* %2
store i8 75, i8* %4
%18 = load i64, i64* %2
%19 = icmp sge i64 %18, 10000
br i1 %19, label %20, label %31
20: ; preds = %14
%21 = load i64, i64* %2
%22 = add nsw i64 %21, 512
%23 = sdiv i64 %22, 1024
store i64 %23, i64* %2
store i8 66, i8* %4
%24 = load i64, i64* %2
%25 = icmp sge i64 %24, 10000
br i1 %25, label %26, label %30
26: ; preds = %20
%27 = load i64, i64* %2
%28 = add nsw i64 %27, 512
%29 = sdiv i64 %28, 1024
store i64 %29, i64* %2
store i8 71, i8* %4
br label %30
30: ; preds = %26, %20
br label %31
31: ; preds = %30, %14
br label %32
32: ; preds = %31, %1
%33 = load i8*, i8** %3
%34 = load i64, i64* %2
%35 = load i8, i8* %4
%36 = call i32 @snprintf(i8* %33, i32 15, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i64 %34, i8 signext %35)
%37 = load i8*, i8** %3
ret i8* %37
}

LLVM IR (-O2):
@format_b.retarray = internal global [8 x [16 x i8]] zeroinitializer
@format_b.index = internal unnamed_addr global i32 0
@.str = [7 x i8] c"%lld%c\00"
define i8* @format_b(i64 %0) {
%2 = load i32, i32* @format_b.index
%3 = zext i32 %2 to i64
%4 = getelementptr inbounds [8 x [16 x i8]], [8 x [16 x i8]]* @format_b.retarray, i64 0, i64 %3, i64 0
%5 = add nsw i32 %2, 1
%6 = srem i32 %5, 8
store i32 %6, i32* @format_b.index
%7 = icmp sgt i64 %0, 9999
br i1 %7, label %8, label %19
8: ; preds = %1
%9 = add nuw nsw i64 %0, 512
%10 = lshr i64 %9, 10
%11 = icmp ugt i64 %0, 10239487
br i1 %11, label %12, label %19
12: ; preds = %8
%13 = add nuw nsw i64 %10, 512
%14 = lshr i64 %13, 10
%15 = icmp ugt i64 %0, 10485235199
br i1 %15, label %16, label %19
16: ; preds = %12
%17 = add nuw nsw i64 %14, 512
%18 = lshr i64 %17, 10
br label %19
19: ; preds = %8, %16, %12, %1
%20 = phi i64 [ %18, %16 ], [ %14, %12 ], [ %10, %8 ], [ %0, %1 ]
%21 = phi i8 [ 71, %16 ], [ 66, %12 ], [ 75, %8 ], [ 66, %1 ]
%22 = tail call i32 @snprintf(i8* %4, i32 15, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i64 %20, i8 signext %21)
ret i8* %4
}