#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*/ piece(char *, char, char *, int);
int /*<<< orphan*/ reverse_string(char *, char *);
int /*<<< orphan*/ sprintf(char *, char *, ...);
int strlen(char *);
scalar_t__ timlib_double_virtually_same(double, double);
char *commas_in_double(char *destination, 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;
  register int counter = 0;
  if (timlib_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 destination;
}


x86 assembly (clang, -O0):
<commas_in_double>:
push   %rbp
mov    %rsp,%rbp
sub    $0x180,%rsp
mov    %rdi,-0x8(%rbp)
movsd  %xmm0,-0x10(%rbp)
lea    -0x160(%rbp),%rax
mov    %rax,-0x168(%rbp)
movl   $0x0,-0x174(%rbp)
movsd  -0x10(%rbp),%xmm0
xorps  %xmm1,%xmm1
call   1030 <timlib_double_virtually_same@plt>
cmp    $0x0,%rax
je     119b <commas_in_double+0x4b>
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x10(%rbp)
lea    -0x120(%rbp),%rdi
movsd  -0x10(%rbp),%xmm0
lea    0xe52(%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,-0x180(%rbp)
lea    -0x50(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    -0x180(%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     12e0 <commas_in_double+0x190>
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    12c9 <commas_in_double+0x179>
mov    -0x170(%rbp),%rax
movsbl -0x1(%rax),%eax
cmp    $0x2d,%eax
je     12c9 <commas_in_double+0x179>
mov    -0x174(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x174(%rbp)
mov    $0x3,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
jne    12c4 <commas_in_double+0x174>
mov    -0x170(%rbp),%rax
lea    -0x50(%rbp),%rcx
cmp    %rcx,%rax
je     12bf <commas_in_double+0x16f>
mov    -0x168(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x168(%rbp)
movb   $0x2c,(%rax)
jmp    12c4 <commas_in_double+0x174>
jmp    12c9 <commas_in_double+0x179>
mov    -0x170(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x170(%rbp)
jmp    1217 <commas_in_double+0xc7>
mov    -0x168(%rbp),%rax
movb   $0x0,(%rax)
lea    -0x90(%rbp),%rdi
lea    -0x160(%rbp),%rsi
call   1040 <reverse_string@plt>
mov    -0x8(%rbp),%rdi
lea    -0x90(%rbp),%rdx
lea    -0x98(%rbp),%rcx
lea    0xcf0(%rip),%rsi
mov    $0x0,%al
call   1060 <sprintf@plt>
mov    -0x8(%rbp),%rax
add    $0x180,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<commas_in_double>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x150,%rsp
movsd  %xmm0,(%rsp)
mov    %rdi,%r14
xorps  %xmm1,%xmm1
call   1030 <timlib_double_virtually_same@plt>
movsd  (%rsp),%xmm0
test   %rax,%rax
je     117c <commas_in_double+0x2c>
xorps  %xmm0,%xmm0
lea    0xe7d(%rip),%rsi
lea    0xd0(%rsp),%r15
mov    %r15,%rdi
mov    $0x1,%al
call   1060 <sprintf@plt>
lea    0x50(%rsp),%rbp
xor    %r12d,%r12d
mov    %rbp,%rdi
mov    $0x2e,%esi
mov    %r15,%rdx
xor    %ecx,%ecx
call   1070 <piece@plt>
lea    0x8(%rsp),%rdi
mov    $0x2e,%esi
mov    %r15,%rdx
mov    $0x1,%ecx
call   1070 <piece@plt>
mov    %rbp,%rdi
call   1050 <strlen@plt>
cltq
add    %rsp,%rax
add    $0x4f,%rax
cmp    %rbp,%rax
jae    1225 <commas_in_double+0xd5>
lea    0x10(%rsp),%rcx
movb   $0x0,(%rcx)
lea    0x90(%rsp),%rbx
lea    0x10(%rsp),%rsi
mov    %rbx,%rdi
call   1040 <reverse_string@plt>
lea    0xe06(%rip),%rsi
lea    0x8(%rsp),%rcx
mov    %r14,%rdi
mov    %rbx,%rdx
xor    %eax,%eax
call   1060 <sprintf@plt>
mov    %r14,%rax
add    $0x150,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0x10(%rsp),%rdx
jmp    123c <commas_in_double+0xec>
nopl   0x0(%rax)
add    $0xffffffffffffffff,%rax
mov    %rcx,%rdx
cmp    %rbp,%rax
jb     11e1 <commas_in_double+0x91>
movzbl (%rax),%ebx
lea    0x1(%rdx),%rcx
mov    %bl,(%rdx)
cmp    %rbp,%rax
jbe    1230 <commas_in_double+0xe0>
cmpb   $0x2d,-0x1(%rax)
je     1230 <commas_in_double+0xe0>
add    $0x1,%r12d
imul   $0xaaaaaaab,%r12d,%esi
add    $0x2aaaaaaa,%esi
cmp    $0x55555554,%esi
ja     1230 <commas_in_double+0xe0>
movb   $0x2c,0x1(%rdx)
add    $0x2,%rdx
mov    %rdx,%rcx
jmp    1230 <commas_in_double+0xe0>

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

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