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

/* Variables and functions */
int /*<<< orphan*/ sprintf(char *, char *, int);
char *comify(int n) {
  static char buffer[16];
  int i = sizeof(buffer) - 1;
  int comma = 0;
  while (1) {
    i -= 4;
    sprintf(&buffer[i], "%03d", n % 1000);
    if (comma)
      buffer[i + 3] = ',';
    comma = 1;
    n /= 1000;
    if (n == 0)
      break;
  }
  while (buffer[i] == '0')
    i++;
  return &buffer[i];
}


x86 assembly (clang, -O0):
<comify>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
movl   $0xf,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
mov    -0x8(%rbp),%eax
sub    $0x4,%eax
mov    %eax,-0x8(%rbp)
movslq -0x8(%rbp),%rax
lea    0x2f03(%rip),%rdi
add    %rax,%rdi
mov    -0x4(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
lea    0xeae(%rip),%rsi
call   1030 <sprintf@plt>
cmpl   $0x0,-0xc(%rbp)
je     1175 <comify+0x65>
mov    -0x8(%rbp),%eax
add    $0x3,%eax
movslq %eax,%rcx
lea    0x2ecf(%rip),%rax
movb   $0x2c,(%rax,%rcx,1)
movl   $0x1,-0xc(%rbp)
mov    -0x4(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    %eax,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
jne    1199 <comify+0x89>
jmp    119e <comify+0x8e>
jmp    1129 <comify+0x19>
jmp    11a3 <comify+0x93>
movslq -0x8(%rbp),%rcx
lea    0x2e92(%rip),%rax
movsbl (%rax,%rcx,1),%eax
cmp    $0x30,%eax
jne    11c9 <comify+0xb9>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    11a3 <comify+0x93>
movslq -0x8(%rbp),%rcx
lea    0x2e6c(%rip),%rax
add    %rcx,%rax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<comify>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edi,%r15d
movslq %edi,%rbx
imul   $0x10624dd3,%rbx,%rax
mov    %rax,%rcx
shr    $0x3f,%rcx
sar    $0x26,%rax
add    %ecx,%eax
imul   $0x3e8,%eax,%eax
mov    %ebx,%edx
sub    %eax,%edx
lea    0x2f05(%rip),%rdi
lea    0xeb3(%rip),%rsi
call   1030 <sprintf@plt>
add    $0x3e7,%ebx
mov    $0xb,%r13d
cmp    $0x7cf,%ebx
jb     11f6 <comify+0xe6>
lea    0x2ed6(%rip),%rbx
movabs $0xb00000000,%r13
movabs $0xfffffffc00000000,%r12
ff ff ff
cs nopw 0x0(%rax,%rax,1)
nop
add    %r12,%r13
movslq %r15d,%rax
imul   $0x10624dd3,%rax,%rbp
mov    %rbp,%r14
shr    $0x3f,%r14
sar    $0x26,%rbp
lea    (%r14,%rbp,1),%r15d
movslq %r15d,%rdx
imul   $0x10624dd3,%rdx,%rax
mov    %rax,%rcx
shr    $0x3f,%rcx
sar    $0x26,%rax
add    %ecx,%eax
imul   $0x3e8,%eax,%eax
sub    %eax,%edx
mov    %rbx,%rdi
lea    0xe2b(%rip),%rsi
call   1030 <sprintf@plt>
movb   $0x2c,0x3(%rbx)
lea    (%r14,%rbp,1),%eax
add    $0x3e7,%eax
add    $0xfffffffffffffffc,%rbx
cmp    $0x7ce,%eax
ja     1190 <comify+0x80>
sar    $0x20,%r13
lea    0x2e43(%rip),%rax
add    %r13,%rax
add    $0xffffffffffffffff,%rax
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
cmpb   $0x30,0x1(%rax)
lea    0x1(%rax),%rax
je     1210 <comify+0x100>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@comify.buffer = internal global [16 x i8] zeroinitializer
@.str = [5 x i8] c"%03d\00"
define i8* @comify(i32 %0) {
%2 = srem i32 %0, 1000
%3 = tail call i32 @sprintf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @comify.buffer, i64 0, i64 11), i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %2)
%4 = add i32 %0, 999
%5 = icmp ult i32 %4, 1999
br i1 %5, label %21, label %6
6: ; preds = %1, %6
%7 = phi i64 [ %10, %6 ], [ 11, %1 ]
%8 = phi i32 [ %9, %6 ], [ %0, %1 ]
%9 = sdiv i32 %8, 1000
%10 = add nsw i64 %7, -4
%11 = getelementptr inbounds [16 x i8], [16 x i8]* @comify.buffer, i64 0, i64 %10
%12 = srem i32 %9, 1000
%13 = tail call i32 @sprintf(i8* %11, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %12)
%14 = add nsw i64 %7, -1
%15 = getelementptr inbounds [16 x i8], [16 x i8]* @comify.buffer, i64 0, i64 %14
store i8 44, i8* %15
%16 = add nsw i32 %9, 999
%17 = icmp ult i32 %16, 1999
br i1 %17, label %18, label %6
18: ; preds = %6
%19 = shl i64 %10, 32
%20 = ashr exact i64 %19, 32
br label %21
21: ; preds = %18, %1
%22 = phi i64 [ %20, %18 ], [ 11, %1 ]
br label %23
23: ; preds = %21, %23
%24 = phi i64 [ %28, %23 ], [ %22, %21 ]
%25 = getelementptr inbounds [16 x i8], [16 x i8]* @comify.buffer, i64 0, i64 %24
%26 = load i8, i8* %25
%27 = icmp eq i8 %26, 48
%28 = add i64 %24, 1
br i1 %27, label %23, label %29
29: ; preds = %23
%30 = getelementptr inbounds [16 x i8], [16 x i8]* @comify.buffer, i64 0, i64 %24
ret i8* %30
}