#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 sprintf(char *, char *, unsigned int);
void score_format(unsigned int score, char *buf) {
  unsigned int n = 0;
  unsigned int scale = 1;
  while (score >= 1000) {
    n = n + scale * (score % 1000);
    score /= 1000;
    scale *= 1000;
  }
  int len = sprintf(buf, "%u", score);
  while (scale != 1) {
    scale /= 1000;
    score = n / scale;
    n = n % scale;
    len += sprintf(buf + len, ",%03u", score);
  }
}


x86 assembly (clang, -O0):
<score_format>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x1,-0x18(%rbp)
cmpl   $0x3e8,-0x4(%rbp)
jb     1178 <score_format+0x68>
mov    -0x14(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%ecx
mov    -0x4(%rbp),%eax
mov    $0x3e8,%esi
xor    %edx,%edx
div    %esi
mov    -0x20(%rbp),%eax
imul   %edx,%ecx
add    %ecx,%eax
mov    %eax,-0x14(%rbp)
mov    -0x4(%rbp),%eax
mov    $0x3e8,%ecx
xor    %edx,%edx
div    %ecx
mov    %eax,-0x4(%rbp)
imul   $0x3e8,-0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
jmp    112d <score_format+0x1d>
mov    -0x10(%rbp),%rdi
mov    -0x4(%rbp),%edx
lea    0xe7a(%rip),%rsi
call   1030 <sprintf@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x1,-0x18(%rbp)
je     11e2 <score_format+0xd2>
mov    -0x18(%rbp),%eax
mov    $0x3e8,%ecx
xor    %edx,%edx
div    %ecx
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%eax
xor    %edx,%edx
divl   -0x18(%rbp)
mov    %eax,-0x4(%rbp)
mov    -0x14(%rbp),%eax
xor    %edx,%edx
divl   -0x18(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
movslq -0x1c(%rbp),%rax
add    %rax,%rdi
mov    -0x4(%rbp),%edx
lea    0xe31(%rip),%rsi
call   1030 <sprintf@plt>
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
jmp    118e <score_format+0x7e>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<score_format>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %edi,%edx
cmp    $0x3e8,%edi
jb     11db <score_format+0xcb>
xor    %r12d,%r12d
mov    $0x1,%ebp
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %edx,%eax
mov    %edx,%ecx
imul   $0x10624dd3,%rcx,%rdx
shr    $0x26,%rdx
imul   $0x3e8,%edx,%ecx
mov    %eax,%esi
sub    %ecx,%esi
imul   %ebp,%esi
add    %esi,%r12d
imul   $0x3e8,%ebp,%ebp
cmp    $0xf423f,%eax
ja     1140 <score_format+0x30>
lea    0xe8d(%rip),%rsi
mov    %r14,%rdi
call   1030 <sprintf@plt>
mov    %eax,%ebx
lea    0xe7f(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %ebp,%eax
imul   $0x10624dd3,%rax,%r13
shr    $0x26,%r13
mov    %r12d,%eax
xor    %edx,%edx
div    %r13d
movslq %ebx,%rbx
lea    (%r14,%rbx,1),%rdi
mov    %edx,%r12d
mov    %r15,%rsi
mov    %eax,%edx
call   1030 <sprintf@plt>
add    %eax,%ebx
add    $0xfffffc18,%ebp
cmp    $0x3e7,%ebp
mov    %r13d,%ebp
ja     1190 <score_format+0x80>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe1e(%rip),%rsi
mov    %r14,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <sprintf@plt>

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

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