#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*/ err_printf(char *, char *);
int sscanf(char *, char *, float *);
int scan_double(double *d, char *val) {
  int ret;
  float v;
  ret = sscanf(val, "%f", &v);
  if (ret != 1) {
    err_printf("bad float value: %s\n", val);
    return -1;
  }
  *d = v;
  return 0;
}


x86 assembly (clang, -O0):
<scan_double>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xec5(%rip),%rsi
lea    -0x20(%rbp),%rdx
call   1030 <sscanf@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x1,-0x1c(%rbp)
je     116d <scan_double+0x4d>
mov    -0x18(%rbp),%rsi
lea    0xea7(%rip),%rdi
call   1040 <err_printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1185 <scan_double+0x65>
movss  -0x20(%rbp),%xmm0
cvtss2sd %xmm0,%xmm0
mov    -0x10(%rbp),%rax
movsd  %xmm0,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<scan_double>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %rdi,%r14
lea    0xecf(%rip),%rsi
lea    0x4(%rsp),%rdx
mov    %rbx,%rdi
call   1030 <sscanf@plt>
cmp    $0x1,%eax
jne    1156 <scan_double+0x36>
movss  0x4(%rsp),%xmm0
cvtss2sd %xmm0,%xmm0
movsd  %xmm0,(%r14)
xor    %eax,%eax
jmp    116a <scan_double+0x4a>
lea    0xea6(%rip),%rdi
mov    %rbx,%rsi
call   1040 <err_printf@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [3 x i8] c"%f\00"
@.str.1 = [21 x i8] c"bad float value: %s\0A\00"
define i32 @scan_double(double* %0, i8* %1) {
%3 = alloca i32
%4 = alloca double*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca float
store double* %0, double** %4
store i8* %1, i8** %5
%8 = load i8*, i8** %5
%9 = call i32 @sscanf(i8* %8, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), float* %7)
store i32 %9, i32* %6
%10 = load i32, i32* %6
%11 = icmp ne i32 %10, 1
br i1 %11, label %12, label %15
12: ; preds = %2
%13 = load i8*, i8** %5
%14 = call i32 @err_printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i8* %13)
store i32 -1, i32* %3
br label %19
15: ; preds = %2
%16 = load float, float* %7
%17 = fpext float %16 to double
%18 = load double*, double** %4
store double %17, double* %18
store i32 0, i32* %3
br label %19
19: ; preds = %15, %12
%20 = load i32, i32* %3
ret i32 %20
}

LLVM IR (-O2):
@.str = [3 x i8] c"%f\00"
@.str.1 = [21 x i8] c"bad float value: %s\0A\00"
define i32 @scan_double(double* nocapture writeonly %0, i8* %1) {
%3 = alloca float
%4 = bitcast float* %3 to i8*
%5 = call i32 @sscanf(i8* %1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), float* %3)
%6 = icmp eq i32 %5, 1
br i1 %6, label %9, label %7
7: ; preds = %2
%8 = call i32 @err_printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i8* %1)
br label %12
9: ; preds = %2
%10 = load float, float* %3
%11 = fpext float %10 to double
store double %11, double* %0
br label %12
12: ; preds = %9, %7
%13 = phi i32 [ -1, %7 ], [ 0, %9 ]
ret i32 %13
}