#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*/ fatal_error(char *, char *, int, char *);
scalar_t__ malloc(int);
int /*<<< orphan*/ snprintf(char *, int, char *, double);
int strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, int);
char *cstr_from_real(double val) {
  char buffer[1000];
  snprintf(buffer, 999, "%lf", val);
  int size = strlen(buffer);
  char *str = (char *)malloc((sizeof(char)) * (size + 1));
  if (str == NULL)
    fatal_error("cubestr.c", "cstr_from_real", 165,
                "Cannot allocate memory for return c-string while converting "
                "from real");
  strncpy(str, buffer, size + 1);
  return str;
}


x86 assembly (clang, -O0):
<cstr_from_real>:
push   %rbp
mov    %rsp,%rbp
sub    $0x400,%rsp
movsd  %xmm0,-0x8(%rbp)
lea    -0x3f0(%rbp),%rdi
movsd  -0x8(%rbp),%xmm0
mov    $0x3e7,%esi
lea    0xe88(%rip),%rdx
call   1050 <snprintf@plt>
lea    -0x3f0(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x3f4(%rbp)
mov    -0x3f4(%rbp),%eax
add    $0x1,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,-0x400(%rbp)
cmpq   $0x0,-0x400(%rbp)
jne    11d9 <cstr_from_real+0x89>
lea    0xe43(%rip),%rdi
lea    0xe46(%rip),%rsi
mov    $0xa5,%edx
lea    0xe49(%rip),%rcx
call   1060 <fatal_error@plt>
mov    -0x400(%rbp),%rdi
lea    -0x3f0(%rbp),%rsi
mov    -0x3f4(%rbp),%edx
add    $0x1,%edx
call   1030 <strncpy@plt>
mov    -0x400(%rbp),%rax
add    $0x400,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cstr_from_real>:
push   %r14
push   %rbx
sub    $0x3e8,%rsp
lea    0xe9f(%rip),%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x3e7,%esi
call   1050 <snprintf@plt>
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebx
add    $0x1,%ebx
mov    %ebx,%edi
call   1070 <malloc@plt>
mov    %rax,%r14
test   %rax,%rax
jne    11ac <cstr_from_real+0x5c>
lea    0xe70(%rip),%rdi
lea    0xe73(%rip),%rsi
lea    0xe7b(%rip),%rcx
mov    $0xa5,%edx
call   1060 <fatal_error@plt>
mov    %rsp,%rsi
mov    %r14,%rdi
mov    %ebx,%edx
call   1030 <strncpy@plt>
mov    %r14,%rax
add    $0x3e8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [4 x i8] c"%lf\00"
@.str.1 = [10 x i8] c"cubestr.c\00"
@.str.2 = [15 x i8] c"cstr_from_real\00"
@.str.3 = [70 x i8] c"Cannot allocate memory for return c-string while converting from real\00"
define i8* @cstr_from_real(double %0) {
%2 = alloca double
%3 = alloca [1000 x i8]
%4 = alloca i32
%5 = alloca i8*
store double %0, double* %2
%6 = getelementptr inbounds [1000 x i8], [1000 x i8]* %3, i64 0, i64 0
%7 = load double, double* %2
%8 = call i32 @snprintf(i8* %6, i32 999, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), double %7)
%9 = getelementptr inbounds [1000 x i8], [1000 x i8]* %3, i64 0, i64 0
%10 = call i32 @strlen(i8* %9)
store i32 %10, i32* %4
%11 = load i32, i32* %4
%12 = add nsw i32 %11, 1
%13 = sext i32 %12 to i64
%14 = mul i64 1, %13
%15 = trunc i64 %14 to i32
%16 = call i64 @malloc(i32 %15)
%17 = inttoptr i64 %16 to i8*
store i8* %17, i8** %5
%18 = load i8*, i8** %5
%19 = icmp eq i8* %18, null
br i1 %19, label %20, label %22
20: ; preds = %1
%21 = call i32 @fatal_error(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i32 165, i8* getelementptr inbounds ([70 x i8], [70 x i8]* @.str.3, i64 0, i64 0))
br label %22
22: ; preds = %20, %1
%23 = load i8*, i8** %5
%24 = getelementptr inbounds [1000 x i8], [1000 x i8]* %3, i64 0, i64 0
%25 = load i32, i32* %4
%26 = add nsw i32 %25, 1
%27 = call i32 @strncpy(i8* %23, i8* %24, i32 %26)
%28 = load i8*, i8** %5
ret i8* %28
}

LLVM IR (-O2):
@.str = [4 x i8] c"%lf\00"
@.str.1 = [10 x i8] c"cubestr.c\00"
@.str.2 = [15 x i8] c"cstr_from_real\00"
@.str.3 = [70 x i8] c"Cannot allocate memory for return c-string while converting from real\00"
define i8* @cstr_from_real(double %0) {
%2 = alloca [1000 x i8]
%3 = getelementptr inbounds [1000 x i8], [1000 x i8]* %2, i64 0, i64 0
%4 = call i32 @snprintf(i8* %3, i32 999, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), double %0)
%5 = call i32 @strlen(i8* %3)
%6 = add nsw i32 %5, 1
%7 = call i64 @malloc(i32 %6)
%8 = inttoptr i64 %7 to i8*
%9 = icmp eq i64 %7, 0
br i1 %9, label %10, label %12
10: ; preds = %1
%11 = call i32 @fatal_error(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i32 165, i8* getelementptr inbounds ([70 x i8], [70 x i8]* @.str.3, i64 0, i64 0))
br label %12
12: ; preds = %10, %1
%13 = call i32 @strncpy(i8* %8, i8* %3, i32 %6)
ret i8* %8
}