#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 */
struct db_app {
  int dummy;
};
typedef int /*<<< orphan*/ sqlite3_stmt;
typedef int /*<<< orphan*/ sqlite3;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
scalar_t__ SQLITE_DONE;
scalar_t__ SQLITE_OK;
scalar_t__ SQLITE_ROW;
int /*<<< orphan*/ closedatabase(int /*<<< orphan*/ *);
int /*<<< orphan*/ db_createapp(struct db_app *, char *, char *, char *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ opendatabase(int /*<<< orphan*/ **);
int /*<<< orphan*/ sqlite3_bind_text(int /*<<< orphan*/ *, int, char const *,
                                     int, int /*<<< orphan*/);
scalar_t__ sqlite3_column_text(int /*<<< orphan*/ *, int);
scalar_t__ sqlite3_finalize(int /*<<< orphan*/ *);
scalar_t__ sqlite3_prepare_v2(int /*<<< orphan*/ *, char *, int,
                              int /*<<< orphan*/ **, int /*<<< orphan*/);
scalar_t__ sqlite3_step(int /*<<< orphan*/ *);
int db_loadapp(struct db_app *app, const char id[6]) {
  sqlite3 *db;
  sqlite3_stmt *res;
  opendatabase(&db);
  if (sqlite3_prepare_v2(db, "SELECT id, name, short FROM apps WHERE id = ?",
                         -1, &res, 0) != SQLITE_OK)
    exit(EXIT_FAILURE);
  sqlite3_bind_text(res, 1, id, 6, 0);
  if (sqlite3_step(res) != SQLITE_ROW)
    exit(EXIT_FAILURE);
  db_createapp(app, (char *)sqlite3_column_text(res, 0),
               (char *)sqlite3_column_text(res, 1),
               (char *)sqlite3_column_text(res, 2));
  if (sqlite3_step(res) != SQLITE_DONE)
    exit(EXIT_FAILURE);
  if (sqlite3_finalize(res) != SQLITE_OK)
    exit(EXIT_FAILURE);
  closedatabase(db);
  return 1;
}


x86 assembly (clang, -O0):
<db_loadapp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x18(%rbp),%rdi
call   1040 <opendatabase@plt>
mov    -0x18(%rbp),%rdi
lea    0xe4c(%rip),%rsi
mov    $0xffffffff,%edx
lea    -0x20(%rbp),%rcx
xor    %r8d,%r8d
call   10a0 <sqlite3_prepare_v2@plt>
mov    0x2e1c(%rip),%rcx
cmp    (%rcx),%rax
je     11e3 <db_loadapp+0x53>
mov    0x2df4(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0x1,%esi
mov    $0x6,%ecx
xor    %r8d,%r8d
call   1070 <sqlite3_bind_text@plt>
mov    -0x20(%rbp),%rdi
call   1030 <sqlite3_step@plt>
mov    0x2dbb(%rip),%rcx
cmp    (%rcx),%rax
je     1224 <db_loadapp+0x94>
mov    0x2db3(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    -0x8(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x20(%rbp),%rdi
xor    %esi,%esi
call   1060 <sqlite3_column_text@plt>
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rdi
mov    $0x1,%esi
call   1060 <sqlite3_column_text@plt>
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
mov    $0x2,%esi
call   1060 <sqlite3_column_text@plt>
mov    -0x38(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x28(%rbp),%rdx
mov    %rax,%rcx
call   1080 <db_createapp@plt>
mov    -0x20(%rbp),%rdi
call   1030 <sqlite3_step@plt>
mov    0x2d59(%rip),%rcx
cmp    (%rcx),%rax
je     1296 <db_loadapp+0x106>
mov    0x2d41(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    -0x20(%rbp),%rdi
call   1090 <sqlite3_finalize@plt>
mov    0x2d42(%rip),%rcx
cmp    (%rcx),%rax
je     12bd <db_loadapp+0x12d>
mov    0x2d1a(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    -0x18(%rbp),%rdi
call   1050 <closedatabase@plt>
mov    $0x1,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<db_loadapp>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%rbx
mov    %rdi,%r14
lea    0x10(%rsp),%rdi
call   1040 <opendatabase@plt>
mov    0x10(%rsp),%rdi
lea    0xe49(%rip),%rsi
lea    0x8(%rsp),%rcx
mov    $0xffffffff,%edx
xor    %r8d,%r8d
call   10a0 <sqlite3_prepare_v2@plt>
mov    0x2e18(%rip),%r12
cmp    (%r12),%rax
jne    1290 <db_loadapp+0x100>
mov    0x8(%rsp),%rdi
mov    $0x1,%esi
mov    %rbx,%rdx
mov    $0x6,%ecx
xor    %r8d,%r8d
call   1070 <sqlite3_bind_text@plt>
mov    0x8(%rsp),%rdi
call   1030 <sqlite3_step@plt>
mov    0x2dc3(%rip),%rcx
cmp    (%rcx),%rax
jne    1290 <db_loadapp+0x100>
mov    0x8(%rsp),%rdi
xor    %esi,%esi
call   1060 <sqlite3_column_text@plt>
mov    %rax,%r15
mov    0x8(%rsp),%rdi
mov    $0x1,%esi
call   1060 <sqlite3_column_text@plt>
mov    %rax,%rbx
mov    0x8(%rsp),%rdi
mov    $0x2,%esi
call   1060 <sqlite3_column_text@plt>
mov    %r14,%rdi
mov    %r15,%rsi
mov    %rbx,%rdx
mov    %rax,%rcx
call   1080 <db_createapp@plt>
mov    0x8(%rsp),%rdi
call   1030 <sqlite3_step@plt>
mov    0x2d78(%rip),%rcx
cmp    (%rcx),%rax
jne    1290 <db_loadapp+0x100>
mov    0x8(%rsp),%rdi
call   1090 <sqlite3_finalize@plt>
cmp    (%r12),%rax
jne    1290 <db_loadapp+0x100>
mov    0x10(%rsp),%rdi
call   1050 <closedatabase@plt>
mov    $0x1,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    0x2d39(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
%struct.db_app = type { i32 }
@.str = [46 x i8] c"SELECT id, name, short FROM apps WHERE id = ?\00"
@SQLITE_OK = global i64 0
@EXIT_FAILURE = global i32 0
@SQLITE_ROW = global i64 0
@SQLITE_DONE = global i64 0
define i32 @db_loadapp(%struct.db_app* %0, i8* %1) {
%3 = alloca %struct.db_app*
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i32*
store %struct.db_app* %0, %struct.db_app** %3
store i8* %1, i8** %4
%7 = call i32 @opendatabase(i32** %5)
%8 = load i32*, i32** %5
%9 = call i64 @sqlite3_prepare_v2(i32* %8, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str, i64 0, i64 0), i32 -1, i32** %6, i32 0)
%10 = load i64, i64* @SQLITE_OK
%11 = icmp ne i64 %9, %10
br i1 %11, label %12, label %15
12: ; preds = %2
%13 = load i32, i32* @EXIT_FAILURE
%14 = call i32 @exit(i32 %13)
unreachable
15: ; preds = %2
%16 = load i32*, i32** %6
%17 = load i8*, i8** %4
%18 = call i32 @sqlite3_bind_text(i32* %16, i32 1, i8* %17, i32 6, i32 0)
%19 = load i32*, i32** %6
%20 = call i64 @sqlite3_step(i32* %19)
%21 = load i64, i64* @SQLITE_ROW
%22 = icmp ne i64 %20, %21
br i1 %22, label %23, label %26
23: ; preds = %15
%24 = load i32, i32* @EXIT_FAILURE
%25 = call i32 @exit(i32 %24)
unreachable
26: ; preds = %15
%27 = load %struct.db_app*, %struct.db_app** %3
%28 = load i32*, i32** %6
%29 = call i64 @sqlite3_column_text(i32* %28, i32 0)
%30 = inttoptr i64 %29 to i8*
%31 = load i32*, i32** %6
%32 = call i64 @sqlite3_column_text(i32* %31, i32 1)
%33 = inttoptr i64 %32 to i8*
%34 = load i32*, i32** %6
%35 = call i64 @sqlite3_column_text(i32* %34, i32 2)
%36 = inttoptr i64 %35 to i8*
%37 = call i32 @db_createapp(%struct.db_app* %27, i8* %30, i8* %33, i8* %36)
%38 = load i32*, i32** %6
%39 = call i64 @sqlite3_step(i32* %38)
%40 = load i64, i64* @SQLITE_DONE
%41 = icmp ne i64 %39, %40
br i1 %41, label %42, label %45
42: ; preds = %26
%43 = load i32, i32* @EXIT_FAILURE
%44 = call i32 @exit(i32 %43)
unreachable
45: ; preds = %26
%46 = load i32*, i32** %6
%47 = call i64 @sqlite3_finalize(i32* %46)
%48 = load i64, i64* @SQLITE_OK
%49 = icmp ne i64 %47, %48
br i1 %49, label %50, label %53
50: ; preds = %45
%51 = load i32, i32* @EXIT_FAILURE
%52 = call i32 @exit(i32 %51)
unreachable
53: ; preds = %45
%54 = load i32*, i32** %5
%55 = call i32 @closedatabase(i32* %54)
ret i32 1
}

LLVM IR (-O2):
%struct.db_app = type { i32 }
@.str = [46 x i8] c"SELECT id, name, short FROM apps WHERE id = ?\00"
@SQLITE_OK = global i64 0
@EXIT_FAILURE = global i32 0
@SQLITE_ROW = global i64 0
@SQLITE_DONE = global i64 0
define i32 @db_loadapp(%struct.db_app* %0, i8* %1) {
%3 = alloca i32*
%4 = alloca i32*
%5 = bitcast i32** %3 to i8*
%6 = bitcast i32** %4 to i8*
%7 = call i32 @opendatabase(i32** %3)
%8 = load i32*, i32** %3
%9 = call i64 @sqlite3_prepare_v2(i32* %8, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str, i64 0, i64 0), i32 -1, i32** %4, i32 0)
%10 = load i64, i64* @SQLITE_OK
%11 = icmp eq i64 %9, %10
br i1 %11, label %15, label %12
12: ; preds = %2
%13 = load i32, i32* @EXIT_FAILURE
%14 = call i32 @exit(i32 %13)
unreachable
15: ; preds = %2
%16 = load i32*, i32** %4
%17 = call i32 @sqlite3_bind_text(i32* %16, i32 1, i8* %1, i32 6, i32 0)
%18 = load i32*, i32** %4
%19 = call i64 @sqlite3_step(i32* %18)
%20 = load i64, i64* @SQLITE_ROW
%21 = icmp eq i64 %19, %20
br i1 %21, label %25, label %22
22: ; preds = %15
%23 = load i32, i32* @EXIT_FAILURE
%24 = call i32 @exit(i32 %23)
unreachable
25: ; preds = %15
%26 = load i32*, i32** %4
%27 = call i64 @sqlite3_column_text(i32* %26, i32 0)
%28 = inttoptr i64 %27 to i8*
%29 = load i32*, i32** %4
%30 = call i64 @sqlite3_column_text(i32* %29, i32 1)
%31 = inttoptr i64 %30 to i8*
%32 = load i32*, i32** %4
%33 = call i64 @sqlite3_column_text(i32* %32, i32 2)
%34 = inttoptr i64 %33 to i8*
%35 = call i32 @db_createapp(%struct.db_app* %0, i8* %28, i8* %31, i8* %34)
%36 = load i32*, i32** %4
%37 = call i64 @sqlite3_step(i32* %36)
%38 = load i64, i64* @SQLITE_DONE
%39 = icmp eq i64 %37, %38
br i1 %39, label %43, label %40
40: ; preds = %25
%41 = load i32, i32* @EXIT_FAILURE
%42 = call i32 @exit(i32 %41)
unreachable
43: ; preds = %25
%44 = load i32*, i32** %4
%45 = call i64 @sqlite3_finalize(i32* %44)
%46 = load i64, i64* @SQLITE_OK
%47 = icmp eq i64 %45, %46
br i1 %47, label %51, label %48
48: ; preds = %43
%49 = load i32, i32* @EXIT_FAILURE
%50 = call i32 @exit(i32 %49)
unreachable
51: ; preds = %43
%52 = load i32*, i32** %3
%53 = call i32 @closedatabase(i32* %52)
ret i32 1
}