#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 */
typedef char wchar_t;

/* Variables and functions */
int snprintf(char *, size_t, char *, double);
int strlen(char *);
size_t double2wcsn(double value, wchar_t *ptr, size_t max_count) {
  int len;
  int n;
  len = snprintf((char *)ptr, max_count, "%f", value);
  if (!(len > 0)) {
    ptr[0] = L'\0';
    return 0;
  }
  len = strlen((char *)ptr);
  for (n = len + 1; n >= 1; n--) {
    *(ptr + n - 1) = *(((char *)ptr) + n - 1);
  }
  return len;
}


x86 assembly (clang, -O0):
<double2wcsn>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movsd  %xmm0,-0x10(%rbp)
mov    %rdi,-0x18(%rbp)
mov    %rsi,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
movsd  -0x10(%rbp),%xmm0
lea    0xeb7(%rip),%rdx
call   1040 <snprintf@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jg     116f <double2wcsn+0x4f>
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    11bc <double2wcsn+0x9c>
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
cmpl   $0x1,-0x28(%rbp)
jl     11b4 <double2wcsn+0x94>
mov    -0x18(%rbp),%rax
movslq -0x28(%rbp),%rcx
mov    -0x1(%rax,%rcx,1),%dl
mov    -0x18(%rbp),%rax
movslq -0x28(%rbp),%rcx
mov    %dl,-0x1(%rax,%rcx,1)
mov    -0x28(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x28(%rbp)
jmp    1184 <double2wcsn+0x64>
movslq -0x24(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<double2wcsn>:
push   %rbx
mov    %rdi,%rbx
lea    0xed5(%rip),%rdx
call   1040 <snprintf@plt>
test   %eax,%eax
jle    1140 <double2wcsn+0x20>
mov    %rbx,%rdi
call   1030 <strlen@plt>
cltq
pop    %rbx
ret
movb   $0x0,(%rbx)
xor    %eax,%eax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [3 x i8] c"%f\00"
define i64 @double2wcsn(double %0, i8* %1, i64 %2) {
%4 = alloca i64
%5 = alloca double
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32
store double %0, double* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%10 = load i8*, i8** %6
%11 = load i64, i64* %7
%12 = load double, double* %5
%13 = call i32 @snprintf(i8* %10, i64 %11, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), double %12)
store i32 %13, i32* %8
%14 = load i32, i32* %8
%15 = icmp sgt i32 %14, 0
br i1 %15, label %19, label %16
16: ; preds = %3
%17 = load i8*, i8** %6
%18 = getelementptr inbounds i8, i8* %17, i64 0
store i8 0, i8* %18
store i64 0, i64* %4
br label %45
19: ; preds = %3
%20 = load i8*, i8** %6
%21 = call i32 @strlen(i8* %20)
store i32 %21, i32* %8
%22 = load i32, i32* %8
%23 = add nsw i32 %22, 1
store i32 %23, i32* %9
br label %24
24: ; preds = %39, %19
%25 = load i32, i32* %9
%26 = icmp sge i32 %25, 1
br i1 %26, label %27, label %42
27: ; preds = %24
%28 = load i8*, i8** %6
%29 = load i32, i32* %9
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i8, i8* %28, i64 %30
%32 = getelementptr inbounds i8, i8* %31, i64 -1
%33 = load i8, i8* %32
%34 = load i8*, i8** %6
%35 = load i32, i32* %9
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds i8, i8* %34, i64 %36
%38 = getelementptr inbounds i8, i8* %37, i64 -1
store i8 %33, i8* %38
br label %39
39: ; preds = %27
%40 = load i32, i32* %9
%41 = add nsw i32 %40, -1
store i32 %41, i32* %9
br label %24
42: ; preds = %24
%43 = load i32, i32* %8
%44 = sext i32 %43 to i64
store i64 %44, i64* %4
br label %45
45: ; preds = %42, %16
%46 = load i64, i64* %4
ret i64 %46
}

LLVM IR (-O2):
@.str = [3 x i8] c"%f\00"
define i64 @double2wcsn(double %0, i8* %1, i64 %2) {
%4 = tail call i32 @snprintf(i8* %1, i64 %2, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), double %0)
%5 = icmp sgt i32 %4, 0
br i1 %5, label %7, label %6
6: ; preds = %3
store i8 0, i8* %1
br label %10
7: ; preds = %3
%8 = tail call i32 @strlen(i8* %1)
%9 = sext i32 %8 to i64
br label %10
10: ; preds = %7, %6
%11 = phi i64 [ %9, %7 ], [ 0, %6 ]
ret i64 %11
}