#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*/ str;

/* Variables and functions */
int labs(long);
long sgn_l(long);
int /*<<< orphan*/ sn_printf(char *, int, char *, long);
char *fmt_num(long n) {
  long sg;
  static char str[32 + 1];
  long max_len;
  char *head;
  char str_tmp[4 + 1];
  long i;
  sg = sgn_l(n);
  max_len = (sizeof(str) / sizeof(char)) - 1;
  head = &(str[max_len]);
  *head = '\0';
  head--;
  for (i = 0; i < max_len; i++) {
    long n1;
    n1 = labs(n) % 10;
    sn_printf(str_tmp, 4, "%1ld", (long)n1);
    head--;
    *head = str_tmp[0];
    n /= 10;
    if (n == 0)
      break;
    if ((i % 3) == 2) {
      head--;
      *head = ',';
    }
  }
  if (sg <= -1) {
    head--;
    *head = '-';
  }
  return head;
}


x86 assembly (clang, -O0):
<fmt_num>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <sgn_l@plt>
mov    %rax,-0x10(%rbp)
movq   $0x20,-0x18(%rbp)
lea    0x2ef8(%rip),%rax
add    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x20(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x20(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%rax
cmp    -0x18(%rbp),%rax
jge    1232 <fmt_num+0x102>
mov    -0x8(%rbp),%rdi
call   1030 <labs@plt>
mov    $0xa,%ecx
cltd
idiv   %ecx
movslq %edx,%rax
mov    %rax,-0x38(%rbp)
lea    -0x25(%rbp),%rdi
mov    -0x38(%rbp),%rcx
mov    $0x4,%esi
lea    0xe4b(%rip),%rdx
call   1040 <sn_printf@plt>
mov    -0x20(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x20(%rbp)
mov    -0x25(%rbp),%cl
mov    -0x20(%rbp),%rax
mov    %cl,(%rax)
mov    -0x8(%rbp),%rax
mov    $0xa,%ecx
cqto
idiv   %rcx
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    11f1 <fmt_num+0xc1>
jmp    1232 <fmt_num+0x102>
mov    -0x30(%rbp),%rax
mov    $0x3,%ecx
cqto
idiv   %rcx
cmp    $0x2,%rdx
jne    121c <fmt_num+0xec>
mov    -0x20(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
movb   $0x2c,(%rax)
jmp    1221 <fmt_num+0xf1>
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
jmp    117b <fmt_num+0x4b>
cmpq   $0xffffffffffffffff,-0x10(%rbp)
jg     1250 <fmt_num+0x120>
mov    -0x20(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
movb   $0x2d,(%rax)
mov    -0x20(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fmt_num>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%rbx
call   1050 <sgn_l@plt>
mov    %rax,0x10(%rsp)
movb   $0x0,0x2f1e(%rip)
lea    0x2f16(%rip),%r14
xor    %r13d,%r13d
lea    0xe9d(%rip),%r15
lea    0xb(%rsp),%r12
movabs $0x6666666666666667,%rbp
66 66 66
jmp    1197 <fmt_num+0x67>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
add    %rax,%rdx
add    $0x1,%r13
mov    %rdx,%rbx
mov    %rcx,%r14
cmp    $0x20,%r13
je     1226 <fmt_num+0xf6>
mov    %rbx,%rdi
call   1030 <labs@plt>
cltq
imul   $0x66666667,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x22,%rcx
add    %edx,%ecx
add    %ecx,%ecx
lea    (%rcx,%rcx,4),%ecx
sub    %ecx,%eax
movslq %eax,%rcx
mov    %r12,%rdi
mov    $0x4,%esi
mov    %r15,%rdx
call   1040 <sn_printf@plt>
lea    -0x1(%r14),%rcx
movzbl 0xb(%rsp),%eax
mov    %al,-0x1(%r14)
mov    %rbx,%rax
imul   %rbp
add    $0x9,%rbx
cmp    $0x13,%rbx
jb     1226 <fmt_num+0xf6>
movzbl %r13b,%eax
imul   $0xab,%eax,%eax
shr    $0x9,%eax
lea    (%rax,%rax,2),%eax
add    $0x2,%al
movzbl %al,%esi
mov    %rdx,%rax
shr    $0x3f,%rax
sar    $0x2,%rdx
cmp    %r13b,%sil
jne    1180 <fmt_num+0x50>
movb   $0x2c,-0x2(%r14)
add    $0xfffffffffffffffe,%r14
mov    %r14,%rcx
jmp    1180 <fmt_num+0x50>
cmpq   $0x0,0x10(%rsp)
jns    1236 <fmt_num+0x106>
movb   $0x2d,-0x1(%rcx)
add    $0xffffffffffffffff,%rcx
mov    %rcx,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@fmt_num.str = internal global [33 x i8] zeroinitializer
@.str = [5 x i8] c"%1ld\00"
define i8* @fmt_num(i64 %0) {
%2 = alloca i64
%3 = alloca i64
%4 = alloca i64
%5 = alloca i8*
%6 = alloca [5 x i8]
%7 = alloca i64
%8 = alloca i64
store i64 %0, i64* %2
%9 = load i64, i64* %2
%10 = call i64 @sgn_l(i64 %9)
store i64 %10, i64* %3
store i64 32, i64* %4
%11 = load i64, i64* %4
%12 = getelementptr inbounds [33 x i8], [33 x i8]* @fmt_num.str, i64 0, i64 %11
store i8* %12, i8** %5
%13 = load i8*, i8** %5
store i8 0, i8* %13
%14 = load i8*, i8** %5
%15 = getelementptr inbounds i8, i8* %14, i32 -1
store i8* %15, i8** %5
store i64 0, i64* %7
br label %16
16: ; preds = %47, %1
%17 = load i64, i64* %7
%18 = load i64, i64* %4
%19 = icmp slt i64 %17, %18
br i1 %19, label %20, label %50
20: ; preds = %16
%21 = load i64, i64* %2
%22 = call i32 @labs(i64 %21)
%23 = srem i32 %22, 10
%24 = sext i32 %23 to i64
store i64 %24, i64* %8
%25 = getelementptr inbounds [5 x i8], [5 x i8]* %6, i64 0, i64 0
%26 = load i64, i64* %8
%27 = call i32 @sn_printf(i8* %25, i32 4, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i64 %26)
%28 = load i8*, i8** %5
%29 = getelementptr inbounds i8, i8* %28, i32 -1
store i8* %29, i8** %5
%30 = getelementptr inbounds [5 x i8], [5 x i8]* %6, i64 0, i64 0
%31 = load i8, i8* %30
%32 = load i8*, i8** %5
store i8 %31, i8* %32
%33 = load i64, i64* %2
%34 = sdiv i64 %33, 10
store i64 %34, i64* %2
%35 = load i64, i64* %2
%36 = icmp eq i64 %35, 0
br i1 %36, label %37, label %38
37: ; preds = %20
br label %50
38: ; preds = %20
%39 = load i64, i64* %7
%40 = srem i64 %39, 3
%41 = icmp eq i64 %40, 2
br i1 %41, label %42, label %46
42: ; preds = %38
%43 = load i8*, i8** %5
%44 = getelementptr inbounds i8, i8* %43, i32 -1
store i8* %44, i8** %5
%45 = load i8*, i8** %5
store i8 44, i8* %45
br label %46
46: ; preds = %42, %38
br label %47
47: ; preds = %46
%48 = load i64, i64* %7
%49 = add nsw i64 %48, 1
store i64 %49, i64* %7
br label %16
50: ; preds = %37, %16
%51 = load i64, i64* %3
%52 = icmp sle i64 %51, -1
br i1 %52, label %53, label %57
53: ; preds = %50
%54 = load i8*, i8** %5
%55 = getelementptr inbounds i8, i8* %54, i32 -1
store i8* %55, i8** %5
%56 = load i8*, i8** %5
store i8 45, i8* %56
br label %57
57: ; preds = %53, %50
%58 = load i8*, i8** %5
ret i8* %58
}

LLVM IR (-O2):
@fmt_num.str = internal global [33 x i8] zeroinitializer
@.str = [5 x i8] c"%1ld\00"
define i8* @fmt_num(i64 %0) {
%2 = alloca [5 x i8]
%3 = getelementptr inbounds [5 x i8], [5 x i8]* %2, i64 0, i64 0
%4 = tail call i64 @sgn_l(i64 %0)
store i8 0, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @fmt_num.str, i64 0, i64 32)
br label %5
5: ; preds = %1, %24
%6 = phi i64 [ %0, %1 ], [ %15, %24 ]
%7 = phi i64 [ 0, %1 ], [ %26, %24 ]
%8 = phi i8* [ getelementptr inbounds ([33 x i8], [33 x i8]* @fmt_num.str, i64 0, i64 31), %1 ], [ %25, %24 ]
%9 = call i32 @labs(i64 %6)
%10 = srem i32 %9, 10
%11 = sext i32 %10 to i64
%12 = call i32 @sn_printf(i8* %3, i32 4, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i64 %11)
%13 = getelementptr inbounds i8, i8* %8, i64 -1
%14 = load i8, i8* %3
store i8 %14, i8* %13
%15 = sdiv i64 %6, 10
%16 = add i64 %6, 9
%17 = icmp ult i64 %16, 19
br i1 %17, label %28, label %18
18: ; preds = %5
%19 = trunc i64 %7 to i8
%20 = urem i8 %19, 3
%21 = icmp eq i8 %20, 2
br i1 %21, label %22, label %24
22: ; preds = %18
%23 = getelementptr inbounds i8, i8* %8, i64 -2
store i8 44, i8* %23
br label %24
24: ; preds = %22, %18
%25 = phi i8* [ %13, %18 ], [ %23, %22 ]
%26 = add nuw nsw i64 %7, 1
%27 = icmp eq i64 %26, 32
br i1 %27, label %28, label %5
28: ; preds = %5, %24
%29 = phi i8* [ %25, %24 ], [ %13, %5 ]
%30 = icmp slt i64 %4, 0
br i1 %30, label %31, label %33
31: ; preds = %28
%32 = getelementptr inbounds i8, i8* %29, i64 -1
store i8 45, i8* %32
br label %33
33: ; preds = %31, %28
%34 = phi i8* [ %32, %31 ], [ %29, %28 ]
ret i8* %34
}