#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 long, ...);
void sprintLongWithCommas(char *s, long long l) {
  long long billions, millions, thousands;
  if (l >= 1000000000) {
    billions = l / 1000000000;
    l -= billions * 1000000000;
    millions = l / 1000000;
    l -= millions * 1000000;
    thousands = l / 1000;
    l -= thousands * 1000;
    sprintf(s, "%lld,%03lld,%03lld,%03lld", billions, millions, thousands, l);
  } else if (l >= 1000000) {
    millions = l / 1000000;
    l -= millions * (long long)1000000;
    thousands = l / 1000;
    l -= thousands * 1000;
    sprintf(s, "%lld,%03lld,%03lld", millions, thousands, l);
  } else if (l >= 1000) {
    thousands = l / 1000;
    l -= thousands * 1000;
    sprintf(s, "%lld,%03lld", thousands, l);
  } else
    sprintf(s, "%lld", l);
}


x86 assembly (clang, -O0):
<sprintLongWithCommas>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpq   $0x3b9aca00,-0x10(%rbp)
3b
jl     11c4 <sprintLongWithCommas+0xb4>
mov    -0x10(%rbp),%rax
mov    $0x3b9aca00,%ecx
cqto
idiv   %rcx
mov    %rax,-0x18(%rbp)
imul   $0x3b9aca00,-0x18(%rbp),%rcx
3b
mov    -0x10(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    $0xf4240,%ecx
cqto
idiv   %rcx
mov    %rax,-0x20(%rbp)
imul   $0xf4240,-0x20(%rbp),%rcx
mov    -0x10(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    $0x3e8,%ecx
cqto
idiv   %rcx
mov    %rax,-0x28(%rbp)
imul   $0x3e8,-0x28(%rbp),%rcx
mov    -0x10(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
mov    -0x28(%rbp),%r8
mov    -0x10(%rbp),%r9
lea    0xe48(%rip),%rsi
mov    $0x0,%al
call   1030 <sprintf@plt>
jmp    12b1 <sprintLongWithCommas+0x1a1>
cmpq   $0xf4240,-0x10(%rbp)
jl     123f <sprintLongWithCommas+0x12f>
mov    -0x10(%rbp),%rax
mov    $0xf4240,%ecx
cqto
idiv   %rcx
mov    %rax,-0x20(%rbp)
imul   $0xf4240,-0x20(%rbp),%rcx
mov    -0x10(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    $0x3e8,%ecx
cqto
idiv   %rcx
mov    %rax,-0x28(%rbp)
imul   $0x3e8,-0x28(%rbp),%rcx
mov    -0x10(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    -0x28(%rbp),%rcx
mov    -0x10(%rbp),%r8
lea    0xde7(%rip),%rsi
mov    $0x0,%al
call   1030 <sprintf@plt>
jmp    12ac <sprintLongWithCommas+0x19c>
cmpq   $0x3e8,-0x10(%rbp)
jl     1291 <sprintLongWithCommas+0x181>
mov    -0x10(%rbp),%rax
mov    $0x3e8,%ecx
cqto
idiv   %rcx
mov    %rax,-0x28(%rbp)
imul   $0x3e8,-0x28(%rbp),%rcx
mov    -0x10(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x28(%rbp),%rdx
mov    -0x10(%rbp),%rcx
lea    0xda8(%rip),%rsi
mov    $0x0,%al
call   1030 <sprintf@plt>
jmp    12a7 <sprintLongWithCommas+0x197>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rdx
lea    0xd99(%rip),%rsi
mov    $0x0,%al
call   1030 <sprintf@plt>
jmp    12ac <sprintLongWithCommas+0x19c>
jmp    12b1 <sprintLongWithCommas+0x1a1>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sprintLongWithCommas>:
mov    %rsi,%r8
cmp    $0x3b9aca00,%rsi
jl     11a6 <sprintLongWithCommas+0x96>
mov    %r8,%rax
shr    $0x9,%rax
movabs $0x44b82fa09b5a53,%rcx
mul    %rcx
mov    %rdx,%r10
shr    $0xb,%r10
imul   $0xffffffffc4653600,%r10,%rsi
add    %r8,%rsi
movabs $0x431bde82d7b634db,%rcx
de 1b 43
mov    %rsi,%rax
imul   %rcx
mov    %rdx,%rcx
shr    $0x3f,%rcx
sar    $0x12,%rdx
add    %rdx,%rcx
imul   $0xfffffffffff0bdc0,%rcx,%r11
add    %rsi,%r11
movabs $0x20c49ba5e353f7cf,%rdx
9b c4 20
mov    %r11,%rax
imul   %rdx
mov    %rdx,%r8
shr    $0x3f,%r8
sar    $0x7,%rdx
add    %rdx,%r8
imul   $0xfffffffffffffc18,%r8,%r9
add    %r11,%r9
lea    0xe64(%rip),%rsi
mov    %r10,%rdx
xor    %eax,%eax
jmp    1030 <sprintf@plt>
cmp    $0xf4240,%r8
jl     11f9 <sprintLongWithCommas+0xe9>
mov    %r8d,%eax
imul   $0x431bde83,%rax,%rdx
shr    $0x32,%rdx
imul   $0xfffffffffff0bdc0,%rdx,%rax
add    %r8,%rax
movslq %eax,%rcx
imul   $0x10624dd3,%rcx,%rcx
mov    %rcx,%rsi
shr    $0x3f,%rsi
sar    $0x26,%rcx
add    %esi,%ecx
movslq %ecx,%rcx
imul   $0xfffffffffffffc18,%rcx,%r8
add    %rax,%r8
lea    0xe28(%rip),%rsi
xor    %eax,%eax
jmp    1030 <sprintf@plt>
cmp    $0x3e8,%r8
jl     1228 <sprintLongWithCommas+0x118>
mov    %r8d,%eax
imul   $0x10624dd3,%rax,%rdx
shr    $0x26,%rdx
imul   $0xfffffffffffffc18,%rdx,%rcx
add    %r8,%rcx
lea    0xe0c(%rip),%rsi
xor    %eax,%eax
jmp    1030 <sprintf@plt>
lea    0xe0a(%rip),%rsi
mov    %r8,%rdx
xor    %eax,%eax
jmp    1030 <sprintf@plt>

LLVM IR (-O0):
@.str = [26 x i8] c"%lld,%03lld,%03lld,%03lld\00"
@.str.1 = [19 x i8] c"%lld,%03lld,%03lld\00"
@.str.2 = [12 x i8] c"%lld,%03lld\00"
@.str.3 = [5 x i8] c"%lld\00"
define void @sprintLongWithCommas(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i64
%6 = alloca i64
%7 = alloca i64
store i8* %0, i8** %3
store i64 %1, i64* %4
%8 = load i64, i64* %4
%9 = icmp sge i64 %8, 1000000000
br i1 %9, label %10, label %35
10: ; preds = %2
%11 = load i64, i64* %4
%12 = sdiv i64 %11, 1000000000
store i64 %12, i64* %5
%13 = load i64, i64* %5
%14 = mul nsw i64 %13, 1000000000
%15 = load i64, i64* %4
%16 = sub nsw i64 %15, %14
store i64 %16, i64* %4
%17 = load i64, i64* %4
%18 = sdiv i64 %17, 1000000
store i64 %18, i64* %6
%19 = load i64, i64* %6
%20 = mul nsw i64 %19, 1000000
%21 = load i64, i64* %4
%22 = sub nsw i64 %21, %20
store i64 %22, i64* %4
%23 = load i64, i64* %4
%24 = sdiv i64 %23, 1000
store i64 %24, i64* %7
%25 = load i64, i64* %7
%26 = mul nsw i64 %25, 1000
%27 = load i64, i64* %4
%28 = sub nsw i64 %27, %26
store i64 %28, i64* %4
%29 = load i8*, i8** %3
%30 = load i64, i64* %5
%31 = load i64, i64* %6
%32 = load i64, i64* %7
%33 = load i64, i64* %4
%34 = call i32 (i8*, i8*, i64, ...) @sprintf(i8* %29, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i64 %30, i64 %31, i64 %32, i64 %33)
br label %76
35: ; preds = %2
%36 = load i64, i64* %4
%37 = icmp sge i64 %36, 1000000
br i1 %37, label %38, label %56
38: ; preds = %35
%39 = load i64, i64* %4
%40 = sdiv i64 %39, 1000000
store i64 %40, i64* %6
%41 = load i64, i64* %6
%42 = mul nsw i64 %41, 1000000
%43 = load i64, i64* %4
%44 = sub nsw i64 %43, %42
store i64 %44, i64* %4
%45 = load i64, i64* %4
%46 = sdiv i64 %45, 1000
store i64 %46, i64* %7
%47 = load i64, i64* %7
%48 = mul nsw i64 %47, 1000
%49 = load i64, i64* %4
%50 = sub nsw i64 %49, %48
store i64 %50, i64* %4
%51 = load i8*, i8** %3
%52 = load i64, i64* %6
%53 = load i64, i64* %7
%54 = load i64, i64* %4
%55 = call i32 (i8*, i8*, i64, ...) @sprintf(i8* %51, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i64 %52, i64 %53, i64 %54)
br label %75
56: ; preds = %35
%57 = load i64, i64* %4
%58 = icmp sge i64 %57, 1000
br i1 %58, label %59, label %70
59: ; preds = %56
%60 = load i64, i64* %4
%61 = sdiv i64 %60, 1000
store i64 %61, i64* %7
%62 = load i64, i64* %7
%63 = mul nsw i64 %62, 1000
%64 = load i64, i64* %4
%65 = sub nsw i64 %64, %63
store i64 %65, i64* %4
%66 = load i8*, i8** %3
%67 = load i64, i64* %7
%68 = load i64, i64* %4
%69 = call i32 (i8*, i8*, i64, ...) @sprintf(i8* %66, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i64 %67, i64 %68)
br label %74
70: ; preds = %56
%71 = load i8*, i8** %3
%72 = load i64, i64* %4
%73 = call i32 (i8*, i8*, i64, ...) @sprintf(i8* %71, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i64 %72)
br label %74
74: ; preds = %70, %59
br label %75
75: ; preds = %74, %38
br label %76
76: ; preds = %75, %10
ret void
}

LLVM IR (-O2):
@.str = [26 x i8] c"%lld,%03lld,%03lld,%03lld\00"
@.str.1 = [19 x i8] c"%lld,%03lld,%03lld\00"
@.str.2 = [12 x i8] c"%lld,%03lld\00"
@.str.3 = [5 x i8] c"%lld\00"
define void @sprintLongWithCommas(i8* nocapture writeonly %0, i64 %1) {
%3 = icmp sgt i64 %1, 999999999
br i1 %3, label %4, label %15
4: ; preds = %2
%5 = udiv i64 %1, 1000000000
%6 = mul nsw i64 %5, -1000000000
%7 = add nsw i64 %6, %1
%8 = sdiv i64 %7, 1000000
%9 = mul nsw i64 %8, -1000000
%10 = add i64 %9, %7
%11 = sdiv i64 %10, 1000
%12 = mul nsw i64 %11, -1000
%13 = add i64 %12, %10
%14 = tail call i32 (i8*, i8*, i64, ...) @sprintf(i8* dereferenceable(1) %0, i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i64 %5, i64 %8, i64 %11, i64 %13)
br label %40
15: ; preds = %2
%16 = icmp sgt i64 %1, 999999
br i1 %16, label %17, label %29
17: ; preds = %15
%18 = trunc i64 %1 to i32
%19 = udiv i32 %18, 1000000
%20 = zext i32 %19 to i64
%21 = mul nsw i64 %20, -1000000
%22 = add nsw i64 %21, %1
%23 = trunc i64 %22 to i32
%24 = sdiv i32 %23, 1000
%25 = sext i32 %24 to i64
%26 = mul nsw i64 %25, -1000
%27 = add nsw i64 %26, %22
%28 = tail call i32 (i8*, i8*, i64, ...) @sprintf(i8* dereferenceable(1) %0, i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i64 %20, i64 %25, i64 %27)
br label %40
29: ; preds = %15
%30 = icmp sgt i64 %1, 999
br i1 %30, label %31, label %38
31: ; preds = %29
%32 = trunc i64 %1 to i32
%33 = udiv i32 %32, 1000
%34 = zext i32 %33 to i64
%35 = mul nsw i64 %34, -1000
%36 = add nsw i64 %35, %1
%37 = tail call i32 (i8*, i8*, i64, ...) @sprintf(i8* dereferenceable(1) %0, i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i64 %34, i64 %36)
br label %40
38: ; preds = %29
%39 = tail call i32 (i8*, i8*, i64, ...) @sprintf(i8* dereferenceable(1) %0, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i64 %1)
br label %40
40: ; preds = %17, %38, %31, %4
ret void
}