#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 */
char *ecvt(double, int, int *, int *);
int /*<<< orphan*/ sprintf(char *, char *, int, double);
scalar_t__ strcspn(char *, char *);
int strspn(char *, char *);
char *fcvt(double x, int n, int *dp, int *sign) {
  char tmp[1500];
  int i, lz;
  if (n > 1400U)
    n = 1400;
  sprintf(tmp, "%.*f", n, x);
  i = (tmp[0] == '-');
  if (tmp[i] == '0')
    lz = strspn(tmp + i + 2, "0");
  else
    lz = -(int)strcspn(tmp + i, ".");
  if (n <= lz) {
    *sign = i;
    *dp = 0;
    if (n > 14U)
      n = 14;
    return "000000000000000" + 14 - n;
  }
  return ecvt(x, n - lz, dp, sign);
}


x86 assembly (clang, -O0):
<fcvt>:
push   %rbp
mov    %rsp,%rbp
sub    $0x620,%rsp
movsd  %xmm0,-0x10(%rbp)
mov    %edi,-0x14(%rbp)
mov    %rsi,-0x20(%rbp)
mov    %rdx,-0x28(%rbp)
cmpl   $0x578,-0x14(%rbp)
jbe    116f <fcvt+0x2f>
movl   $0x578,-0x14(%rbp)
lea    -0x610(%rbp),%rdi
mov    -0x14(%rbp),%edx
movsd  -0x10(%rbp),%xmm0
lea    0xe7b(%rip),%rsi
call   1060 <sprintf@plt>
movsbl -0x610(%rbp),%eax
cmp    $0x2d,%eax
sete   %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x614(%rbp)
movslq -0x614(%rbp),%rax
movsbl -0x610(%rbp,%rax,1),%eax
ff
cmp    $0x30,%eax
jne    11e6 <fcvt+0xa6>
lea    -0x610(%rbp),%rdi
movslq -0x614(%rbp),%rax
add    %rax,%rdi
add    $0x2,%rdi
lea    0xe3f(%rip),%rsi
call   1040 <strspn@plt>
mov    %eax,-0x618(%rbp)
jmp    120f <fcvt+0xcf>
lea    -0x610(%rbp),%rdi
movslq -0x614(%rbp),%rax
add    %rax,%rdi
lea    0xe07(%rip),%rsi
call   1050 <strcspn@plt>
mov    %eax,%ecx
xor    %eax,%eax
sub    %ecx,%eax
mov    %eax,-0x618(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x618(%rbp),%eax
jg     1265 <fcvt+0x125>
mov    -0x614(%rbp),%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x20(%rbp),%rax
movl   $0x0,(%rax)
cmpl   $0xe,-0x14(%rbp)
jbe    1245 <fcvt+0x105>
movl   $0xe,-0x14(%rbp)
movslq -0x14(%rbp),%rax
xor    %ecx,%ecx
sub    %rax,%rcx
lea    0xdb2(%rip),%rax
add    $0xe,%rax
add    %rcx,%rax
mov    %rax,-0x8(%rbp)
jmp    1284 <fcvt+0x144>
movsd  -0x10(%rbp),%xmm0
mov    -0x14(%rbp),%edi
sub    -0x618(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1030 <ecvt@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x620,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fcvt>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x5f0,%rsp
mov    %rdx,%r14
mov    %rsi,%r15
mov    %edi,%r12d
cmp    $0x578,%edi
mov    $0x578,%ebx
cmovb  %edi,%ebx
lea    0xe93(%rip),%rsi
lea    0x10(%rsp),%rdi
mov    %ebx,%edx
movsd  %xmm0,0x8(%rsp)
call   1060 <sprintf@plt>
xor    %ebp,%ebp
cmpb   $0x2d,0x10(%rsp)
sete   %bpl
lea    (%rsp,%rbp,1),%rdi
add    $0x10,%rdi
cmpb   $0x30,0x10(%rsp,%rbp,1)
jne    11ab <fcvt+0x6b>
add    $0x2,%rdi
lea    0xe71(%rip),%rsi
call   1040 <strspn@plt>
jmp    11b9 <fcvt+0x79>
lea    0xe53(%rip),%rsi
call   1050 <strcspn@plt>
neg    %eax
mov    %ebx,%edi
sub    %eax,%edi
jle    11df <fcvt+0x9f>
movsd  0x8(%rsp),%xmm0
mov    %r15,%rsi
mov    %r14,%rdx
add    $0x5f0,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <ecvt@plt>
mov    %ebp,(%r14)
movl   $0x0,(%r15)
cmp    $0xe,%ebx
mov    $0xe,%eax
cmovb  %r12d,%eax
movslq %eax,%rcx
lea    0xe08(%rip),%rax
sub    %rcx,%rax
add    $0xe,%rax
add    $0x5f0,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [5 x i8] c"%.*f\00"
@.str.1 = [2 x i8] c"0\00"
@.str.2 = [2 x i8] c".\00"
@.str.3 = [16 x i8] c"000000000000000\00"
define i8* @fcvt(double %0, i32 %1, i32* %2, i32* %3) {
%5 = alloca i8*
%6 = alloca double
%7 = alloca i32
%8 = alloca i32*
%9 = alloca i32*
%10 = alloca [1500 x i8]
%11 = alloca i32
%12 = alloca i32
store double %0, double* %6
store i32 %1, i32* %7
store i32* %2, i32** %8
store i32* %3, i32** %9
%13 = load i32, i32* %7
%14 = icmp ugt i32 %13, 1400
br i1 %14, label %15, label %16
15: ; preds = %4
store i32 1400, i32* %7
br label %16
16: ; preds = %15, %4
%17 = getelementptr inbounds [1500 x i8], [1500 x i8]* %10, i64 0, i64 0
%18 = load i32, i32* %7
%19 = load double, double* %6
%20 = call i32 @sprintf(i8* %17, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %18, double %19)
%21 = getelementptr inbounds [1500 x i8], [1500 x i8]* %10, i64 0, i64 0
%22 = load i8, i8* %21
%23 = sext i8 %22 to i32
%24 = icmp eq i32 %23, 45
%25 = zext i1 %24 to i32
store i32 %25, i32* %11
%26 = load i32, i32* %11
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds [1500 x i8], [1500 x i8]* %10, i64 0, i64 %27
%29 = load i8, i8* %28
%30 = sext i8 %29 to i32
%31 = icmp eq i32 %30, 48
br i1 %31, label %32, label %39
32: ; preds = %16
%33 = getelementptr inbounds [1500 x i8], [1500 x i8]* %10, i64 0, i64 0
%34 = load i32, i32* %11
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds i8, i8* %33, i64 %35
%37 = getelementptr inbounds i8, i8* %36, i64 2
%38 = call i32 @strspn(i8* %37, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32 %38, i32* %12
br label %47
39: ; preds = %16
%40 = getelementptr inbounds [1500 x i8], [1500 x i8]* %10, i64 0, i64 0
%41 = load i32, i32* %11
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds i8, i8* %40, i64 %42
%44 = call i64 @strcspn(i8* %43, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%45 = trunc i64 %44 to i32
%46 = sub nsw i32 0, %45
store i32 %46, i32* %12
br label %47
47: ; preds = %39, %32
%48 = load i32, i32* %7
%49 = load i32, i32* %12
%50 = icmp sle i32 %48, %49
br i1 %50, label %51, label %63
51: ; preds = %47
%52 = load i32, i32* %11
%53 = load i32*, i32** %9
store i32 %52, i32* %53
%54 = load i32*, i32** %8
store i32 0, i32* %54
%55 = load i32, i32* %7
%56 = icmp ugt i32 %55, 14
br i1 %56, label %57, label %58
57: ; preds = %51
store i32 14, i32* %7
br label %58
58: ; preds = %57, %51
%59 = load i32, i32* %7
%60 = sext i32 %59 to i64
%61 = sub i64 0, %60
%62 = getelementptr inbounds i8, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 14), i64 %61
store i8* %62, i8** %5
br label %71
63: ; preds = %47
%64 = load double, double* %6
%65 = load i32, i32* %7
%66 = load i32, i32* %12
%67 = sub nsw i32 %65, %66
%68 = load i32*, i32** %8
%69 = load i32*, i32** %9
%70 = call i8* @ecvt(double %64, i32 %67, i32* %68, i32* %69)
store i8* %70, i8** %5
br label %71
71: ; preds = %63, %58
%72 = load i8*, i8** %5
ret i8* %72
}

LLVM IR (-O2):
@.str = [5 x i8] c"%.*f\00"
@.str.1 = [2 x i8] c"0\00"
@.str.2 = [2 x i8] c".\00"
@.str.3 = [16 x i8] c"000000000000000\00"
define i8* @fcvt(double %0, i32 %1, i32* %2, i32* %3) {
%5 = alloca [1500 x i8]
%6 = getelementptr inbounds [1500 x i8], [1500 x i8]* %5, i64 0, i64 0
%7 = icmp ult i32 %1, 1400
%8 = select i1 %7, i32 %1, i32 1400
%9 = call i32 @sprintf(i8* %6, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %8, double %0)
%10 = load i8, i8* %6
%11 = icmp eq i8 %10, 45
%12 = zext i1 %11 to i32
%13 = zext i1 %11 to i64
%14 = getelementptr inbounds [1500 x i8], [1500 x i8]* %5, i64 0, i64 %13
%15 = load i8, i8* %14
%16 = icmp eq i8 %15, 48
br i1 %16, label %17, label %20
17: ; preds = %4
%18 = getelementptr inbounds i8, i8* %14, i64 2
%19 = call i32 @strspn(i8* %18, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
br label %24
20: ; preds = %4
%21 = call i64 @strcspn(i8* %14, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%22 = trunc i64 %21 to i32
%23 = sub nsw i32 0, %22
br label %24
24: ; preds = %20, %17
%25 = phi i32 [ %19, %17 ], [ %23, %20 ]
%26 = icmp sgt i32 %8, %25
br i1 %26, label %33, label %27
27: ; preds = %24
store i32 %12, i32* %3
store i32 0, i32* %2
%28 = icmp ult i32 %8, 14
%29 = select i1 %28, i32 %1, i32 14
%30 = sext i32 %29 to i64
%31 = sub nsw i64 0, %30
%32 = getelementptr inbounds i8, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 14), i64 %31
br label %36
33: ; preds = %24
%34 = sub nsw i32 %8, %25
%35 = tail call i8* @ecvt(double %0, i32 %34, i32* %2, i32* %3)
br label %36
36: ; preds = %33, %27
%37 = phi i8* [ %32, %27 ], [ %35, %33 ]
ret i8* %37
}