#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*/ snprintf(char *, int, char *, int);
unsigned int strlen(char *);
char *num_punct(int foo) {
  int index_new, rest, x;
  unsigned int nindex;
  char buf[16];
  static char buf_new[16];
  snprintf(buf, 16, "%d", foo);
  rest = strlen(buf) % 3;
  for (nindex = index_new = 0; nindex < strlen(buf); nindex++, index_new++) {
    x = nindex - rest;
    if (nindex != 0 && (x % 3) == 0) {
      buf_new[index_new] = ',';
      index_new++;
      buf_new[index_new] = buf[nindex];
    } else
      buf_new[index_new] = buf[nindex];
  }
  buf_new[index_new] = '\0';
  return buf_new;
}


x86 assembly (clang, -O0):
<num_punct>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
lea    -0x30(%rbp),%rdi
mov    -0x4(%rbp),%ecx
mov    $0x10,%esi
lea    0xec2(%rip),%rdx
call   1040 <snprintf@plt>
lea    -0x30(%rbp),%rdi
call   1030 <strlen@plt>
mov    $0x3,%ecx
xor    %edx,%edx
div    %ecx
mov    %edx,-0xc(%rbp)
movl   $0x0,-0x8(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x34(%rbp)
lea    -0x30(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x34(%rbp),%eax
cmp    %ecx,%eax
jae    120c <num_punct+0xec>
mov    -0x14(%rbp),%eax
sub    -0xc(%rbp),%eax
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     11db <num_punct+0xbb>
mov    -0x10(%rbp),%eax
mov    $0x3,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
jne    11db <num_punct+0xbb>
movslq -0x8(%rbp),%rcx
lea    0x2e8c(%rip),%rax
movb   $0x2c,(%rax,%rcx,1)
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
mov    -0x14(%rbp),%eax
mov    -0x30(%rbp,%rax,1),%dl
movslq -0x8(%rbp),%rcx
lea    0x2e6d(%rip),%rax
mov    %dl,(%rax,%rcx,1)
jmp    11f0 <num_punct+0xd0>
mov    -0x14(%rbp),%eax
mov    -0x30(%rbp,%rax,1),%dl
movslq -0x8(%rbp),%rcx
lea    0x2e53(%rip),%rax
mov    %dl,(%rax,%rcx,1)
jmp    11f5 <num_punct+0xd5>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    1166 <num_punct+0x46>
movslq -0x8(%rbp),%rcx
lea    0x2e29(%rip),%rax
movb   $0x0,(%rax,%rcx,1)
lea    0x2e1e(%rip),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<num_punct>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ecx
lea    0xecb(%rip),%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x10,%esi
call   1040 <snprintf@plt>
mov    %rbx,%rdi
call   1030 <strlen@plt>
mov    %eax,%r14d
mov    %rbx,%rdi
call   1030 <strlen@plt>
test   %eax,%eax
je     11f4 <num_punct+0xd4>
mov    (%rsp),%al
mov    %al,0x2ed7(%rip)
mov    %rsp,%rdi
call   1030 <strlen@plt>
mov    $0x1,%ecx
cmp    $0x2,%eax
jb     11fb <num_punct+0xdb>
mov    %r14d,%eax
mov    $0xaaaaaaab,%ecx
imul   %rax,%rcx
shr    $0x21,%rcx
lea    (%rcx,%rcx,2),%r15d
sub    %r14d,%r15d
mov    $0x1,%ebx
mov    $0x1,%ebp
lea    0x2e99(%rip),%r12
mov    %rsp,%r14
jmp    11d1 <num_punct+0xb1>
nopl   0x0(%rax)
movzbl (%rsp,%rbp,1),%eax
movslq %ebx,%rbx
mov    %al,(%rbx,%r12,1)
add    $0x1,%rbp
add    $0x1,%ebx
mov    %r14,%rdi
call   1030 <strlen@plt>
mov    %eax,%eax
cmp    %rax,%rbp
jae    11f8 <num_punct+0xd8>
lea    (%r15,%rbp,1),%eax
imul   $0xaaaaaaab,%eax,%eax
add    $0x2aaaaaaa,%eax
cmp    $0x55555554,%eax
ja     11b0 <num_punct+0x90>
movslq %ebx,%rbx
movb   $0x2c,(%rbx,%r12,1)
add    $0x1,%ebx
jmp    11b0 <num_punct+0x90>
xor    %ecx,%ecx
jmp    11fb <num_punct+0xdb>
movslq %ebx,%rcx
lea    0x2e3e(%rip),%rax
movb   $0x0,(%rcx,%rax,1)
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@num_punct.buf_new = internal global [16 x i8] zeroinitializer
@.str = [3 x i8] c"%d\00"
define i8* @num_punct(i32 %0) {
%2 = alloca [16 x i8]
%3 = getelementptr inbounds [16 x i8], [16 x i8]* %2, i64 0, i64 0
%4 = call i32 @snprintf(i8* %3, i32 16, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %0)
%5 = call i32 @strlen(i8* %3)
%6 = urem i32 %5, 3
%7 = call i32 @strlen(i8* %3)
%8 = icmp eq i32 %7, 0
br i1 %8, label %37, label %9
9: ; preds = %1
%10 = load i8, i8* %3
store i8 %10, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @num_punct.buf_new, i64 0, i64 0)
%11 = call i32 @strlen(i8* %3)
%12 = icmp ugt i32 %11, 1
br i1 %12, label %13, label %37
13: ; preds = %9, %24
%14 = phi i64 [ %30, %24 ], [ 1, %9 ]
%15 = phi i32 [ %31, %24 ], [ 1, %9 ]
%16 = trunc i64 %14 to i32
%17 = sub i32 %16, %6
%18 = srem i32 %17, 3
%19 = icmp eq i32 %18, 0
br i1 %19, label %20, label %24
20: ; preds = %13
%21 = sext i32 %15 to i64
%22 = getelementptr inbounds [16 x i8], [16 x i8]* @num_punct.buf_new, i64 0, i64 %21
store i8 44, i8* %22
%23 = add nsw i32 %15, 1
br label %24
24: ; preds = %13, %20
%25 = phi i32 [ %23, %20 ], [ %15, %13 ]
%26 = getelementptr inbounds [16 x i8], [16 x i8]* %2, i64 0, i64 %14
%27 = load i8, i8* %26
%28 = sext i32 %25 to i64
%29 = getelementptr inbounds [16 x i8], [16 x i8]* @num_punct.buf_new, i64 0, i64 %28
store i8 %27, i8* %29
%30 = add nuw nsw i64 %14, 1
%31 = add nsw i32 %25, 1
%32 = call i32 @strlen(i8* %3)
%33 = zext i32 %32 to i64
%34 = icmp ult i64 %30, %33
br i1 %34, label %13, label %35
35: ; preds = %24
%36 = sext i32 %31 to i64
br label %37
37: ; preds = %35, %9, %1
%38 = phi i64 [ 0, %1 ], [ 1, %9 ], [ %36, %35 ]
%39 = getelementptr inbounds [16 x i8], [16 x i8]* @num_punct.buf_new, i64 0, i64 %38
store i8 0, i8* %39
ret i8* getelementptr inbounds ([16 x i8], [16 x i8]* @num_punct.buf_new, i64 0, i64 0)
}