#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 */
struct lconv {
  char *thousands_sep;
};
typedef int /*<<< orphan*/ retbuf;

/* Variables and functions */
struct lconv *localeconv();
char *commaprint(unsigned long n) {
  static int comma = '\0';
  static char retbuf[30];
  char *p = &retbuf[sizeof(retbuf) - 1];
  int i = 0;
  if (comma == '\0') {
    struct lconv *lcp = localeconv();
    if (lcp != NULL) {
      if (lcp->thousands_sep != NULL && *lcp->thousands_sep != '\0')
        comma = *lcp->thousands_sep;
      else
        comma = ',';
    }
  }
  *p = '\0';
  do {
    if (i % 3 == 0 && i != 0)
      *--p = comma;
    *--p = '0' + n % 10;
    n /= 10;
    i++;
  } while (n != 0);
  return p;
}


x86 assembly (clang, -O0):
<commaprint>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
lea    0x2f2d(%rip),%rax
add    $0x1d,%rax
mov    %rax,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
cmpl   $0x0,0x2f07(%rip)
jne    119f <commaprint+0x8f>
mov    $0x0,%al
call   1030 <localeconv@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     119a <commaprint+0x8a>
mov    -0x20(%rbp),%rax
cmpq   $0x0,(%rax)
je     118b <commaprint+0x7b>
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     118b <commaprint+0x7b>
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
movsbl (%rax),%eax
mov    %eax,0x2eba(%rip)
jmp    1195 <commaprint+0x85>
movl   $0x2c,0x2eab(%rip)
jmp    119a <commaprint+0x8a>
jmp    119f <commaprint+0x8f>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x14(%rbp),%eax
mov    $0x3,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
jne    11de <commaprint+0xce>
cmpl   $0x0,-0x14(%rbp)
je     11de <commaprint+0xce>
mov    0x2e76(%rip),%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %rax,%rdx
add    $0xffffffffffffffff,%rdx
mov    %rdx,-0x10(%rbp)
mov    %cl,-0x1(%rax)
mov    -0x8(%rbp),%rax
mov    $0xa,%ecx
xor    %edx,%edx
div    %rcx
add    $0x30,%rdx
mov    %dl,%cl
mov    -0x10(%rbp),%rax
mov    %rax,%rdx
add    $0xffffffffffffffff,%rdx
mov    %rdx,-0x10(%rbp)
mov    %cl,-0x1(%rax)
mov    -0x8(%rbp),%rax
mov    $0xa,%ecx
xor    %edx,%edx
div    %rcx
mov    %rax,-0x8(%rbp)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    11a6 <commaprint+0x96>
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<commaprint>:
push   %rbx
mov    %rdi,%rbx
mov    0x2f25(%rip),%r9d
test   %r9d,%r9d
je     11a5 <commaprint+0x95>
movb   $0x0,0x2f42(%rip)
lea    0x2f3b(%rip),%rcx
xor    %r11d,%r11d
mov    $0xaaaaaaab,%r8d
movabs $0xcccccccccccccccd,%r10
cc cc cc
xor    %esi,%esi
jmp    117c <commaprint+0x6c>
nopl   0x0(%rax)
mov    %rbx,%rax
mul    %r10
shr    $0x3,%rdx
lea    (%rdx,%rdx,1),%eax
lea    (%rax,%rax,4),%eax
mov    %ebx,%edi
sub    %eax,%edi
or     $0x30,%dil
mov    %dil,-0x1(%rcx)
add    $0xffffffffffffffff,%rcx
add    $0x1,%esi
cmp    $0x9,%rbx
mov    %rdx,%rbx
jbe    11a0 <commaprint+0x90>
mov    %r11d,%eax
add    $0xffffffff,%r11d
jae    1150 <commaprint+0x40>
mov    %esi,%edx
imul   %r8,%rdx
shr    $0x21,%rdx
lea    (%rdx,%rdx,2),%edx
add    %edx,%eax
jne    1150 <commaprint+0x40>
mov    %r9b,-0x1(%rcx)
add    $0xffffffffffffffff,%rcx
jmp    1150 <commaprint+0x40>
mov    %rcx,%rax
pop    %rbx
ret
xor    %eax,%eax
call   1030 <localeconv@plt>
test   %rax,%rax
je     11ce <commaprint+0xbe>
mov    (%rax),%rax
test   %rax,%rax
je     11da <commaprint+0xca>
movsbl (%rax),%r9d
test   %r9d,%r9d
je     11da <commaprint+0xca>
mov    %r9d,0x2e77(%rip)
jmp    1124 <commaprint+0x14>
mov    0x2e6b(%rip),%r9d
jmp    1124 <commaprint+0x14>
movl   $0x2c,0x2e5c(%rip)
mov    $0x2c,%r9d
jmp    1124 <commaprint+0x14>

LLVM IR (-O0):
%struct.lconv = type { i8* }
@commaprint.comma = internal global i32 0
@commaprint.retbuf = internal global [30 x i8] zeroinitializer
define i8* @commaprint(i64 %0) {
%2 = alloca i64
%3 = alloca i8*
%4 = alloca i32
%5 = alloca %struct.lconv*
store i64 %0, i64* %2
store i8* getelementptr inbounds ([30 x i8], [30 x i8]* @commaprint.retbuf, i64 0, i64 29), i8** %3
store i32 0, i32* %4
%6 = load i32, i32* @commaprint.comma
%7 = icmp eq i32 %6, 0
br i1 %7, label %8, label %33
8: ; preds = %1
%9 = call %struct.lconv* (...) @localeconv()
store %struct.lconv* %9, %struct.lconv** %5
%10 = load %struct.lconv*, %struct.lconv** %5
%11 = icmp ne %struct.lconv* %10, null
br i1 %11, label %12, label %32
12: ; preds = %8
%13 = load %struct.lconv*, %struct.lconv** %5
%14 = getelementptr inbounds %struct.lconv, %struct.lconv* %13, i32 0, i32 0
%15 = load i8*, i8** %14
%16 = icmp ne i8* %15, null
br i1 %16, label %17, label %30
17: ; preds = %12
%18 = load %struct.lconv*, %struct.lconv** %5
%19 = getelementptr inbounds %struct.lconv, %struct.lconv* %18, i32 0, i32 0
%20 = load i8*, i8** %19
%21 = load i8, i8* %20
%22 = sext i8 %21 to i32
%23 = icmp ne i32 %22, 0
br i1 %23, label %24, label %30
24: ; preds = %17
%25 = load %struct.lconv*, %struct.lconv** %5
%26 = getelementptr inbounds %struct.lconv, %struct.lconv* %25, i32 0, i32 0
%27 = load i8*, i8** %26
%28 = load i8, i8* %27
%29 = sext i8 %28 to i32
store i32 %29, i32* @commaprint.comma
br label %31
30: ; preds = %17, %12
store i32 44, i32* @commaprint.comma
br label %31
31: ; preds = %30, %24
br label %32
32: ; preds = %31, %8
br label %33
33: ; preds = %32, %1
%34 = load i8*, i8** %3
store i8 0, i8* %34
br label %35
35: ; preds = %58, %33
%36 = load i32, i32* %4
%37 = srem i32 %36, 3
%38 = icmp eq i32 %37, 0
br i1 %38, label %39, label %47
39: ; preds = %35
%40 = load i32, i32* %4
%41 = icmp ne i32 %40, 0
br i1 %41, label %42, label %47
42: ; preds = %39
%43 = load i32, i32* @commaprint.comma
%44 = trunc i32 %43 to i8
%45 = load i8*, i8** %3
%46 = getelementptr inbounds i8, i8* %45, i32 -1
store i8* %46, i8** %3
store i8 %44, i8* %46
br label %47
47: ; preds = %42, %39, %35
%48 = load i64, i64* %2
%49 = urem i64 %48, 10
%50 = add i64 48, %49
%51 = trunc i64 %50 to i8
%52 = load i8*, i8** %3
%53 = getelementptr inbounds i8, i8* %52, i32 -1
store i8* %53, i8** %3
store i8 %51, i8* %53
%54 = load i64, i64* %2
%55 = udiv i64 %54, 10
store i64 %55, i64* %2
%56 = load i32, i32* %4
%57 = add nsw i32 %56, 1
store i32 %57, i32* %4
br label %58
58: ; preds = %47
%59 = load i64, i64* %2
%60 = icmp ne i64 %59, 0
br i1 %60, label %35, label %61
61: ; preds = %58
%62 = load i8*, i8** %3
ret i8* %62
}

LLVM IR (-O2):
%struct.lconv = type { i8* }
@commaprint.comma = internal unnamed_addr global i32 0
@commaprint.retbuf = internal global [30 x i8] zeroinitializer
define i8* @commaprint(i64 %0) {
%2 = load i32, i32* @commaprint.comma
%3 = icmp eq i32 %2, 0
br i1 %3, label %4, label %19
4: ; preds = %1
%5 = tail call %struct.lconv* (...) @localeconv()
%6 = icmp eq %struct.lconv* %5, null
br i1 %6, label %7, label %9
7: ; preds = %4
%8 = load i32, i32* @commaprint.comma
br label %19
9: ; preds = %4
%10 = getelementptr inbounds %struct.lconv, %struct.lconv* %5, i64 0, i32 0
%11 = load i8*, i8** %10
%12 = icmp eq i8* %11, null
br i1 %12, label %18, label %13
13: ; preds = %9
%14 = load i8, i8* %11
%15 = icmp eq i8 %14, 0
br i1 %15, label %18, label %16
16: ; preds = %13
%17 = sext i8 %14 to i32
store i32 %17, i32* @commaprint.comma
br label %19
18: ; preds = %13, %9
store i32 44, i32* @commaprint.comma
br label %19
19: ; preds = %7, %18, %16, %1
%20 = phi i32 [ %8, %7 ], [ 44, %18 ], [ %17, %16 ], [ %2, %1 ]
store i8 0, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @commaprint.retbuf, i64 0, i64 29)
%21 = trunc i32 %20 to i8
br label %22
22: ; preds = %32, %19
%23 = phi i8* [ getelementptr inbounds ([30 x i8], [30 x i8]* @commaprint.retbuf, i64 0, i64 29), %19 ], [ %37, %32 ]
%24 = phi i32 [ 0, %19 ], [ %39, %32 ]
%25 = phi i64 [ %0, %19 ], [ %38, %32 ]
%26 = urem i32 %24, 3
%27 = icmp eq i32 %26, 0
%28 = icmp ne i32 %24, 0
%29 = and i1 %28, %27
br i1 %29, label %30, label %32
30: ; preds = %22
%31 = getelementptr inbounds i8, i8* %23, i64 -1
store i8 %21, i8* %31
br label %32
32: ; preds = %30, %22
%33 = phi i8* [ %31, %30 ], [ %23, %22 ]
%34 = urem i64 %25, 10
%35 = trunc i64 %34 to i8
%36 = or i8 %35, 48
%37 = getelementptr inbounds i8, i8* %33, i64 -1
store i8 %36, i8* %37
%38 = udiv i64 %25, 10
%39 = add nuw nsw i32 %24, 1
%40 = icmp ult i64 %25, 10
br i1 %40, label %41, label %22
41: ; preds = %32
ret i8* %37
}