char *comma(unsigned int u) {
  static char buf[5][20];
  static int bi = 0;
  int comma = 0;
  char *bp;
  bi = (bi + 1) % 5;
  bp = buf[bi] + 19;
  *--bp = 0;
  do {
    if (comma == 3) {
      *--bp = ',';
      comma = 0;
    }
    comma++;
    *--bp = '0' + (u % 10);
    u /= 10;
  } while (u);
  return bp;
}


x86 assembly (clang, -O0):
<comma>:
push   %rbp
mov    %rsp,%rbp
mov    %edi,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
mov    0x2f80(%rip),%eax
add    $0x1,%eax
mov    $0x5,%ecx
cltd
idiv   %ecx
mov    %edx,0x2f6f(%rip)
movslq 0x2f68(%rip),%rcx
lea    0x2efd(%rip),%rax
imul   $0x14,%rcx,%rcx
add    %rcx,%rax
add    $0x13,%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0xffffffffffffffff,%rcx
mov    %rcx,-0x10(%rbp)
movb   $0x0,-0x1(%rax)
cmpl   $0x3,-0x8(%rbp)
jne    1179 <comma+0x79>
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0xffffffffffffffff,%rcx
mov    %rcx,-0x10(%rbp)
movb   $0x2c,-0x1(%rax)
movl   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
mov    -0x4(%rbp),%eax
mov    $0xa,%ecx
xor    %edx,%edx
div    %ecx
add    $0x30,%edx
mov    %dl,%cl
mov    -0x10(%rbp),%rax
mov    %rax,%rdx
add    $0xffffffffffffffff,%rdx
mov    %rdx,-0x10(%rbp)
mov    %cl,-0x1(%rax)
mov    -0x4(%rbp),%eax
mov    $0xa,%ecx
xor    %edx,%edx
div    %ecx
mov    %eax,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
jne    1155 <comma+0x55>
mov    -0x10(%rbp),%rax
pop    %rbp
ret

x86 assembly (clang, -O2):
<comma>:
mov    0x2f8e(%rip),%eax
mov    %eax,%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
imul   $0x66666667,%rcx,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x21,%rcx
add    %edx,%ecx
lea    (%rcx,%rcx,4),%ecx
neg    %ecx
add    %ecx,%eax
add    $0x1,%eax
mov    %eax,0x2f62(%rip)
lea    (%rax,%rax,4),%rcx
lea    0x2ef3(%rip),%rdx
lea    (%rdx,%rcx,4),%rax
add    $0x12,%rax
movb   $0x0,0x12(%rdx,%rcx,4)
xor    %edx,%edx
mov    $0xcccccccd,%r8d
jmp    118a <comma+0x8a>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
add    $0x1,%edx
mov    %edi,%esi
imul   %r8,%rsi
shr    $0x23,%rsi
lea    (%rsi,%rsi,1),%ecx
lea    (%rcx,%rcx,4),%r9d
mov    %edi,%ecx
sub    %r9d,%ecx
or     $0x30,%cl
mov    %cl,-0x1(%rax)
add    $0xffffffffffffffff,%rax
cmp    $0x9,%edi
mov    %esi,%edi
jbe    119b <comma+0x9b>
cmp    $0x3,%edx
jne    1160 <comma+0x60>
movb   $0x2c,-0x1(%rax)
add    $0xffffffffffffffff,%rax
xor    %edx,%edx
jmp    1160 <comma+0x60>
ret

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

LLVM IR (-O2):
@comma.buf = internal global [5 x [20 x i8]] zeroinitializer
@comma.bi = internal unnamed_addr global i32 0
define i8* @comma(i32 %0) {
%2 = load i32, i32* @comma.bi
%3 = add nsw i32 %2, 1
%4 = srem i32 %3, 5
store i32 %4, i32* @comma.bi
%5 = zext i32 %4 to i64
%6 = getelementptr inbounds [5 x [20 x i8]], [5 x [20 x i8]]* @comma.buf, i64 0, i64 %5, i64 18
store i8 0, i8* %6
br label %7
7: ; preds = %14, %1
%8 = phi i32 [ %0, %1 ], [ %22, %14 ]
%9 = phi i32 [ 0, %1 ], [ %17, %14 ]
%10 = phi i8* [ %6, %1 ], [ %21, %14 ]
%11 = icmp eq i32 %9, 3
br i1 %11, label %12, label %14
12: ; preds = %7
%13 = getelementptr inbounds i8, i8* %10, i64 -1
store i8 44, i8* %13
br label %14
14: ; preds = %12, %7
%15 = phi i32 [ 0, %12 ], [ %9, %7 ]
%16 = phi i8* [ %13, %12 ], [ %10, %7 ]
%17 = add nsw i32 %15, 1
%18 = urem i32 %8, 10
%19 = trunc i32 %18 to i8
%20 = or i8 %19, 48
%21 = getelementptr inbounds i8, i8* %16, i64 -1
store i8 %20, i8* %21
%22 = udiv i32 %8, 10
%23 = icmp ult i32 %8, 10
br i1 %23, label %24, label %7
24: ; preds = %14
ret i8* %21
}