#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*/ fmtnum(char *, size_t, char *);
char *malloc(size_t);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
char *commanum(const void *a) {
  size_t len;
  int i;
  char *num;
  char buf[128];
  i = *(int *)a;
  len = 1;
  while ((i /= 10))
    ++len;
  len += (len - 1) / 3;
  if (!(num = malloc(len + 1)))
    return NULL;
  snprintf(buf, 128, "%d", *(int *)a);
  fmtnum(num, len + 1, buf);
  return num;
}


x86 assembly (clang, -O0):
<commanum>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
movq   $0x1,-0x18(%rbp)
mov    -0x1c(%rbp),%eax
mov    $0xa,%ecx
cltd
idiv   %ecx
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
je     1178 <commanum+0x48>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    1150 <commanum+0x20>
mov    -0x18(%rbp),%rax
sub    $0x1,%rax
mov    $0x3,%ecx
xor    %edx,%edx
div    %rcx
add    -0x18(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
add    $0x1,%rdi
call   1040 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    11ba <commanum+0x8a>
movq   $0x0,-0x8(%rbp)
jmp    11f8 <commanum+0xc8>
lea    -0xb0(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    (%rax),%ecx
mov    $0x80,%esi
lea    0xe2d(%rip),%rdx
call   1030 <snprintf@plt>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
add    $0x1,%rsi
lea    -0xb0(%rbp),%rdx
call   1050 <fmtnum@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0xb0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<commanum>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x88,%rsp
mov    (%rdi),%r14d
lea    0x9(%r14),%eax
mov    $0x1,%ecx
cmp    $0x13,%eax
jb     1185 <commanum+0x55>
mov    $0x1,%ecx
mov    %r14d,%eax
nopw   0x0(%rax,%rax,1)
cltq
imul   $0x66666667,%rax,%rdx
mov    %rdx,%rsi
shr    $0x3f,%rsi
sar    $0x22,%rdx
lea    (%rdx,%rsi,1),%eax
add    $0x1,%rcx
add    %esi,%edx
add    $0x9,%edx
cmp    $0x12,%edx
ja     1160 <commanum+0x30>
lea    -0x1(%rcx),%rax
movabs $0xaaaaaaaaaaaaaaab,%rdx
aa aa aa
mul    %rdx
shr    %rdx
lea    (%rcx,%rdx,1),%r15
add    $0x1,%r15
mov    %r15,%rdi
call   1040 <malloc@plt>
mov    %rax,%rbx
test   %rax,%rax
je     11d9 <commanum+0xa9>
lea    0xe48(%rip),%rdx
mov    %rsp,%r12
mov    %r12,%rdi
mov    $0x80,%esi
mov    %r14d,%ecx
call   1030 <snprintf@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
call   1050 <fmtnum@plt>
mov    %rbx,%rax
add    $0x88,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [3 x i8] c"%d\00"
define i8* @commanum(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i32
%6 = alloca i8*
%7 = alloca [128 x i8]
store i8* %0, i8** %3
%8 = load i8*, i8** %3
%9 = bitcast i8* %8 to i32*
%10 = load i32, i32* %9
store i32 %10, i32* %5
store i64 1, i64* %4
br label %11
11: ; preds = %15, %1
%12 = load i32, i32* %5
%13 = sdiv i32 %12, 10
store i32 %13, i32* %5
%14 = icmp ne i32 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %11
%16 = load i64, i64* %4
%17 = add i64 %16, 1
store i64 %17, i64* %4
br label %11
18: ; preds = %11
%19 = load i64, i64* %4
%20 = sub i64 %19, 1
%21 = udiv i64 %20, 3
%22 = load i64, i64* %4
%23 = add i64 %22, %21
store i64 %23, i64* %4
%24 = load i64, i64* %4
%25 = add i64 %24, 1
%26 = call i8* @malloc(i64 %25)
store i8* %26, i8** %6
%27 = icmp ne i8* %26, null
br i1 %27, label %29, label %28
28: ; preds = %18
store i8* null, i8** %2
br label %41
29: ; preds = %18
%30 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 0
%31 = load i8*, i8** %3
%32 = bitcast i8* %31 to i32*
%33 = load i32, i32* %32
%34 = call i32 @snprintf(i8* %30, i32 128, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %33)
%35 = load i8*, i8** %6
%36 = load i64, i64* %4
%37 = add i64 %36, 1
%38 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 0
%39 = call i32 @fmtnum(i8* %35, i64 %37, i8* %38)
%40 = load i8*, i8** %6
store i8* %40, i8** %2
br label %41
41: ; preds = %29, %28
%42 = load i8*, i8** %2
ret i8* %42
}

LLVM IR (-O2):
@.str = [3 x i8] c"%d\00"
define i8* @commanum(i8* nocapture %0) {
%2 = alloca [128 x i8]
%3 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 0
%4 = bitcast i8* %0 to i32*
%5 = load i32, i32* %4
%6 = add i32 %5, 9
%7 = icmp ult i32 %6, 19
br i1 %7, label %15, label %8
8: ; preds = %1, %8
%9 = phi i32 [ %11, %8 ], [ %5, %1 ]
%10 = phi i64 [ %12, %8 ], [ 1, %1 ]
%11 = sdiv i32 %9, 10
%12 = add i64 %10, 1
%13 = add nsw i32 %11, 9
%14 = icmp ult i32 %13, 19
br i1 %14, label %15, label %8
15: ; preds = %8, %1
%16 = phi i64 [ 1, %1 ], [ %12, %8 ]
%17 = add i64 %16, -1
%18 = udiv i64 %17, 3
%19 = add i64 %16, 1
%20 = add i64 %19, %18
%21 = tail call i8* @malloc(i64 %20)
%22 = icmp eq i8* %21, null
br i1 %22, label %26, label %23
23: ; preds = %15
%24 = call i32 @snprintf(i8* %3, i32 128, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %5)
%25 = call i32 @fmtnum(i8* %21, i64 %20, i8* %3)
br label %26
26: ; preds = %15, %23
ret i8* %21
}