#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*/ exit(int /*<<< orphan*/);
char *malloc(int);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ snprintf(char *, int, char *, double);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
char *doubleToString(double number, int type) {
  char result_buffer[100];
  char *result_string;
  if (type == 2)
    snprintf(result_buffer, 100, "%ld", (long)number);
  else
    snprintf(result_buffer, 100, "%f", number);
  result_string = malloc(sizeof(char) * strlen(result_buffer) + 1);
  if (result_string == NULL) {
    printf("Out of mem in numberToString");
    exit(0);
  }
  strcpy(result_string, result_buffer);
  return result_string;
}


x86 assembly (clang, -O0):
<doubleToString>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
movsd  %xmm0,-0x8(%rbp)
mov    %edi,-0xc(%rbp)
cmpl   $0x2,-0xc(%rbp)
jne    11a2 <doubleToString+0x42>
lea    -0x70(%rbp),%rdi
cvttsd2si -0x8(%rbp),%rax
cvtsi2sd %rax,%xmm0
mov    $0x64,%esi
lea    0xe68(%rip),%rdx
call   1060 <snprintf@plt>
jmp    11bc <doubleToString+0x5c>
lea    -0x70(%rbp),%rdi
movsd  -0x8(%rbp),%xmm0
mov    $0x64,%esi
lea    0xe4d(%rip),%rdx
call   1060 <snprintf@plt>
lea    -0x70(%rbp),%rdi
call   1040 <strlen@plt>
cltq
shl    $0x0,%rax
add    $0x1,%rax
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,-0x78(%rbp)
cmpq   $0x0,-0x78(%rbp)
jne    11f8 <doubleToString+0x98>
lea    0xe1b(%rip),%rdi
call   1050 <printf@plt>
xor    %edi,%edi
call   1080 <exit@plt>
mov    -0x78(%rbp),%rdi
lea    -0x70(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x78(%rbp),%rax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<doubleToString>:
push   %rbx
sub    $0x70,%rsp
cmp    $0x2,%edi
jne    1180 <doubleToString+0x20>
cvttsd2si %xmm0,%rax
xorps  %xmm0,%xmm0
cvtsi2sd %rax,%xmm0
lea    0xe82(%rip),%rdx
jmp    1187 <doubleToString+0x27>
lea    0xe7d(%rip),%rdx
mov    %rsp,%rdi
mov    $0x64,%esi
call   1060 <snprintf@plt>
mov    %rsp,%rdi
call   1040 <strlen@plt>
lea    0x1(%rax),%edi
call   1070 <malloc@plt>
test   %rax,%rax
je     11c0 <doubleToString+0x60>
mov    %rax,%rbx
mov    %rsp,%rsi
mov    %rax,%rdi
call   1030 <strcpy@plt>
mov    %rbx,%rax
add    $0x70,%rsp
pop    %rbx
ret
lea    0xe40(%rip),%rdi
call   1050 <printf@plt>
xor    %edi,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [4 x i8] c"%ld\00"
@.str.1 = [3 x i8] c"%f\00"
@.str.2 = [29 x i8] c"Out of mem in numberToString\00"
define i8* @doubleToString(double %0, i32 %1) {
%3 = alloca double
%4 = alloca i32
%5 = alloca [100 x i8]
%6 = alloca i8*
store double %0, double* %3
store i32 %1, i32* %4
%7 = load i32, i32* %4
%8 = icmp eq i32 %7, 2
br i1 %8, label %9, label %15
9: ; preds = %2
%10 = getelementptr inbounds [100 x i8], [100 x i8]* %5, i64 0, i64 0
%11 = load double, double* %3
%12 = fptosi double %11 to i64
%13 = sitofp i64 %12 to double
%14 = call i32 @snprintf(i8* %10, i32 100, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), double %13)
br label %19
15: ; preds = %2
%16 = getelementptr inbounds [100 x i8], [100 x i8]* %5, i64 0, i64 0
%17 = load double, double* %3
%18 = call i32 @snprintf(i8* %16, i32 100, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), double %17)
br label %19
19: ; preds = %15, %9
%20 = getelementptr inbounds [100 x i8], [100 x i8]* %5, i64 0, i64 0
%21 = call i32 @strlen(i8* %20)
%22 = sext i32 %21 to i64
%23 = mul i64 1, %22
%24 = add i64 %23, 1
%25 = trunc i64 %24 to i32
%26 = call i8* @malloc(i32 %25)
store i8* %26, i8** %6
%27 = load i8*, i8** %6
%28 = icmp eq i8* %27, null
br i1 %28, label %29, label %32
29: ; preds = %19
%30 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0))
%31 = call i32 @exit(i32 0)
unreachable
32: ; preds = %19
%33 = load i8*, i8** %6
%34 = getelementptr inbounds [100 x i8], [100 x i8]* %5, i64 0, i64 0
%35 = call i32 @strcpy(i8* %33, i8* %34)
%36 = load i8*, i8** %6
ret i8* %36
}

LLVM IR (-O2):
@.str = [4 x i8] c"%ld\00"
@.str.1 = [3 x i8] c"%f\00"
@.str.2 = [29 x i8] c"Out of mem in numberToString\00"
define i8* @doubleToString(double %0, i32 %1) {
%3 = alloca [100 x i8]
%4 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%5 = icmp eq i32 %1, 2
br i1 %5, label %6, label %10
6: ; preds = %2
%7 = fptosi double %0 to i64
%8 = sitofp i64 %7 to double
%9 = call i32 @snprintf(i8* %4, i32 100, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), double %8)
br label %12
10: ; preds = %2
%11 = call i32 @snprintf(i8* %4, i32 100, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), double %0)
br label %12
12: ; preds = %10, %6
%13 = call i32 @strlen(i8* %4)
%14 = add i32 %13, 1
%15 = call i8* @malloc(i32 %14)
%16 = icmp eq i8* %15, null
br i1 %16, label %17, label %20
17: ; preds = %12
%18 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0))
%19 = call i32 @exit(i32 0)
unreachable
20: ; preds = %12
%21 = call i32 @strcpy(i8* %15, i8* %4)
ret i8* %15
}