#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 */
scalar_t__ double_virtually_same(double, double);
int /*<<< orphan*/ piece(char *, char, char *, int);
int /*<<< orphan*/ reverse_string(char *, char *);
int /*<<< orphan*/ sprintf(char *, char *, ...);
char *strdup(char *);
int strlen(char *);
char *string_commas_double(double d) {
  char integer_part[64];
  char buffer[64];
  char decimal_part[8];
  char d_string[128];
  char reversed_integer[64];
  char *reversed_integer_pointer = reversed_integer;
  char *integer_part_pointer;
  int counter = 0;
  char destination[1024];
  if (double_virtually_same(d, 0.0))
    d = 0.0;
  sprintf(d_string, "%.3lf", d);
  piece(integer_part, '.', d_string, 0);
  piece(decimal_part, '.', d_string, 1);
  integer_part_pointer = integer_part + strlen(integer_part) - 1;
  while (integer_part_pointer >= integer_part) {
    *reversed_integer_pointer++ = *integer_part_pointer;
    if (integer_part_pointer > integer_part &&
        *(integer_part_pointer - 1) != '-') {
      if (!(++counter % 3)) {
        if (integer_part_pointer != integer_part) {
          *reversed_integer_pointer++ = ',';
        }
      }
    }
    integer_part_pointer--;
  }
  *reversed_integer_pointer = '\0';
  reverse_string(buffer, reversed_integer);
  sprintf(destination, "%s.%s", buffer, decimal_part);
  return strdup(destination);
}


x86 assembly (clang, -O0):
<string_commas_double>:
push   %rbp
mov    %rsp,%rbp
sub    $0x590,%rsp
movsd  %xmm0,-0x8(%rbp)
lea    -0x160(%rbp),%rax
mov    %rax,-0x168(%rbp)
movl   $0x0,-0x174(%rbp)
movsd  -0x8(%rbp),%xmm0
xorps  %xmm1,%xmm1
call   1050 <double_virtually_same@plt>
cmp    $0x0,%rax
je     11a7 <string_commas_double+0x47>
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x8(%rbp)
lea    -0x120(%rbp),%rdi
movsd  -0x8(%rbp),%xmm0
lea    0xe46(%rip),%rsi
mov    $0x1,%al
call   1060 <sprintf@plt>
lea    -0x50(%rbp),%rdi
lea    -0x120(%rbp),%rdx
mov    $0x2e,%esi
xor    %ecx,%ecx
call   1070 <piece@plt>
lea    -0x98(%rbp),%rdi
lea    -0x120(%rbp),%rdx
mov    $0x2e,%esi
mov    $0x1,%ecx
call   1070 <piece@plt>
lea    -0x50(%rbp),%rax
mov    %rax,-0x588(%rbp)
lea    -0x50(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x588(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x170(%rbp)
mov    -0x170(%rbp),%rax
lea    -0x50(%rbp),%rcx
cmp    %rcx,%rax
jb     12ec <string_commas_double+0x18c>
mov    -0x170(%rbp),%rax
mov    (%rax),%cl
mov    -0x168(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x168(%rbp)
mov    %cl,(%rax)
mov    -0x170(%rbp),%rax
lea    -0x50(%rbp),%rcx
cmp    %rcx,%rax
jbe    12d5 <string_commas_double+0x175>
mov    -0x170(%rbp),%rax
movsbl -0x1(%rax),%eax
cmp    $0x2d,%eax
je     12d5 <string_commas_double+0x175>
mov    -0x174(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x174(%rbp)
mov    $0x3,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
jne    12d0 <string_commas_double+0x170>
mov    -0x170(%rbp),%rax
lea    -0x50(%rbp),%rcx
cmp    %rcx,%rax
je     12cb <string_commas_double+0x16b>
mov    -0x168(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x168(%rbp)
movb   $0x2c,(%rax)
jmp    12d0 <string_commas_double+0x170>
jmp    12d5 <string_commas_double+0x175>
mov    -0x170(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x170(%rbp)
jmp    1223 <string_commas_double+0xc3>
mov    -0x168(%rbp),%rax
movb   $0x0,(%rax)
lea    -0x90(%rbp),%rdi
lea    -0x160(%rbp),%rsi
call   1030 <reverse_string@plt>
lea    -0x580(%rbp),%rdi
lea    -0x90(%rbp),%rdx
lea    -0x98(%rbp),%rcx
lea    0xce1(%rip),%rsi
mov    $0x0,%al
call   1060 <sprintf@plt>
lea    -0x580(%rbp),%rdi
call   1080 <strdup@plt>
add    $0x590,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<string_commas_double>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x558,%rsp
movsd  %xmm0,(%rsp)
xorps  %xmm1,%xmm1
call   1050 <double_virtually_same@plt>
movsd  (%rsp),%xmm0
test   %rax,%rax
je     1187 <string_commas_double+0x27>
xorps  %xmm0,%xmm0
lea    0xe72(%rip),%rsi
lea    0xd0(%rsp),%r14
mov    %r14,%rdi
mov    $0x1,%al
call   1060 <sprintf@plt>
lea    0x50(%rsp),%rbp
xor    %r15d,%r15d
mov    %rbp,%rdi
mov    $0x2e,%esi
mov    %r14,%rdx
xor    %ecx,%ecx
call   1070 <piece@plt>
lea    0x8(%rsp),%rdi
mov    $0x2e,%esi
mov    %r14,%rdx
mov    $0x1,%ecx
call   1070 <piece@plt>
mov    %rbp,%rdi
call   1040 <strlen@plt>
cltq
add    %rsp,%rax
add    $0x4f,%rax
cmp    %rbp,%rax
jae    123b <string_commas_double+0xdb>
lea    0x10(%rsp),%rcx
movb   $0x0,(%rcx)
lea    0x90(%rsp),%r14
lea    0x10(%rsp),%rsi
mov    %r14,%rdi
call   1030 <reverse_string@plt>
lea    0xdfb(%rip),%rsi
lea    0x150(%rsp),%rbx
lea    0x8(%rsp),%rcx
mov    %rbx,%rdi
mov    %r14,%rdx
xor    %eax,%eax
call   1060 <sprintf@plt>
mov    %rbx,%rdi
call   1080 <strdup@plt>
add    $0x558,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0x10(%rsp),%rdx
jmp    125c <string_commas_double+0xfc>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
add    $0xffffffffffffffff,%rax
mov    %rcx,%rdx
cmp    %rbp,%rax
jb     11ec <string_commas_double+0x8c>
movzbl (%rax),%ebx
lea    0x1(%rdx),%rcx
mov    %bl,(%rdx)
cmp    %rbp,%rax
jbe    1250 <string_commas_double+0xf0>
cmpb   $0x2d,-0x1(%rax)
je     1250 <string_commas_double+0xf0>
add    $0x1,%r15d
imul   $0xaaaaaaab,%r15d,%esi
add    $0x2aaaaaaa,%esi
cmp    $0x55555554,%esi
ja     1250 <string_commas_double+0xf0>
movb   $0x2c,0x1(%rdx)
add    $0x2,%rdx
mov    %rdx,%rcx
jmp    1250 <string_commas_double+0xf0>

LLVM IR (-O0):
@.str = [6 x i8] c"%.3lf\00"
@.str.1 = [6 x i8] c"%s.%s\00"
define i8* @string_commas_double(double %0) {
%2 = alloca double
%3 = alloca [64 x i8]
%4 = alloca [64 x i8]
%5 = alloca [8 x i8]
%6 = alloca [128 x i8]
%7 = alloca [64 x i8]
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i32
%11 = alloca [1024 x i8]
store double %0, double* %2
%12 = getelementptr inbounds [64 x i8], [64 x i8]* %7, i64 0, i64 0
store i8* %12, i8** %8
store i32 0, i32* %10
%13 = load double, double* %2
%14 = call i64 @double_virtually_same(double %13, double 0.000000e+00)
%15 = icmp ne i64 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %1
store double 0.000000e+00, double* %2
br label %17
17: ; preds = %16, %1
%18 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%19 = load double, double* %2
%20 = call i32 (i8*, i8*, ...) @sprintf(i8* %18, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), double %19)
%21 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 0
%22 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%23 = call i32 @piece(i8* %21, i8 signext 46, i8* %22, i32 0)
%24 = getelementptr inbounds [8 x i8], [8 x i8]* %5, i64 0, i64 0
%25 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%26 = call i32 @piece(i8* %24, i8 signext 46, i8* %25, i32 1)
%27 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 0
%28 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 0
%29 = call i32 @strlen(i8* %28)
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i8, i8* %27, i64 %30
%32 = getelementptr inbounds i8, i8* %31, i64 -1
store i8* %32, i8** %9
br label %33
33: ; preds = %65, %17
%34 = load i8*, i8** %9
%35 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 0
%36 = icmp uge i8* %34, %35
br i1 %36, label %37, label %68
37: ; preds = %33
%38 = load i8*, i8** %9
%39 = load i8, i8* %38
%40 = load i8*, i8** %8
%41 = getelementptr inbounds i8, i8* %40, i32 1
store i8* %41, i8** %8
store i8 %39, i8* %40
%42 = load i8*, i8** %9
%43 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 0
%44 = icmp ugt i8* %42, %43
br i1 %44, label %45, label %65
45: ; preds = %37
%46 = load i8*, i8** %9
%47 = getelementptr inbounds i8, i8* %46, i64 -1
%48 = load i8, i8* %47
%49 = sext i8 %48 to i32
%50 = icmp ne i32 %49, 45
br i1 %50, label %51, label %65
51: ; preds = %45
%52 = load i32, i32* %10
%53 = add nsw i32 %52, 1
store i32 %53, i32* %10
%54 = srem i32 %53, 3
%55 = icmp ne i32 %54, 0
br i1 %55, label %64, label %56
56: ; preds = %51
%57 = load i8*, i8** %9
%58 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 0
%59 = icmp ne i8* %57, %58
br i1 %59, label %60, label %63
60: ; preds = %56
%61 = load i8*, i8** %8
%62 = getelementptr inbounds i8, i8* %61, i32 1
store i8* %62, i8** %8
store i8 44, i8* %61
br label %63
63: ; preds = %60, %56
br label %64
64: ; preds = %63, %51
br label %65
65: ; preds = %64, %45, %37
%66 = load i8*, i8** %9
%67 = getelementptr inbounds i8, i8* %66, i32 -1
store i8* %67, i8** %9
br label %33
68: ; preds = %33
%69 = load i8*, i8** %8
store i8 0, i8* %69
%70 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 0
%71 = getelementptr inbounds [64 x i8], [64 x i8]* %7, i64 0, i64 0
%72 = call i32 @reverse_string(i8* %70, i8* %71)
%73 = getelementptr inbounds [1024 x i8], [1024 x i8]* %11, i64 0, i64 0
%74 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 0
%75 = getelementptr inbounds [8 x i8], [8 x i8]* %5, i64 0, i64 0
%76 = call i32 (i8*, i8*, ...) @sprintf(i8* %73, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %74, i8* %75)
%77 = getelementptr inbounds [1024 x i8], [1024 x i8]* %11, i64 0, i64 0
%78 = call i8* @strdup(i8* %77)
ret i8* %78
}

LLVM IR (-O2):
@.str = [6 x i8] c"%.3lf\00"
@.str.1 = [6 x i8] c"%s.%s\00"
define noalias i8* @string_commas_double(double %0) {
%2 = alloca [64 x i8]
%3 = alloca [64 x i8]
%4 = alloca [8 x i8]
%5 = alloca [128 x i8]
%6 = alloca [64 x i8]
%7 = alloca [1024 x i8]
%8 = getelementptr inbounds [64 x i8], [64 x i8]* %2, i64 0, i64 0
%9 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 0
%10 = getelementptr inbounds [8 x i8], [8 x i8]* %4, i64 0, i64 0
%11 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 0
%12 = getelementptr inbounds [64 x i8], [64 x i8]* %6, i64 0, i64 0
%13 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%14 = tail call i64 @double_virtually_same(double %0, double 0.000000e+00)
%15 = icmp eq i64 %14, 0
%16 = select i1 %15, double %0, double 0.000000e+00
%17 = call i32 (i8*, i8*, ...) @sprintf(i8* %11, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), double %16)
%18 = call i32 @piece(i8* %8, i8 signext 46, i8* %11, i32 0)
%19 = call i32 @piece(i8* %10, i8 signext 46, i8* %11, i32 1)
%20 = call i32 @strlen(i8* %8)
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds [64 x i8], [64 x i8]* %2, i64 0, i64 %21
%23 = getelementptr inbounds i8, i8* %22, i64 -1
%24 = icmp ult i8* %23, %8
br i1 %24, label %48, label %25
25: ; preds = %1, %43
%26 = phi i8* [ %46, %43 ], [ %23, %1 ]
%27 = phi i32 [ %45, %43 ], [ 0, %1 ]
%28 = phi i8* [ %26, %43 ], [ %22, %1 ]
%29 = phi i8* [ %44, %43 ], [ %12, %1 ]
%30 = load i8, i8* %26
%31 = getelementptr inbounds i8, i8* %29, i64 1
store i8 %30, i8* %29
%32 = icmp ugt i8* %26, %8
br i1 %32, label %33, label %43
33: ; preds = %25
%34 = getelementptr inbounds i8, i8* %28, i64 -2
%35 = load i8, i8* %34
%36 = icmp eq i8 %35, 45
br i1 %36, label %43, label %37
37: ; preds = %33
%38 = add nsw i32 %27, 1
%39 = srem i32 %38, 3
%40 = icmp eq i32 %39, 0
br i1 %40, label %41, label %43
41: ; preds = %37
%42 = getelementptr inbounds i8, i8* %29, i64 2
store i8 44, i8* %31
br label %43
43: ; preds = %37, %41, %33, %25
%44 = phi i8* [ %42, %41 ], [ %31, %37 ], [ %31, %33 ], [ %31, %25 ]
%45 = phi i32 [ %38, %41 ], [ %38, %37 ], [ %27, %33 ], [ %27, %25 ]
%46 = getelementptr inbounds i8, i8* %26, i64 -1
%47 = icmp ult i8* %46, %8
br i1 %47, label %48, label %25
48: ; preds = %43, %1
%49 = phi i8* [ %12, %1 ], [ %44, %43 ]
store i8 0, i8* %49
%50 = call i32 @reverse_string(i8* %9, i8* %12)
%51 = call i32 (i8*, i8*, ...) @sprintf(i8* %13, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %9, i8* %10)
%52 = call i8* @strdup(i8* %13)
ret i8* %52
}