#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 */
scalar_t__ errno;
scalar_t__ isinf(double);
scalar_t__ isnan(double);
double strtod(char *, char **);
int string_to_double(char *str, double *d) {
  char *endptr;
  *d = strtod(str, &endptr);
  if (*d == 0) {
    if (errno != 0) {
      return 3;
    } else if (endptr - str == 0) {
      return -1;
    } else {
      return 0;
    }
  } else if (isnan(*d))
    return 4;
  else if (isinf(*d) && *d > 0)
    return 1;
  else if (isinf(*d) && *d < 0)
    return 2;
  else
    return 0;
}


x86 assembly (clang, -O0):
<string_to_double>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1030 <strtod@plt>
mov    -0x18(%rbp),%rax
movsd  %xmm0,(%rax)
mov    -0x18(%rbp),%rax
movsd  (%rax),%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jne    11ba <string_to_double+0x8a>
jp     11ba <string_to_double+0x8a>
mov    0x2e69(%rip),%rax
cmpq   $0x0,(%rax)
je     118d <string_to_double+0x5d>
movl   $0x3,-0x4(%rbp)
jmp    1250 <string_to_double+0x120>
mov    -0x20(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
cmp    $0x0,%rax
jne    11ae <string_to_double+0x7e>
movl   $0xffffffff,-0x4(%rbp)
jmp    1250 <string_to_double+0x120>
movl   $0x0,-0x4(%rbp)
jmp    1250 <string_to_double+0x120>
mov    -0x18(%rbp),%rax
movsd  (%rax),%xmm0
call   1040 <isnan@plt>
cmp    $0x0,%rax
je     11dd <string_to_double+0xad>
movl   $0x4,-0x4(%rbp)
jmp    1250 <string_to_double+0x120>
mov    -0x18(%rbp),%rax
movsd  (%rax),%xmm0
call   1050 <isinf@plt>
cmp    $0x0,%rax
je     1215 <string_to_double+0xe5>
mov    -0x18(%rbp),%rax
movsd  (%rax),%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jbe    1215 <string_to_double+0xe5>
movl   $0x1,-0x4(%rbp)
jmp    1250 <string_to_double+0x120>
mov    -0x18(%rbp),%rax
movsd  (%rax),%xmm0
call   1050 <isinf@plt>
cmp    $0x0,%rax
je     1249 <string_to_double+0x119>
mov    -0x18(%rbp),%rax
xorps  %xmm0,%xmm0
ucomisd (%rax),%xmm0
jbe    1249 <string_to_double+0x119>
movl   $0x2,-0x4(%rbp)
jmp    1250 <string_to_double+0x120>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<string_to_double>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
mov    %rsp,%rsi
call   1030 <strtod@plt>
movsd  %xmm0,(%r14)
xorpd  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jne    1172 <string_to_double+0x42>
jp     1172 <string_to_double+0x42>
mov    0x2e86(%rip),%rcx
mov    $0x3,%eax
cmpq   $0x0,(%rcx)
jne    11c3 <string_to_double+0x93>
xor    %eax,%eax
cmp    %rbx,(%rsp)
sete   %al
neg    %eax
jmp    11c3 <string_to_double+0x93>
call   1040 <isnan@plt>
mov    %rax,%rcx
mov    $0x4,%eax
test   %rcx,%rcx
jne    11c3 <string_to_double+0x93>
movsd  (%r14),%xmm0
call   1050 <isinf@plt>
movsd  (%r14),%xmm0
test   %rax,%rax
je     11a7 <string_to_double+0x77>
mov    $0x1,%eax
xorpd  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
ja     11c3 <string_to_double+0x93>
call   1050 <isinf@plt>
test   %rax,%rax
je     11c1 <string_to_double+0x91>
mov    $0x2,%eax
xorpd  %xmm0,%xmm0
ucomisd (%r14),%xmm0
ja     11c3 <string_to_double+0x93>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@errno = global i64 0
define i32 @string_to_double(i8* %0, double* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca double*
%6 = alloca i8*
store i8* %0, i8** %4
store double* %1, double** %5
%7 = load i8*, i8** %4
%8 = call double @strtod(i8* %7, i8** %6)
%9 = load double*, double** %5
store double %8, double* %9
%10 = load double*, double** %5
%11 = load double, double* %10
%12 = fcmp oeq double %11, 0.000000e+00
br i1 %12, label %13, label %26
13: ; preds = %2
%14 = load i64, i64* @errno
%15 = icmp ne i64 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %13
store i32 3, i32* %3
br label %53
17: ; preds = %13
%18 = load i8*, i8** %6
%19 = load i8*, i8** %4
%20 = ptrtoint i8* %18 to i64
%21 = ptrtoint i8* %19 to i64
%22 = sub i64 %20, %21
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %17
store i32 -1, i32* %3
br label %53
25: ; preds = %17
store i32 0, i32* %3
br label %53
26: ; preds = %2
%27 = load double*, double** %5
%28 = load double, double* %27
%29 = call i64 @isnan(double %28)
%30 = icmp ne i64 %29, 0
br i1 %30, label %31, label %32
31: ; preds = %26
store i32 4, i32* %3
br label %53
32: ; preds = %26
%33 = load double*, double** %5
%34 = load double, double* %33
%35 = call i64 @isinf(double %34)
%36 = icmp ne i64 %35, 0
br i1 %36, label %37, label %42
37: ; preds = %32
%38 = load double*, double** %5
%39 = load double, double* %38
%40 = fcmp ogt double %39, 0.000000e+00
br i1 %40, label %41, label %42
41: ; preds = %37
store i32 1, i32* %3
br label %53
42: ; preds = %37, %32
%43 = load double*, double** %5
%44 = load double, double* %43
%45 = call i64 @isinf(double %44)
%46 = icmp ne i64 %45, 0
br i1 %46, label %47, label %52
47: ; preds = %42
%48 = load double*, double** %5
%49 = load double, double* %48
%50 = fcmp olt double %49, 0.000000e+00
br i1 %50, label %51, label %52
51: ; preds = %47
store i32 2, i32* %3
br label %53
52: ; preds = %47, %42
store i32 0, i32* %3
br label %53
53: ; preds = %52, %51, %41, %31, %25, %24, %16
%54 = load i32, i32* %3
ret i32 %54
}

LLVM IR (-O2):
@errno = global i64 0
define i32 @string_to_double(i8* %0, double* nocapture %1) {
%3 = alloca i8*
%4 = bitcast i8** %3 to i8*
%5 = call double @strtod(i8* %0, i8** %3)
store double %5, double* %1
%6 = fcmp oeq double %5, 0.000000e+00
br i1 %6, label %7, label %14
7: ; preds = %2
%8 = load i64, i64* @errno
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %31
10: ; preds = %7
%11 = load i8*, i8** %3
%12 = icmp eq i8* %11, %0
%13 = sext i1 %12 to i32
br label %31
14: ; preds = %2
%15 = tail call i64 @isnan(double %5)
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %31
17: ; preds = %14
%18 = load double, double* %1
%19 = tail call i64 @isinf(double %18)
%20 = icmp ne i64 %19, 0
%21 = load double, double* %1
%22 = fcmp ogt double %21, 0.000000e+00
%23 = select i1 %20, i1 %22, i1 false
br i1 %23, label %31, label %24
24: ; preds = %17
%25 = tail call i64 @isinf(double %21)
%26 = icmp eq i64 %25, 0
br i1 %26, label %30, label %27
27: ; preds = %24
%28 = load double, double* %1
%29 = fcmp olt double %28, 0.000000e+00
br i1 %29, label %31, label %30
30: ; preds = %27, %24
br label %31
31: ; preds = %17, %27, %14, %10, %7, %30
%32 = phi i32 [ 0, %30 ], [ 3, %7 ], [ %13, %10 ], [ 4, %14 ], [ 2, %27 ], [ 1, %17 ]
ret i32 %32
}