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

/* Variables and functions */
int /*<<< orphan*/ sprintf(char *, char *, long);
char *commafy(char *s, long n) {
  char *ptr = s;
  long x = n;
  long d = 1;
  while (x >= 1000) {
    x /= 1000;
    d *= 1000;
  }
  ptr += sprintf(ptr, "%ld", x);
  while (d > 1) {
    n %= d;
    d /= 1000;
    x = n / d;
    ptr += sprintf(ptr, ",%03ld", x);
  }
  return s;
}


x86 assembly (clang, -O0):
<commafy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
movq   $0x1,-0x28(%rbp)
cmpq   $0x3e8,-0x20(%rbp)
jl     1169 <commafy+0x59>
mov    -0x20(%rbp),%rax
mov    $0x3e8,%ecx
cqto
idiv   %rcx
mov    %rax,-0x20(%rbp)
imul   $0x3e8,-0x28(%rbp),%rax
mov    %rax,-0x28(%rbp)
jmp    1138 <commafy+0x28>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rdx
lea    0xe88(%rip),%rsi
call   1030 <sprintf@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x18(%rbp)
cmpq   $0x1,-0x28(%rbp)
jle    11f2 <commafy+0xe2>
mov    -0x28(%rbp),%rcx
mov    -0x10(%rbp),%rax
cqto
idiv   %rcx
mov    %rdx,-0x10(%rbp)
mov    -0x28(%rbp),%rax
mov    $0x3e8,%ecx
cqto
idiv   %rcx
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
cqto
idivq  -0x28(%rbp)
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rdx
lea    0xe2c(%rip),%rsi
call   1030 <sprintf@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x18(%rbp)
jmp    118d <commafy+0x7d>
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<commafy>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r15
mov    %rdi,%r14
cmp    $0x3e8,%rsi
mov    %rdi,(%rsp)
jl     1203 <commafy+0xf3>
mov    $0x1,%ebx
movabs $0x20c49ba5e353f7cf,%rbp
9b c4 20
mov    %r15,%rdx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %rdx,%rcx
mov    %rdx,%rax
shr    $0x3,%rax
mul    %rbp
shr    $0x4,%rdx
imul   $0x3e8,%rbx,%rbx
cmp    $0xf423f,%rcx
ja     1150 <commafy+0x40>
lea    0xe88(%rip),%rsi
mov    %r14,%rdi
call   1030 <sprintf@plt>
movslq %eax,%r13
add    %r14,%r13
lea    0xe77(%rip),%r12
jmp    11b7 <commafy+0xa7>
nop
mov    %r15,%rax
cqto
idiv   %r14
mov    %rax,%rdx
mov    %r13,%rdi
mov    %r12,%rsi
call   1030 <sprintf@plt>
cltq
add    %rax,%r13
cmp    $0x7cf,%rbx
mov    %r14,%rbx
jbe    1215 <commafy+0x105>
mov    %r15,%rax
or     %rbx,%rax
shr    $0x20,%rax
je     11d0 <commafy+0xc0>
mov    %r15,%rax
cqto
idiv   %rbx
mov    %rdx,%r15
jmp    11da <commafy+0xca>
mov    %r15d,%eax
xor    %edx,%edx
div    %ebx
mov    %edx,%r15d
mov    %rbx,%rax
shr    $0x3,%rax
mul    %rbp
mov    %rdx,%r14
shr    $0x4,%r14
mov    %r15,%rax
or     %r14,%rax
shr    $0x20,%rax
jne    1190 <commafy+0x80>
mov    %r15d,%eax
xor    %edx,%edx
div    %r14d
mov    %eax,%edx
jmp    119b <commafy+0x8b>
lea    0xdf6(%rip),%rsi
mov    %r14,%rdi
mov    %r15,%rdx
call   1030 <sprintf@plt>
mov    (%rsp),%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [4 x i8] c"%ld\00"
@.str.1 = [7 x i8] c",%03ld\00"
define i8* @commafy(i8* returned writeonly %0, i64 %1) {
%3 = icmp sgt i64 %1, 999
br i1 %3, label %6, label %4
4: ; preds = %2
%5 = tail call i32 @sprintf(i8* dereferenceable(1) %0, i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i64 %1)
br label %27
6: ; preds = %2, %6
%7 = phi i64 [ %10, %6 ], [ 1, %2 ]
%8 = phi i64 [ %9, %6 ], [ %1, %2 ]
%9 = udiv i64 %8, 1000
%10 = mul nsw i64 %7, 1000
%11 = icmp ugt i64 %8, 999999
br i1 %11, label %6, label %12
12: ; preds = %6
%13 = tail call i32 @sprintf(i8* dereferenceable(1) %0, i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i64 %9)
%14 = sext i32 %13 to i64
%15 = getelementptr inbounds i8, i8* %0, i64 %14
br label %16
16: ; preds = %12, %16
%17 = phi i64 [ %21, %16 ], [ %10, %12 ]
%18 = phi i8* [ %25, %16 ], [ %15, %12 ]
%19 = phi i64 [ %20, %16 ], [ %1, %12 ]
%20 = srem i64 %19, %17
%21 = udiv i64 %17, 1000
%22 = sdiv i64 %20, %21
%23 = tail call i32 @sprintf(i8* dereferenceable(1) %18, i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i64 %22)
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds i8, i8* %18, i64 %24
%26 = icmp ugt i64 %17, 1999
br i1 %26, label %16, label %27
27: ; preds = %16, %4
ret i8* %0
}