#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_DONE;
int SQLITE_OK;
int /*<<< orphan*/ SQLITE_STATIC;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int, int, char *);
int /*<<< orphan*/ sqlite3_bind_blob(int /*<<< orphan*/ *, int, void *, int,
                                     int /*<<< orphan*/);
int /*<<< orphan*/ sqlite3_bind_text(int /*<<< orphan*/ *, int, char const *,
                                     int, 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 write_blob(sqlite3 *db, const char *fname, void *buf_ptr, int buf_len) {
  sqlite3_stmt *stmt;
  const char *sql = "INSERT INTO blobs(file_name, data) VALUES(?, ?)";
  int rc;
  rc = sqlite3_prepare_v2(db, sql, -1, &stmt, 0);
  if (rc != SQLITE_OK) {
    fprintf(stderr, "%d: Statement preparation failed (%d: \'%s\')\n", 165,
            sqlite3_errcode(db), sqlite3_errmsg(db));
    return rc;
  }
  sqlite3_bind_text(stmt, 1, fname, -1, SQLITE_STATIC);
  sqlite3_bind_blob(stmt, 2, buf_ptr, buf_len, SQLITE_STATIC);
  rc = sqlite3_step(stmt);
  if (rc != SQLITE_DONE) {
    fprintf(stderr, "%d: SQL statement execution failed (%d: \'%s\')\n", 177,
            sqlite3_errcode(db), sqlite3_errmsg(db));
    return 1;
  }
  rc = sqlite3_finalize(stmt);
  return rc;
}


x86 assembly (clang, -O0):
<write_blob>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
lea    0xe62(%rip),%rax
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    $0xffffffff,%edx
lea    -0x30(%rbp),%rcx
xor    %r8d,%r8d
call   10a0 <sqlite3_prepare_v2@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
mov    0x2e20(%rip),%rcx
cmp    (%rcx),%eax
je     1216 <write_blob+0x96>
mov    0x2e01(%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    0xe2f(%rip),%rsi
mov    $0xa5,%edx
call   1060 <fprintf@plt>
mov    -0x3c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12cd <write_blob+0x14d>
mov    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    0x2dab(%rip),%rax
mov    (%rax),%r8d
mov    $0x1,%esi
mov    $0xffffffff,%ecx
call   1070 <sqlite3_bind_text@plt>
mov    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    -0x24(%rbp),%ecx
mov    0x2d87(%rip),%rax
mov    (%rax),%r8d
mov    $0x2,%esi
call   1080 <sqlite3_bind_blob@plt>
mov    -0x30(%rbp),%rdi
call   1050 <sqlite3_step@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
mov    0x2d5c(%rip),%rcx
cmp    (%rcx),%eax
je     12bb <write_blob+0x13b>
mov    0x2d5d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <sqlite3_errcode@plt>
mov    %eax,-0x48(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <sqlite3_errmsg@plt>
mov    -0x4c(%rbp),%edi
mov    -0x48(%rbp),%ecx
mov    %rax,%r8
lea    0xdb8(%rip),%rsi
mov    $0xb1,%edx
call   1060 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12cd <write_blob+0x14d>
mov    -0x30(%rbp),%rdi
call   1090 <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):
<write_blob>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %ecx,%r14d
mov    %rdx,%r15
mov    %rsi,%r12
mov    %rdi,%rbx
lea    0xe62(%rip),%rsi
mov    %rsp,%rcx
mov    $0xffffffff,%edx
xor    %r8d,%r8d
call   10a0 <sqlite3_prepare_v2@plt>
mov    %eax,%ebp
mov    0x2e31(%rip),%rax
cmp    (%rax),%ebp
jne    121a <write_blob+0x9a>
mov    (%rsp),%rdi
mov    0x2e0a(%rip),%r13
mov    0x0(%r13),%r8d
mov    $0x1,%ebp
mov    $0x1,%esi
mov    %r12,%rdx
mov    $0xffffffff,%ecx
call   1070 <sqlite3_bind_text@plt>
mov    (%rsp),%rdi
mov    0x0(%r13),%r8d
mov    $0x2,%esi
mov    %r15,%rdx
mov    %r14d,%ecx
call   1080 <sqlite3_bind_blob@plt>
mov    (%rsp),%rdi
call   1050 <sqlite3_step@plt>
mov    0x2dbf(%rip),%rcx
cmp    (%rcx),%eax
jne    1248 <write_blob+0xc8>
mov    (%rsp),%rdi
call   1090 <sqlite3_finalize@plt>
mov    %eax,%ebp
jmp    127f <write_blob+0xff>
mov    0x2db7(%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    0xdf2(%rip),%rsi
mov    %r14d,%edi
mov    $0xa5,%edx
jmp    1274 <write_blob+0xf4>
mov    0x2d89(%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    0xdf1(%rip),%rsi
mov    %r14d,%edi
mov    $0xb1,%edx
mov    %r15d,%ecx
mov    %rax,%r8
call   1060 <fprintf@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [48 x i8] c"INSERT INTO blobs(file_name, data) VALUES(?, ?)\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_DONE = global i32 0
@.str.2 = [47 x i8] c"%d: SQL statement execution failed (%d: '%s')\0A\00"
define i32 @write_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 ([48 x i8], [48 x i8]* @.str, i64 0, i64 0), i8** %11
%13 = load i32*, i32** %6
%14 = load i8*, i8** %11
%15 = call i32 @sqlite3_prepare_v2(i32* %13, i8* %14, i32 -1, i32** %10, i32 0)
store i32 %15, i32* %12
%16 = load i32, i32* %12
%17 = load i32, i32* @SQLITE_OK
%18 = icmp ne i32 %16, %17
br i1 %18, label %19, label %27
19: ; preds = %4
%20 = load i32, i32* @stderr
%21 = load i32*, i32** %6
%22 = call i32 @sqlite3_errcode(i32* %21)
%23 = load i32*, i32** %6
%24 = call i8* @sqlite3_errmsg(i32* %23)
%25 = call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0), i32 165, i32 %22, i8* %24)
%26 = load i32, i32* %12
store i32 %26, i32* %5
br label %53
27: ; preds = %4
%28 = load i32*, i32** %10
%29 = load i8*, i8** %7
%30 = load i32, i32* @SQLITE_STATIC
%31 = call i32 @sqlite3_bind_text(i32* %28, i32 1, i8* %29, i32 -1, i32 %30)
%32 = load i32*, i32** %10
%33 = load i8*, i8** %8
%34 = load i32, i32* %9
%35 = load i32, i32* @SQLITE_STATIC
%36 = call i32 @sqlite3_bind_blob(i32* %32, i32 2, i8* %33, i32 %34, i32 %35)
%37 = load i32*, i32** %10
%38 = call i32 @sqlite3_step(i32* %37)
store i32 %38, i32* %12
%39 = load i32, i32* %12
%40 = load i32, i32* @SQLITE_DONE
%41 = icmp ne i32 %39, %40
br i1 %41, label %42, label %49
42: ; preds = %27
%43 = load i32, i32* @stderr
%44 = load i32*, i32** %6
%45 = call i32 @sqlite3_errcode(i32* %44)
%46 = load i32*, i32** %6
%47 = call i8* @sqlite3_errmsg(i32* %46)
%48 = call i32 @fprintf(i32 %43, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.2, i64 0, i64 0), i32 177, i32 %45, i8* %47)
store i32 1, i32* %5
br label %53
49: ; preds = %27
%50 = load i32*, i32** %10
%51 = call i32 @sqlite3_finalize(i32* %50)
store i32 %51, i32* %12
%52 = load i32, i32* %12
store i32 %52, i32* %5
br label %53
53: ; preds = %49, %42, %19
%54 = load i32, i32* %5
ret i32 %54
}

LLVM IR (-O2):
@.str = [48 x i8] c"INSERT INTO blobs(file_name, data) VALUES(?, ?)\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_DONE = global i32 0
@.str.2 = [47 x i8] c"%d: SQL statement execution failed (%d: '%s')\0A\00"
define i32 @write_blob(i32* %0, i8* %1, i8* %2, i32 %3) {
%5 = alloca i32*
%6 = bitcast i32** %5 to i8*
%7 = call i32 @sqlite3_prepare_v2(i32* %0, i8* getelementptr inbounds ([48 x i8], [48 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 165, i32 %12, i8* %13)
br label %34
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 = load i32, i32* @SQLITE_STATIC
%21 = call i32 @sqlite3_bind_blob(i32* %19, i32 2, i8* %2, i32 %3, i32 %20)
%22 = load i32*, i32** %5
%23 = call i32 @sqlite3_step(i32* %22)
%24 = load i32, i32* @SQLITE_DONE
%25 = icmp eq i32 %23, %24
br i1 %25, label %31, label %26
26: ; preds = %15
%27 = load i32, i32* @stderr
%28 = call i32 @sqlite3_errcode(i32* %0)
%29 = call i8* @sqlite3_errmsg(i32* %0)
%30 = call i32 @fprintf(i32 %27, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.2, i64 0, i64 0), i32 177, i32 %28, i8* %29)
br label %34
31: ; preds = %15
%32 = load i32*, i32** %5
%33 = call i32 @sqlite3_finalize(i32* %32)
br label %34
34: ; preds = %31, %26, %10
%35 = phi i32 [ %7, %10 ], [ 1, %26 ], [ %33, %31 ]
ret i32 %35
}