#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 EINVAL;
int ERANGE;
double HUGE_VAL;
int errno;
double strtod(char const *, char **);
int _checkstrtod(const char *str, double *ret, char **endptr) {
  errno = 0;
  *ret = strtod(str, endptr);
  if (*endptr == str)
    return -EINVAL;
  if (*ret == HUGE_VAL || *ret == -HUGE_VAL)
    return -ERANGE;
  if (!*ret && errno != 0)
    return -errno;
  return 0;
}


x86 assembly (clang, -O0):
<_checkstrtod>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    0x2ea5(%rip),%rax
movl   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1030 <strtod@plt>
mov    -0x18(%rbp),%rax
movsd  %xmm0,(%rax)
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
cmp    -0x10(%rbp),%rax
jne    116a <_checkstrtod+0x5a>
xor    %eax,%eax
mov    0x2e68(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1220 <_checkstrtod+0x110>
mov    -0x18(%rbp),%rax
movsd  (%rax),%xmm0
mov    0x2e6f(%rip),%rax
ucomisd (%rax),%xmm0
jne    118e <_checkstrtod+0x7e>
jp     118e <_checkstrtod+0x7e>
jmp    11c8 <_checkstrtod+0xb8>
mov    -0x18(%rbp),%rax
movsd  (%rax),%xmm0
mov    0x2e4b(%rip),%rax
movsd  (%rax),%xmm1
movq   %xmm1,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm1
ucomisd %xmm1,%xmm0
jne    11db <_checkstrtod+0xcb>
jp     11db <_checkstrtod+0xcb>
xor    %eax,%eax
mov    0x2e0f(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1220 <_checkstrtod+0x110>
mov    -0x18(%rbp),%rax
movsd  (%rax),%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jne    1219 <_checkstrtod+0x109>
jp     1219 <_checkstrtod+0x109>
mov    0x2dd3(%rip),%rax
cmpl   $0x0,(%rax)
je     1219 <_checkstrtod+0x109>
xor    %eax,%eax
mov    0x2dc1(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1220 <_checkstrtod+0x110>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_checkstrtod>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r15
mov    %rsi,%r14
mov    %rdi,%rbx
mov    0x2ea8(%rip),%r12
movl   $0x0,(%r12)
mov    %rdx,%rsi
call   1030 <strtod@plt>
movsd  %xmm0,(%r14)
cmp    %rbx,(%r15)
je     1180 <_checkstrtod+0x70>
mov    0x2e9f(%rip),%rax
movsd  (%rax),%xmm1
ucomisd %xmm1,%xmm0
jne    1155 <_checkstrtod+0x45>
jnp    1189 <_checkstrtod+0x79>
xorpd  0xea3(%rip),%xmm1
ucomisd %xmm1,%xmm0
jne    1165 <_checkstrtod+0x55>
jnp    1189 <_checkstrtod+0x79>
xor    %ecx,%ecx
xor    %eax,%eax
sub    (%r12),%eax
cmovae %ecx,%eax
xorpd  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
cmovne %ecx,%eax
cmovp  %ecx,%eax
jmp    1194 <_checkstrtod+0x84>
mov    0x2e41(%rip),%rcx
jmp    1190 <_checkstrtod+0x80>
mov    0x2e50(%rip),%rcx
xor    %eax,%eax
sub    (%rcx),%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@errno = global i32 0
@EINVAL = global i32 0
@HUGE_VAL = global double 0.000000e+00
@ERANGE = global i32 0
define i32 @_checkstrtod(i8* %0, double* %1, i8** %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca double*
%7 = alloca i8**
store i8* %0, i8** %5
store double* %1, double** %6
store i8** %2, i8*** %7
store i32 0, i32* @errno
%8 = load i8*, i8** %5
%9 = load i8**, i8*** %7
%10 = call double @strtod(i8* %8, i8** %9)
%11 = load double*, double** %6
store double %10, double* %11
%12 = load i8**, i8*** %7
%13 = load i8*, i8** %12
%14 = load i8*, i8** %5
%15 = icmp eq i8* %13, %14
br i1 %15, label %16, label %19
16: ; preds = %3
%17 = load i32, i32* @EINVAL
%18 = sub nsw i32 0, %17
store i32 %18, i32* %4
br label %44
19: ; preds = %3
%20 = load double*, double** %6
%21 = load double, double* %20
%22 = load double, double* @HUGE_VAL
%23 = fcmp oeq double %21, %22
br i1 %23, label %30, label %24
24: ; preds = %19
%25 = load double*, double** %6
%26 = load double, double* %25
%27 = load double, double* @HUGE_VAL
%28 = fneg double %27
%29 = fcmp oeq double %26, %28
br i1 %29, label %30, label %33
30: ; preds = %24, %19
%31 = load i32, i32* @ERANGE
%32 = sub nsw i32 0, %31
store i32 %32, i32* %4
br label %44
33: ; preds = %24
%34 = load double*, double** %6
%35 = load double, double* %34
%36 = fcmp une double %35, 0.000000e+00
br i1 %36, label %43, label %37
37: ; preds = %33
%38 = load i32, i32* @errno
%39 = icmp ne i32 %38, 0
br i1 %39, label %40, label %43
40: ; preds = %37
%41 = load i32, i32* @errno
%42 = sub nsw i32 0, %41
store i32 %42, i32* %4
br label %44
43: ; preds = %37, %33
store i32 0, i32* %4
br label %44
44: ; preds = %43, %40, %30, %16
%45 = load i32, i32* %4
ret i32 %45
}

LLVM IR (-O2):
@errno = global i32 0
@EINVAL = global i32 0
@HUGE_VAL = global double 0.000000e+00
@ERANGE = global i32 0
define i32 @_checkstrtod(i8* %0, double* nocapture writeonly %1, i8** nocapture %2) {
store i32 0, i32* @errno
%4 = tail call double @strtod(i8* %0, i8** %2)
store double %4, double* %1
%5 = load i8*, i8** %2
%6 = icmp eq i8* %5, %0
br i1 %6, label %7, label %10
7: ; preds = %3
%8 = load i32, i32* @EINVAL
%9 = sub nsw i32 0, %8
br label %26
10: ; preds = %3
%11 = load double, double* @HUGE_VAL
%12 = fcmp oeq double %4, %11
%13 = fneg double %11
%14 = fcmp oeq double %4, %13
%15 = or i1 %12, %14
br i1 %15, label %16, label %19
16: ; preds = %10
%17 = load i32, i32* @ERANGE
%18 = sub nsw i32 0, %17
br label %26
19: ; preds = %10
%20 = fcmp oeq double %4, 0.000000e+00
%21 = load i32, i32* @errno
%22 = icmp ne i32 %21, 0
%23 = select i1 %20, i1 %22, i1 false
%24 = sub nsw i32 0, %21
%25 = select i1 %23, i32 %24, i32 0
br label %26
26: ; preds = %19, %16, %7
%27 = phi i32 [ %9, %7 ], [ %18, %16 ], [ %25, %19 ]
ret i32 %27
}