#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_package {
  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_createpackage(struct db_package *, char *, char *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ opendatabase(int /*<<< orphan*/ **);
int /*<<< orphan*/ sqlite3_bind_int(int /*<<< orphan*/ *, int, unsigned int);
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_loadpackage(struct db_package *package, unsigned int id) {
  sqlite3 *db;
  sqlite3_stmt *res;
  opendatabase(&db);
  if (sqlite3_prepare_v2(db, "SELECT sha1, date FROM packages WHERE id = ?", -1,
                         &res, 0) != SQLITE_OK)
    exit(EXIT_FAILURE);
  sqlite3_bind_int(res, 1, id);
  if (sqlite3_step(res) != SQLITE_ROW)
    exit(EXIT_FAILURE);
  db_createpackage(package, (char *)sqlite3_column_text(res, 0),
                   (char *)sqlite3_column_text(res, 1));
  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_loadpackage>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
lea    -0x18(%rbp),%rdi
call   1050 <opendatabase@plt>
mov    -0x18(%rbp),%rdi
lea    0xe4d(%rip),%rsi
mov    $0xffffffff,%edx
lea    -0x20(%rbp),%rcx
xor    %r8d,%r8d
call   10a0 <sqlite3_prepare_v2@plt>
mov    0x2e1d(%rip),%rcx
cmp    (%rcx),%rax
je     11e2 <db_loadpackage+0x52>
mov    0x2df5(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    -0x20(%rbp),%rdi
mov    -0xc(%rbp),%edx
mov    $0x1,%esi
call   1030 <sqlite3_bind_int@plt>
mov    -0x20(%rbp),%rdi
call   1040 <sqlite3_step@plt>
mov    0x2dc5(%rip),%rcx
cmp    (%rcx),%rax
je     121a <db_loadpackage+0x8a>
mov    0x2dbd(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    -0x8(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rdi
xor    %esi,%esi
call   1080 <sqlite3_column_text@plt>
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
mov    $0x1,%esi
call   1080 <sqlite3_column_text@plt>
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    %rax,%rdx
call   1060 <db_createpackage@plt>
mov    -0x20(%rbp),%rdi
call   1040 <sqlite3_step@plt>
mov    0x2d79(%rip),%rcx
cmp    (%rcx),%rax
je     1276 <db_loadpackage+0xe6>
mov    0x2d61(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    -0x20(%rbp),%rdi
call   1090 <sqlite3_finalize@plt>
mov    0x2d62(%rip),%rcx
cmp    (%rcx),%rax
je     129d <db_loadpackage+0x10d>
mov    0x2d3a(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    -0x18(%rbp),%rdi
call   1070 <closedatabase@plt>
mov    $0x1,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<db_loadpackage>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %esi,%ebp
mov    %rdi,%rbx
lea    0x8(%rsp),%rdi
call   1050 <opendatabase@plt>
mov    0x8(%rsp),%rdi
lea    0xe4d(%rip),%rsi
mov    %rsp,%rcx
mov    $0xffffffff,%edx
xor    %r8d,%r8d
call   10a0 <sqlite3_prepare_v2@plt>
mov    0x2e1e(%rip),%r14
cmp    (%r14),%rax
jne    125d <db_loadpackage+0xcd>
mov    (%rsp),%rdi
mov    $0x1,%esi
mov    %ebp,%edx
call   1030 <sqlite3_bind_int@plt>
mov    (%rsp),%rdi
call   1040 <sqlite3_step@plt>
mov    0x2dd5(%rip),%rcx
cmp    (%rcx),%rax
jne    125d <db_loadpackage+0xcd>
mov    (%rsp),%rdi
xor    %esi,%esi
call   1080 <sqlite3_column_text@plt>
mov    %rax,%rbp
mov    (%rsp),%rdi
mov    $0x1,%esi
call   1080 <sqlite3_column_text@plt>
mov    %rbx,%rdi
mov    %rbp,%rsi
mov    %rax,%rdx
call   1060 <db_createpackage@plt>
mov    (%rsp),%rdi
call   1040 <sqlite3_step@plt>
mov    0x2da6(%rip),%rcx
cmp    (%rcx),%rax
jne    125d <db_loadpackage+0xcd>
mov    (%rsp),%rdi
call   1090 <sqlite3_finalize@plt>
cmp    (%r14),%rax
jne    125d <db_loadpackage+0xcd>
mov    0x8(%rsp),%rdi
call   1070 <closedatabase@plt>
mov    $0x1,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2d6c(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
%struct.db_package = type { i32 }
@.str = [45 x i8] c"SELECT sha1, date FROM packages 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_loadpackage(%struct.db_package* %0, i32 %1) {
%3 = alloca %struct.db_package*
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i32*
store %struct.db_package* %0, %struct.db_package** %3
store i32 %1, i32* %4
%7 = call i32 @opendatabase(i32** %5)
%8 = load i32*, i32** %5
%9 = call i64 @sqlite3_prepare_v2(i32* %8, i8* getelementptr inbounds ([45 x i8], [45 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 i32, i32* %4
%18 = call i32 @sqlite3_bind_int(i32* %16, i32 1, i32 %17)
%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_package*, %struct.db_package** %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 = call i32 @db_createpackage(%struct.db_package* %27, i8* %30, i8* %33)
%35 = load i32*, i32** %6
%36 = call i64 @sqlite3_step(i32* %35)
%37 = load i64, i64* @SQLITE_DONE
%38 = icmp ne i64 %36, %37
br i1 %38, label %39, label %42
39: ; preds = %26
%40 = load i32, i32* @EXIT_FAILURE
%41 = call i32 @exit(i32 %40)
unreachable
42: ; preds = %26
%43 = load i32*, i32** %6
%44 = call i64 @sqlite3_finalize(i32* %43)
%45 = load i64, i64* @SQLITE_OK
%46 = icmp ne i64 %44, %45
br i1 %46, label %47, label %50
47: ; preds = %42
%48 = load i32, i32* @EXIT_FAILURE
%49 = call i32 @exit(i32 %48)
unreachable
50: ; preds = %42
%51 = load i32*, i32** %5
%52 = call i32 @closedatabase(i32* %51)
ret i32 1
}

LLVM IR (-O2):
%struct.db_package = type { i32 }
@.str = [45 x i8] c"SELECT sha1, date FROM packages 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_loadpackage(%struct.db_package* %0, i32 %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 ([45 x i8], [45 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_int(i32* %16, i32 1, i32 %1)
%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 = call i32 @db_createpackage(%struct.db_package* %0, i8* %28, i8* %31)
%33 = load i32*, i32** %4
%34 = call i64 @sqlite3_step(i32* %33)
%35 = load i64, i64* @SQLITE_DONE
%36 = icmp eq i64 %34, %35
br i1 %36, label %40, label %37
37: ; preds = %25
%38 = load i32, i32* @EXIT_FAILURE
%39 = call i32 @exit(i32 %38)
unreachable
40: ; preds = %25
%41 = load i32*, i32** %4
%42 = call i64 @sqlite3_finalize(i32* %41)
%43 = load i64, i64* @SQLITE_OK
%44 = icmp eq i64 %42, %43
br i1 %44, label %48, label %45
45: ; preds = %40
%46 = load i32, i32* @EXIT_FAILURE
%47 = call i32 @exit(i32 %46)
unreachable
48: ; preds = %40
%49 = load i32*, i32** %3
%50 = call i32 @closedatabase(i32* %49)
ret i32 1
}