#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
int SQLITE_OK;
#define SQLITE_ROW 128
int /*<<< orphan*/ SQLITE_STATIC;
int /*<<< orphan*/ finalizeStatement(int /*<<< orphan*/ *,
                                     int /*<<< orphan*/ *);
int /*<<< orphan*/ *prepareStatement(int /*<<< orphan*/ *, char *);
int sqlite3_bind_text(int /*<<< orphan*/ *, int, char const *, int,
                      int /*<<< orphan*/);
int sqlite3_step(int /*<<< orphan*/ *);
int /*<<< orphan*/ sqlite_error(int, int /*<<< orphan*/ *, char *);
int tableExists(sqlite3 *db, const char *tableName) {
  int res;
  int exists = 0;
  sqlite3_stmt *statement = NULL;
  statement = prepareStatement(
      db, "SELECT 1 FROM sqlite_master WHERE type='table' AND name=?");
  res = sqlite3_bind_text(statement, 1, tableName, -1, SQLITE_STATIC);
  if (res != SQLITE_OK)
    sqlite_error(res, db, "table existence bind of name failed.");
  res = sqlite3_step(statement);
  switch (res) {
  case SQLITE_DONE:
    exists = 0;
    break;
  case SQLITE_ROW:
    exists = 1;
    break;
  default:
    sqlite_error(res, db, "select from sqlite_master failed.");
  }
  finalizeStatement(db, statement);
  return exists;
}


x86 assembly (clang, -O0):
<tableExists>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe86(%rip),%rsi
call   1070 <prepareStatement@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    0x2e46(%rip),%rax
mov    (%rax),%r8d
mov    $0x1,%esi
mov    $0xffffffff,%ecx
call   1050 <sqlite3_bind_text@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    0x2e37(%rip),%rcx
cmp    (%rcx),%eax
je     11cc <tableExists+0x7c>
mov    -0x14(%rbp),%edi
mov    -0x8(%rbp),%rsi
lea    0xe73(%rip),%rdx
call   1030 <sqlite_error@plt>
mov    -0x20(%rbp),%rdi
call   1040 <sqlite3_step@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x24(%rbp)
sub    $0x80,%eax
je     120d <tableExists+0xbd>
jmp    11ee <tableExists+0x9e>
mov    -0x24(%rbp),%eax
sub    $0x81,%eax
jne    1219 <tableExists+0xc9>
jmp    1201 <tableExists+0xb1>
movl   $0x0,-0x18(%rbp)
jmp    122c <tableExists+0xdc>
movl   $0x1,-0x18(%rbp)
jmp    122c <tableExists+0xdc>
mov    -0x14(%rbp),%edi
mov    -0x8(%rbp),%rsi
lea    0xe38(%rip),%rdx
call   1030 <sqlite_error@plt>
mov    -0x8(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1060 <finalizeStatement@plt>
mov    -0x18(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tableExists>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%rbp
mov    %rdi,%r14
lea    0xe9f(%rip),%rsi
call   1070 <prepareStatement@plt>
mov    %rax,%rbx
mov    0x2e68(%rip),%rax
mov    (%rax),%r8d
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %rbp,%rdx
mov    $0xffffffff,%ecx
call   1050 <sqlite3_bind_text@plt>
mov    0x2e59(%rip),%rcx
cmp    (%rcx),%eax
je     11a4 <tableExists+0x54>
lea    0xea0(%rip),%rdx
mov    %eax,%edi
mov    %r14,%rsi
call   1030 <sqlite_error@plt>
mov    %rbx,%rdi
call   1040 <sqlite3_step@plt>
xor    %ebp,%ebp
cmp    $0x81,%eax
je     11d4 <tableExists+0x84>
cmp    $0x80,%eax
jne    11c3 <tableExists+0x73>
mov    $0x1,%ebp
jmp    11d4 <tableExists+0x84>
lea    0xe95(%rip),%rdx
mov    %eax,%edi
mov    %r14,%rsi
call   1030 <sqlite_error@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1060 <finalizeStatement@plt>
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [58 x i8] c"SELECT 1 FROM sqlite_master WHERE type='table' AND name=?\00"
@SQLITE_STATIC = global i32 0
@SQLITE_OK = global i32 0
@.str.1 = [37 x i8] c"table existence bind of name failed.\00"
@.str.2 = [34 x i8] c"select from sqlite_master failed.\00"
define i32 @tableExists(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
store i32 0, i32* %6
store i32* null, i32** %7
%8 = load i32*, i32** %3
%9 = call i32* @prepareStatement(i32* %8, i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str, i64 0, i64 0))
store i32* %9, i32** %7
%10 = load i32*, i32** %7
%11 = load i8*, i8** %4
%12 = load i32, i32* @SQLITE_STATIC
%13 = call i32 @sqlite3_bind_text(i32* %10, i32 1, i8* %11, i32 -1, i32 %12)
store i32 %13, i32* %5
%14 = load i32, i32* %5
%15 = load i32, i32* @SQLITE_OK
%16 = icmp ne i32 %14, %15
br i1 %16, label %17, label %21
17: ; preds = %2
%18 = load i32, i32* %5
%19 = load i32*, i32** %3
%20 = call i32 @sqlite_error(i32 %18, i32* %19, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0))
br label %21
21: ; preds = %17, %2
%22 = load i32*, i32** %7
%23 = call i32 @sqlite3_step(i32* %22)
store i32 %23, i32* %5
%24 = load i32, i32* %5
switch i32 %24, label %27 [
i32 129, label %25
i32 128, label %26
]
25: ; preds = %21
store i32 0, i32* %6
br label %31
26: ; preds = %21
store i32 1, i32* %6
br label %31
27: ; preds = %21
%28 = load i32, i32* %5
%29 = load i32*, i32** %3
%30 = call i32 @sqlite_error(i32 %28, i32* %29, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0))
br label %31
31: ; preds = %27, %26, %25
%32 = load i32*, i32** %3
%33 = load i32*, i32** %7
%34 = call i32 @finalizeStatement(i32* %32, i32* %33)
%35 = load i32, i32* %6
ret i32 %35
}

LLVM IR (-O2):
@.str = [58 x i8] c"SELECT 1 FROM sqlite_master WHERE type='table' AND name=?\00"
@SQLITE_STATIC = global i32 0
@SQLITE_OK = global i32 0
@.str.1 = [37 x i8] c"table existence bind of name failed.\00"
@.str.2 = [34 x i8] c"select from sqlite_master failed.\00"
define i32 @tableExists(i32* %0, i8* %1) {
%3 = tail call i32* @prepareStatement(i32* %0, i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str, i64 0, i64 0))
%4 = load i32, i32* @SQLITE_STATIC
%5 = tail call i32 @sqlite3_bind_text(i32* %3, i32 1, i8* %1, i32 -1, i32 %4)
%6 = load i32, i32* @SQLITE_OK
%7 = icmp eq i32 %5, %6
br i1 %7, label %10, label %8
8: ; preds = %2
%9 = tail call i32 @sqlite_error(i32 %5, i32* %0, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0))
br label %10
10: ; preds = %8, %2
%11 = tail call i32 @sqlite3_step(i32* %3)
switch i32 %11, label %13 [
i32 129, label %15
i32 128, label %12
]
12: ; preds = %10
br label %15
13: ; preds = %10
%14 = tail call i32 @sqlite_error(i32 %11, i32* %0, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0))
br label %15
15: ; preds = %10, %13, %12
%16 = phi i32 [ 0, %13 ], [ 1, %12 ], [ 0, %10 ]
%17 = tail call i32 @finalizeStatement(i32* %0, i32* %3)
ret i32 %16
}