void num2str(string, num, field, ndec) char *string;
double num;
int field;
int ndec;
{
  char numform[8];
  if (field > 0) {
    if (ndec > 0) {
      sprintf(numform, "%%%d.%df", field, ndec);
      sprintf(string, numform, num);
    } else {
      sprintf(numform, "%%%dd", field);
      sprintf(string, numform, (int)num);
    }
  } else {
    if (ndec > 0) {
      sprintf(numform, "%%.%df", ndec);
      sprintf(string, numform, num);
    } else {
      sprintf(string, "%d", (int)num);
    }
  }
  return;
}


x86 assembly (clang, -O0):
<num2str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movsd  %xmm0,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
cmpl   $0x0,-0x14(%rbp)
jle    119a <num2str+0x8a>
cmpl   $0x0,-0x18(%rbp)
jle    116c <num2str+0x5c>
lea    -0x20(%rbp),%rdi
mov    -0x14(%rbp),%edx
mov    -0x18(%rbp),%ecx
lea    0xeb4(%rip),%rsi
mov    $0x0,%al
call   1030 <sprintf@plt>
mov    -0x8(%rbp),%rdi
lea    -0x20(%rbp),%rsi
movsd  -0x10(%rbp),%xmm0
mov    $0x1,%al
call   1030 <sprintf@plt>
jmp    1195 <num2str+0x85>
lea    -0x20(%rbp),%rdi
mov    -0x14(%rbp),%edx
lea    0xe8f(%rip),%rsi
mov    $0x0,%al
call   1030 <sprintf@plt>
mov    -0x8(%rbp),%rdi
lea    -0x20(%rbp),%rsi
cvttsd2si -0x10(%rbp),%edx
mov    $0x0,%al
call   1030 <sprintf@plt>
jmp    11ee <num2str+0xde>
cmpl   $0x0,-0x18(%rbp)
jle    11d2 <num2str+0xc2>
lea    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%edx
lea    0xe5d(%rip),%rsi
mov    $0x0,%al
call   1030 <sprintf@plt>
mov    -0x8(%rbp),%rdi
lea    -0x20(%rbp),%rsi
movsd  -0x10(%rbp),%xmm0
mov    $0x1,%al
call   1030 <sprintf@plt>
jmp    11e9 <num2str+0xd9>
mov    -0x8(%rbp),%rdi
cvttsd2si -0x10(%rbp),%edx
lea    0xe34(%rip),%rsi
mov    $0x0,%al
call   1030 <sprintf@plt>
jmp    11ee <num2str+0xde>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<num2str>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %edx,%ecx
mov    %rdi,%rbx
test   %esi,%esi
jle    1144 <num2str+0x34>
mov    %esi,%edx
movsd  %xmm0,0x8(%rsp)
test   %ecx,%ecx
jle    117b <num2str+0x6b>
lea    0xecd(%rip),%rsi
lea    0x10(%rsp),%r14
mov    %r14,%rdi
xor    %eax,%eax
call   1030 <sprintf@plt>
jmp    1166 <num2str+0x56>
test   %ecx,%ecx
jle    11ac <num2str+0x9c>
lea    0xec0(%rip),%rsi
lea    0x10(%rsp),%r14
mov    %r14,%rdi
mov    %ecx,%edx
xor    %eax,%eax
movsd  %xmm0,0x8(%rsp)
call   1030 <sprintf@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
movsd  0x8(%rsp),%xmm0
mov    $0x1,%al
call   1030 <sprintf@plt>
jmp    11a4 <num2str+0x94>
lea    0xe87(%rip),%rsi
lea    0x10(%rsp),%r14
mov    %r14,%rdi
xor    %eax,%eax
call   1030 <sprintf@plt>
cvttsd2si 0x8(%rsp),%edx
mov    %rbx,%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1030 <sprintf@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret
cvttsd2si %xmm0,%edx
lea    0xe5f(%rip),%rsi
mov    %rbx,%rdi
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
jmp    1030 <sprintf@plt>

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

LLVM IR (-O2):
@.str = [9 x i8] c"%%%d.%df\00"
@.str.1 = [6 x i8] c"%%%dd\00"
@.str.2 = [7 x i8] c"%%.%df\00"
@.str.3 = [3 x i8] c"%d\00"
define void @num2str(i8* nocapture writeonly %0, double %1, i32 %2, i32 %3) {
%5 = alloca [8 x i8]
%6 = getelementptr inbounds [8 x i8], [8 x i8]* %5, i64 0, i64 0
%7 = icmp sgt i32 %2, 0
%8 = icmp sgt i32 %3, 0
br i1 %7, label %9, label %17
9: ; preds = %4
br i1 %8, label %10, label %13
10: ; preds = %9
%11 = call i32 (i8*, i8*, ...) @sprintf(i8* %6, i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %2, i32 %3)
%12 = call i32 (i8*, i8*, ...) @sprintf(i8* dereferenceable(1) %0, i8* %6, double %1)
br label %24
13: ; preds = %9
%14 = call i32 (i8*, i8*, ...) @sprintf(i8* %6, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 %2)
%15 = fptosi double %1 to i32
%16 = call i32 (i8*, i8*, ...) @sprintf(i8* dereferenceable(1) %0, i8* %6, i32 %15)
br label %24
17: ; preds = %4
br i1 %8, label %18, label %21
18: ; preds = %17
%19 = call i32 (i8*, i8*, ...) @sprintf(i8* %6, i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32 %3)
%20 = call i32 (i8*, i8*, ...) @sprintf(i8* dereferenceable(1) %0, i8* %6, double %1)
br label %24
21: ; preds = %17
%22 = fptosi double %1 to i32
%23 = tail call i32 (i8*, i8*, ...) @sprintf(i8* dereferenceable(1) %0, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32 %22)
br label %24
24: ; preds = %18, %21, %10, %13
ret void
}