#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__ ERANGE;
int /*<<< orphan*/ err(int /*<<< orphan*/);
scalar_t__ errno;
int /*<<< orphan*/ str(char *, char *);
double strtod(char *, char **);
double try_str_to_double(char *arg) {
  char *endptr;
  errno = 0;
  double result = strtod(arg, &endptr);
  if (errno == ERANGE) {
    err(str("'%s' is out of range", arg));
  }
  if (*endptr != '\0') {
    err(str("cannot parse '%s' as a float", arg));
  }
  return result;
}


x86 assembly (clang, -O0):
<try_str_to_double>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e95(%rip),%rax
movq   $0x0,(%rax)
mov    -0x8(%rbp),%rdi
lea    -0x10(%rbp),%rsi
call   1030 <strtod@plt>
movsd  %xmm0,-0x18(%rbp)
mov    0x2e75(%rip),%rax
mov    (%rax),%rax
mov    0x2e7b(%rip),%rcx
cmp    (%rcx),%rax
jne    118d <try_str_to_double+0x5d>
mov    -0x8(%rbp),%rsi
lea    0xe7f(%rip),%rdi
call   1050 <str@plt>
mov    %eax,%edi
call   1040 <err@plt>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     11b4 <try_str_to_double+0x84>
mov    -0x8(%rbp),%rsi
lea    0xe6d(%rip),%rdi
call   1050 <str@plt>
mov    %eax,%edi
call   1040 <err@plt>
movsd  -0x18(%rbp),%xmm0
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<try_str_to_double>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%rbx
mov    0x2e97(%rip),%r14
movq   $0x0,(%r14)
lea    0x10(%rsp),%rsi
call   1030 <strtod@plt>
movsd  %xmm0,0x8(%rsp)
mov    (%r14),%rax
mov    0x2e86(%rip),%rcx
cmp    (%rcx),%rax
jne    117d <try_str_to_double+0x4d>
lea    0xe92(%rip),%rdi
mov    %rbx,%rsi
call   1050 <str@plt>
mov    %eax,%edi
call   1040 <err@plt>
mov    0x10(%rsp),%rax
cmpb   $0x0,(%rax)
je     119d <try_str_to_double+0x6d>
lea    0xe87(%rip),%rdi
mov    %rbx,%rsi
call   1050 <str@plt>
mov    %eax,%edi
call   1040 <err@plt>
movsd  0x8(%rsp),%xmm0
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@errno = global i64 0
@ERANGE = global i64 0
@.str = [21 x i8] c"'%s' is out of range\00"
@.str.1 = [29 x i8] c"cannot parse '%s' as a float\00"
define double @try_str_to_double(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca double
store i8* %0, i8** %2
store i64 0, i64* @errno
%5 = load i8*, i8** %2
%6 = call double @strtod(i8* %5, i8** %3)
store double %6, double* %4
%7 = load i64, i64* @errno
%8 = load i64, i64* @ERANGE
%9 = icmp eq i64 %7, %8
br i1 %9, label %10, label %14
10: ; preds = %1
%11 = load i8*, i8** %2
%12 = call i32 @str(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %11)
%13 = call i32 @err(i32 %12)
br label %14
14: ; preds = %10, %1
%15 = load i8*, i8** %3
%16 = load i8, i8* %15
%17 = sext i8 %16 to i32
%18 = icmp ne i32 %17, 0
br i1 %18, label %19, label %23
19: ; preds = %14
%20 = load i8*, i8** %2
%21 = call i32 @str(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i8* %20)
%22 = call i32 @err(i32 %21)
br label %23
23: ; preds = %19, %14
%24 = load double, double* %4
ret double %24
}

LLVM IR (-O2):
@errno = global i64 0
@ERANGE = global i64 0
@.str = [21 x i8] c"'%s' is out of range\00"
@.str.1 = [29 x i8] c"cannot parse '%s' as a float\00"
define double @try_str_to_double(i8* %0) {
%2 = alloca i8*
%3 = bitcast i8** %2 to i8*
store i64 0, i64* @errno
%4 = call double @strtod(i8* %0, i8** %2)
%5 = load i64, i64* @errno
%6 = load i64, i64* @ERANGE
%7 = icmp eq i64 %5, %6
br i1 %7, label %8, label %11
8: ; preds = %1
%9 = tail call i32 @str(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %0)
%10 = tail call i32 @err(i32 %9)
br label %11
11: ; preds = %8, %1
%12 = load i8*, i8** %2
%13 = load i8, i8* %12
%14 = icmp eq i8 %13, 0
br i1 %14, label %18, label %15
15: ; preds = %11
%16 = tail call i32 @str(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%17 = tail call i32 @err(i32 %16)
br label %18
18: ; preds = %15, %11
ret double %4
}