#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 */
long double DBL_MAX;
long double DBL_MIN;
int /*<<< orphan*/ ERANGE;
double HUGE_VAL;
int /*<<< orphan*/ errno;
long double strtold(char const *, char **);
double strtod(const char *restrict nptr, char **restrict endptr) {
  long double ld = strtold(nptr, endptr);
  if (ld > DBL_MAX) {
    errno = ERANGE;
    return HUGE_VAL;
  } else if (ld < -DBL_MAX) {
    errno = ERANGE;
    return -HUGE_VAL;
  } else if (ld > -DBL_MIN && ld < DBL_MIN) {
    errno = ERANGE;
    return ld < 0 ? -0. : +0.;
  }
  return (double)ld;
}


x86 assembly (clang, -O0):
<strtod>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1030 <strtold@plt>
fstpt  -0x30(%rbp)
fldt   -0x30(%rbp)
mov    0x2e8e(%rip),%rax
fldt   (%rax)
fxch   %st(1)
fucomip %st(1),%st
fstp   %st(0)
jbe    1174 <strtod+0x64>
jmp    114d <strtod+0x3d>
mov    0x2e8c(%rip),%rax
mov    (%rax),%ecx
mov    0x2e73(%rip),%rax
mov    %ecx,(%rax)
mov    0x2e82(%rip),%rax
movsd  (%rax),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    1274 <strtod+0x164>
fldt   -0x30(%rbp)
mov    0x2e4a(%rip),%rax
fldt   (%rax)
fchs
fucomip %st(1),%st
fstp   %st(0)
jbe    11cf <strtod+0xbf>
jmp    1191 <strtod+0x81>
mov    0x2e48(%rip),%rax
mov    (%rax),%ecx
mov    0x2e2f(%rip),%rax
mov    %ecx,(%rax)
mov    0x2e3e(%rip),%rax
movsd  (%rax),%xmm0
movq   %xmm0,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    1274 <strtod+0x164>
fldt   -0x30(%rbp)
mov    0x2de7(%rip),%rax
fldt   (%rax)
fchs
fxch   %st(1)
fucomip %st(1),%st
fstp   %st(0)
jbe    125a <strtod+0x14a>
jmp    11ee <strtod+0xde>
fldt   -0x30(%rbp)
mov    0x2dc8(%rip),%rax
fldt   (%rax)
fucomip %st(1),%st
fstp   %st(0)
jbe    125a <strtod+0x14a>
jmp    1209 <strtod+0xf9>
xor    %eax,%eax
mov    0x2dce(%rip),%rax
mov    (%rax),%ecx
mov    0x2db5(%rip),%rax
mov    %ecx,(%rax)
fldt   -0x30(%rbp)
fldz
fucomip %st(1),%st
fstp   %st(0)
movsd  0xdd2(%rip),%xmm0
xorps  %xmm1,%xmm1
movsd  %xmm1,-0x48(%rbp)
movsd  %xmm0,-0x40(%rbp)
ja     124b <strtod+0x13b>
movsd  -0x48(%rbp),%xmm0
movsd  %xmm0,-0x40(%rbp)
movsd  -0x40(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    1274 <strtod+0x164>
jmp    125f <strtod+0x14f>
jmp    1264 <strtod+0x154>
fldt   -0x30(%rbp)
fstpl  -0x38(%rbp)
movsd  -0x38(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strtod>:
push   %rax
call   1030 <strtold@plt>
mov    0x2eab(%rip),%rax
fldt   (%rax)
fxch   %st(1)
fucomi %st(1),%st
jbe    1148 <strtod+0x38>
fstp   %st(0)
fstp   %st(0)
mov    0x2eb0(%rip),%rax
mov    (%rax),%eax
mov    0x2e97(%rip),%rcx
mov    %eax,(%rcx)
mov    0x2ea6(%rip),%rax
movsd  (%rax),%xmm0
pop    %rax
ret
fxch   %st(1)
fchs
fucomip %st(1),%st
jbe    1178 <strtod+0x68>
fstp   %st(0)
mov    0x2e87(%rip),%rax
mov    (%rax),%eax
mov    0x2e6e(%rip),%rcx
mov    %eax,(%rcx)
mov    0x2e7d(%rip),%rax
movsd  (%rax),%xmm0
xorps  0xe9a(%rip),%xmm0
pop    %rax
ret
mov    0x2e41(%rip),%rax
fldt   (%rax)
fld    %st(0)
fchs
fxch   %st(2)
fucomi %st(2),%st
fstp   %st(2)
jbe    11b4 <strtod+0xa4>
fucomip %st(1),%st
fldz
jbe    11b4 <strtod+0xa4>
fstp   %st(0)
mov    0x2e44(%rip),%rax
mov    (%rax),%eax
mov    0x2e2b(%rip),%rcx
mov    %eax,(%rcx)
fldz
fucomip %st(1),%st
fstp   %st(0)
ja     11c0 <strtod+0xb0>
xorps  %xmm0,%xmm0
pop    %rax
ret
fstp   %st(0)
fstpl  (%rsp)
movsd  (%rsp),%xmm0
pop    %rax
ret
movsd  0xe38(%rip),%xmm0
pop    %rax
ret

LLVM IR (-O0):
@DBL_MAX = global x86_fp80 0xK00000000000000000000
@ERANGE = global i32 0
@errno = global i32 0
@HUGE_VAL = global double 0.000000e+00
@DBL_MIN = global x86_fp80 0xK00000000000000000000
define double @strtod(i8* noalias %0, i8** noalias %1) {
%3 = alloca double
%4 = alloca i8*
%5 = alloca i8**
%6 = alloca x86_fp80
store i8* %0, i8** %4
store i8** %1, i8*** %5
%7 = load i8*, i8** %4
%8 = load i8**, i8*** %5
%9 = call x86_fp80 @strtold(i8* %7, i8** %8)
store x86_fp80 %9, x86_fp80* %6
%10 = load x86_fp80, x86_fp80* %6
%11 = load x86_fp80, x86_fp80* @DBL_MAX
%12 = fcmp ogt x86_fp80 %10, %11
br i1 %12, label %13, label %16
13: ; preds = %2
%14 = load i32, i32* @ERANGE
store i32 %14, i32* @errno
%15 = load double, double* @HUGE_VAL
store double %15, double* %3
br label %45
16: ; preds = %2
%17 = load x86_fp80, x86_fp80* %6
%18 = load x86_fp80, x86_fp80* @DBL_MAX
%19 = fneg x86_fp80 %18
%20 = fcmp olt x86_fp80 %17, %19
br i1 %20, label %21, label %25
21: ; preds = %16
%22 = load i32, i32* @ERANGE
store i32 %22, i32* @errno
%23 = load double, double* @HUGE_VAL
%24 = fneg double %23
store double %24, double* %3
br label %45
25: ; preds = %16
%26 = load x86_fp80, x86_fp80* %6
%27 = load x86_fp80, x86_fp80* @DBL_MIN
%28 = fneg x86_fp80 %27
%29 = fcmp ogt x86_fp80 %26, %28
br i1 %29, label %30, label %40
30: ; preds = %25
%31 = load x86_fp80, x86_fp80* %6
%32 = load x86_fp80, x86_fp80* @DBL_MIN
%33 = fcmp olt x86_fp80 %31, %32
br i1 %33, label %34, label %40
34: ; preds = %30
%35 = load i32, i32* @ERANGE
store i32 %35, i32* @errno
%36 = load x86_fp80, x86_fp80* %6
%37 = fcmp olt x86_fp80 %36, 0xK00000000000000000000
%38 = zext i1 %37 to i64
%39 = select i1 %37, double -0.000000e+00, double 0.000000e+00
store double %39, double* %3
br label %45
40: ; preds = %30, %25
br label %41
41: ; preds = %40
br label %42
42: ; preds = %41
%43 = load x86_fp80, x86_fp80* %6
%44 = fptrunc x86_fp80 %43 to double
store double %44, double* %3
br label %45
45: ; preds = %42, %34, %21, %13
%46 = load double, double* %3
ret double %46
}

LLVM IR (-O2):
@DBL_MAX = global x86_fp80 0xK00000000000000000000
@ERANGE = global i32 0
@errno = global i32 0
@HUGE_VAL = global double 0.000000e+00
@DBL_MIN = global x86_fp80 0xK00000000000000000000
define double @strtod(i8* noalias %0, i8** noalias nocapture %1) {
%3 = tail call x86_fp80 @strtold(i8* %0, i8** %1)
%4 = load x86_fp80, x86_fp80* @DBL_MAX
%5 = fcmp ogt x86_fp80 %3, %4
br i1 %5, label %6, label %9
6: ; preds = %2
%7 = load i32, i32* @ERANGE
store i32 %7, i32* @errno
%8 = load double, double* @HUGE_VAL
br label %28
9: ; preds = %2
%10 = fneg x86_fp80 %4
%11 = fcmp olt x86_fp80 %3, %10
br i1 %11, label %12, label %16
12: ; preds = %9
%13 = load i32, i32* @ERANGE
store i32 %13, i32* @errno
%14 = load double, double* @HUGE_VAL
%15 = fneg double %14
br label %28
16: ; preds = %9
%17 = load x86_fp80, x86_fp80* @DBL_MIN
%18 = fneg x86_fp80 %17
%19 = fcmp ogt x86_fp80 %3, %18
%20 = fcmp olt x86_fp80 %3, %17
%21 = and i1 %19, %20
br i1 %21, label %22, label %26
22: ; preds = %16
%23 = load i32, i32* @ERANGE
store i32 %23, i32* @errno
%24 = fcmp olt x86_fp80 %3, 0xK00000000000000000000
%25 = select i1 %24, double -0.000000e+00, double 0.000000e+00
br label %28
26: ; preds = %16
%27 = fptrunc x86_fp80 %3 to double
br label %28
28: ; preds = %26, %22, %12, %6
%29 = phi double [ %8, %6 ], [ %15, %12 ], [ %25, %22 ], [ %27, %26 ]
ret double %29
}