#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 */
#define SQLITE_DONE 129
#define SQLITE_ROW 128
int /*<<< orphan*/ *database_prepare(int /*<<< orphan*/ *, char *);
int /*<<< orphan*/ logprintf(int, char *, ...);
scalar_t__ sqlite3_column_text(int /*<<< orphan*/ *, int /*<<< orphan*/);
int sqlite3_errmsg(int /*<<< orphan*/ *);
int /*<<< orphan*/ sqlite3_finalize(int /*<<< orphan*/ *);
int sqlite3_step(int /*<<< orphan*/ *);
int strtoul(char *, int /*<<< orphan*/ *, int);
int database_schema_version(sqlite3 *conn) {
  int status;
  char *QUERY_SCHEMA_VERSION =
      "SELECT value FROM meta WHERE key='schema_version'";
  sqlite3_stmt *query_version = database_prepare(conn, QUERY_SCHEMA_VERSION);
  if (!query_version) {
    logprintf(0, "Failed to query the database schema version, your database "
                 "file might not be a valid master database\n");
    return -1;
  }
  switch (sqlite3_step(query_version)) {
  case SQLITE_ROW:
    status = strtoul((char *)sqlite3_column_text(query_version, 0), NULL, 10);
    logprintf(1, "Database schema version is %d\n", status);
    break;
  case SQLITE_DONE:
    logprintf(0, "The database did not contain a schema_version key\n");
    status = -1;
    break;
  default:
    logprintf(0, "Failed to get schema version: %s\n", sqlite3_errmsg(conn));
    status = -1;
  }
  sqlite3_finalize(query_version);
  return status;
}


x86 assembly (clang, -O0):
<database_schema_version>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
lea    0xe7d(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1050 <database_prepare@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11bf <database_schema_version+0x4f>
xor    %edi,%edi
lea    0xe86(%rip),%rsi
mov    $0x0,%al
call   1090 <logprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1277 <database_schema_version+0x107>
mov    -0x28(%rbp),%rdi
call   1040 <sqlite3_step@plt>
mov    %eax,%ecx
mov    %ecx,-0x2c(%rbp)
sub    $0x80,%eax
je     11f0 <database_schema_version+0x80>
jmp    11dd <database_schema_version+0x6d>
mov    -0x2c(%rbp),%eax
sub    $0x81,%eax
je     122a <database_schema_version+0xba>
jmp    1246 <database_schema_version+0xd6>
mov    -0x28(%rbp),%rdi
xor    %esi,%esi
call   1060 <sqlite3_column_text@plt>
mov    %rax,%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0xa,%edx
call   1080 <strtoul@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edx
mov    $0x1,%edi
lea    0xe7a(%rip),%rsi
mov    $0x0,%al
call   1090 <logprintf@plt>
jmp    1268 <database_schema_version+0xf8>
xor    %edi,%edi
lea    0xe84(%rip),%rsi
mov    $0x0,%al
call   1090 <logprintf@plt>
movl   $0xffffffff,-0x14(%rbp)
jmp    1268 <database_schema_version+0xf8>
mov    -0x10(%rbp),%rdi
call   1030 <sqlite3_errmsg@plt>
mov    %eax,%edx
xor    %edi,%edi
lea    0xe90(%rip),%rsi
mov    $0x0,%al
call   1090 <logprintf@plt>
movl   $0xffffffff,-0x14(%rbp)
mov    -0x28(%rbp),%rdi
call   1070 <sqlite3_finalize@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<database_schema_version>:
push   %rbp
push   %rbx
push   %rax
mov    %rdi,%rbp
lea    0xe83(%rip),%rsi
call   1050 <database_prepare@plt>
test   %rax,%rax
je     11d2 <database_schema_version+0x62>
mov    %rax,%rbx
mov    %rax,%rdi
call   1040 <sqlite3_step@plt>
cmp    $0x81,%eax
je     11e9 <database_schema_version+0x79>
cmp    $0x80,%eax
jne    11fb <database_schema_version+0x8b>
mov    %rbx,%rdi
xor    %esi,%esi
call   1060 <sqlite3_column_text@plt>
mov    %rax,%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   1080 <strtoul@plt>
mov    %eax,%ebp
lea    0xed6(%rip),%rsi
mov    $0x1,%edi
mov    %eax,%edx
xor    %eax,%eax
call   1090 <logprintf@plt>
jmp    121a <database_schema_version+0xaa>
lea    0xe59(%rip),%rsi
xor    %edi,%edi
xor    %eax,%eax
call   1090 <logprintf@plt>
mov    $0xffffffff,%ebp
jmp    1222 <database_schema_version+0xb2>
lea    0xec7(%rip),%rsi
xor    %edi,%edi
xor    %eax,%eax
call   1090 <logprintf@plt>
jmp    1215 <database_schema_version+0xa5>
mov    %rbp,%rdi
call   1030 <sqlite3_errmsg@plt>
lea    0xee0(%rip),%rsi
xor    %edi,%edi
mov    %eax,%edx
xor    %eax,%eax
call   1090 <logprintf@plt>
mov    $0xffffffff,%ebp
mov    %rbx,%rdi
call   1070 <sqlite3_finalize@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [50 x i8] c"SELECT value FROM meta WHERE key='schema_version'\00"
@.str.1 = [102 x i8] c"Failed to query the database schema version, your database file might not be a valid master database\0A\00"
@.str.2 = [31 x i8] c"Database schema version is %d\0A\00"
@.str.3 = [51 x i8] c"The database did not contain a schema_version key\0A\00"
@.str.4 = [34 x i8] c"Failed to get schema version: %s\0A\00"
define i32 @database_schema_version(i32* %0) {
%2 = alloca i32
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32*
store i32* %0, i32** %3
store i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str, i64 0, i64 0), i8** %5
%7 = load i32*, i32** %3
%8 = load i8*, i8** %5
%9 = call i32* @database_prepare(i32* %7, i8* %8)
store i32* %9, i32** %6
%10 = load i32*, i32** %6
%11 = icmp ne i32* %10, null
br i1 %11, label %14, label %12
12: ; preds = %1
%13 = call i32 (i32, i8*, ...) @logprintf(i32 0, i8* getelementptr inbounds ([102 x i8], [102 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %2
br label %34
14: ; preds = %1
%15 = load i32*, i32** %6
%16 = call i32 @sqlite3_step(i32* %15)
switch i32 %16, label %26 [
i32 128, label %17
i32 129, label %24
]
17: ; preds = %14
%18 = load i32*, i32** %6
%19 = call i64 @sqlite3_column_text(i32* %18, i32 0)
%20 = inttoptr i64 %19 to i8*
%21 = call i32 @strtoul(i8* %20, i32* null, i32 10)
store i32 %21, i32* %4
%22 = load i32, i32* %4
%23 = call i32 (i32, i8*, ...) @logprintf(i32 1, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i32 %22)
br label %30
24: ; preds = %14
%25 = call i32 (i32, i8*, ...) @logprintf(i32 0, i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %4
br label %30
26: ; preds = %14
%27 = load i32*, i32** %3
%28 = call i32 @sqlite3_errmsg(i32* %27)
%29 = call i32 (i32, i8*, ...) @logprintf(i32 0, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.4, i64 0, i64 0), i32 %28)
store i32 -1, i32* %4
br label %30
30: ; preds = %26, %24, %17
%31 = load i32*, i32** %6
%32 = call i32 @sqlite3_finalize(i32* %31)
%33 = load i32, i32* %4
store i32 %33, i32* %2
br label %34
34: ; preds = %30, %12
%35 = load i32, i32* %2
ret i32 %35
}

LLVM IR (-O2):
@.str = [50 x i8] c"SELECT value FROM meta WHERE key='schema_version'\00"
@.str.1 = [102 x i8] c"Failed to query the database schema version, your database file might not be a valid master database\0A\00"
@.str.2 = [31 x i8] c"Database schema version is %d\0A\00"
@.str.3 = [51 x i8] c"The database did not contain a schema_version key\0A\00"
@.str.4 = [34 x i8] c"Failed to get schema version: %s\0A\00"
define i32 @database_schema_version(i32* %0) {
%2 = tail call i32* @database_prepare(i32* %0, i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str, i64 0, i64 0))
%3 = icmp eq i32* %2, null
br i1 %3, label %4, label %6
4: ; preds = %1
%5 = tail call i32 (i32, i8*, ...) @logprintf(i32 0, i8* getelementptr inbounds ([102 x i8], [102 x i8]* @.str.1, i64 0, i64 0))
br label %21
6: ; preds = %1
%7 = tail call i32 @sqlite3_step(i32* %2)
switch i32 %7, label %15 [
i32 128, label %8
i32 129, label %13
]
8: ; preds = %6
%9 = tail call i64 @sqlite3_column_text(i32* %2, i32 0)
%10 = inttoptr i64 %9 to i8*
%11 = tail call i32 @strtoul(i8* nocapture %10, i32* null, i32 10)
%12 = tail call i32 (i32, i8*, ...) @logprintf(i32 1, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i32 %11)
br label %18
13: ; preds = %6
%14 = tail call i32 (i32, i8*, ...) @logprintf(i32 0, i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.3, i64 0, i64 0))
br label %18
15: ; preds = %6
%16 = tail call i32 @sqlite3_errmsg(i32* %0)
%17 = tail call i32 (i32, i8*, ...) @logprintf(i32 0, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.4, i64 0, i64 0), i32 %16)
br label %18
18: ; preds = %15, %13, %8
%19 = phi i32 [ -1, %15 ], [ -1, %13 ], [ %11, %8 ]
%20 = tail call i32 @sqlite3_finalize(i32* %2)
br label %21
21: ; preds = %18, %4
%22 = phi i32 [ %19, %18 ], [ -1, %4 ]
ret i32 %22
}