#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;

/* Variables and functions */
int SQLITE_OK;
int /*<<< orphan*/ ensure_db_directory_exists();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
char *get_db_directory();
int /*<<< orphan*/ sprintf(char *, char *, char const *);
int /*<<< orphan*/ sqlite3_close(int /*<<< orphan*/ *);
char *sqlite3_errmsg(int /*<<< orphan*/ *);
int sqlite3_exec(int /*<<< orphan*/ *, char *, int /*<<< orphan*/ *,
                 int /*<<< orphan*/, char **);
int /*<<< orphan*/ sqlite3_free(char *);
int sqlite3_open(char *, int /*<<< orphan*/ **);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strlen(char const *);
sqlite3 *open_db(void) {
  sqlite3 *db;
  int return_code;
  char *error_message = NULL;
  const char *db_directory = get_db_directory();
  char db_filename[strlen(db_directory) + 3];
  sprintf(db_filename, "%sdb", db_directory);
  ensure_db_directory_exists();
  return_code = sqlite3_open(db_filename, &db);
  if (return_code) {
    fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
    sqlite3_close(db);
    return NULL;
  }
  return_code = sqlite3_exec(db,
                             "CREATE TABLE IF NOT EXISTS items ( "
                             "id INTEGER PRIMARY KEY AUTOINCREMENT,"
                             "description TEXT NOT NULL, "
                             "created_on BIGINT NOT NULL, "
                             "expires_on BIGINT NOT NULL, "
                             "done_on BIGINT NOT NULL DEFAULT 0"
                             ");",
                             NULL, 0, &error_message);
  if (return_code != SQLITE_OK) {
    fprintf(stderr, "SQL error: %s\n", error_message);
    sqlite3_free(error_message);
  }
  return db;
}


x86 assembly (clang, -O0):
<open_db>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movq   $0x0,-0x20(%rbp)
xor    %eax,%eax
call   10a0 <get_db_directory@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
call   1050 <strlen@plt>
add    $0x3,%eax
mov    %rsp,%rcx
mov    %rcx,-0x30(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x48(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%rdx
lea    0xe0b(%rip),%rsi
call   10c0 <sprintf@plt>
mov    $0x0,%al
call   1070 <ensure_db_directory_exists@plt>
mov    -0x48(%rbp),%rdi
lea    -0x10(%rbp),%rsi
call   1040 <sqlite3_open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     125f <open_db+0xbf>
mov    0x2db6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <sqlite3_errmsg@plt>
mov    -0x4c(%rbp),%edi
mov    %rax,%rdx
lea    0xdc8(%rip),%rsi
call   1080 <fprintf@plt>
mov    -0x10(%rbp),%rdi
call   1090 <sqlite3_close@plt>
movq   $0x0,-0x8(%rbp)
movl   $0x1,-0x3c(%rbp)
jmp    12bf <open_db+0x11f>
mov    -0x10(%rbp),%rdi
lea    0xdb4(%rip),%rsi
xor    %eax,%eax
mov    %eax,%edx
xor    %ecx,%ecx
lea    -0x20(%rbp),%r8
call   1030 <sqlite3_exec@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    0x2d62(%rip),%rcx
cmp    (%rcx),%eax
je     12b0 <open_db+0x110>
mov    0x2d43(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rdx
lea    0xe3b(%rip),%rsi
call   1080 <fprintf@plt>
mov    -0x20(%rbp),%rdi
call   10b0 <sqlite3_free@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
movl   $0x1,-0x3c(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,%rsp
mov    -0x8(%rbp),%rax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_db>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
movq   $0x0,-0x28(%rbp)
xor    %eax,%eax
call   10a0 <get_db_directory@plt>
mov    %rax,%r14
mov    %rax,%rdi
call   1050 <strlen@plt>
mov    %rsp,%r15
add    $0x3,%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
lea    0xe1b(%rip),%rsi
mov    %rbx,%rdi
mov    %r14,%rdx
call   10c0 <sprintf@plt>
xor    %eax,%eax
call   1070 <ensure_db_directory_exists@plt>
lea    -0x20(%rbp),%rsi
mov    %rbx,%rdi
call   1040 <sqlite3_open@plt>
test   %eax,%eax
je     1237 <open_db+0x97>
mov    0x2dca(%rip),%rax
mov    (%rax),%ebx
mov    -0x20(%rbp),%rdi
call   1060 <sqlite3_errmsg@plt>
lea    0xde5(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1080 <fprintf@plt>
mov    -0x20(%rbp),%rdi
call   1090 <sqlite3_close@plt>
xor    %eax,%eax
jmp    1280 <open_db+0xe0>
mov    -0x20(%rbp),%rdi
lea    0xddc(%rip),%rsi
lea    -0x28(%rbp),%r8
xor    %edx,%edx
xor    %ecx,%ecx
call   1030 <sqlite3_exec@plt>
mov    0x2d92(%rip),%rcx
cmp    (%rcx),%eax
je     127c <open_db+0xdc>
mov    0x2d77(%rip),%rax
mov    (%rax),%edi
mov    -0x28(%rbp),%rdx
lea    0xe6f(%rip),%rsi
call   1080 <fprintf@plt>
mov    -0x28(%rbp),%rdi
call   10b0 <sqlite3_free@plt>
mov    -0x20(%rbp),%rax
mov    %r15,%rsp
lea    -0x18(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [5 x i8] c"%sdb\00"
@stderr = global i32 0
@.str.1 = [25 x i8] c"Can't open database: %s\0A\00"
@.str.2 = [191 x i8] c"CREATE TABLE IF NOT EXISTS items ( id INTEGER PRIMARY KEY AUTOINCREMENT,description TEXT NOT NULL, created_on BIGINT NOT NULL, expires_on BIGINT NOT NULL, done_on BIGINT NOT NULL DEFAULT 0);\00"
@SQLITE_OK = global i32 0
@.str.3 = [15 x i8] c"SQL error: %s\0A\00"
define i32* @open_db() {
%1 = alloca i32*
%2 = alloca i32*
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
store i8* null, i8** %4
%9 = call i8* (...) @get_db_directory()
store i8* %9, i8** %5
%10 = load i8*, i8** %5
%11 = call i32 @strlen(i8* %10)
%12 = add nsw i32 %11, 3
%13 = zext i32 %12 to i64
%14 = call i8* @llvm.stacksave()
store i8* %14, i8** %6
%15 = alloca i8, i64 %13
store i64 %13, i64* %7
%16 = load i8*, i8** %5
%17 = call i32 @sprintf(i8* %15, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %16)
%18 = call i32 (...) @ensure_db_directory_exists()
%19 = call i32 @sqlite3_open(i8* %15, i32** %2)
store i32 %19, i32* %3
%20 = load i32, i32* %3
%21 = icmp ne i32 %20, 0
br i1 %21, label %22, label %29
22: ; preds = %0
%23 = load i32, i32* @stderr
%24 = load i32*, i32** %2
%25 = call i8* @sqlite3_errmsg(i32* %24)
%26 = call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %25)
%27 = load i32*, i32** %2
%28 = call i32 @sqlite3_close(i32* %27)
store i32* null, i32** %1
store i32 1, i32* %8
br label %43
29: ; preds = %0
%30 = load i32*, i32** %2
%31 = call i32 @sqlite3_exec(i32* %30, i8* getelementptr inbounds ([191 x i8], [191 x i8]* @.str.2, i64 0, i64 0), i32* null, i32 0, i8** %4)
store i32 %31, i32* %3
%32 = load i32, i32* %3
%33 = load i32, i32* @SQLITE_OK
%34 = icmp ne i32 %32, %33
br i1 %34, label %35, label %41
35: ; preds = %29
%36 = load i32, i32* @stderr
%37 = load i8*, i8** %4
%38 = call i32 @fprintf(i32 %36, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i8* %37)
%39 = load i8*, i8** %4
%40 = call i32 @sqlite3_free(i8* %39)
br label %41
41: ; preds = %35, %29
%42 = load i32*, i32** %2
store i32* %42, i32** %1
store i32 1, i32* %8
br label %43
43: ; preds = %41, %22
%44 = load i8*, i8** %6
%45 = load i32*, i32** %1
ret i32* %45
}

LLVM IR (-O2):
@.str = [5 x i8] c"%sdb\00"
@stderr = global i32 0
@.str.1 = [25 x i8] c"Can't open database: %s\0A\00"
@.str.2 = [191 x i8] c"CREATE TABLE IF NOT EXISTS items ( id INTEGER PRIMARY KEY AUTOINCREMENT,description TEXT NOT NULL, created_on BIGINT NOT NULL, expires_on BIGINT NOT NULL, done_on BIGINT NOT NULL DEFAULT 0);\00"
@SQLITE_OK = global i32 0
@.str.3 = [15 x i8] c"SQL error: %s\0A\00"
define i32* @open_db() {
%1 = alloca i32*
%2 = alloca i8*
%3 = bitcast i32** %1 to i8*
%4 = bitcast i8** %2 to i8*
store i8* null, i8** %2
%5 = tail call i8* (...) @get_db_directory()
%6 = tail call i32 @strlen(i8* %5)
%7 = add nsw i32 %6, 3
%8 = zext i32 %7 to i64
%9 = tail call i8* @llvm.stacksave()
%10 = alloca i8, i64 %8
%11 = call i32 @sprintf(i8* %10, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %5)
%12 = tail call i32 (...) @ensure_db_directory_exists()
%13 = call i32 @sqlite3_open(i8* %10, i32** %1)
%14 = icmp eq i32 %13, 0
br i1 %14, label %22, label %15
15: ; preds = %0
%16 = load i32, i32* @stderr
%17 = load i32*, i32** %1
%18 = call i8* @sqlite3_errmsg(i32* %17)
%19 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %18)
%20 = load i32*, i32** %1
%21 = call i32 @sqlite3_close(i32* %20)
br label %35
22: ; preds = %0
%23 = load i32*, i32** %1
%24 = call i32 @sqlite3_exec(i32* %23, i8* getelementptr inbounds ([191 x i8], [191 x i8]* @.str.2, i64 0, i64 0), i32* null, i32 0, i8** %2)
%25 = load i32, i32* @SQLITE_OK
%26 = icmp eq i32 %24, %25
br i1 %26, label %33, label %27
27: ; preds = %22
%28 = load i32, i32* @stderr
%29 = load i8*, i8** %2
%30 = call i32 @fprintf(i32 %28, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i8* %29)
%31 = load i8*, i8** %2
%32 = call i32 @sqlite3_free(i8* %31)
br label %33
33: ; preds = %27, %22
%34 = load i32*, i32** %1
br label %35
35: ; preds = %33, %15
%36 = phi i32* [ null, %15 ], [ %34, %33 ]
ret i32* %36
}