#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 /*<<< orphan*/ EXIT_FAILURE;
scalar_t__ SQLITE_DONE;
scalar_t__ SQLITE_OK;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ sqlite3_bind_blob(int /*<<< orphan*/ *, int, char const *,
                                     int, int /*<<< orphan*/ *);
int /*<<< orphan*/ sqlite3_bind_int(int /*<<< orphan*/ *, int, int);
char *sqlite3_errmsg(int /*<<< orphan*/ *);
scalar_t__ sqlite3_finalize(int /*<<< orphan*/ *);
scalar_t__ sqlite3_prepare_v2(int /*<<< orphan*/ *, char const *, int,
                              int /*<<< orphan*/ **, int /*<<< orphan*/ *);
scalar_t__ sqlite3_step(int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
void mbtiles_write_tile(sqlite3 *outdb, int z, int tx, int ty, const char *data,
                        int size) {
  sqlite3_stmt *stmt;
  const char *query = "insert into tiles (zoom_level, tile_column, tile_row, "
                      "tile_data) values (?, ?, ?, ?)";
  if (sqlite3_prepare_v2(outdb, query, -1, &stmt, NULL) != SQLITE_OK) {
    fprintf(stderr, "sqlite3 insert prep failed\n");
    exit(EXIT_FAILURE);
  }
  sqlite3_bind_int(stmt, 1, z);
  sqlite3_bind_int(stmt, 2, tx);
  sqlite3_bind_int(stmt, 3, (1 << z) - 1 - ty);
  sqlite3_bind_blob(stmt, 4, data, size, NULL);
  if (sqlite3_step(stmt) != SQLITE_DONE) {
    fprintf(stderr, "sqlite3 insert failed: %s\n", sqlite3_errmsg(outdb));
  }
  if (sqlite3_finalize(stmt) != SQLITE_OK) {
    fprintf(stderr, "sqlite3 finalize failed: %s\n", sqlite3_errmsg(outdb));
  }
}


x86 assembly (clang, -O0):
<mbtiles_write_tile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %ecx,-0x14(%rbp)
mov    %r8,-0x20(%rbp)
mov    %r9d,-0x24(%rbp)
lea    0xe5c(%rip),%rax
mov    %rax,-0x38(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    $0xffffffff,%edx
lea    -0x30(%rbp),%rcx
xor    %eax,%eax
mov    %eax,%r8d
call   1090 <sqlite3_prepare_v2@plt>
mov    0x2e1e(%rip),%rcx
cmp    (%rcx),%rax
je     11f8 <mbtiles_write_tile+0x78>
mov    0x2dfe(%rip),%rax
mov    (%rax),%edi
lea    0xe72(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    0x2dd7(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x30(%rbp),%rdi
mov    -0xc(%rbp),%edx
mov    $0x1,%esi
call   1030 <sqlite3_bind_int@plt>
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%edx
mov    $0x2,%esi
call   1030 <sqlite3_bind_int@plt>
mov    -0x30(%rbp),%rdi
mov    -0xc(%rbp),%ecx
mov    $0x1,%edx
shl    %cl,%edx
sub    $0x1,%edx
sub    -0x14(%rbp),%edx
mov    $0x3,%esi
call   1030 <sqlite3_bind_int@plt>
mov    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    -0x24(%rbp),%ecx
mov    $0x4,%esi
xor    %eax,%eax
mov    %eax,%r8d
call   1070 <sqlite3_bind_blob@plt>
mov    -0x30(%rbp),%rdi
call   1050 <sqlite3_step@plt>
mov    0x2d6e(%rip),%rcx
cmp    (%rcx),%rax
je     1294 <mbtiles_write_tile+0x114>
mov    0x2d66(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <sqlite3_errmsg@plt>
mov    -0x3c(%rbp),%edi
mov    %rax,%rdx
lea    0xde4(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x30(%rbp),%rdi
call   1080 <sqlite3_finalize@plt>
mov    0x2d44(%rip),%rcx
cmp    (%rcx),%rax
je     12d6 <mbtiles_write_tile+0x156>
mov    0x2d24(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <sqlite3_errmsg@plt>
mov    -0x40(%rbp),%edi
mov    %rax,%rdx
lea    0xdbd(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mbtiles_write_tile>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %r9d,0x14(%rsp)
mov    %r8,%r15
mov    %ecx,%r13d
mov    %edx,%r14d
mov    %esi,%ebp
mov    %rdi,%r12
lea    0xe58(%rip),%rsi
mov    $0xffffffff,%ebx
lea    0x8(%rsp),%rcx
mov    $0xffffffff,%edx
xor    %r8d,%r8d
call   1090 <sqlite3_prepare_v2@plt>
mov    0x2e22(%rip),%rcx
cmp    (%rcx),%rax
jne    12a8 <mbtiles_write_tile+0x128>
mov    0x8(%rsp),%rdi
mov    $0x1,%esi
mov    %ebp,%edx
call   1030 <sqlite3_bind_int@plt>
mov    0x8(%rsp),%rdi
mov    $0x2,%esi
mov    %r14d,%edx
call   1030 <sqlite3_bind_int@plt>
mov    %ebp,%ecx
shl    %cl,%ebx
mov    0x8(%rsp),%rdi
not    %ebx
sub    %r13d,%ebx
mov    $0x3,%esi
mov    %ebx,%edx
call   1030 <sqlite3_bind_int@plt>
mov    0x8(%rsp),%rdi
mov    $0x4,%esi
mov    %r15,%rdx
mov    0x14(%rsp),%ecx
xor    %r8d,%r8d
call   1070 <sqlite3_bind_blob@plt>
mov    0x8(%rsp),%rdi
call   1050 <sqlite3_step@plt>
mov    0x2d9a(%rip),%rcx
cmp    (%rcx),%rax
je     125f <mbtiles_write_tile+0xdf>
mov    0x2d96(%rip),%rax
mov    (%rax),%ebx
mov    %r12,%rdi
call   1040 <sqlite3_errmsg@plt>
lea    0xe1e(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    0x8(%rsp),%rdi
call   1080 <sqlite3_finalize@plt>
mov    0x2d78(%rip),%rcx
cmp    (%rcx),%rax
je     1299 <mbtiles_write_tile+0x119>
mov    0x2d5c(%rip),%rax
mov    (%rax),%ebx
mov    %r12,%rdi
call   1040 <sqlite3_errmsg@plt>
lea    0xdff(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d29(%rip),%rax
mov    (%rax),%edi
lea    0xd9d(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    0x2d02(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [85 x i8] c"insert into tiles (zoom_level, tile_column, tile_row, tile_data) values (?, ?, ?, ?)\00"
@SQLITE_OK = global i64 0
@stderr = global i32 0
@.str.1 = [28 x i8] c"sqlite3 insert prep failed\0A\00"
@EXIT_FAILURE = global i32 0
@SQLITE_DONE = global i64 0
@.str.2 = [27 x i8] c"sqlite3 insert failed: %s\0A\00"
@.str.3 = [29 x i8] c"sqlite3 finalize failed: %s\0A\00"
define void @mbtiles_write_tile(i32* %0, i32 %1, i32 %2, i32 %3, i8* %4, i32 %5) {
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i32
%13 = alloca i32*
%14 = alloca i8*
store i32* %0, i32** %7
store i32 %1, i32* %8
store i32 %2, i32* %9
store i32 %3, i32* %10
store i8* %4, i8** %11
store i32 %5, i32* %12
store i8* getelementptr inbounds ([85 x i8], [85 x i8]* @.str, i64 0, i64 0), i8** %14
%15 = load i32*, i32** %7
%16 = load i8*, i8** %14
%17 = call i64 @sqlite3_prepare_v2(i32* %15, i8* %16, i32 -1, i32** %13, i32* null)
%18 = load i64, i64* @SQLITE_OK
%19 = icmp ne i64 %17, %18
br i1 %19, label %20, label %25
20: ; preds = %6
%21 = load i32, i32* @stderr
%22 = call i32 (i32, i8*, ...) @fprintf(i32 %21, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
%23 = load i32, i32* @EXIT_FAILURE
%24 = call i32 @exit(i32 %23)
unreachable
25: ; preds = %6
%26 = load i32*, i32** %13
%27 = load i32, i32* %8
%28 = call i32 @sqlite3_bind_int(i32* %26, i32 1, i32 %27)
%29 = load i32*, i32** %13
%30 = load i32, i32* %9
%31 = call i32 @sqlite3_bind_int(i32* %29, i32 2, i32 %30)
%32 = load i32*, i32** %13
%33 = load i32, i32* %8
%34 = shl i32 1, %33
%35 = sub nsw i32 %34, 1
%36 = load i32, i32* %10
%37 = sub nsw i32 %35, %36
%38 = call i32 @sqlite3_bind_int(i32* %32, i32 3, i32 %37)
%39 = load i32*, i32** %13
%40 = load i8*, i8** %11
%41 = load i32, i32* %12
%42 = call i32 @sqlite3_bind_blob(i32* %39, i32 4, i8* %40, i32 %41, i32* null)
%43 = load i32*, i32** %13
%44 = call i64 @sqlite3_step(i32* %43)
%45 = load i64, i64* @SQLITE_DONE
%46 = icmp ne i64 %44, %45
br i1 %46, label %47, label %52
47: ; preds = %25
%48 = load i32, i32* @stderr
%49 = load i32*, i32** %7
%50 = call i8* @sqlite3_errmsg(i32* %49)
%51 = call i32 (i32, i8*, ...) @fprintf(i32 %48, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i8* %50)
br label %52
52: ; preds = %47, %25
%53 = load i32*, i32** %13
%54 = call i64 @sqlite3_finalize(i32* %53)
%55 = load i64, i64* @SQLITE_OK
%56 = icmp ne i64 %54, %55
br i1 %56, label %57, label %62
57: ; preds = %52
%58 = load i32, i32* @stderr
%59 = load i32*, i32** %7
%60 = call i8* @sqlite3_errmsg(i32* %59)
%61 = call i32 (i32, i8*, ...) @fprintf(i32 %58, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i8* %60)
br label %62
62: ; preds = %57, %52
ret void
}

LLVM IR (-O2):
@.str = [85 x i8] c"insert into tiles (zoom_level, tile_column, tile_row, tile_data) values (?, ?, ?, ?)\00"
@SQLITE_OK = global i64 0
@stderr = global i32 0
@.str.1 = [28 x i8] c"sqlite3 insert prep failed\0A\00"
@EXIT_FAILURE = global i32 0
@SQLITE_DONE = global i64 0
@.str.2 = [27 x i8] c"sqlite3 insert failed: %s\0A\00"
@.str.3 = [29 x i8] c"sqlite3 finalize failed: %s\0A\00"
define void @mbtiles_write_tile(i32* %0, i32 %1, i32 %2, i32 %3, i8* %4, i32 %5) {
%7 = alloca i32*
%8 = bitcast i32** %7 to i8*
%9 = call i64 @sqlite3_prepare_v2(i32* %0, i8* getelementptr inbounds ([85 x i8], [85 x i8]* @.str, i64 0, i64 0), i32 -1, i32** %7, i32* null)
%10 = load i64, i64* @SQLITE_OK
%11 = icmp eq i64 %9, %10
br i1 %11, label %17, label %12
12: ; preds = %6
%13 = load i32, i32* @stderr
%14 = call i32 (i32, i8*, ...) @fprintf(i32 %13, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
%15 = load i32, i32* @EXIT_FAILURE
%16 = call i32 @exit(i32 %15)
unreachable
17: ; preds = %6
%18 = load i32*, i32** %7
%19 = call i32 @sqlite3_bind_int(i32* %18, i32 1, i32 %1)
%20 = load i32*, i32** %7
%21 = call i32 @sqlite3_bind_int(i32* %20, i32 2, i32 %2)
%22 = load i32*, i32** %7
%23 = shl nsw i32 -1, %1
%24 = xor i32 %23, -1
%25 = sub nsw i32 %24, %3
%26 = call i32 @sqlite3_bind_int(i32* %22, i32 3, i32 %25)
%27 = load i32*, i32** %7
%28 = call i32 @sqlite3_bind_blob(i32* %27, i32 4, i8* %4, i32 %5, i32* null)
%29 = load i32*, i32** %7
%30 = call i64 @sqlite3_step(i32* %29)
%31 = load i64, i64* @SQLITE_DONE
%32 = icmp eq i64 %30, %31
br i1 %32, label %37, label %33
33: ; preds = %17
%34 = load i32, i32* @stderr
%35 = call i8* @sqlite3_errmsg(i32* %0)
%36 = call i32 (i32, i8*, ...) @fprintf(i32 %34, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i8* %35)
br label %37
37: ; preds = %33, %17
%38 = load i32*, i32** %7
%39 = call i64 @sqlite3_finalize(i32* %38)
%40 = load i64, i64* @SQLITE_OK
%41 = icmp eq i64 %39, %40
br i1 %41, label %46, label %42
42: ; preds = %37
%43 = load i32, i32* @stderr
%44 = call i8* @sqlite3_errmsg(i32* %0)
%45 = call i32 (i32, i8*, ...) @fprintf(i32 %43, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i8* %44)
br label %46
46: ; preds = %42, %37
ret void
}