#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 */
struct lh_kv_elem {
  int dummy;
};

/* Variables and functions */
char *lh_kv_string(struct lh_kv_elem const *, char const *,
                   int /*<<< orphan*/ *);
scalar_t__ strtod(char const *, char **);
double lh_kv_double(const struct lh_kv_elem *kvs, const char *key, int *error) {
  const char *str = lh_kv_string(kvs, key, NULL);
  if (!str) {
    if (error)
      *error = 1;
    return 0;
  }
  char *pEnd = NULL;
  double n = (double)strtod(str, &pEnd);
  if ((*str != '\0' && *pEnd == '\0'))
    return n;
  if (error)
    *error = 1;
  return 0;
  ;
}


x86 assembly (clang, -O0):
<lh_kv_double>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1040 <lh_kv_string@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1176 <lh_kv_double+0x56>
cmpq   $0x0,-0x20(%rbp)
je     1169 <lh_kv_double+0x49>
mov    -0x20(%rbp),%rax
movl   $0x1,(%rax)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    11e1 <lh_kv_double+0xc1>
movq   $0x0,-0x30(%rbp)
mov    -0x28(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1030 <strtod@plt>
cvtsi2sd %rax,%xmm0
movsd  %xmm0,-0x38(%rbp)
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     11c4 <lh_kv_double+0xa4>
mov    -0x30(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    11c4 <lh_kv_double+0xa4>
movsd  -0x38(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    11e1 <lh_kv_double+0xc1>
cmpq   $0x0,-0x20(%rbp)
je     11d9 <lh_kv_double+0xb9>
mov    -0x20(%rbp),%rax
movl   $0x1,(%rax)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lh_kv_double>:
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r14
xor    %edx,%edx
call   1040 <lh_kv_string@plt>
test   %rax,%rax
je     1157 <lh_kv_double+0x37>
mov    %rax,%rbx
movq   $0x0,(%rsp)
mov    %rsp,%rsi
mov    %rax,%rdi
call   1030 <strtod@plt>
cmpb   $0x0,(%rbx)
je     1157 <lh_kv_double+0x37>
mov    (%rsp),%rcx
cmpb   $0x0,(%rcx)
je     1169 <lh_kv_double+0x49>
xorpd  %xmm0,%xmm0
test   %r14,%r14
je     116e <lh_kv_double+0x4e>
movl   $0x1,(%r14)
jmp    116e <lh_kv_double+0x4e>
cvtsi2sd %rax,%xmm0
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.lh_kv_elem = type { i32 }
define double @lh_kv_double(%struct.lh_kv_elem* %0, i8* %1, i32* %2) {
%4 = alloca double
%5 = alloca %struct.lh_kv_elem*
%6 = alloca i8*
%7 = alloca i32*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca double
store %struct.lh_kv_elem* %0, %struct.lh_kv_elem** %5
store i8* %1, i8** %6
store i32* %2, i32** %7
%11 = load %struct.lh_kv_elem*, %struct.lh_kv_elem** %5
%12 = load i8*, i8** %6
%13 = call i8* @lh_kv_string(%struct.lh_kv_elem* %11, i8* %12, i32* null)
store i8* %13, i8** %8
%14 = load i8*, i8** %8
%15 = icmp ne i8* %14, null
br i1 %15, label %22, label %16
16: ; preds = %3
%17 = load i32*, i32** %7
%18 = icmp ne i32* %17, null
br i1 %18, label %19, label %21
19: ; preds = %16
%20 = load i32*, i32** %7
store i32 1, i32* %20
br label %21
21: ; preds = %19, %16
store double 0.000000e+00, double* %4
br label %43
22: ; preds = %3
store i8* null, i8** %9
%23 = load i8*, i8** %8
%24 = call i64 @strtod(i8* %23, i8** %9)
%25 = sitofp i64 %24 to double
store double %25, double* %10
%26 = load i8*, i8** %8
%27 = load i8, i8* %26
%28 = sext i8 %27 to i32
%29 = icmp ne i32 %28, 0
br i1 %29, label %30, label %37
30: ; preds = %22
%31 = load i8*, i8** %9
%32 = load i8, i8* %31
%33 = sext i8 %32 to i32
%34 = icmp eq i32 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %30
%36 = load double, double* %10
store double %36, double* %4
br label %43
37: ; preds = %30, %22
%38 = load i32*, i32** %7
%39 = icmp ne i32* %38, null
br i1 %39, label %40, label %42
40: ; preds = %37
%41 = load i32*, i32** %7
store i32 1, i32* %41
br label %42
42: ; preds = %40, %37
store double 0.000000e+00, double* %4
br label %43
43: ; preds = %42, %35, %21
%44 = load double, double* %4
ret double %44
}

LLVM IR (-O2):
%struct.lh_kv_elem = type { i32 }
define double @lh_kv_double(%struct.lh_kv_elem* %0, i8* %1, i32* writeonly %2) {
%4 = alloca i8*
%5 = tail call i8* @lh_kv_string(%struct.lh_kv_elem* %0, i8* %1, i32* null)
%6 = icmp eq i8* %5, null
br i1 %6, label %7, label %10
7: ; preds = %3
%8 = icmp eq i32* %2, null
br i1 %8, label %25, label %9
9: ; preds = %7
store i32 1, i32* %2
br label %25
10: ; preds = %3
%11 = bitcast i8** %4 to i8*
store i8* null, i8** %4
%12 = call i64 @strtod(i8* %5, i8** %4)
%13 = load i8, i8* %5
%14 = icmp eq i8 %13, 0
br i1 %14, label %20, label %15
15: ; preds = %10
%16 = sitofp i64 %12 to double
%17 = load i8*, i8** %4
%18 = load i8, i8* %17
%19 = icmp eq i8 %18, 0
br i1 %19, label %23, label %20
20: ; preds = %15, %10
%21 = icmp eq i32* %2, null
br i1 %21, label %23, label %22
22: ; preds = %20
store i32 1, i32* %2
br label %23
23: ; preds = %20, %22, %15
%24 = phi double [ %16, %15 ], [ 0.000000e+00, %22 ], [ 0.000000e+00, %20 ]
br label %25
25: ; preds = %7, %9, %23
%26 = phi double [ %24, %23 ], [ 0.000000e+00, %9 ], [ 0.000000e+00, %7 ]
ret double %26
}