#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, ...);
char *format_k(long amt) {
  static char retarray[8][16];
  static int index = 0;
  register char *ret;
  register char tag = 'k';
  ret = retarray[index];
  index = (index + 1) % 8;
  if (amt >= 100000) {
    tag = 'm';
    {
      amt = (amt + 512) / 1024;
      if (amt >= 10000) {
        tag = 'g';
        if (amt < 100 * 1024) {
          snprintf(ret, sizeof(retarray[index]) - 1, "%.1f%c", amt / 1024.0,
                   tag);
          return ret;
        }
        amt = (amt + 512) / 1024;
      }
    }
  }
  if (tag == 'k') {
    snprintf(ret, sizeof(retarray[index]) - 1, "%ld", amt);
  } else {
    snprintf(ret, sizeof(retarray[index]) - 1, "%ld%c", amt, tag);
  }
  return (ret);
}


x86 assembly (clang, -O0):
<format_k>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
movb   $0x6b,-0x19(%rbp)
movslq 0x2f99(%rip),%rcx
lea    0x2f12(%rip),%rax
shl    $0x4,%rcx
add    %rcx,%rax
mov    %rax,-0x18(%rbp)
mov    0x2f81(%rip),%eax
add    $0x1,%eax
mov    $0x8,%ecx
cltd
idiv   %ecx
mov    %edx,0x2f70(%rip)
cmpq   $0x186a0,-0x10(%rbp)
jl     11f7 <format_k+0xe7>
movb   $0x6d,-0x19(%rbp)
mov    -0x10(%rbp),%rax
add    $0x200,%rax
mov    $0x400,%ecx
cqto
idiv   %rcx
mov    %rax,-0x10(%rbp)
cmpq   $0x2710,-0x10(%rbp)
jl     11f2 <format_k+0xe2>
movb   $0x67,-0x19(%rbp)
cmpq   $0x19000,-0x10(%rbp)
jge    11da <format_k+0xca>
mov    -0x18(%rbp),%rdi
cvtsi2sdq -0x10(%rbp),%xmm0
movsd  0xe54(%rip),%xmm1
divsd  %xmm1,%xmm0
cvttsd2si %xmm0,%rcx
movsbl -0x19(%rbp),%r8d
mov    $0xf,%esi
lea    0xe42(%rip),%rdx
mov    $0x0,%al
call   1030 <snprintf@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    124c <format_k+0x13c>
mov    -0x10(%rbp),%rax
add    $0x200,%rax
mov    $0x400,%ecx
cqto
idiv   %rcx
mov    %rax,-0x10(%rbp)
jmp    11f7 <format_k+0xe7>
movsbl -0x19(%rbp),%eax
cmp    $0x6b,%eax
jne    1224 <format_k+0x114>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0xf,%esi
lea    0xdf7(%rip),%rdx
mov    $0x0,%al
call   1030 <snprintf@plt>
jmp    1244 <format_k+0x134>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rcx
movsbl -0x19(%rbp),%r8d
mov    $0xf,%esi
lea    0xdd6(%rip),%rdx
mov    $0x0,%al
call   1030 <snprintf@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<format_k>:
push   %rbx
mov    %rdi,%rax
mov    0x2fa6(%rip),%ecx
mov    %rcx,%rdx
shl    $0x4,%rdx
lea    0x2f18(%rip),%rbx
add    %rdx,%rbx
lea    0x1(%rcx),%edx
lea    0x8(%rcx),%esi
test   %edx,%edx
cmovns %edx,%esi
and    $0xfffffff8,%esi
neg    %esi
add    %esi,%ecx
add    $0x1,%ecx
mov    %ecx,0x2f7a(%rip)
cmp    $0x186a0,%rdi
jl     1199 <format_k+0x89>
lea    0x200(%rax),%rcx
shr    $0xa,%rcx
mov    $0x6d,%r8d
cmp    $0x9c3e00,%rax
jb     11c5 <format_k+0xb5>
cmp    $0x63ffdff,%rax
ja     11b4 <format_k+0xa4>
cvtsi2sd %rcx,%xmm0
mulsd  0xe83(%rip),%xmm0
cvttsd2si %xmm0,%rcx
lea    0xe7f(%rip),%rdx
mov    %rbx,%rdi
mov    $0xf,%esi
mov    $0x67,%r8d
jmp    11d4 <format_k+0xc4>
lea    0xe6f(%rip),%rdx
mov    %rbx,%rdi
mov    $0xf,%esi
mov    %rax,%rcx
xor    %eax,%eax
call   1030 <snprintf@plt>
jmp    11db <format_k+0xcb>
add    $0x200,%rcx
shr    $0xa,%rcx
mov    $0x67,%r8d
lea    0xe47(%rip),%rdx
mov    %rbx,%rdi
mov    $0xf,%esi
xor    %eax,%eax
call   1030 <snprintf@plt>
mov    %rbx,%rax
pop    %rbx
ret

LLVM IR (-O0):
@format_k.retarray = internal global [8 x [16 x i8]] zeroinitializer
@format_k.index = internal global i32 0
@.str = [7 x i8] c"%.1f%c\00"
@.str.1 = [4 x i8] c"%ld\00"
@.str.2 = [6 x i8] c"%ld%c\00"
define i8* @format_k(i64 %0) {
%2 = alloca i8*
%3 = alloca i64
%4 = alloca i8*
%5 = alloca i8
store i64 %0, i64* %3
store i8 107, i8* %5
%6 = load i32, i32* @format_k.index
%7 = sext i32 %6 to i64
%8 = getelementptr inbounds [8 x [16 x i8]], [8 x [16 x i8]]* @format_k.retarray, i64 0, i64 %7
%9 = getelementptr inbounds [16 x i8], [16 x i8]* %8, i64 0, i64 0
store i8* %9, i8** %4
%10 = load i32, i32* @format_k.index
%11 = add nsw i32 %10, 1
%12 = srem i32 %11, 8
store i32 %12, i32* @format_k.index
%13 = load i64, i64* %3
%14 = icmp sge i64 %13, 100000
br i1 %14, label %15, label %39
15: ; preds = %1
store i8 109, i8* %5
%16 = load i64, i64* %3
%17 = add nsw i64 %16, 512
%18 = sdiv i64 %17, 1024
store i64 %18, i64* %3
%19 = load i64, i64* %3
%20 = icmp sge i64 %19, 10000
br i1 %20, label %21, label %38
21: ; preds = %15
store i8 103, i8* %5
%22 = load i64, i64* %3
%23 = icmp slt i64 %22, 102400
br i1 %23, label %24, label %34
24: ; preds = %21
%25 = load i8*, i8** %4
%26 = load i64, i64* %3
%27 = sitofp i64 %26 to double
%28 = fdiv double %27, 1.024000e+03
%29 = fptosi double %28 to i64
%30 = load i8, i8* %5
%31 = sext i8 %30 to i32
%32 = call i32 (i8*, i32, i8*, i64, ...) @snprintf(i8* %25, i32 15, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i64 %29, i32 %31)
%33 = load i8*, i8** %4
store i8* %33, i8** %2
br label %55
34: ; preds = %21
%35 = load i64, i64* %3
%36 = add nsw i64 %35, 512
%37 = sdiv i64 %36, 1024
store i64 %37, i64* %3
br label %38
38: ; preds = %34, %15
br label %39
39: ; preds = %38, %1
%40 = load i8, i8* %5
%41 = sext i8 %40 to i32
%42 = icmp eq i32 %41, 107
br i1 %42, label %43, label %47
43: ; preds = %39
%44 = load i8*, i8** %4
%45 = load i64, i64* %3
%46 = call i32 (i8*, i32, i8*, i64, ...) @snprintf(i8* %44, i32 15, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i64 %45)
br label %53
47: ; preds = %39
%48 = load i8*, i8** %4
%49 = load i64, i64* %3
%50 = load i8, i8* %5
%51 = sext i8 %50 to i32
%52 = call i32 (i8*, i32, i8*, i64, ...) @snprintf(i8* %48, i32 15, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i64 %49, i32 %51)
br label %53
53: ; preds = %47, %43
%54 = load i8*, i8** %4
store i8* %54, i8** %2
br label %55
55: ; preds = %53, %24
%56 = load i8*, i8** %2
ret i8* %56
}

LLVM IR (-O2):
@format_k.retarray = internal global [8 x [16 x i8]] zeroinitializer
@format_k.index = internal unnamed_addr global i32 0
@.str = [7 x i8] c"%.1f%c\00"
@.str.1 = [4 x i8] c"%ld\00"
@.str.2 = [6 x i8] c"%ld%c\00"
define i8* @format_k(i64 %0) {
%2 = load i32, i32* @format_k.index
%3 = zext i32 %2 to i64
%4 = getelementptr inbounds [8 x [16 x i8]], [8 x [16 x i8]]* @format_k.retarray, i64 0, i64 %3, i64 0
%5 = add nsw i32 %2, 1
%6 = srem i32 %5, 8
store i32 %6, i32* @format_k.index
%7 = icmp sgt i64 %0, 99999
br i1 %7, label %8, label %22
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 %24
12: ; preds = %8
%13 = icmp ult i64 %0, 104857088
br i1 %13, label %14, label %19
14: ; preds = %12
%15 = sitofp i64 %10 to double
%16 = fmul double %15, 0x3F50000000000000
%17 = fptosi double %16 to i64
%18 = tail call i32 (i8*, i32, i8*, i64, ...) @snprintf(i8* %4, i32 15, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i64 %17, i32 103)
br label %28
19: ; preds = %12
%20 = add nuw nsw i64 %10, 512
%21 = lshr i64 %20, 10
br label %24
22: ; preds = %1
%23 = tail call i32 (i8*, i32, i8*, i64, ...) @snprintf(i8* %4, i32 15, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i64 %0)
br label %28
24: ; preds = %19, %8
%25 = phi i64 [ %10, %8 ], [ %21, %19 ]
%26 = phi i32 [ 109, %8 ], [ 103, %19 ]
%27 = tail call i32 (i8*, i32, i8*, i64, ...) @snprintf(i8* %4, i32 15, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i64 %25, i32 %26)
br label %28
28: ; preds = %22, %24, %14
ret i8* %4
}