char *commafy(long n) {
  static char buffer[64];
  int i = sizeof(buffer);
  buffer[--i] = 0;
  int count = 0;
  while (1) {
    buffer[--i] = (n % 10) + '0';
    n /= 10;
    if (n == 0)
      break;
    count++;
    if (count == 3) {
      buffer[--i] = ',';
      count = 0;
    }
  }
  return &buffer[i];
}


x86 assembly (clang, -O0):
<commafy>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
movl   $0x40,-0xc(%rbp)
mov    -0xc(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0xc(%rbp)
movslq %eax,%rcx
lea    0x2f0e(%rip),%rax
movb   $0x0,(%rax,%rcx,1)
movl   $0x0,-0x10(%rbp)
mov    -0x8(%rbp),%rax
mov    $0xa,%ecx
cqto
idiv   %rcx
add    $0x30,%rdx
mov    -0xc(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0xc(%rbp)
movslq %eax,%rcx
lea    0x2ede(%rip),%rax
mov    %dl,(%rax,%rcx,1)
mov    -0x8(%rbp),%rax
mov    $0xa,%ecx
cqto
idiv   %rcx
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    1177 <commafy+0x77>
jmp    11ad <commafy+0xad>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
cmpl   $0x3,-0x10(%rbp)
jne    11a8 <commafy+0xa8>
mov    -0xc(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0xc(%rbp)
movslq %eax,%rcx
lea    0x2e93(%rip),%rax
movb   $0x2c,(%rax,%rcx,1)
movl   $0x0,-0x10(%rbp)
jmp    112d <commafy+0x2d>
movslq -0xc(%rbp),%rcx
lea    0x2e78(%rip),%rax
add    %rcx,%rax
pop    %rbp
ret

x86 assembly (clang, -O2):
<commafy>:
movabs $0x6666666666666667,%r9
66 66 66
mov    %rdi,%rax
imul   %r9
movb   $0x0,0x2f58(%rip)
mov    %rdx,%rax
shr    $0x3f,%rax
shr    $0x2,%edx
add    %eax,%edx
add    %edx,%edx
lea    (%rdx,%rdx,4),%eax
mov    %edi,%ecx
sub    %eax,%ecx
add    $0x30,%cl
mov    %cl,0x2f39(%rip)
lea    0x9(%rdi),%rax
mov    $0x3e,%esi
cmp    $0x13,%rax
jae    114f <commafy+0x4f>
lea    0x2ee5(%rip),%rax
add    %rsi,%rax
ret
mov    $0x3f,%ecx
xor    %r10d,%r10d
mov    $0x3e,%esi
lea    0x2ecd(%rip),%r8
jmp    11a6 <commafy+0xa6>
cs nopw 0x0(%rax,%rax,1)
nop
mov    %esi,%ecx
mov    %rdi,%rax
imul   %r9
mov    %rdx,%rax
shr    $0x3f,%rax
shr    $0x2,%edx
add    %eax,%edx
add    %edx,%edx
lea    (%rdx,%rdx,4),%eax
mov    %edi,%edx
sub    %eax,%edx
add    $0x30,%dl
movslq %esi,%rsi
mov    %dl,-0x1(%rsi,%r8,1)
add    $0xffffffffffffffff,%rsi
lea    0x9(%rdi),%rax
cmp    $0x12,%rax
jbe    1144 <commafy+0x44>
mov    %rdi,%rax
imul   %r9
mov    %rdx,%rdi
mov    %rdx,%rax
shr    $0x3f,%rax
sar    $0x2,%rdi
add    %rax,%rdi
add    $0x1,%r10d
cmp    $0x3,%r10d
jne    1170 <commafy+0x70>
movslq %ecx,%rsi
movb   $0x2c,-0x2(%rsi,%r8,1)
add    $0xfffffffffffffffe,%rsi
xor    %r10d,%r10d
jmp    1170 <commafy+0x70>

LLVM IR (-O0):
@commafy.buffer = internal global [64 x i8] zeroinitializer
define i8* @commafy(i64 %0) {
%2 = alloca i64
%3 = alloca i32
%4 = alloca i32
store i64 %0, i64* %2
store i32 64, i32* %3
%5 = load i32, i32* %3
%6 = add nsw i32 %5, -1
store i32 %6, i32* %3
%7 = sext i32 %6 to i64
%8 = getelementptr inbounds [64 x i8], [64 x i8]* @commafy.buffer, i64 0, i64 %7
store i8 0, i8* %8
store i32 0, i32* %4
br label %9
9: ; preds = %1, %33
%10 = load i64, i64* %2
%11 = srem i64 %10, 10
%12 = add nsw i64 %11, 48
%13 = trunc i64 %12 to i8
%14 = load i32, i32* %3
%15 = add nsw i32 %14, -1
store i32 %15, i32* %3
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds [64 x i8], [64 x i8]* @commafy.buffer, i64 0, i64 %16
store i8 %13, i8* %17
%18 = load i64, i64* %2
%19 = sdiv i64 %18, 10
store i64 %19, i64* %2
%20 = load i64, i64* %2
%21 = icmp eq i64 %20, 0
br i1 %21, label %22, label %23
22: ; preds = %9
br label %34
23: ; preds = %9
%24 = load i32, i32* %4
%25 = add nsw i32 %24, 1
store i32 %25, i32* %4
%26 = load i32, i32* %4
%27 = icmp eq i32 %26, 3
br i1 %27, label %28, label %33
28: ; preds = %23
%29 = load i32, i32* %3
%30 = add nsw i32 %29, -1
store i32 %30, i32* %3
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds [64 x i8], [64 x i8]* @commafy.buffer, i64 0, i64 %31
store i8 44, i8* %32
store i32 0, i32* %4
br label %33
33: ; preds = %28, %23
br label %9
34: ; preds = %22
%35 = load i32, i32* %3
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds [64 x i8], [64 x i8]* @commafy.buffer, i64 0, i64 %36
ret i8* %37
}

LLVM IR (-O2):
@commafy.buffer = internal global [64 x i8] zeroinitializer
define i8* @commafy(i64 %0) {
store i8 0, i8* getelementptr inbounds ([64 x i8], [64 x i8]* @commafy.buffer, i64 0, i64 63)
%2 = srem i64 %0, 10
%3 = trunc i64 %2 to i8
%4 = add nsw i8 %3, 48
store i8 %4, i8* getelementptr inbounds ([64 x i8], [64 x i8]* @commafy.buffer, i64 0, i64 62)
%5 = add i64 %0, 9
%6 = icmp ult i64 %5, 19
br i1 %6, label %30, label %7
7: ; preds = %1, %19
%8 = phi i64 [ %12, %19 ], [ %0, %1 ]
%9 = phi i32 [ %25, %19 ], [ 62, %1 ]
%10 = phi i32 [ %21, %19 ], [ 0, %1 ]
%11 = phi i32 [ %20, %19 ], [ 63, %1 ]
%12 = sdiv i64 %8, 10
%13 = add nsw i32 %10, 1
%14 = icmp eq i32 %13, 3
br i1 %14, label %15, label %19
15: ; preds = %7
%16 = add nsw i32 %11, -2
%17 = sext i32 %16 to i64
%18 = getelementptr inbounds [64 x i8], [64 x i8]* @commafy.buffer, i64 0, i64 %17
store i8 44, i8* %18
br label %19
19: ; preds = %15, %7
%20 = phi i32 [ %16, %15 ], [ %9, %7 ]
%21 = phi i32 [ 0, %15 ], [ %13, %7 ]
%22 = srem i64 %12, 10
%23 = trunc i64 %22 to i8
%24 = add nsw i8 %23, 48
%25 = add nsw i32 %20, -1
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds [64 x i8], [64 x i8]* @commafy.buffer, i64 0, i64 %26
store i8 %24, i8* %27
%28 = add nsw i64 %12, 9
%29 = icmp ult i64 %28, 19
br i1 %29, label %30, label %7
30: ; preds = %19, %1
%31 = phi i64 [ 62, %1 ], [ %26, %19 ]
%32 = getelementptr inbounds [64 x i8], [64 x i8]* @commafy.buffer, i64 0, i64 %31
ret i8* %32
}