#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_stmt;
typedef int /*<<< orphan*/ sqlite3;

/* Variables and functions */
int SQLITE_OK;
int SQLITE_ROW;
int /*<<< orphan*/ SQLITE_STATIC;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int, int, char *);
scalar_t__ malloc(int);
int /*<<< orphan*/ sqlite3_bind_text(int /*<<< orphan*/ *, int, char const *,
                                     int, int /*<<< orphan*/);
int sqlite3_column_bytes(int /*<<< orphan*/ *, int /*<<< orphan*/);
int sqlite3_errcode(int /*<<< orphan*/ *);
char *sqlite3_errmsg(int /*<<< orphan*/ *);
int sqlite3_finalize(int /*<<< orphan*/ *);
int sqlite3_prepare_v2(int /*<<< orphan*/ *, char const *, int,
                       int /*<<< orphan*/ **, int /*<<< orphan*/);
int sqlite3_step(int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
int read_blob(sqlite3 *db, const char *fname, unsigned char **buf_ptr,
              int *buf_len) {
  sqlite3_stmt *stmt;
  const char *sql = "SELECT data FROM blobs WHERE file_name = ?";
  int rc;
  *buf_ptr = 0;
  *buf_len = 0;
  rc = sqlite3_prepare_v2(db, sql, -1, &stmt, 0);
  if (rc != SQLITE_OK) {
    fprintf(stderr, "%d: Statement preparation failed (%d: \'%s\')\n", 201,
            sqlite3_errcode(db), sqlite3_errmsg(db));
    return rc;
  }
  sqlite3_bind_text(stmt, 1, fname, -1, SQLITE_STATIC);
  rc = sqlite3_step(stmt);
  if (rc == SQLITE_ROW) {
    *buf_len = sqlite3_column_bytes(stmt, 0);
    *buf_ptr = (unsigned char *)malloc(*buf_len);
  }
  rc = sqlite3_finalize(stmt);
  return rc;
}


x86 assembly (clang, -O0):
<read_blob>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
lea    0xe51(%rip),%rax
mov    %rax,-0x38(%rbp)
mov    -0x20(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x28(%rbp),%rax
movl   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    $0xffffffff,%edx
lea    -0x30(%rbp),%rcx
xor    %r8d,%r8d
call   10b0 <sqlite3_prepare_v2@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
mov    0x2dfa(%rip),%rcx
cmp    (%rcx),%eax
je     123c <read_blob+0xac>
mov    0x2ddb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <sqlite3_errcode@plt>
mov    %eax,-0x40(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <sqlite3_errmsg@plt>
mov    -0x44(%rbp),%edi
mov    -0x40(%rbp),%ecx
mov    %rax,%r8
lea    0xe04(%rip),%rsi
mov    $0xc9,%edx
call   1060 <fprintf@plt>
mov    -0x3c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12b5 <read_blob+0x125>
mov    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    0x2d85(%rip),%rax
mov    (%rax),%r8d
mov    $0x1,%esi
mov    $0xffffffff,%ecx
call   1090 <sqlite3_bind_text@plt>
mov    -0x30(%rbp),%rdi
call   1050 <sqlite3_step@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
mov    0x2d55(%rip),%rcx
cmp    (%rcx),%eax
jne    12a3 <read_blob+0x113>
mov    -0x30(%rbp),%rdi
xor    %esi,%esi
call   1070 <sqlite3_column_bytes@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x28(%rbp),%rax
mov    (%rax),%edi
call   1080 <malloc@plt>
mov    %rax,%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x30(%rbp),%rdi
call   10a0 <sqlite3_finalize@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_blob>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rcx,%r15
mov    %rdx,%r14
mov    %rsi,%r12
mov    %rdi,%rbx
movq   $0x0,(%rdx)
movl   $0x0,(%rcx)
lea    0xe44(%rip),%rsi
lea    0x8(%rsp),%rcx
mov    $0xffffffff,%edx
xor    %r8d,%r8d
call   10b0 <sqlite3_prepare_v2@plt>
mov    %eax,%ebp
mov    0x2e11(%rip),%rax
cmp    (%rax),%ebp
jne    1238 <read_blob+0xa8>
mov    0x8(%rsp),%rdi
mov    0x2de9(%rip),%rax
mov    (%rax),%r8d
mov    $0x1,%esi
mov    %r12,%rdx
mov    $0xffffffff,%ecx
call   1090 <sqlite3_bind_text@plt>
mov    0x8(%rsp),%rdi
call   1050 <sqlite3_step@plt>
mov    0x2dbb(%rip),%rcx
cmp    (%rcx),%eax
jne    122a <read_blob+0x9a>
mov    0x8(%rsp),%rdi
xor    %esi,%esi
call   1070 <sqlite3_column_bytes@plt>
mov    %eax,(%r15)
mov    %eax,%edi
call   1080 <malloc@plt>
mov    %rax,(%r14)
mov    0x8(%rsp),%rdi
call   10a0 <sqlite3_finalize@plt>
mov    %eax,%ebp
jmp    126f <read_blob+0xdf>
mov    0x2d99(%rip),%rax
mov    (%rax),%r14d
mov    %rbx,%rdi
call   1030 <sqlite3_errcode@plt>
mov    %eax,%r15d
mov    %rbx,%rdi
call   1040 <sqlite3_errmsg@plt>
lea    0xdcf(%rip),%rsi
mov    %r14d,%edi
mov    $0xc9,%edx
mov    %r15d,%ecx
mov    %rax,%r8
call   1060 <fprintf@plt>
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [43 x i8] c"SELECT data FROM blobs WHERE file_name = ?\00"
@SQLITE_OK = global i32 0
@stderr = global i32 0
@.str.1 = [45 x i8] c"%d: Statement preparation failed (%d: '%s')\0A\00"
@SQLITE_STATIC = global i32 0
@SQLITE_ROW = global i32 0
define i32 @read_blob(i32* %0, i8* %1, i8** %2, i32* %3) {
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i8*
%8 = alloca i8**
%9 = alloca i32*
%10 = alloca i32*
%11 = alloca i8*
%12 = alloca i32
store i32* %0, i32** %6
store i8* %1, i8** %7
store i8** %2, i8*** %8
store i32* %3, i32** %9
store i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0), i8** %11
%13 = load i8**, i8*** %8
store i8* null, i8** %13
%14 = load i32*, i32** %9
store i32 0, i32* %14
%15 = load i32*, i32** %6
%16 = load i8*, i8** %11
%17 = call i32 @sqlite3_prepare_v2(i32* %15, i8* %16, i32 -1, i32** %10, i32 0)
store i32 %17, i32* %12
%18 = load i32, i32* %12
%19 = load i32, i32* @SQLITE_OK
%20 = icmp ne i32 %18, %19
br i1 %20, label %21, label %29
21: ; preds = %4
%22 = load i32, i32* @stderr
%23 = load i32*, i32** %6
%24 = call i32 @sqlite3_errcode(i32* %23)
%25 = load i32*, i32** %6
%26 = call i8* @sqlite3_errmsg(i32* %25)
%27 = call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0), i32 201, i32 %24, i8* %26)
%28 = load i32, i32* %12
store i32 %28, i32* %5
br label %52
29: ; preds = %4
%30 = load i32*, i32** %10
%31 = load i8*, i8** %7
%32 = load i32, i32* @SQLITE_STATIC
%33 = call i32 @sqlite3_bind_text(i32* %30, i32 1, i8* %31, i32 -1, i32 %32)
%34 = load i32*, i32** %10
%35 = call i32 @sqlite3_step(i32* %34)
store i32 %35, i32* %12
%36 = load i32, i32* %12
%37 = load i32, i32* @SQLITE_ROW
%38 = icmp eq i32 %36, %37
br i1 %38, label %39, label %48
39: ; preds = %29
%40 = load i32*, i32** %10
%41 = call i32 @sqlite3_column_bytes(i32* %40, i32 0)
%42 = load i32*, i32** %9
store i32 %41, i32* %42
%43 = load i32*, i32** %9
%44 = load i32, i32* %43
%45 = call i64 @malloc(i32 %44)
%46 = inttoptr i64 %45 to i8*
%47 = load i8**, i8*** %8
store i8* %46, i8** %47
br label %48
48: ; preds = %39, %29
%49 = load i32*, i32** %10
%50 = call i32 @sqlite3_finalize(i32* %49)
store i32 %50, i32* %12
%51 = load i32, i32* %12
store i32 %51, i32* %5
br label %52
52: ; preds = %48, %21
%53 = load i32, i32* %5
ret i32 %53
}

LLVM IR (-O2):
@.str = [43 x i8] c"SELECT data FROM blobs WHERE file_name = ?\00"
@SQLITE_OK = global i32 0
@stderr = global i32 0
@.str.1 = [45 x i8] c"%d: Statement preparation failed (%d: '%s')\0A\00"
@SQLITE_STATIC = global i32 0
@SQLITE_ROW = global i32 0
define i32 @read_blob(i32* %0, i8* %1, i8** nocapture writeonly %2, i32* nocapture writeonly %3) {
%5 = alloca i32*
%6 = bitcast i32** %5 to i8*
store i8* null, i8** %2
store i32 0, i32* %3
%7 = call i32 @sqlite3_prepare_v2(i32* %0, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0), i32 -1, i32** %5, i32 0)
%8 = load i32, i32* @SQLITE_OK
%9 = icmp eq i32 %7, %8
br i1 %9, label %15, label %10
10: ; preds = %4
%11 = load i32, i32* @stderr
%12 = call i32 @sqlite3_errcode(i32* %0)
%13 = call i8* @sqlite3_errmsg(i32* %0)
%14 = call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0), i32 201, i32 %12, i8* %13)
br label %31
15: ; preds = %4
%16 = load i32*, i32** %5
%17 = load i32, i32* @SQLITE_STATIC
%18 = call i32 @sqlite3_bind_text(i32* %16, i32 1, i8* %1, i32 -1, i32 %17)
%19 = load i32*, i32** %5
%20 = call i32 @sqlite3_step(i32* %19)
%21 = load i32, i32* @SQLITE_ROW
%22 = icmp eq i32 %20, %21
br i1 %22, label %23, label %28
23: ; preds = %15
%24 = load i32*, i32** %5
%25 = call i32 @sqlite3_column_bytes(i32* %24, i32 0)
store i32 %25, i32* %3
%26 = call i64 @malloc(i32 %25)
%27 = inttoptr i64 %26 to i8*
store i8* %27, i8** %2
br label %28
28: ; preds = %23, %15
%29 = load i32*, i32** %5
%30 = call i32 @sqlite3_finalize(i32* %29)
br label %31
31: ; preds = %28, %10
%32 = phi i32 [ %7, %10 ], [ %30, %28 ]
ret i32 %32
}