#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_ROW;
int sqlite3_column_int(int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ sqlite3_finalize(int /*<<< orphan*/ *);
int /*<<< orphan*/ sqlite3_free(char *);
char *sqlite3_mprintf(char *, char *);
int sqlite3_prepare_v2(int /*<<< orphan*/ *, char *, int, int /*<<< orphan*/ **,
                       int /*<<< orphan*/ *);
int sqlite3_step(int /*<<< orphan*/ *);
int db_upgrade_v19_directory_id(sqlite3 *hdl, char *virtual_path) {
  sqlite3_stmt *stmt;
  char *query;
  int id;
  int ret;
  query = sqlite3_mprintf("SELECT d.id FROM directories d WHERE d.disabled = 0 "
                          "AND d.virtual_path = '%q';",
                          virtual_path);
  if (!query) {
    DPRINTF(1, 2, "Out of memory for query string\n");
    return -1;
  }
  ret = sqlite3_prepare_v2(hdl, query, -1, &stmt, NULL);
  if (ret < 0) {
    DPRINTF(1, 2, "Error preparing query '%s'\n", query);
    sqlite3_free(query);
    return -1;
  }
  ret = sqlite3_step(stmt);
  if (ret == SQLITE_ROW)
    id = sqlite3_column_int(stmt, 0);
  else if (ret == SQLITE_DONE)
    id = 0;
  else {
    DPRINTF(1, 2, "Error stepping query '%s'\n", query);
    sqlite3_free(query);
    sqlite3_finalize(stmt);
    return -1;
  }
  sqlite3_free(query);
  sqlite3_finalize(stmt);
  return id;
}


x86 assembly (clang, -O0):
<db_upgrade_v19_directory_id>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rsi
lea    0xe75(%rip),%rdi
call   1080 <sqlite3_mprintf@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11c3 <db_upgrade_v19_directory_id+0x53>
mov    $0x1,%edi
mov    $0x2,%esi
lea    0xe9f(%rip),%rdx
mov    $0x0,%al
call   1090 <DPRINTF@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c2 <db_upgrade_v19_directory_id+0x152>
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    $0xffffffff,%edx
lea    -0x20(%rbp),%rcx
xor    %eax,%eax
mov    %eax,%r8d
call   1060 <sqlite3_prepare_v2@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x30(%rbp)
jge    121c <db_upgrade_v19_directory_id+0xac>
mov    -0x28(%rbp),%rcx
mov    $0x1,%edi
mov    $0x2,%esi
lea    0xe6f(%rip),%rdx
mov    $0x0,%al
call   1090 <DPRINTF@plt>
mov    -0x28(%rbp),%rdi
call   1070 <sqlite3_free@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c2 <db_upgrade_v19_directory_id+0x152>
mov    -0x20(%rbp),%rdi
call   1040 <sqlite3_step@plt>
mov    %eax,-0x30(%rbp)
mov    -0x30(%rbp),%eax
mov    0x2da6(%rip),%rcx
cmp    (%rcx),%eax
jne    124d <db_upgrade_v19_directory_id+0xdd>
mov    -0x20(%rbp),%rdi
xor    %esi,%esi
call   1030 <sqlite3_column_int@plt>
mov    %eax,-0x2c(%rbp)
jmp    12aa <db_upgrade_v19_directory_id+0x13a>
mov    -0x30(%rbp),%eax
mov    0x2d89(%rip),%rcx
cmp    (%rcx),%eax
jne    126b <db_upgrade_v19_directory_id+0xfb>
movl   $0x0,-0x2c(%rbp)
jmp    12a5 <db_upgrade_v19_directory_id+0x135>
mov    -0x28(%rbp),%rcx
mov    $0x1,%edi
mov    $0x2,%esi
lea    0xe0b(%rip),%rdx
mov    $0x0,%al
call   1090 <DPRINTF@plt>
mov    -0x28(%rbp),%rdi
call   1070 <sqlite3_free@plt>
mov    -0x20(%rbp),%rdi
call   1050 <sqlite3_finalize@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c2 <db_upgrade_v19_directory_id+0x152>
jmp    12aa <db_upgrade_v19_directory_id+0x13a>
mov    -0x28(%rbp),%rdi
call   1070 <sqlite3_free@plt>
mov    -0x20(%rbp),%rdi
call   1050 <sqlite3_finalize@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<db_upgrade_v19_directory_id>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbp
lea    0xe7e(%rip),%rdi
call   1080 <sqlite3_mprintf@plt>
test   %rax,%rax
je     11d7 <db_upgrade_v19_directory_id+0x67>
mov    %rax,%rbx
mov    $0xffffffff,%r14d
lea    0x8(%rsp),%rcx
mov    %rbp,%rdi
mov    %rax,%rsi
mov    $0xffffffff,%edx
xor    %r8d,%r8d
call   1060 <sqlite3_prepare_v2@plt>
test   %eax,%eax
js     11f1 <db_upgrade_v19_directory_id+0x81>
mov    0x8(%rsp),%rdi
call   1040 <sqlite3_step@plt>
mov    0x2e16(%rip),%rcx
cmp    (%rcx),%eax
jne    1216 <db_upgrade_v19_directory_id+0xa6>
mov    0x8(%rsp),%rdi
xor    %esi,%esi
call   1030 <sqlite3_column_int@plt>
mov    %eax,%r14d
jmp    1224 <db_upgrade_v19_directory_id+0xb4>
lea    0xe71(%rip),%rdx
mov    $0x1,%edi
mov    $0x2,%esi
xor    %eax,%eax
call   1090 <DPRINTF@plt>
jmp    1265 <db_upgrade_v19_directory_id+0xf5>
lea    0xe77(%rip),%rdx
mov    $0x1,%edi
mov    $0x2,%esi
mov    %rbx,%rcx
xor    %eax,%eax
call   1090 <DPRINTF@plt>
mov    %rbx,%rdi
call   1070 <sqlite3_free@plt>
jmp    126b <db_upgrade_v19_directory_id+0xfb>
mov    0x2dc3(%rip),%rcx
xor    %r14d,%r14d
cmp    (%rcx),%eax
jne    1238 <db_upgrade_v19_directory_id+0xc8>
mov    %rbx,%rdi
call   1070 <sqlite3_free@plt>
mov    0x8(%rsp),%rdi
call   1050 <sqlite3_finalize@plt>
jmp    126b <db_upgrade_v19_directory_id+0xfb>
lea    0xe4c(%rip),%rdx
mov    $0x1,%edi
mov    $0x2,%esi
mov    %rbx,%rcx
xor    %eax,%eax
call   1090 <DPRINTF@plt>
mov    %rbx,%rdi
call   1070 <sqlite3_free@plt>
mov    0x8(%rsp),%rdi
call   1050 <sqlite3_finalize@plt>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [79 x i8] c"SELECT d.id FROM directories d WHERE d.disabled = 0 AND d.virtual_path = '%q';\00"
@.str.1 = [32 x i8] c"Out of memory for query string\0A\00"
@.str.2 = [28 x i8] c"Error preparing query '%s'\0A\00"
@SQLITE_ROW = global i32 0
@SQLITE_DONE = global i32 0
@.str.3 = [27 x i8] c"Error stepping query '%s'\0A\00"
define i32 @db_upgrade_v19_directory_id(i32* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
store i32* %0, i32** %4
store i8* %1, i8** %5
%10 = load i8*, i8** %5
%11 = call i8* @sqlite3_mprintf(i8* getelementptr inbounds ([79 x i8], [79 x i8]* @.str, i64 0, i64 0), i8* %10)
store i8* %11, i8** %7
%12 = load i8*, i8** %7
%13 = icmp ne i8* %12, null
br i1 %13, label %16, label %14
14: ; preds = %2
%15 = call i32 (i32, i32, i8*, ...) @DPRINTF(i32 1, i32 2, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %55
16: ; preds = %2
%17 = load i32*, i32** %4
%18 = load i8*, i8** %7
%19 = call i32 @sqlite3_prepare_v2(i32* %17, i8* %18, i32 -1, i32** %6, i32* null)
store i32 %19, i32* %9
%20 = load i32, i32* %9
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %27
22: ; preds = %16
%23 = load i8*, i8** %7
%24 = call i32 (i32, i32, i8*, ...) @DPRINTF(i32 1, i32 2, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i8* %23)
%25 = load i8*, i8** %7
%26 = call i32 @sqlite3_free(i8* %25)
store i32 -1, i32* %3
br label %55
27: ; preds = %16
%28 = load i32*, i32** %6
%29 = call i32 @sqlite3_step(i32* %28)
store i32 %29, i32* %9
%30 = load i32, i32* %9
%31 = load i32, i32* @SQLITE_ROW
%32 = icmp eq i32 %30, %31
br i1 %32, label %33, label %36
33: ; preds = %27
%34 = load i32*, i32** %6
%35 = call i32 @sqlite3_column_int(i32* %34, i32 0)
store i32 %35, i32* %8
br label %49
36: ; preds = %27
%37 = load i32, i32* %9
%38 = load i32, i32* @SQLITE_DONE
%39 = icmp eq i32 %37, %38
br i1 %39, label %40, label %41
40: ; preds = %36
store i32 0, i32* %8
br label %48
41: ; preds = %36
%42 = load i8*, i8** %7
%43 = call i32 (i32, i32, i8*, ...) @DPRINTF(i32 1, i32 2, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0), i8* %42)
%44 = load i8*, i8** %7
%45 = call i32 @sqlite3_free(i8* %44)
%46 = load i32*, i32** %6
%47 = call i32 @sqlite3_finalize(i32* %46)
store i32 -1, i32* %3
br label %55
48: ; preds = %40
br label %49
49: ; preds = %48, %33
%50 = load i8*, i8** %7
%51 = call i32 @sqlite3_free(i8* %50)
%52 = load i32*, i32** %6
%53 = call i32 @sqlite3_finalize(i32* %52)
%54 = load i32, i32* %8
store i32 %54, i32* %3
br label %55
55: ; preds = %49, %41, %22, %14
%56 = load i32, i32* %3
ret i32 %56
}

LLVM IR (-O2):
@.str = [79 x i8] c"SELECT d.id FROM directories d WHERE d.disabled = 0 AND d.virtual_path = '%q';\00"
@.str.1 = [32 x i8] c"Out of memory for query string\0A\00"
@.str.2 = [28 x i8] c"Error preparing query '%s'\0A\00"
@SQLITE_ROW = global i32 0
@SQLITE_DONE = global i32 0
@.str.3 = [27 x i8] c"Error stepping query '%s'\0A\00"
define i32 @db_upgrade_v19_directory_id(i32* %0, i8* %1) {
%3 = alloca i32*
%4 = bitcast i32** %3 to i8*
%5 = tail call i8* @sqlite3_mprintf(i8* getelementptr inbounds ([79 x i8], [79 x i8]* @.str, i64 0, i64 0), i8* %1)
%6 = icmp eq i8* %5, null
br i1 %6, label %7, label %9
7: ; preds = %2
%8 = tail call i32 (i32, i32, i8*, ...) @DPRINTF(i32 1, i32 2, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0))
br label %36
9: ; preds = %2
%10 = call i32 @sqlite3_prepare_v2(i32* %0, i8* %5, i32 -1, i32** %3, i32* null)
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %9
%13 = call i32 (i32, i32, i8*, ...) @DPRINTF(i32 1, i32 2, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i8* %5)
%14 = call i32 @sqlite3_free(i8* %5)
br label %36
15: ; preds = %9
%16 = load i32*, i32** %3
%17 = call i32 @sqlite3_step(i32* %16)
%18 = load i32, i32* @SQLITE_ROW
%19 = icmp eq i32 %17, %18
br i1 %19, label %20, label %23
20: ; preds = %15
%21 = load i32*, i32** %3
%22 = call i32 @sqlite3_column_int(i32* %21, i32 0)
br label %31
23: ; preds = %15
%24 = load i32, i32* @SQLITE_DONE
%25 = icmp eq i32 %17, %24
br i1 %25, label %31, label %26
26: ; preds = %23
%27 = call i32 (i32, i32, i8*, ...) @DPRINTF(i32 1, i32 2, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0), i8* %5)
%28 = call i32 @sqlite3_free(i8* %5)
%29 = load i32*, i32** %3
%30 = call i32 @sqlite3_finalize(i32* %29)
br label %36
31: ; preds = %23, %20
%32 = phi i32 [ %22, %20 ], [ 0, %23 ]
%33 = call i32 @sqlite3_free(i8* %5)
%34 = load i32*, i32** %3
%35 = call i32 @sqlite3_finalize(i32* %34)
br label %36
36: ; preds = %31, %26, %12, %7
%37 = phi i32 [ -1, %12 ], [ %32, %31 ], [ -1, %26 ], [ -1, %7 ]
ret i32 %37
}