#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*/ DPRINTF(int, int, char *, ...);
int SQLITE_DONE;
int SQLITE_OK;
int SQLITE_ROW;
int /*<<< orphan*/ log_fatal_null(int, char *, int);
int /*<<< orphan*/ sqlite3_column_text(int /*<<< orphan*/ *,
                                       int /*<<< orphan*/);
int /*<<< orphan*/ sqlite3_errmsg(int /*<<< orphan*/ *);
int sqlite3_exec(int /*<<< orphan*/ *, char *, int /*<<< orphan*/ *,
                 int /*<<< orphan*/ *, char **);
int /*<<< orphan*/ sqlite3_finalize(int /*<<< orphan*/ *);
int /*<<< orphan*/ sqlite3_free(char *);
char *sqlite3_mprintf(char const *, ...);
int sqlite3_prepare_v2(int /*<<< orphan*/ *, char *, int, int /*<<< orphan*/ **,
                       int /*<<< orphan*/ *);
int sqlite3_step(int /*<<< orphan*/ *);
int db_table_upgrade(sqlite3 *hdl, const char *name,
                     const char *newtablequery) {
  sqlite3_stmt *stmt;
  char *query;
  char *errmsg;
  int ret;
  DPRINTF(1, 2, "Upgrading %s table...\n", name);
  do {
    if ((query = sqlite3_mprintf(newtablequery)) == NULL)
      log_fatal_null(2, "query = sqlite3_mprintf(newtablequery)", 186);
  } while (0);
  ret = sqlite3_exec(hdl, query, NULL, NULL, &errmsg);
  if (ret != SQLITE_OK)
    goto error;
  sqlite3_free(query);
  do {
    if ((query = sqlite3_mprintf(
             "SELECT group_concat(name) FROM pragma_table_info('new_%s');",
             name)) == NULL)
      log_fatal_null(2,
                     "query = sqlite3_mprintf(\"SELECT group_concat(name) FROM "
                     "pragma_table_info('new_%s');\", name)",
                     196);
  } while (0);
  ret = sqlite3_prepare_v2(hdl, query, -1, &stmt, NULL);
  if (ret != SQLITE_OK) {
    errmsg = sqlite3_mprintf("%s", sqlite3_errmsg(hdl));
    goto error;
  }
  ret = sqlite3_step(stmt);
  if (ret != SQLITE_ROW) {
    if (ret == SQLITE_DONE)
      errmsg = sqlite3_mprintf(
          "Getting col names from pragma_table_info returned nothing");
    else
      errmsg = sqlite3_mprintf("%s", sqlite3_errmsg(hdl));
    sqlite3_finalize(stmt);
    goto error;
  }
  sqlite3_free(query);
  do {
    if ((query = sqlite3_mprintf("INSERT INTO new_%s SELECT %s FROM %s;", name,
                                 sqlite3_column_text(stmt, 0), name)) == NULL)
      log_fatal_null(2,
                     "query = sqlite3_mprintf(\"INSERT INTO new_%s SELECT %s "
                     "FROM %s;\", name, sqlite3_column_text(stmt, 0), name)",
                     219);
  } while (0);
  sqlite3_finalize(stmt);
  ret = sqlite3_exec(hdl, query, NULL, NULL, &errmsg);
  if (ret != SQLITE_OK)
    goto error;
  sqlite3_free(query);
  do {
    if ((query = sqlite3_mprintf("DROP TABLE %s;", name)) == NULL)
      log_fatal_null(2, "query = sqlite3_mprintf(\"DROP TABLE %s;\", name)",
                     230);
  } while (0);
  ret = sqlite3_exec(hdl, query, NULL, NULL, &errmsg);
  if (ret != SQLITE_OK)
    goto error;
  sqlite3_free(query);
  do {
    if ((query = sqlite3_mprintf("ALTER TABLE new_%s RENAME TO %s;", name,
                                 name)) == NULL)
      log_fatal_null(2,
                     "query = sqlite3_mprintf(\"ALTER TABLE new_%s RENAME TO "
                     "%s;\", name, name)",
                     239);
  } while (0);
  ret = sqlite3_exec(hdl, query, NULL, NULL, &errmsg);
  if (ret != SQLITE_OK)
    goto error;
  sqlite3_free(query);
  DPRINTF(1, 2, "Upgrade of %s table complete!\n", name);
  return 0;
error:
  DPRINTF(1, 2, "DB error %d running query '%s': %s\n", ret, query, errmsg);
  sqlite3_free(query);
  sqlite3_free(errmsg);
  return -1;
}


x86 assembly (clang, -O0):
<db_table_upgrade>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x18(%rbp),%rcx
mov    $0x1,%edi
mov    $0x2,%esi
lea    0xe37(%rip),%rdx
mov    $0x0,%al
call   10c0 <DPRINTF@plt>
mov    -0x20(%rbp),%rdi
mov    $0x0,%al
call   10b0 <sqlite3_mprintf@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jne    11ff <db_table_upgrade+0x5f>
mov    $0x2,%edi
lea    0xe22(%rip),%rsi
mov    $0xba,%edx
call   1040 <log_fatal_null@plt>
jmp    1204 <db_table_upgrade+0x64>
mov    -0x10(%rbp),%rdi
mov    -0x30(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x38(%rbp),%r8
mov    %rcx,%rdx
call   1030 <sqlite3_exec@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
mov    0x2dbf(%rip),%rcx
cmp    (%rcx),%eax
je     1236 <db_table_upgrade+0x96>
jmp    14f7 <db_table_upgrade+0x357>
mov    -0x30(%rbp),%rdi
call   10a0 <sqlite3_free@plt>
mov    -0x18(%rbp),%rsi
lea    0xdf4(%rip),%rdi
mov    $0x0,%al
call   10b0 <sqlite3_mprintf@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jne    1275 <db_table_upgrade+0xd5>
mov    $0x2,%edi
lea    0xe0f(%rip),%rsi
mov    $0xc4,%edx
call   1040 <log_fatal_null@plt>
jmp    127a <db_table_upgrade+0xda>
mov    -0x10(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    $0xffffffff,%edx
lea    -0x28(%rbp),%rcx
xor    %eax,%eax
mov    %eax,%r8d
call   1090 <sqlite3_prepare_v2@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
mov    0x2d46(%rip),%rcx
cmp    (%rcx),%eax
je     12cc <db_table_upgrade+0x12c>
mov    -0x10(%rbp),%rdi
call   1050 <sqlite3_errmsg@plt>
mov    %eax,%esi
lea    0xe1b(%rip),%rdi
mov    $0x0,%al
call   10b0 <sqlite3_mprintf@plt>
mov    %rax,-0x38(%rbp)
jmp    14f7 <db_table_upgrade+0x357>
mov    -0x28(%rbp),%rdi
call   1060 <sqlite3_step@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
mov    0x2cee(%rip),%rcx
cmp    (%rcx),%eax
je     133e <db_table_upgrade+0x19e>
mov    -0x3c(%rbp),%eax
mov    0x2ce4(%rip),%rcx
cmp    (%rcx),%eax
jne    1313 <db_table_upgrade+0x173>
lea    0xdd7(%rip),%rdi
mov    $0x0,%al
call   10b0 <sqlite3_mprintf@plt>
mov    %rax,-0x38(%rbp)
jmp    1330 <db_table_upgrade+0x190>
mov    -0x10(%rbp),%rdi
call   1050 <sqlite3_errmsg@plt>
mov    %eax,%esi
lea    0xdb2(%rip),%rdi
mov    $0x0,%al
call   10b0 <sqlite3_mprintf@plt>
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%rdi
call   1080 <sqlite3_finalize@plt>
jmp    14f7 <db_table_upgrade+0x357>
mov    -0x30(%rbp),%rdi
call   10a0 <sqlite3_free@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x28(%rbp),%rdi
xor    %esi,%esi
call   1070 <sqlite3_column_text@plt>
mov    -0x48(%rbp),%rsi
mov    %eax,%edx
mov    -0x18(%rbp),%rcx
lea    0xda9(%rip),%rdi
mov    $0x0,%al
call   10b0 <sqlite3_mprintf@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jne    1396 <db_table_upgrade+0x1f6>
mov    $0x2,%edi
lea    0xdae(%rip),%rsi
mov    $0xdb,%edx
call   1040 <log_fatal_null@plt>
jmp    139b <db_table_upgrade+0x1fb>
mov    -0x28(%rbp),%rdi
call   1080 <sqlite3_finalize@plt>
mov    -0x10(%rbp),%rdi
mov    -0x30(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x38(%rbp),%r8
mov    %rcx,%rdx
call   1030 <sqlite3_exec@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
mov    0x2c1f(%rip),%rcx
cmp    (%rcx),%eax
je     13d6 <db_table_upgrade+0x236>
jmp    14f7 <db_table_upgrade+0x357>
mov    -0x30(%rbp),%rdi
call   10a0 <sqlite3_free@plt>
mov    -0x18(%rbp),%rsi
lea    0xdbb(%rip),%rdi
mov    $0x0,%al
call   10b0 <sqlite3_mprintf@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jne    1415 <db_table_upgrade+0x275>
mov    $0x2,%edi
lea    0xda9(%rip),%rsi
mov    $0xe6,%edx
call   1040 <log_fatal_null@plt>
jmp    141a <db_table_upgrade+0x27a>
mov    -0x10(%rbp),%rdi
mov    -0x30(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x38(%rbp),%r8
mov    %rcx,%rdx
call   1030 <sqlite3_exec@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
mov    0x2ba9(%rip),%rcx
cmp    (%rcx),%eax
je     144c <db_table_upgrade+0x2ac>
jmp    14f7 <db_table_upgrade+0x357>
mov    -0x30(%rbp),%rdi
call   10a0 <sqlite3_free@plt>
mov    -0x18(%rbp),%rsi
mov    -0x18(%rbp),%rdx
lea    0xd80(%rip),%rdi
mov    $0x0,%al
call   10b0 <sqlite3_mprintf@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jne    148f <db_table_upgrade+0x2ef>
mov    $0x2,%edi
lea    0xd80(%rip),%rsi
mov    $0xef,%edx
call   1040 <log_fatal_null@plt>
jmp    1494 <db_table_upgrade+0x2f4>
mov    -0x10(%rbp),%rdi
mov    -0x30(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x38(%rbp),%r8
mov    %rcx,%rdx
call   1030 <sqlite3_exec@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
mov    0x2b2f(%rip),%rcx
cmp    (%rcx),%eax
je     14c6 <db_table_upgrade+0x326>
jmp    14f7 <db_table_upgrade+0x357>
mov    -0x30(%rbp),%rdi
call   10a0 <sqlite3_free@plt>
mov    -0x18(%rbp),%rcx
mov    $0x1,%edi
mov    $0x2,%esi
lea    0xd69(%rip),%rdx
mov    $0x0,%al
call   10c0 <DPRINTF@plt>
movl   $0x0,-0x4(%rbp)
jmp    1533 <db_table_upgrade+0x393>
mov    -0x3c(%rbp),%ecx
mov    -0x30(%rbp),%r8
mov    -0x38(%rbp),%r9
mov    $0x1,%edi
mov    $0x2,%esi
lea    0xd59(%rip),%rdx
mov    $0x0,%al
call   10c0 <DPRINTF@plt>
mov    -0x30(%rbp),%rdi
call   10a0 <sqlite3_free@plt>
mov    -0x38(%rbp),%rdi
call   10a0 <sqlite3_free@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<db_table_upgrade>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %rdi,%r15
lea    0xe42(%rip),%rdx
mov    $0x1,%edi
mov    $0x2,%esi
mov    %r14,%rcx
xor    %eax,%eax
call   10c0 <DPRINTF@plt>
mov    %rbx,%rdi
xor    %eax,%eax
call   10b0 <sqlite3_mprintf@plt>
mov    %rax,%r12
test   %rax,%rax
jne    11fa <db_table_upgrade+0x5a>
lea    0xe2c(%rip),%rsi
mov    $0x2,%edi
mov    $0xba,%edx
call   1040 <log_fatal_null@plt>
lea    0x8(%rsp),%r8
mov    %r15,%rdi
mov    %r12,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
call   1030 <sqlite3_exec@plt>
mov    0x2dd3(%rip),%r13
cmp    0x0(%r13),%eax
jne    13d2 <db_table_upgrade+0x232>
mov    %r12,%rdi
call   10a0 <sqlite3_free@plt>
lea    0xe10(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   10b0 <sqlite3_mprintf@plt>
mov    %rax,%r12
test   %rax,%rax
jne    1256 <db_table_upgrade+0xb6>
lea    0xe33(%rip),%rsi
mov    $0x2,%edi
mov    $0xc4,%edx
call   1040 <log_fatal_null@plt>
lea    0x10(%rsp),%rcx
mov    %r15,%rdi
mov    %r12,%rsi
mov    $0xffffffff,%edx
xor    %r8d,%r8d
call   1090 <sqlite3_prepare_v2@plt>
cmp    0x0(%r13),%eax
jne    13d6 <db_table_upgrade+0x236>
mov    0x10(%rsp),%rdi
call   1060 <sqlite3_step@plt>
mov    %eax,%ebp
mov    0x2d45(%rip),%rax
cmp    (%rax),%ebp
jne    13f7 <db_table_upgrade+0x257>
mov    %r12,%rdi
call   10a0 <sqlite3_free@plt>
mov    0x10(%rsp),%rdi
xor    %esi,%esi
call   1070 <sqlite3_column_text@plt>
lea    0xe66(%rip),%rdi
mov    %r14,%rsi
mov    %eax,%edx
mov    %r14,%rcx
xor    %eax,%eax
call   10b0 <sqlite3_mprintf@plt>
mov    %rax,%r12
test   %rax,%rax
jne    12db <db_table_upgrade+0x13b>
lea    0xe6e(%rip),%rsi
mov    $0x2,%edi
mov    $0xdb,%edx
call   1040 <log_fatal_null@plt>
mov    0x10(%rsp),%rdi
call   1080 <sqlite3_finalize@plt>
lea    0x8(%rsp),%r8
mov    %r15,%rdi
mov    %r12,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
call   1030 <sqlite3_exec@plt>
cmp    0x0(%r13),%eax
jne    13d2 <db_table_upgrade+0x232>
mov    %r12,%rdi
call   10a0 <sqlite3_free@plt>
lea    0xe93(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   10b0 <sqlite3_mprintf@plt>
mov    %rax,%r12
test   %rax,%rax
jne    133a <db_table_upgrade+0x19a>
lea    0xe89(%rip),%rsi
mov    $0x2,%edi
mov    $0xe6,%edx
call   1040 <log_fatal_null@plt>
lea    0x8(%rsp),%r8
mov    %r15,%rdi
mov    %r12,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
call   1030 <sqlite3_exec@plt>
cmp    0x0(%r13),%eax
jne    13d2 <db_table_upgrade+0x232>
mov    %r12,%rdi
call   10a0 <sqlite3_free@plt>
lea    0xe81(%rip),%rdi
mov    %r14,%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   10b0 <sqlite3_mprintf@plt>
mov    %rax,%r12
test   %rax,%rax
jne    138e <db_table_upgrade+0x1ee>
lea    0xe86(%rip),%rsi
mov    $0x2,%edi
mov    $0xef,%edx
call   1040 <log_fatal_null@plt>
xor    %ebx,%ebx
lea    0x8(%rsp),%r8
mov    %r15,%rdi
mov    %r12,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
call   1030 <sqlite3_exec@plt>
cmp    0x0(%r13),%eax
jne    13d2 <db_table_upgrade+0x232>
mov    %r12,%rdi
call   10a0 <sqlite3_free@plt>
lea    0xe94(%rip),%rdx
mov    $0x1,%edi
mov    $0x2,%esi
mov    %r14,%rcx
xor    %eax,%eax
call   10c0 <DPRINTF@plt>
jmp    1472 <db_table_upgrade+0x2d2>
mov    %eax,%ebp
jmp    1439 <db_table_upgrade+0x299>
mov    %eax,%ebp
mov    %r15,%rdi
call   1050 <sqlite3_errmsg@plt>
lea    0xcf0(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   10b0 <sqlite3_mprintf@plt>
mov    %rax,0x8(%rsp)
jmp    1439 <db_table_upgrade+0x299>
mov    0x2bda(%rip),%rax
cmp    (%rax),%ebp
jne    1412 <db_table_upgrade+0x272>
lea    0xcd1(%rip),%rdi
xor    %eax,%eax
call   10b0 <sqlite3_mprintf@plt>
jmp    142a <db_table_upgrade+0x28a>
mov    %r15,%rdi
call   1050 <sqlite3_errmsg@plt>
lea    0xcb6(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   10b0 <sqlite3_mprintf@plt>
mov    %rax,0x8(%rsp)
mov    0x10(%rsp),%rdi
call   1080 <sqlite3_finalize@plt>
mov    0x8(%rsp),%r9
lea    0xe27(%rip),%rdx
mov    $0x1,%edi
mov    $0x2,%esi
mov    %ebp,%ecx
mov    %r12,%r8
xor    %eax,%eax
call   10c0 <DPRINTF@plt>
mov    %r12,%rdi
call   10a0 <sqlite3_free@plt>
mov    0x8(%rsp),%rdi
call   10a0 <sqlite3_free@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [23 x i8] c"Upgrading %s table...\0A\00"
@.str.1 = [39 x i8] c"query = sqlite3_mprintf(newtablequery)\00"
@SQLITE_OK = global i32 0
@.str.2 = [60 x i8] c"SELECT group_concat(name) FROM pragma_table_info('new_%s');\00"
@.str.3 = [93 x i8] c"query = sqlite3_mprintf(\22SELECT group_concat(name) FROM pragma_table_info('new_%s');\22, name)\00"
@.str.4 = [3 x i8] c"%s\00"
@SQLITE_ROW = global i32 0
@SQLITE_DONE = global i32 0
@.str.5 = [58 x i8] c"Getting col names from pragma_table_info returned nothing\00"
@.str.6 = [38 x i8] c"INSERT INTO new_%s SELECT %s FROM %s;\00"
@.str.7 = [107 x i8] c"query = sqlite3_mprintf(\22INSERT INTO new_%s SELECT %s FROM %s;\22, name, sqlite3_column_text(stmt, 0), name)\00"
@.str.8 = [15 x i8] c"DROP TABLE %s;\00"
@.str.9 = [48 x i8] c"query = sqlite3_mprintf(\22DROP TABLE %s;\22, name)\00"
@.str.10 = [33 x i8] c"ALTER TABLE new_%s RENAME TO %s;\00"
@.str.11 = [72 x i8] c"query = sqlite3_mprintf(\22ALTER TABLE new_%s RENAME TO %s;\22, name, name)\00"
@.str.12 = [31 x i8] c"Upgrade of %s table complete!\0A\00"
@.str.13 = [36 x i8] c"DB error %d running query '%s': %s\0A\00"
define i32 @db_table_upgrade(i32* %0, i8* %1, i8* %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32*
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i32
store i32* %0, i32** %5
store i8* %1, i8** %6
store i8* %2, i8** %7
%12 = load i8*, i8** %6
%13 = call i32 (i32, i32, i8*, ...) @DPRINTF(i32 1, i32 2, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %12)
br label %14
14: ; preds = %3
%15 = load i8*, i8** %7
%16 = call i8* (i8*, ...) @sqlite3_mprintf(i8* %15)
store i8* %16, i8** %9
%17 = icmp eq i8* %16, null
br i1 %17, label %18, label %20
18: ; preds = %14
%19 = call i32 @log_fatal_null(i32 2, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i32 186)
br label %20
20: ; preds = %18, %14
br label %21
21: ; preds = %20
%22 = load i32*, i32** %5
%23 = load i8*, i8** %9
%24 = call i32 @sqlite3_exec(i32* %22, i8* %23, i32* null, i32* null, i8** %10)
store i32 %24, i32* %11
%25 = load i32, i32* %11
%26 = load i32, i32* @SQLITE_OK
%27 = icmp ne i32 %25, %26
br i1 %27, label %28, label %29
28: ; preds = %21
br label %134
29: ; preds = %21
%30 = load i8*, i8** %9
%31 = call i32 @sqlite3_free(i8* %30)
br label %32
32: ; preds = %29
%33 = load i8*, i8** %6
%34 = call i8* (i8*, ...) @sqlite3_mprintf(i8* getelementptr inbounds ([60 x i8], [60 x i8]* @.str.2, i64 0, i64 0), i8* %33)
store i8* %34, i8** %9
%35 = icmp eq i8* %34, null
br i1 %35, label %36, label %38
36: ; preds = %32
%37 = call i32 @log_fatal_null(i32 2, i8* getelementptr inbounds ([93 x i8], [93 x i8]* @.str.3, i64 0, i64 0), i32 196)
br label %38
38: ; preds = %36, %32
br label %39
39: ; preds = %38
%40 = load i32*, i32** %5
%41 = load i8*, i8** %9
%42 = call i32 @sqlite3_prepare_v2(i32* %40, i8* %41, i32 -1, i32** %8, i32* null)
store i32 %42, i32* %11
%43 = load i32, i32* %11
%44 = load i32, i32* @SQLITE_OK
%45 = icmp ne i32 %43, %44
br i1 %45, label %46, label %50
46: ; preds = %39
%47 = load i32*, i32** %5
%48 = call i32 @sqlite3_errmsg(i32* %47)
%49 = call i8* (i8*, ...) @sqlite3_mprintf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32 %48)
store i8* %49, i8** %10
br label %134
50: ; preds = %39
%51 = load i32*, i32** %8
%52 = call i32 @sqlite3_step(i32* %51)
store i32 %52, i32* %11
%53 = load i32, i32* %11
%54 = load i32, i32* @SQLITE_ROW
%55 = icmp ne i32 %53, %54
br i1 %55, label %56, label %69
56: ; preds = %50
%57 = load i32, i32* %11
%58 = load i32, i32* @SQLITE_DONE
%59 = icmp eq i32 %57, %58
br i1 %59, label %60, label %62
60: ; preds = %56
%61 = call i8* (i8*, ...) @sqlite3_mprintf(i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.5, i64 0, i64 0))
store i8* %61, i8** %10
br label %66
62: ; preds = %56
%63 = load i32*, i32** %5
%64 = call i32 @sqlite3_errmsg(i32* %63)
%65 = call i8* (i8*, ...) @sqlite3_mprintf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32 %64)
store i8* %65, i8** %10
br label %66
66: ; preds = %62, %60
%67 = load i32*, i32** %8
%68 = call i32 @sqlite3_finalize(i32* %67)
br label %134
69: ; preds = %50
%70 = load i8*, i8** %9
%71 = call i32 @sqlite3_free(i8* %70)
br label %72
72: ; preds = %69
%73 = load i8*, i8** %6
%74 = load i32*, i32** %8
%75 = call i32 @sqlite3_column_text(i32* %74, i32 0)
%76 = load i8*, i8** %6
%77 = call i8* (i8*, ...) @sqlite3_mprintf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.6, i64 0, i64 0), i8* %73, i32 %75, i8* %76)
store i8* %77, i8** %9
%78 = icmp eq i8* %77, null
br i1 %78, label %79, label %81
79: ; preds = %72
%80 = call i32 @log_fatal_null(i32 2, i8* getelementptr inbounds ([107 x i8], [107 x i8]* @.str.7, i64 0, i64 0), i32 219)
br label %81
81: ; preds = %79, %72
br label %82
82: ; preds = %81
%83 = load i32*, i32** %8
%84 = call i32 @sqlite3_finalize(i32* %83)
%85 = load i32*, i32** %5
%86 = load i8*, i8** %9
%87 = call i32 @sqlite3_exec(i32* %85, i8* %86, i32* null, i32* null, i8** %10)
store i32 %87, i32* %11
%88 = load i32, i32* %11
%89 = load i32, i32* @SQLITE_OK
%90 = icmp ne i32 %88, %89
br i1 %90, label %91, label %92
91: ; preds = %82
br label %134
92: ; preds = %82
%93 = load i8*, i8** %9
%94 = call i32 @sqlite3_free(i8* %93)
br label %95
95: ; preds = %92
%96 = load i8*, i8** %6
%97 = call i8* (i8*, ...) @sqlite3_mprintf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.8, i64 0, i64 0), i8* %96)
store i8* %97, i8** %9
%98 = icmp eq i8* %97, null
br i1 %98, label %99, label %101
99: ; preds = %95
%100 = call i32 @log_fatal_null(i32 2, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.9, i64 0, i64 0), i32 230)
br label %101
101: ; preds = %99, %95
br label %102
102: ; preds = %101
%103 = load i32*, i32** %5
%104 = load i8*, i8** %9
%105 = call i32 @sqlite3_exec(i32* %103, i8* %104, i32* null, i32* null, i8** %10)
store i32 %105, i32* %11
%106 = load i32, i32* %11
%107 = load i32, i32* @SQLITE_OK
%108 = icmp ne i32 %106, %107
br i1 %108, label %109, label %110
109: ; preds = %102
br label %134
110: ; preds = %102
%111 = load i8*, i8** %9
%112 = call i32 @sqlite3_free(i8* %111)
br label %113
113: ; preds = %110
%114 = load i8*, i8** %6
%115 = load i8*, i8** %6
%116 = call i8* (i8*, ...) @sqlite3_mprintf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.10, i64 0, i64 0), i8* %114, i8* %115)
store i8* %116, i8** %9
%117 = icmp eq i8* %116, null
br i1 %117, label %118, label %120
118: ; preds = %113
%119 = call i32 @log_fatal_null(i32 2, i8* getelementptr inbounds ([72 x i8], [72 x i8]* @.str.11, i64 0, i64 0), i32 239)
br label %120
120: ; preds = %118, %113
br label %121
121: ; preds = %120
%122 = load i32*, i32** %5
%123 = load i8*, i8** %9
%124 = call i32 @sqlite3_exec(i32* %122, i8* %123, i32* null, i32* null, i8** %10)
store i32 %124, i32* %11
%125 = load i32, i32* %11
%126 = load i32, i32* @SQLITE_OK
%127 = icmp ne i32 %125, %126
br i1 %127, label %128, label %129
128: ; preds = %121
br label %134
129: ; preds = %121
%130 = load i8*, i8** %9
%131 = call i32 @sqlite3_free(i8* %130)
%132 = load i8*, i8** %6
%133 = call i32 (i32, i32, i8*, ...) @DPRINTF(i32 1, i32 2, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.12, i64 0, i64 0), i8* %132)
store i32 0, i32* %4
br label %143
134: ; preds = %128, %109, %91, %66, %46, %28
%135 = load i32, i32* %11
%136 = load i8*, i8** %9
%137 = load i8*, i8** %10
%138 = call i32 (i32, i32, i8*, ...) @DPRINTF(i32 1, i32 2, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.13, i64 0, i64 0), i32 %135, i8* %136, i8* %137)
%139 = load i8*, i8** %9
%140 = call i32 @sqlite3_free(i8* %139)
%141 = load i8*, i8** %10
%142 = call i32 @sqlite3_free(i8* %141)
store i32 -1, i32* %4
br label %143
143: ; preds = %134, %129
%144 = load i32, i32* %4
ret i32 %144
}

LLVM IR (-O2):
@.str = [23 x i8] c"Upgrading %s table...\0A\00"
@.str.1 = [39 x i8] c"query = sqlite3_mprintf(newtablequery)\00"
@SQLITE_OK = global i32 0
@.str.2 = [60 x i8] c"SELECT group_concat(name) FROM pragma_table_info('new_%s');\00"
@.str.3 = [93 x i8] c"query = sqlite3_mprintf(\22SELECT group_concat(name) FROM pragma_table_info('new_%s');\22, name)\00"
@.str.4 = [3 x i8] c"%s\00"
@SQLITE_ROW = global i32 0
@SQLITE_DONE = global i32 0
@.str.5 = [58 x i8] c"Getting col names from pragma_table_info returned nothing\00"
@.str.6 = [38 x i8] c"INSERT INTO new_%s SELECT %s FROM %s;\00"
@.str.7 = [107 x i8] c"query = sqlite3_mprintf(\22INSERT INTO new_%s SELECT %s FROM %s;\22, name, sqlite3_column_text(stmt, 0), name)\00"
@.str.8 = [15 x i8] c"DROP TABLE %s;\00"
@.str.9 = [48 x i8] c"query = sqlite3_mprintf(\22DROP TABLE %s;\22, name)\00"
@.str.10 = [33 x i8] c"ALTER TABLE new_%s RENAME TO %s;\00"
@.str.11 = [72 x i8] c"query = sqlite3_mprintf(\22ALTER TABLE new_%s RENAME TO %s;\22, name, name)\00"
@.str.12 = [31 x i8] c"Upgrade of %s table complete!\0A\00"
@.str.13 = [36 x i8] c"DB error %d running query '%s': %s\0A\00"
define i32 @db_table_upgrade(i32* %0, i8* %1, i8* %2) {
%4 = alloca i32*
%5 = alloca i8*
%6 = bitcast i32** %4 to i8*
%7 = bitcast i8** %5 to i8*
%8 = tail call i32 (i32, i32, i8*, ...) @DPRINTF(i32 1, i32 2, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %1)
%9 = tail call i8* (i8*, ...) @sqlite3_mprintf(i8* %2)
%10 = icmp eq i8* %9, null
br i1 %10, label %11, label %13
11: ; preds = %3
%12 = tail call i32 @log_fatal_null(i32 2, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i32 186)
br label %13
13: ; preds = %11, %3
%14 = call i32 @sqlite3_exec(i32* %0, i8* %9, i32* null, i32* null, i8** %5)
%15 = load i32, i32* @SQLITE_OK
%16 = icmp eq i32 %14, %15
br i1 %16, label %17, label %84
17: ; preds = %13
%18 = call i32 @sqlite3_free(i8* %9)
%19 = call i8* (i8*, ...) @sqlite3_mprintf(i8* getelementptr inbounds ([60 x i8], [60 x i8]* @.str.2, i64 0, i64 0), i8* %1)
%20 = icmp eq i8* %19, null
br i1 %20, label %21, label %23
21: ; preds = %17
%22 = call i32 @log_fatal_null(i32 2, i8* getelementptr inbounds ([93 x i8], [93 x i8]* @.str.3, i64 0, i64 0), i32 196)
br label %23
23: ; preds = %21, %17
%24 = call i32 @sqlite3_prepare_v2(i32* %0, i8* %19, i32 -1, i32** %4, i32* null)
%25 = load i32, i32* @SQLITE_OK
%26 = icmp eq i32 %24, %25
br i1 %26, label %30, label %27
27: ; preds = %23
%28 = call i32 @sqlite3_errmsg(i32* %0)
%29 = call i8* (i8*, ...) @sqlite3_mprintf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32 %28)
store i8* %29, i8** %5
br label %84
30: ; preds = %23
%31 = load i32*, i32** %4
%32 = call i32 @sqlite3_step(i32* %31)
%33 = load i32, i32* @SQLITE_ROW
%34 = icmp eq i32 %32, %33
br i1 %34, label %47, label %35
35: ; preds = %30
%36 = load i32, i32* @SQLITE_DONE
%37 = icmp eq i32 %32, %36
br i1 %37, label %38, label %40
38: ; preds = %35
%39 = call i8* (i8*, ...) @sqlite3_mprintf(i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.5, i64 0, i64 0))
br label %43
40: ; preds = %35
%41 = call i32 @sqlite3_errmsg(i32* %0)
%42 = call i8* (i8*, ...) @sqlite3_mprintf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32 %41)
br label %43
43: ; preds = %40, %38
%44 = phi i8* [ %42, %40 ], [ %39, %38 ]
store i8* %44, i8** %5
%45 = load i32*, i32** %4
%46 = call i32 @sqlite3_finalize(i32* %45)
br label %84
47: ; preds = %30
%48 = call i32 @sqlite3_free(i8* %19)
%49 = load i32*, i32** %4
%50 = call i32 @sqlite3_column_text(i32* %49, i32 0)
%51 = call i8* (i8*, ...) @sqlite3_mprintf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.6, i64 0, i64 0), i8* %1, i32 %50, i8* %1)
%52 = icmp eq i8* %51, null
br i1 %52, label %53, label %55
53: ; preds = %47
%54 = call i32 @log_fatal_null(i32 2, i8* getelementptr inbounds ([107 x i8], [107 x i8]* @.str.7, i64 0, i64 0), i32 219)
br label %55
55: ; preds = %53, %47
%56 = load i32*, i32** %4
%57 = call i32 @sqlite3_finalize(i32* %56)
%58 = call i32 @sqlite3_exec(i32* %0, i8* %51, i32* null, i32* null, i8** %5)
%59 = load i32, i32* @SQLITE_OK
%60 = icmp eq i32 %58, %59
br i1 %60, label %61, label %84
61: ; preds = %55
%62 = call i32 @sqlite3_free(i8* %51)
%63 = call i8* (i8*, ...) @sqlite3_mprintf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.8, i64 0, i64 0), i8* %1)
%64 = icmp eq i8* %63, null
br i1 %64, label %65, label %67
65: ; preds = %61
%66 = call i32 @log_fatal_null(i32 2, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.9, i64 0, i64 0), i32 230)
br label %67
67: ; preds = %65, %61
%68 = call i32 @sqlite3_exec(i32* %0, i8* %63, i32* null, i32* null, i8** %5)
%69 = load i32, i32* @SQLITE_OK
%70 = icmp eq i32 %68, %69
br i1 %70, label %71, label %84
71: ; preds = %67
%72 = call i32 @sqlite3_free(i8* %63)
%73 = call i8* (i8*, ...) @sqlite3_mprintf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.10, i64 0, i64 0), i8* %1, i8* %1)
%74 = icmp eq i8* %73, null
br i1 %74, label %75, label %77
75: ; preds = %71
%76 = call i32 @log_fatal_null(i32 2, i8* getelementptr inbounds ([72 x i8], [72 x i8]* @.str.11, i64 0, i64 0), i32 239)
br label %77
77: ; preds = %75, %71
%78 = call i32 @sqlite3_exec(i32* %0, i8* %73, i32* null, i32* null, i8** %5)
%79 = load i32, i32* @SQLITE_OK
%80 = icmp eq i32 %78, %79
br i1 %80, label %81, label %84
81: ; preds = %77
%82 = call i32 @sqlite3_free(i8* %73)
%83 = call i32 (i32, i32, i8*, ...) @DPRINTF(i32 1, i32 2, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.12, i64 0, i64 0), i8* %1)
br label %92
84: ; preds = %77, %67, %55, %13, %43, %27
%85 = phi i8* [ %9, %13 ], [ %19, %27 ], [ %19, %43 ], [ %51, %55 ], [ %63, %67 ], [ %73, %77 ]
%86 = phi i32 [ %14, %13 ], [ %24, %27 ], [ %32, %43 ], [ %58, %55 ], [ %68, %67 ], [ %78, %77 ]
%87 = load i8*, i8** %5
%88 = call i32 (i32, i32, i8*, ...) @DPRINTF(i32 1, i32 2, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.13, i64 0, i64 0), i32 %86, i8* %85, i8* %87)
%89 = call i32 @sqlite3_free(i8* %85)
%90 = load i8*, i8** %5
%91 = call i32 @sqlite3_free(i8* %90)
br label %92
92: ; preds = %84, %81
%93 = phi i32 [ -1, %84 ], [ 0, %81 ]
ret i32 %93
}