#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*/ sprintf(char *, char *, int, ...);
void num2str(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
cvttsd2si -0x10(%rbp),%edx
mov    $0x0,%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
cvttsd2si -0x10(%rbp),%edx
mov    $0x0,%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    1163 <num2str+0x53>
lea    0xecd(%rip),%rsi
lea    0x10(%rsp),%r14
mov    %r14,%rdi
xor    %eax,%eax
call   1030 <sprintf@plt>
jmp    1179 <num2str+0x69>
test   %ecx,%ecx
jle    1194 <num2str+0x84>
lea    0xec0(%rip),%rsi
lea    0x10(%rsp),%r14
mov    %r14,%rdi
mov    %ecx,%edx
xor    %eax,%eax
movsd  %xmm0,0x8(%rsp)
jmp    1174 <num2str+0x64>
lea    0xe9f(%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    0xe77(%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 %35
12: ; preds = %4
%13 = load i32, i32* %8
%14 = icmp sgt i32 %13, 0
br i1 %14, label %15, label %25
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*, i32, ...) @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 = fptosi double %22 to i32
%24 = call i32 (i8*, i8*, i32, ...) @sprintf(i8* %20, i8* %21, i32 %23)
br label %34
25: ; preds = %12
%26 = getelementptr inbounds [8 x i8], [8 x i8]* %9, i64 0, i64 0
%27 = load i32, i32* %7
%28 = call i32 (i8*, i8*, i32, ...) @sprintf(i8* %26, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 %27)
%29 = load i8*, i8** %5
%30 = getelementptr inbounds [8 x i8], [8 x i8]* %9, i64 0, i64 0
%31 = load double, double* %6
%32 = fptosi double %31 to i32
%33 = call i32 (i8*, i8*, i32, ...) @sprintf(i8* %29, i8* %30, i32 %32)
br label %34
34: ; preds = %25, %15
br label %53
35: ; preds = %4
%36 = load i32, i32* %8
%37 = icmp sgt i32 %36, 0
br i1 %37, label %38, label %47
38: ; preds = %35
%39 = getelementptr inbounds [8 x i8], [8 x i8]* %9, i64 0, i64 0
%40 = load i32, i32* %8
%41 = call i32 (i8*, i8*, i32, ...) @sprintf(i8* %39, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32 %40)
%42 = load i8*, i8** %5
%43 = getelementptr inbounds [8 x i8], [8 x i8]* %9, i64 0, i64 0
%44 = load double, double* %6
%45 = fptosi double %44 to i32
%46 = call i32 (i8*, i8*, i32, ...) @sprintf(i8* %42, i8* %43, i32 %45)
br label %52
47: ; preds = %35
%48 = load i8*, i8** %5
%49 = load double, double* %6
%50 = fptosi double %49 to i32
%51 = call i32 (i8*, i8*, i32, ...) @sprintf(i8* %48, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32 %50)
br label %52
52: ; preds = %47, %38
br label %53
53: ; preds = %52, %34
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 %18
9: ; preds = %4
br i1 %8, label %10, label %14
10: ; preds = %9
%11 = call i32 (i8*, i8*, i32, ...) @sprintf(i8* %6, i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %2, i32 %3)
%12 = fptosi double %1 to i32
%13 = call i32 (i8*, i8*, i32, ...) @sprintf(i8* dereferenceable(1) %0, i8* %6, i32 %12)
br label %26
14: ; preds = %9
%15 = call i32 (i8*, i8*, i32, ...) @sprintf(i8* %6, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 %2)
%16 = fptosi double %1 to i32
%17 = call i32 (i8*, i8*, i32, ...) @sprintf(i8* dereferenceable(1) %0, i8* %6, i32 %16)
br label %26
18: ; preds = %4
br i1 %8, label %19, label %23
19: ; preds = %18
%20 = call i32 (i8*, i8*, i32, ...) @sprintf(i8* %6, i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32 %3)
%21 = fptosi double %1 to i32
%22 = call i32 (i8*, i8*, i32, ...) @sprintf(i8* dereferenceable(1) %0, i8* %6, i32 %21)
br label %26
23: ; preds = %18
%24 = fptosi double %1 to i32
%25 = tail call i32 (i8*, i8*, i32, ...) @sprintf(i8* dereferenceable(1) %0, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32 %24)
br label %26
26: ; preds = %19, %23, %10, %14
ret void
}