#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 /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ assert(int /*<<< orphan*/);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ free(char *);
scalar_t__ isdigit(char);
char *malloc(int);
int /*<<< orphan*/ stderr;
char *strchr(char *, char);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
double strtod(char *, char **);
double read_double_from_string(char *string, char **end) {
  assert(string != NULL);
  assert(end != NULL);
  char *cursor = string;
  while (*cursor == ' ') {
    cursor++;
  }
  if (!isdigit(*cursor)) {
    *end = string;
    return 0;
  }
  char *xcursor = cursor;
  while (*xcursor == '.' || isdigit(*xcursor)) {
    xcursor++;
  }
  if (*xcursor == 'x') {
    size_t copy_length = strlen(cursor) + 1;
    char *copy = malloc(sizeof(char) * copy_length);
    if (!copy) {
      fprintf(stderr, "Fatal error: couldn't allocate %zu bytes!\nExiting\n",
              sizeof(char) * copy_length);
      exit(EXIT_FAILURE);
    }
    strcpy(copy, cursor);
    char *x_copycursor = strchr(copy, 'x');
    *x_copycursor = ' ';
    char *copy_end = copy;
    double val = strtod(copy, &copy_end);
    if (*end == copy) {
      *end = string;
      free(copy);
      return 0;
    }
    *end = cursor + (copy_end - copy);
    free(copy);
    return val;
  } else {
    double val = strtod(cursor, end);
    if (*end == cursor) {
      *end = string;
      return 0;
    }
    return val;
  }
}


x86 assembly (clang, -O0):
<read_double_from_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1090 <assert@plt>
cmpq   $0x0,-0x18(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1090 <assert@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x20,%eax
jne    11fd <read_double_from_string+0x5d>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    11dc <read_double_from_string+0x3c>
mov    -0x20(%rbp),%rax
movsbl (%rax),%edi
call   1040 <isdigit@plt>
cmp    $0x0,%rax
jne    122b <read_double_from_string+0x8b>
mov    -0x10(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    13d4 <read_double_from_string+0x234>
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
movsbl (%rax),%ecx
mov    $0x1,%al
cmp    $0x2e,%ecx
mov    %al,-0x59(%rbp)
je     125e <read_double_from_string+0xbe>
mov    -0x28(%rbp),%rax
movsbl (%rax),%edi
call   1040 <isdigit@plt>
cmp    $0x0,%rax
setne  %al
mov    %al,-0x59(%rbp)
mov    -0x59(%rbp),%al
test   $0x1,%al
jne    126e <read_double_from_string+0xce>
jmp    127f <read_double_from_string+0xdf>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    1233 <read_double_from_string+0x93>
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x78,%eax
jne    138f <read_double_from_string+0x1ef>
mov    -0x20(%rbp),%rdi
call   1070 <strlen@plt>
add    $0x1,%eax
cltq
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   10b0 <malloc@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    12ec <read_double_from_string+0x14c>
mov    0x2d1a(%rip),%rax
mov    (%rax),%edi
mov    -0x30(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edx
lea    0xd27(%rip),%rsi
call   10a0 <fprintf@plt>
mov    0x2cf3(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
mov    -0x38(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1050 <strcpy@plt>
mov    -0x38(%rbp),%rdi
mov    $0x78,%esi
call   1080 <strchr@plt>
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rax
movb   $0x20,(%rax)
mov    -0x38(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x38(%rbp),%rdi
lea    -0x48(%rbp),%rsi
call   1060 <strtod@plt>
movsd  %xmm0,-0x50(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
cmp    -0x38(%rbp),%rax
jne    135e <read_double_from_string+0x1be>
mov    -0x10(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    13d4 <read_double_from_string+0x234>
mov    -0x20(%rbp),%rcx
mov    -0x48(%rbp),%rax
mov    -0x38(%rbp),%rdx
sub    %rdx,%rax
add    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
movsd  -0x50(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    13d4 <read_double_from_string+0x234>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1060 <strtod@plt>
movsd  %xmm0,-0x58(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
cmp    -0x20(%rbp),%rax
jne    13ca <read_double_from_string+0x22a>
mov    -0x10(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    13d4 <read_double_from_string+0x234>
movsd  -0x58(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_double_from_string>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %rdi,%r15
xor    %edi,%edi
test   %r15,%r15
setne  %dil
call   1090 <assert@plt>
xor    %edi,%edi
test   %r14,%r14
setne  %dil
call   1090 <assert@plt>
lea    -0x1(%r15),%rbp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
movzbl 0x1(%rbp),%eax
add    $0x1,%rbp
cmp    $0x20,%al
je     11e0 <read_double_from_string+0x40>
movsbl %al,%edi
call   1040 <isdigit@plt>
test   %rax,%rax
je     12b9 <read_double_from_string+0x119>
mov    %rbp,%rbx
jmp    1214 <read_double_from_string+0x74>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
add    $0x1,%rbx
movzbl (%rbx),%eax
cmp    $0x2e,%al
je     1210 <read_double_from_string+0x70>
movsbl %al,%edi
call   1040 <isdigit@plt>
test   %rax,%rax
jne    1210 <read_double_from_string+0x70>
cmpb   $0x78,(%rbx)
jne    12a9 <read_double_from_string+0x109>
mov    %rbp,%rdi
call   1070 <strlen@plt>
mov    %eax,%ebx
add    $0x1,%ebx
mov    %ebx,%edi
call   10b0 <malloc@plt>
test   %rax,%rax
je     12cc <read_double_from_string+0x12c>
mov    %rax,%r12
mov    %rax,%rdi
mov    %rbp,%rsi
call   1050 <strcpy@plt>
mov    %r12,%rdi
mov    $0x78,%esi
call   1080 <strchr@plt>
movb   $0x20,(%rax)
mov    %r12,(%rsp)
mov    %rsp,%rsi
mov    %r12,%rdi
call   1060 <strtod@plt>
mov    (%rsp),%rax
sub    %r12,%rax
add    %rbp,%rax
xorps  %xmm1,%xmm1
cmp    %r12,(%r14)
cmove  %r15,%rax
je     1290 <read_double_from_string+0xf0>
movaps %xmm0,%xmm1
movsd  %xmm1,0x8(%rsp)
mov    %rax,(%r14)
mov    %r12,%rdi
call   1030 <free@plt>
movsd  0x8(%rsp),%xmm0
jmp    12bf <read_double_from_string+0x11f>
mov    %rbp,%rdi
mov    %r14,%rsi
call   1060 <strtod@plt>
cmp    (%r14),%rbp
jne    12bf <read_double_from_string+0x11f>
mov    %r15,(%r14)
xorps  %xmm0,%xmm0
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d0d(%rip),%rax
mov    (%rax),%edi
lea    0xd24(%rip),%rsi
mov    %ebx,%edx
call   10a0 <fprintf@plt>
mov    0x2cee(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [51 x i8] c"Fatal error: couldn't allocate %zu bytes!\0AExiting\0A\00"
@EXIT_FAILURE = global i32 0
define double @read_double_from_string(i8* %0, i8** %1) {
%3 = alloca double
%4 = alloca i8*
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca double
%13 = alloca double
store i8* %0, i8** %4
store i8** %1, i8*** %5
%14 = load i8*, i8** %4
%15 = icmp ne i8* %14, null
%16 = zext i1 %15 to i32
%17 = call i32 @assert(i32 %16)
%18 = load i8**, i8*** %5
%19 = icmp ne i8** %18, null
%20 = zext i1 %19 to i32
%21 = call i32 @assert(i32 %20)
%22 = load i8*, i8** %4
store i8* %22, i8** %6
br label %23
23: ; preds = %28, %2
%24 = load i8*, i8** %6
%25 = load i8, i8* %24
%26 = sext i8 %25 to i32
%27 = icmp eq i32 %26, 32
br i1 %27, label %28, label %31
28: ; preds = %23
%29 = load i8*, i8** %6
%30 = getelementptr inbounds i8, i8* %29, i32 1
store i8* %30, i8** %6
br label %23
31: ; preds = %23
%32 = load i8*, i8** %6
%33 = load i8, i8* %32
%34 = call i64 @isdigit(i8 signext %33)
%35 = icmp ne i64 %34, 0
br i1 %35, label %39, label %36
36: ; preds = %31
%37 = load i8*, i8** %4
%38 = load i8**, i8*** %5
store i8* %37, i8** %38
store double 0.000000e+00, double* %3
br label %124
39: ; preds = %31
%40 = load i8*, i8** %6
store i8* %40, i8** %7
br label %41
41: ; preds = %53, %39
%42 = load i8*, i8** %7
%43 = load i8, i8* %42
%44 = sext i8 %43 to i32
%45 = icmp eq i32 %44, 46
br i1 %45, label %51, label %46
46: ; preds = %41
%47 = load i8*, i8** %7
%48 = load i8, i8* %47
%49 = call i64 @isdigit(i8 signext %48)
%50 = icmp ne i64 %49, 0
br label %51
51: ; preds = %46, %41
%52 = phi i1 [ true, %41 ], [ %50, %46 ]
br i1 %52, label %53, label %56
53: ; preds = %51
%54 = load i8*, i8** %7
%55 = getelementptr inbounds i8, i8* %54, i32 1
store i8* %55, i8** %7
br label %41
56: ; preds = %51
%57 = load i8*, i8** %7
%58 = load i8, i8* %57
%59 = sext i8 %58 to i32
%60 = icmp eq i32 %59, 120
br i1 %60, label %61, label %111
61: ; preds = %56
%62 = load i8*, i8** %6
%63 = call i32 @strlen(i8* %62)
%64 = add nsw i32 %63, 1
%65 = sext i32 %64 to i64
store i64 %65, i64* %8
%66 = load i64, i64* %8
%67 = mul i64 1, %66
%68 = trunc i64 %67 to i32
%69 = call i8* @malloc(i32 %68)
store i8* %69, i8** %9
%70 = load i8*, i8** %9
%71 = icmp ne i8* %70, null
br i1 %71, label %80, label %72
72: ; preds = %61
%73 = load i32, i32* @stderr
%74 = load i64, i64* %8
%75 = mul i64 1, %74
%76 = trunc i64 %75 to i32
%77 = call i32 @fprintf(i32 %73, i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str, i64 0, i64 0), i32 %76)
%78 = load i32, i32* @EXIT_FAILURE
%79 = call i32 @exit(i32 %78)
unreachable
80: ; preds = %61
%81 = load i8*, i8** %9
%82 = load i8*, i8** %6
%83 = call i32 @strcpy(i8* %81, i8* %82)
%84 = load i8*, i8** %9
%85 = call i8* @strchr(i8* %84, i8 signext 120)
store i8* %85, i8** %10
%86 = load i8*, i8** %10
store i8 32, i8* %86
%87 = load i8*, i8** %9
store i8* %87, i8** %11
%88 = load i8*, i8** %9
%89 = call double @strtod(i8* %88, i8** %11)
store double %89, double* %12
%90 = load i8**, i8*** %5
%91 = load i8*, i8** %90
%92 = load i8*, i8** %9
%93 = icmp eq i8* %91, %92
br i1 %93, label %94, label %99
94: ; preds = %80
%95 = load i8*, i8** %4
%96 = load i8**, i8*** %5
store i8* %95, i8** %96
%97 = load i8*, i8** %9
%98 = call i32 @free(i8* %97)
store double 0.000000e+00, double* %3
br label %124
99: ; preds = %80
%100 = load i8*, i8** %6
%101 = load i8*, i8** %11
%102 = load i8*, i8** %9
%103 = ptrtoint i8* %101 to i64
%104 = ptrtoint i8* %102 to i64
%105 = sub i64 %103, %104
%106 = getelementptr inbounds i8, i8* %100, i64 %105
%107 = load i8**, i8*** %5
store i8* %106, i8** %107
%108 = load i8*, i8** %9
%109 = call i32 @free(i8* %108)
%110 = load double, double* %12
store double %110, double* %3
br label %124
111: ; preds = %56
%112 = load i8*, i8** %6
%113 = load i8**, i8*** %5
%114 = call double @strtod(i8* %112, i8** %113)
store double %114, double* %13
%115 = load i8**, i8*** %5
%116 = load i8*, i8** %115
%117 = load i8*, i8** %6
%118 = icmp eq i8* %116, %117
br i1 %118, label %119, label %122
119: ; preds = %111
%120 = load i8*, i8** %4
%121 = load i8**, i8*** %5
store i8* %120, i8** %121
store double 0.000000e+00, double* %3
br label %124
122: ; preds = %111
%123 = load double, double* %13
store double %123, double* %3
br label %124
124: ; preds = %122, %119, %99, %94, %36
%125 = load double, double* %3
ret double %125
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [51 x i8] c"Fatal error: couldn't allocate %zu bytes!\0AExiting\0A\00"
@EXIT_FAILURE = global i32 0
define double @read_double_from_string(i8* %0, i8** %1) {
%3 = alloca i8*
%4 = icmp ne i8* %0, null
%5 = zext i1 %4 to i32
%6 = tail call i32 @assert(i32 %5)
%7 = icmp ne i8** %1, null
%8 = zext i1 %7 to i32
%9 = tail call i32 @assert(i32 %8)
br label %10
10: ; preds = %10, %2
%11 = phi i8* [ %0, %2 ], [ %14, %10 ]
%12 = load i8, i8* %11
%13 = icmp eq i8 %12, 32
%14 = getelementptr inbounds i8, i8* %11, i64 1
br i1 %13, label %10, label %15
15: ; preds = %10
%16 = tail call i64 @isdigit(i8 signext %12)
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %19
18: ; preds = %15
store i8* %0, i8** %1
br label %61
19: ; preds = %15, %26
%20 = phi i8* [ %27, %26 ], [ %11, %15 ]
%21 = load i8, i8* %20
%22 = icmp eq i8 %21, 46
br i1 %22, label %26, label %23
23: ; preds = %19
%24 = tail call i64 @isdigit(i8 signext %21)
%25 = icmp eq i64 %24, 0
br i1 %25, label %28, label %26
26: ; preds = %19, %23
%27 = getelementptr inbounds i8, i8* %20, i64 1
br label %19
28: ; preds = %23
%29 = load i8, i8* %20
%30 = icmp eq i8 %29, 120
br i1 %30, label %31, label %56
31: ; preds = %28
%32 = tail call i32 @strlen(i8* %11)
%33 = add nsw i32 %32, 1
%34 = tail call i8* @malloc(i32 %33)
%35 = icmp eq i8* %34, null
br i1 %35, label %36, label %41
36: ; preds = %31
%37 = load i32, i32* @stderr
%38 = tail call i32 @fprintf(i32 %37, i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str, i64 0, i64 0), i32 %33)
%39 = load i32, i32* @EXIT_FAILURE
%40 = tail call i32 @exit(i32 %39)
unreachable
41: ; preds = %31
%42 = tail call i32 @strcpy(i8* %34, i8* %11)
%43 = tail call i8* @strchr(i8* %34, i8 signext 120)
store i8 32, i8* %43
%44 = bitcast i8** %3 to i8*
store i8* %34, i8** %3
%45 = call double @strtod(i8* %34, i8** %3)
%46 = load i8*, i8** %1
%47 = icmp eq i8* %46, %34
%48 = load i8*, i8** %3
%49 = ptrtoint i8* %48 to i64
%50 = ptrtoint i8* %34 to i64
%51 = sub i64 %49, %50
%52 = getelementptr inbounds i8, i8* %11, i64 %51
%53 = select i1 %47, i8* %0, i8* %52
%54 = select i1 %47, double 0.000000e+00, double %45
store i8* %53, i8** %1
%55 = tail call i32 @free(i8* %34)
br label %61
56: ; preds = %28
%57 = tail call double @strtod(i8* %11, i8** %1)
%58 = load i8*, i8** %1
%59 = icmp eq i8* %58, %11
br i1 %59, label %60, label %61
60: ; preds = %56
store i8* %0, i8** %1
br label %61
61: ; preds = %41, %56, %60, %18
%62 = phi double [ 0.000000e+00, %18 ], [ %54, %41 ], [ 0.000000e+00, %60 ], [ %57, %56 ]
ret double %62
}