#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*/ sql_error(int /*<<< orphan*/ *);
int sql_prepare(int /*<<< orphan*/ *, char const *, int /*<<< orphan*/ **, int);
int sql_step(int /*<<< orphan*/ *, int);
scalar_t__ sqlite3_column_count(int /*<<< orphan*/ *);
int sqlite3_column_int(int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ sqlite3_finalize(int /*<<< orphan*/ *);
int query_int(sqlite3 *db, const char *sql) {
  sqlite3_stmt *stmt;
  int rc;
  int ret;
  rc = sql_prepare(db, sql, &stmt, -1);
  if (rc != SQLITE_OK || stmt == 0 || sqlite3_column_count(stmt) == 0) {
    sql_error(db);
    ret = 0;
  } else {
    rc = sql_step(stmt, -1);
    if (rc == SQLITE_ROW) {
      ret = sqlite3_column_int(stmt, 0);
    } else {
      ret = 0;
    }
  }
  sqlite3_finalize(stmt);
  return ret;
}


x86 assembly (clang, -O0):
<query_int>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
lea    -0x18(%rbp),%rdx
mov    $0xffffffff,%ecx
call   1050 <sql_prepare@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    0x2e55(%rip),%rcx
cmp    (%rcx),%eax
jne    11b9 <query_int+0x59>
cmpq   $0x0,-0x18(%rbp)
je     11b9 <query_int+0x59>
mov    -0x18(%rbp),%rdi
call   1060 <sqlite3_column_count@plt>
cmp    $0x0,%rax
jne    11ce <query_int+0x6e>
mov    -0x8(%rbp),%rdi
call   1030 <sql_error@plt>
movl   $0x0,-0x20(%rbp)
jmp    1210 <query_int+0xb0>
mov    -0x18(%rbp),%rdi
mov    $0xffffffff,%esi
call   1070 <sql_step@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    0x2def(%rip),%rcx
cmp    (%rcx),%eax
jne    1204 <query_int+0xa4>
mov    -0x18(%rbp),%rdi
xor    %esi,%esi
call   1040 <sqlite3_column_int@plt>
mov    %eax,-0x20(%rbp)
jmp    120b <query_int+0xab>
movl   $0x0,-0x20(%rbp)
jmp    1210 <query_int+0xb0>
mov    -0x18(%rbp),%rdi
call   1080 <sqlite3_finalize@plt>
mov    -0x20(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<query_int>:
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
lea    0x8(%rsp),%rdx
mov    $0xffffffff,%ecx
call   1050 <sql_prepare@plt>
mov    0x2e6a(%rip),%rcx
cmp    (%rcx),%eax
jne    11c2 <query_int+0x62>
mov    0x8(%rsp),%rdi
test   %rdi,%rdi
je     11c2 <query_int+0x62>
call   1060 <sqlite3_column_count@plt>
test   %rax,%rax
je     11c2 <query_int+0x62>
mov    0x8(%rsp),%rdi
mov    $0xffffffff,%esi
call   1070 <sql_step@plt>
mov    0x2e2c(%rip),%rcx
xor    %ebx,%ebx
cmp    (%rcx),%eax
jne    11cc <query_int+0x6c>
mov    0x8(%rsp),%rdi
xor    %esi,%esi
call   1040 <sqlite3_column_int@plt>
mov    %eax,%ebx
jmp    11cc <query_int+0x6c>
mov    %rbx,%rdi
call   1030 <sql_error@plt>
xor    %ebx,%ebx
mov    0x8(%rsp),%rdi
call   1080 <sqlite3_finalize@plt>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@SQLITE_OK = global i32 0
@SQLITE_ROW = global i32 0
define i32 @query_int(i32* %0, i8* %1) {
%3 = alloca i32*
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
store i32* %0, i32** %3
store i8* %1, i8** %4
%8 = load i32*, i32** %3
%9 = load i8*, i8** %4
%10 = call i32 @sql_prepare(i32* %8, i8* %9, i32** %5, i32 -1)
store i32 %10, i32* %6
%11 = load i32, i32* %6
%12 = load i32, i32* @SQLITE_OK
%13 = icmp ne i32 %11, %12
br i1 %13, label %21, label %14
14: ; preds = %2
%15 = load i32*, i32** %5
%16 = icmp eq i32* %15, null
br i1 %16, label %21, label %17
17: ; preds = %14
%18 = load i32*, i32** %5
%19 = call i64 @sqlite3_column_count(i32* %18)
%20 = icmp eq i64 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %17, %14, %2
%22 = load i32*, i32** %3
%23 = call i32 @sql_error(i32* %22)
store i32 0, i32* %7
br label %35
24: ; preds = %17
%25 = load i32*, i32** %5
%26 = call i32 @sql_step(i32* %25, i32 -1)
store i32 %26, i32* %6
%27 = load i32, i32* %6
%28 = load i32, i32* @SQLITE_ROW
%29 = icmp eq i32 %27, %28
br i1 %29, label %30, label %33
30: ; preds = %24
%31 = load i32*, i32** %5
%32 = call i32 @sqlite3_column_int(i32* %31, i32 0)
store i32 %32, i32* %7
br label %34
33: ; preds = %24
store i32 0, i32* %7
br label %34
34: ; preds = %33, %30
br label %35
35: ; preds = %34, %21
%36 = load i32*, i32** %5
%37 = call i32 @sqlite3_finalize(i32* %36)
%38 = load i32, i32* %7
ret i32 %38
}

LLVM IR (-O2):
@SQLITE_OK = global i32 0
@SQLITE_ROW = global i32 0
define i32 @query_int(i32* %0, i8* %1) {
%3 = alloca i32*
%4 = bitcast i32** %3 to i8*
%5 = call i32 @sql_prepare(i32* %0, i8* %1, i32** %3, i32 -1)
%6 = load i32, i32* @SQLITE_OK
%7 = icmp ne i32 %5, %6
%8 = load i32*, i32** %3
%9 = icmp eq i32* %8, null
%10 = select i1 %7, i1 true, i1 %9
br i1 %10, label %14, label %11
11: ; preds = %2
%12 = call i64 @sqlite3_column_count(i32* %8)
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %11, %2
%15 = call i32 @sql_error(i32* %0)
br label %24
16: ; preds = %11
%17 = load i32*, i32** %3
%18 = call i32 @sql_step(i32* %17, i32 -1)
%19 = load i32, i32* @SQLITE_ROW
%20 = icmp eq i32 %18, %19
br i1 %20, label %21, label %24
21: ; preds = %16
%22 = load i32*, i32** %3
%23 = call i32 @sqlite3_column_int(i32* %22, i32 0)
br label %24
24: ; preds = %16, %21, %14
%25 = phi i32 [ 0, %14 ], [ %23, %21 ], [ 0, %16 ]
%26 = load i32*, i32** %3
%27 = call i32 @sqlite3_finalize(i32* %26)
ret i32 %25
}