#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 *formatLong(long num) {
  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' + num % 10;
    num /= 10;
    i++;
  } while (num != 0);
  return p;
}


x86 assembly (clang, -O0):
<formatLong>:
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 <formatLong+0x8f>
mov    $0x0,%al
call   1030 <localeconv@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     119a <formatLong+0x8a>
mov    -0x20(%rbp),%rax
cmpq   $0x0,(%rax)
je     118b <formatLong+0x7b>
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     118b <formatLong+0x7b>
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
movsbl (%rax),%eax
mov    %eax,0x2eba(%rip)
jmp    1195 <formatLong+0x85>
movl   $0x2c,0x2eab(%rip)
jmp    119a <formatLong+0x8a>
jmp    119f <formatLong+0x8f>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x14(%rbp),%eax
mov    $0x3,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
jne    11de <formatLong+0xce>
cmpl   $0x0,-0x14(%rbp)
je     11de <formatLong+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
cqto
idiv   %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
cqto
idiv   %rcx
mov    %rax,-0x8(%rbp)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    11a6 <formatLong+0x96>
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<formatLong>:
push   %rbx
mov    %rdi,%rbx
mov    0x2f25(%rip),%r9d
test   %r9d,%r9d
je     11b3 <formatLong+0xa3>
movb   $0x0,0x2f42(%rip)
lea    0x2f3b(%rip),%rcx
xor    %r11d,%r11d
mov    $0xaaaaaaab,%r8d
movabs $0x6666666666666667,%r10
66 66 66
xor    %esi,%esi
jmp    118a <formatLong+0x7a>
nopl   0x0(%rax)
mov    %rbx,%rax
imul   %r10
mov    %rdx,%rax
shr    $0x3f,%rax
sar    $0x2,%rdx
add    %rax,%rdx
lea    (%rdx,%rdx,1),%eax
lea    (%rax,%rax,4),%eax
mov    %ebx,%edi
sub    %eax,%edi
add    $0x30,%dil
mov    %dil,-0x1(%rcx)
add    $0xffffffffffffffff,%rcx
add    $0x1,%esi
add    $0x9,%rbx
cmp    $0x12,%rbx
mov    %rdx,%rbx
jbe    11ae <formatLong+0x9e>
mov    %r11d,%eax
add    $0xffffffff,%r11d
jae    1150 <formatLong+0x40>
mov    %esi,%edx
imul   %r8,%rdx
shr    $0x21,%rdx
lea    (%rdx,%rdx,2),%edx
add    %edx,%eax
jne    1150 <formatLong+0x40>
mov    %r9b,-0x1(%rcx)
add    $0xffffffffffffffff,%rcx
jmp    1150 <formatLong+0x40>
mov    %rcx,%rax
pop    %rbx
ret
xor    %eax,%eax
call   1030 <localeconv@plt>
test   %rax,%rax
je     11dc <formatLong+0xcc>
mov    (%rax),%rax
test   %rax,%rax
je     11e8 <formatLong+0xd8>
movsbl (%rax),%r9d
test   %r9d,%r9d
je     11e8 <formatLong+0xd8>
mov    %r9d,0x2e69(%rip)
jmp    1124 <formatLong+0x14>
mov    0x2e5d(%rip),%r9d
jmp    1124 <formatLong+0x14>
movl   $0x2c,0x2e4e(%rip)
mov    $0x2c,%r9d
jmp    1124 <formatLong+0x14>

LLVM IR (-O0):
%struct.lconv = type { i8* }
@formatLong.comma = internal global i32 0
@formatLong.retbuf = internal global [30 x i8] zeroinitializer
define i8* @formatLong(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]* @formatLong.retbuf, i64 0, i64 29), i8** %3
store i32 0, i32* %4
%6 = load i32, i32* @formatLong.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* @formatLong.comma
br label %31
30: ; preds = %17, %12
store i32 44, i32* @formatLong.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* @formatLong.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 = srem i64 %48, 10
%50 = add nsw 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 = sdiv 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* }
@formatLong.comma = internal unnamed_addr global i32 0
@formatLong.retbuf = internal global [30 x i8] zeroinitializer
define i8* @formatLong(i64 %0) {
%2 = load i32, i32* @formatLong.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* @formatLong.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* @formatLong.comma
br label %19
18: ; preds = %13, %9
store i32 44, i32* @formatLong.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]* @formatLong.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]* @formatLong.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 = srem i64 %25, 10
%35 = trunc i64 %34 to i8
%36 = add nsw i8 %35, 48
%37 = getelementptr inbounds i8, i8* %33, i64 -1
store i8 %36, i8* %37
%38 = sdiv i64 %25, 10
%39 = add nuw nsw i32 %24, 1
%40 = add i64 %25, 9
%41 = icmp ult i64 %40, 19
br i1 %41, label %42, label %22
42: ; preds = %32
ret i8* %37
}