#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 */
typedef int /*<<< orphan*/ sqlite3_value;
typedef int /*<<< orphan*/ sqlite3_context;

/* Variables and functions */
int /*<<< orphan*/ sqlite3_result_double(int /*<<< orphan*/ *,
                                         int /*<<< orphan*/);
int /*<<< orphan*/ sqlite3_result_error(int /*<<< orphan*/ *, char *, int);
int /*<<< orphan*/ sqlite3_result_null(int /*<<< orphan*/ *);
double sqlite3_value_double(int /*<<< orphan*/ *);
scalar_t__ sqlite3_value_text(int /*<<< orphan*/ *);
int /*<<< orphan*/ sqrt(double);
int sscanf(char *, char *, double *);
extern void sql_sqrt(sqlite3_context *context, int argc, sqlite3_value **argv) {
  double val;
  char *v;
  v = (char *)sqlite3_value_text(argv[0]);
  if (v == NULL) {
    sqlite3_result_null(context);
  } else {
    if (sscanf(v, "%lf", &val) == 1) {
      val = sqlite3_value_double(argv[0]);
      sqlite3_result_double(context, sqrt(val));
    } else {
      sqlite3_result_error(context, "Invalid numerical value", -1);
    }
  }
}


x86 assembly (clang, -O0):
<sql_sqrt>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
call   1090 <sqlite3_value_text@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11ac <sql_sqrt+0x3c>
mov    -0x8(%rbp),%rdi
call   1040 <sqlite3_result_null@plt>
jmp    1216 <sql_sqrt+0xa6>
mov    -0x28(%rbp),%rdi
lea    0xe49(%rip),%rsi
lea    -0x20(%rbp),%rdx
call   1050 <sscanf@plt>
cmp    $0x1,%eax
jne    11fc <sql_sqrt+0x8c>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
call   1080 <sqlite3_value_double@plt>
movsd  %xmm0,-0x20(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x30(%rbp)
movsd  -0x20(%rbp),%xmm0
call   1070 <sqrt@plt>
mov    -0x30(%rbp),%rdi
mov    %eax,%esi
call   1060 <sqlite3_result_double@plt>
jmp    1211 <sql_sqrt+0xa1>
mov    -0x8(%rbp),%rdi
lea    0xdfd(%rip),%rsi
mov    $0xffffffff,%edx
call   1030 <sqlite3_result_error@plt>
jmp    1216 <sql_sqrt+0xa6>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sql_sqrt>:
push   %r14
push   %rbx
push   %rax
mov    %rdx,%rbx
mov    %rdi,%r14
mov    (%rdx),%rdi
call   1090 <sqlite3_value_text@plt>
test   %rax,%rax
je     11bc <sql_sqrt+0x4c>
lea    0xe72(%rip),%rsi
mov    %rsp,%rdx
mov    %rax,%rdi
call   1050 <sscanf@plt>
cmp    $0x1,%eax
jne    11cb <sql_sqrt+0x5b>
mov    (%rbx),%rdi
call   1080 <sqlite3_value_double@plt>
movsd  %xmm0,(%rsp)
call   1070 <sqrt@plt>
mov    %r14,%rdi
mov    %eax,%esi
call   1060 <sqlite3_result_double@plt>
jmp    11df <sql_sqrt+0x6f>
mov    %r14,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1040 <sqlite3_result_null@plt>
lea    0xe32(%rip),%rsi
mov    %r14,%rdi
mov    $0xffffffff,%edx
call   1030 <sqlite3_result_error@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [4 x i8] c"%lf\00"
@.str.1 = [24 x i8] c"Invalid numerical value\00"
define void @sql_sqrt(i32* %0, i32 %1, i32** %2) {
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i32**
%7 = alloca double
%8 = alloca i8*
store i32* %0, i32** %4
store i32 %1, i32* %5
store i32** %2, i32*** %6
%9 = load i32**, i32*** %6
%10 = getelementptr inbounds i32*, i32** %9, i64 0
%11 = load i32*, i32** %10
%12 = call i64 @sqlite3_value_text(i32* %11)
%13 = inttoptr i64 %12 to i8*
store i8* %13, i8** %8
%14 = load i8*, i8** %8
%15 = icmp eq i8* %14, null
br i1 %15, label %16, label %19
16: ; preds = %3
%17 = load i32*, i32** %4
%18 = call i32 @sqlite3_result_null(i32* %17)
br label %36
19: ; preds = %3
%20 = load i8*, i8** %8
%21 = call i32 @sscanf(i8* %20, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), double* %7)
%22 = icmp eq i32 %21, 1
br i1 %22, label %23, label %32
23: ; preds = %19
%24 = load i32**, i32*** %6
%25 = getelementptr inbounds i32*, i32** %24, i64 0
%26 = load i32*, i32** %25
%27 = call double @sqlite3_value_double(i32* %26)
store double %27, double* %7
%28 = load i32*, i32** %4
%29 = load double, double* %7
%30 = call i32 @sqrt(double %29)
%31 = call i32 @sqlite3_result_double(i32* %28, i32 %30)
br label %35
32: ; preds = %19
%33 = load i32*, i32** %4
%34 = call i32 @sqlite3_result_error(i32* %33, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i32 -1)
br label %35
35: ; preds = %32, %23
br label %36
36: ; preds = %35, %16
ret void
}

LLVM IR (-O2):
@.str = [4 x i8] c"%lf\00"
@.str.1 = [24 x i8] c"Invalid numerical value\00"
define void @sql_sqrt(i32* %0, i32 %1, i32** nocapture %2) {
%4 = alloca double
%5 = bitcast double* %4 to i8*
%6 = load i32*, i32** %2
%7 = tail call i64 @sqlite3_value_text(i32* %6)
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %3
%10 = tail call i32 @sqlite3_result_null(i32* %0)
br label %22
11: ; preds = %3
%12 = inttoptr i64 %7 to i8*
%13 = call i32 @sscanf(i8* %12, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), double* %4)
%14 = icmp eq i32 %13, 1
br i1 %14, label %15, label %20
15: ; preds = %11
%16 = load i32*, i32** %2
%17 = call double @sqlite3_value_double(i32* %16)
store double %17, double* %4
%18 = call i32 @sqrt(double %17)
%19 = call i32 @sqlite3_result_double(i32* %0, i32 %18)
br label %22
20: ; preds = %11
%21 = call i32 @sqlite3_result_error(i32* %0, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i32 -1)
br label %22
22: ; preds = %15, %20, %9
ret void
}