#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*/ SQLITE_TRANSIENT;
int /*<<< orphan*/ sqlite3_result_null(int /*<<< orphan*/ *);
int /*<<< orphan*/ sqlite3_result_text(int /*<<< orphan*/ *, char *, int,
                                       int /*<<< orphan*/);
char *sqlite3_value_text(int /*<<< orphan*/ *);
void firstchar(sqlite3_context *context, int argc, sqlite3_value **argv) {
  if (argc == 1) {
    const char *text = sqlite3_value_text(argv[0]);
    if (text && text[0]) {
      char result[2];
      result[0] = text[0];
      result[1] = '\0';
      sqlite3_result_text(context, result, -1, SQLITE_TRANSIENT);
      return;
    }
  }
  sqlite3_result_null(context);
}


x86 assembly (clang, -O0):
<firstchar>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
cmpl   $0x1,-0xc(%rbp)
jne    11aa <firstchar+0x7a>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
call   1050 <sqlite3_value_text@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11a5 <firstchar+0x75>
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     11a5 <firstchar+0x75>
mov    -0x20(%rbp),%rax
mov    (%rax),%al
mov    %al,-0x22(%rbp)
movb   $0x0,-0x21(%rbp)
mov    -0x8(%rbp),%rdi
lea    -0x22(%rbp),%rsi
mov    0x2e4c(%rip),%rax
mov    (%rax),%ecx
mov    $0xffffffff,%edx
call   1030 <sqlite3_result_text@plt>
jmp    11b3 <firstchar+0x83>
jmp    11aa <firstchar+0x7a>
mov    -0x8(%rbp),%rdi
call   1040 <sqlite3_result_null@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<firstchar>:
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
cmp    $0x1,%esi
jne    117a <firstchar+0x4a>
mov    (%rdx),%rdi
call   1050 <sqlite3_value_text@plt>
test   %rax,%rax
je     117a <firstchar+0x4a>
mov    (%rax),%al
test   %al,%al
je     117a <firstchar+0x4a>
mov    %al,0xe(%rsp)
movb   $0x0,0xf(%rsp)
mov    0x2e80(%rip),%rax
mov    (%rax),%ecx
lea    0xe(%rsp),%rsi
mov    %rbx,%rdi
mov    $0xffffffff,%edx
call   1030 <sqlite3_result_text@plt>
add    $0x10,%rsp
pop    %rbx
ret
mov    %rbx,%rdi
add    $0x10,%rsp
pop    %rbx
jmp    1040 <sqlite3_result_null@plt>

LLVM IR (-O0):
@SQLITE_TRANSIENT = global i32 0
define void @firstchar(i32* %0, i32 %1, i32** %2) {
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i32**
%7 = alloca i8*
%8 = alloca [2 x i8]
store i32* %0, i32** %4
store i32 %1, i32* %5
store i32** %2, i32*** %6
%9 = load i32, i32* %5
%10 = icmp eq i32 %9, 1
br i1 %10, label %11, label %35
11: ; preds = %3
%12 = load i32**, i32*** %6
%13 = getelementptr inbounds i32*, i32** %12, i64 0
%14 = load i32*, i32** %13
%15 = call i8* @sqlite3_value_text(i32* %14)
store i8* %15, i8** %7
%16 = load i8*, i8** %7
%17 = icmp ne i8* %16, null
br i1 %17, label %18, label %34
18: ; preds = %11
%19 = load i8*, i8** %7
%20 = getelementptr inbounds i8, i8* %19, i64 0
%21 = load i8, i8* %20
%22 = sext i8 %21 to i32
%23 = icmp ne i32 %22, 0
br i1 %23, label %24, label %34
24: ; preds = %18
%25 = load i8*, i8** %7
%26 = getelementptr inbounds i8, i8* %25, i64 0
%27 = load i8, i8* %26
%28 = getelementptr inbounds [2 x i8], [2 x i8]* %8, i64 0, i64 0
store i8 %27, i8* %28
%29 = getelementptr inbounds [2 x i8], [2 x i8]* %8, i64 0, i64 1
store i8 0, i8* %29
%30 = load i32*, i32** %4
%31 = getelementptr inbounds [2 x i8], [2 x i8]* %8, i64 0, i64 0
%32 = load i32, i32* @SQLITE_TRANSIENT
%33 = call i32 @sqlite3_result_text(i32* %30, i8* %31, i32 -1, i32 %32)
br label %38
34: ; preds = %18, %11
br label %35
35: ; preds = %34, %3
%36 = load i32*, i32** %4
%37 = call i32 @sqlite3_result_null(i32* %36)
br label %38
38: ; preds = %35, %24
ret void
}

LLVM IR (-O2):
@SQLITE_TRANSIENT = global i32 0
define void @firstchar(i32* %0, i32 %1, i32** nocapture %2) {
%4 = alloca [2 x i8]
%5 = icmp eq i32 %1, 1
br i1 %5, label %6, label %18
6: ; preds = %3
%7 = load i32*, i32** %2
%8 = tail call i8* @sqlite3_value_text(i32* %7)
%9 = icmp eq i8* %8, null
br i1 %9, label %18, label %10
10: ; preds = %6
%11 = load i8, i8* %8
%12 = icmp eq i8 %11, 0
br i1 %12, label %18, label %13
13: ; preds = %10
%14 = getelementptr inbounds [2 x i8], [2 x i8]* %4, i64 0, i64 0
store i8 %11, i8* %14
%15 = getelementptr inbounds [2 x i8], [2 x i8]* %4, i64 0, i64 1
store i8 0, i8* %15
%16 = load i32, i32* @SQLITE_TRANSIENT
%17 = call i32 @sqlite3_result_text(i32* %0, i8* %14, i32 -1, i32 %16)
br label %20
18: ; preds = %6, %10, %3
%19 = tail call i32 @sqlite3_result_null(i32* %0)
br label %20
20: ; preds = %13, %18
ret void
}