#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 *, int, char *);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
void comma_print(char *s, long size) {
  int i, m, neg;
  char tbuf[80];
  if (!size) {
    s[0] = '0';
    s[1] = '\0';
    return;
  }
  s[0] = '\0';
  neg = 0;
  if (size < 0) {
    size = -size;
    neg = 1;
  }
  for (i = 0, m = size % 1000; size;
       i++, size = (size - m) / 1000, m = size % 1000) {
    sprintf(tbuf, m == size ? "%d%s" : "%03d%s", m, i > 0 ? "," : "");
    strcat(tbuf, s);
    strcpy(s, tbuf);
  }
  if (neg) {
    strcpy(tbuf, "-");
    strcat(tbuf, s);
    strcpy(s, tbuf);
  }
}


x86 assembly (clang, -O0):
<comma_print>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    115f <comma_print+0x2f>
mov    -0x8(%rbp),%rax
movb   $0x30,(%rax)
mov    -0x8(%rbp),%rax
movb   $0x0,0x1(%rax)
jmp    1279 <comma_print+0x149>
mov    -0x8(%rbp),%rax
movb   $0x0,(%rax)
movl   $0x0,-0x1c(%rbp)
cmpq   $0x0,-0x10(%rbp)
jge    1189 <comma_print+0x59>
xor    %eax,%eax
sub    -0x10(%rbp),%rax
mov    %rax,-0x10(%rbp)
movl   $0x1,-0x1c(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    $0x3e8,%ecx
cqto
idiv   %rcx
mov    %edx,%eax
mov    %eax,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1245 <comma_print+0x115>
lea    -0x70(%rbp),%rdi
movslq -0x18(%rbp),%rcx
mov    -0x10(%rbp),%rdx
lea    0xe44(%rip),%rsi
lea    0xe38(%rip),%rax
cmp    %rdx,%rcx
cmove  %rax,%rsi
mov    -0x18(%rbp),%edx
mov    -0x14(%rbp),%r8d
lea    0xe30(%rip),%rcx
lea    0xe28(%rip),%rax
cmp    $0x0,%r8d
cmovg  %rax,%rcx
call   1050 <sprintf@plt>
lea    -0x70(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1040 <strcat@plt>
mov    -0x8(%rbp),%rdi
lea    -0x70(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
sub    %rcx,%rax
mov    $0x3e8,%ecx
cqto
idiv   %rcx
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    $0x3e8,%ecx
cqto
idiv   %rcx
mov    %edx,%eax
mov    %eax,-0x18(%rbp)
jmp    11a3 <comma_print+0x73>
cmpl   $0x0,-0x1c(%rbp)
je     1279 <comma_print+0x149>
lea    -0x70(%rbp),%rdi
lea    0xdb4(%rip),%rsi
call   1030 <strcpy@plt>
lea    -0x70(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1040 <strcat@plt>
mov    -0x8(%rbp),%rdi
lea    -0x70(%rbp),%rsi
call   1030 <strcpy@plt>
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<comma_print>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x68,%rsp
mov    %rdi,%r15
test   %rsi,%rsi
je     1257 <comma_print+0x127>
movb   $0x0,(%r15)
mov    %rsi,%rbx
neg    %rbx
mov    %rsi,0x8(%rsp)
cmovs  %rsi,%rbx
mov    %rbx,%rax
shr    $0x3,%rax
movabs $0x20c49ba5e353f7cf,%r13
9b c4 20
mul    %r13
shr    $0x4,%rdx
imul   $0x3e8,%rdx,%rax
mov    %rbx,%rdx
sub    %rax,%rdx
xor    %ebp,%ebp
lea    0xe81(%rip),%r14
lea    0x10(%rsp),%r12
cmp    %rdx,%rbx
lea    0xe6b(%rip),%rsi
lea    0xe5f(%rip),%rax
cmove  %rax,%rsi
sub    $0x1,%ebp
mov    %r14,%rcx
lea    0xe5b(%rip),%rax
cmovb  %rax,%rcx
mov    %r12,%rdi
call   1050 <sprintf@plt>
mov    %r12,%rdi
mov    %r15,%rsi
call   1040 <strcat@plt>
mov    %r15,%rdi
mov    %r12,%rsi
call   1030 <strcpy@plt>
mov    %rbx,%rax
imul   %r13
mov    %rdx,%rcx
mov    %rdx,%rax
shr    $0x3f,%rax
sar    $0x7,%rcx
add    %rax,%rcx
mov    %rcx,%rax
imul   %r13
mov    %rdx,%rax
shr    $0x3f,%rax
sar    $0x7,%rdx
add    %rax,%rdx
imul   $0x3e8,%rdx,%rax
mov    %rcx,%rdx
sub    %rax,%rdx
add    $0x3e7,%rbx
cmp    $0x7ce,%rbx
mov    %rcx,%rbx
ja     1190 <comma_print+0x60>
cmpq   $0x0,0x8(%rsp)
jns    125d <comma_print+0x12d>
lea    0xddc(%rip),%rsi
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
call   1030 <strcpy@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
call   1040 <strcat@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
call   1030 <strcpy@plt>
jmp    125d <comma_print+0x12d>
movw   $0x30,(%r15)
add    $0x68,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [5 x i8] c"%d%s\00"
@.str.1 = [7 x i8] c"%03d%s\00"
@.str.2 = [2 x i8] c",\00"
@.str.3 = [1 x i8] zeroinitializer
@.str.4 = [2 x i8] c"-\00"
define void @comma_print(i8* %0, i64 %1) {
%3 = alloca [80 x i8]
%4 = getelementptr inbounds [80 x i8], [80 x i8]* %3, i64 0, i64 0
%5 = icmp eq i64 %1, 0
br i1 %5, label %6, label %8
6: ; preds = %2
store i8 48, i8* %0
%7 = getelementptr inbounds i8, i8* %0, i64 1
store i8 0, i8* %7
br label %35
8: ; preds = %2
store i8 0, i8* %0
%9 = icmp slt i64 %1, 0
%10 = sub nsw i64 0, %1
%11 = select i1 %9, i64 %10, i64 %1
%12 = urem i64 %11, 1000
br label %13
13: ; preds = %8, %13
%14 = phi i64 [ %27, %13 ], [ %12, %8 ]
%15 = phi i32 [ %25, %13 ], [ 0, %8 ]
%16 = phi i64 [ %26, %13 ], [ %11, %8 ]
%17 = trunc i64 %14 to i32
%18 = icmp eq i64 %16, %14
%19 = select i1 %18, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0)
%20 = icmp eq i32 %15, 0
%21 = select i1 %20, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0)
%22 = call i32 @sprintf(i8* %4, i8* dereferenceable(1) %19, i32 %17, i8* %21)
%23 = call i32 @strcat(i8* %4, i8* %0)
%24 = call i32 @strcpy(i8* %0, i8* %4)
%25 = add nuw nsw i32 %15, 1
%26 = sdiv i64 %16, 1000
%27 = srem i64 %26, 1000
%28 = add i64 %16, 999
%29 = icmp ult i64 %28, 1999
br i1 %29, label %30, label %13
30: ; preds = %13
br i1 %9, label %31, label %35
31: ; preds = %30
%32 = call i32 @strcpy(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%33 = call i32 @strcat(i8* %4, i8* %0)
%34 = call i32 @strcpy(i8* %0, i8* %4)
br label %35
35: ; preds = %30, %31, %6
ret void
}