#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 /*<<< orphan*/ EXIT_FAILURE;
int PATH_MAX;
int SQLITE_OPEN_CREATE;
int SQLITE_OPEN_READWRITE;
int /*<<< orphan*/ database_sql_exec(int /*<<< orphan*/ *, char *,
                                     int /*<<< orphan*/, int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
scalar_t__ getenv(char *);
int /*<<< orphan*/ sprintf(char *, char *, char const *);
int sqlite3_open_v2(char *, int /*<<< orphan*/ **, int, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcpy(char *, char *);
sqlite3 *database_init(void) {
  sqlite3 *db;
  int result;
  char *sql;
  const char *home;
  char database_file_name[PATH_MAX];
  home = (const char *)getenv("HOME");
  if (home)
    sprintf(database_file_name, "%s/.cmdrec.db", home);
  else
    strcpy(database_file_name, "cmdrec.db");
  result = sqlite3_open_v2(database_file_name, &db,
                           SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, 0);
  if (!db) {
    fprintf(stderr, "sqlite3_open_v2(): error result = %d\n", result);
    exit(EXIT_FAILURE);
  }
  database_sql_exec(
      db,
      "CREATE TABLE cmdrec (file text primary key on conflict replace, cmd "
      "text, cwd text, args text, function text, pid text, ppid text);",
      0, 1);
  database_sql_exec(db,
                    "CREATE TABLE srcinc (header text primary key on conflict "
                    "replace, source text);",
                    0, 1);
  return db;
}


x86 assembly (clang, -O0):
<database_init>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    0x2e71(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x28(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x38(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x30(%rbp)
lea    0xe55(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11d8 <database_init+0x68>
mov    -0x38(%rbp),%rdi
mov    -0x20(%rbp),%rdx
lea    0xe37(%rip),%rsi
call   1070 <sprintf@plt>
jmp    11e8 <database_init+0x78>
mov    -0x38(%rbp),%rdi
lea    0xe26(%rip),%rsi
call   1050 <strcpy@plt>
mov    -0x38(%rbp),%rdi
mov    0x2df5(%rip),%rax
mov    (%rax),%edx
mov    0x2ddc(%rip),%rax
or     (%rax),%edx
lea    -0x8(%rbp),%rsi
xor    %ecx,%ecx
call   1080 <sqlite3_open_v2@plt>
mov    %eax,-0xc(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    123d <database_init+0xcd>
mov    0x2daa(%rip),%rax
mov    (%rax),%edi
mov    -0xc(%rbp),%edx
lea    0xde9(%rip),%rsi
call   1060 <fprintf@plt>
mov    0x2d8a(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    -0x8(%rbp),%rdi
lea    0xdf1(%rip),%rsi
xor    %edx,%edx
mov    $0x1,%ecx
call   1040 <database_sql_exec@plt>
mov    -0x8(%rbp),%rdi
lea    0xe5e(%rip),%rsi
xor    %edx,%edx
mov    $0x1,%ecx
call   1040 <database_sql_exec@plt>
mov    -0x8(%rbp),%rax
mov    -0x28(%rbp),%rcx
mov    %rcx,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<database_init>:
push   %rbp
mov    %rsp,%rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsp,%r14
mov    0x2e6b(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
lea    0xe61(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     11bd <database_init+0x4d>
lea    0xe55(%rip),%rsi
mov    %rbx,%rdi
mov    %rax,%rdx
call   1070 <sprintf@plt>
jmp    11cc <database_init+0x5c>
lea    0xe45(%rip),%rsi
mov    %rbx,%rdi
call   1050 <strcpy@plt>
mov    0x2e15(%rip),%rax
mov    0x2dfe(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
lea    -0x18(%rbp),%rsi
mov    %rbx,%rdi
xor    %ecx,%ecx
call   1080 <sqlite3_open_v2@plt>
mov    -0x18(%rbp),%rdi
test   %rdi,%rdi
je     122f <database_init+0xbf>
lea    0xe3d(%rip),%rsi
xor    %edx,%edx
mov    $0x1,%ecx
call   1040 <database_sql_exec@plt>
mov    -0x18(%rbp),%rdi
lea    0xeaa(%rip),%rsi
xor    %edx,%edx
mov    $0x1,%ecx
call   1040 <database_sql_exec@plt>
mov    -0x18(%rbp),%rax
mov    %r14,%rsp
lea    -0x10(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2d92(%rip),%rcx
mov    (%rcx),%edi
lea    0xdd4(%rip),%rsi
mov    %eax,%edx
call   1060 <fprintf@plt>
mov    0x2d73(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@PATH_MAX = global i32 0
@.str = [5 x i8] c"HOME\00"
@.str.1 = [14 x i8] c"%s/.cmdrec.db\00"
@.str.2 = [10 x i8] c"cmdrec.db\00"
@SQLITE_OPEN_READWRITE = global i32 0
@SQLITE_OPEN_CREATE = global i32 0
@stderr = global i32 0
@.str.3 = [38 x i8] c"sqlite3_open_v2(): error result = %d\0A\00"
@EXIT_FAILURE = global i32 0
@.str.4 = [132 x i8] c"CREATE TABLE cmdrec (file text primary key on conflict replace, cmd text, cwd text, args text, function text, pid text, ppid text);\00"
@.str.5 = [80 x i8] c"CREATE TABLE srcinc (header text primary key on conflict replace, source text);\00"
define i32* @database_init() {
%1 = alloca i32*
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = load i32, i32* @PATH_MAX
%8 = zext i32 %7 to i64
%9 = call i8* @llvm.stacksave()
store i8* %9, i8** %5
%10 = alloca i8, i64 %8
store i64 %8, i64* %6
%11 = call i64 @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%12 = inttoptr i64 %11 to i8*
store i8* %12, i8** %4
%13 = load i8*, i8** %4
%14 = icmp ne i8* %13, null
br i1 %14, label %15, label %18
15: ; preds = %0
%16 = load i8*, i8** %4
%17 = call i32 @sprintf(i8* %10, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i8* %16)
br label %20
18: ; preds = %0
%19 = call i32 @strcpy(i8* %10, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
br label %20
20: ; preds = %18, %15
%21 = load i32, i32* @SQLITE_OPEN_READWRITE
%22 = load i32, i32* @SQLITE_OPEN_CREATE
%23 = or i32 %21, %22
%24 = call i32 @sqlite3_open_v2(i8* %10, i32** %1, i32 %23, i32 0)
store i32 %24, i32* %2
%25 = load i32*, i32** %1
%26 = icmp ne i32* %25, null
br i1 %26, label %33, label %27
27: ; preds = %20
%28 = load i32, i32* @stderr
%29 = load i32, i32* %2
%30 = call i32 @fprintf(i32 %28, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.3, i64 0, i64 0), i32 %29)
%31 = load i32, i32* @EXIT_FAILURE
%32 = call i32 @exit(i32 %31)
unreachable
33: ; preds = %20
%34 = load i32*, i32** %1
%35 = call i32 @database_sql_exec(i32* %34, i8* getelementptr inbounds ([132 x i8], [132 x i8]* @.str.4, i64 0, i64 0), i32 0, i32 1)
%36 = load i32*, i32** %1
%37 = call i32 @database_sql_exec(i32* %36, i8* getelementptr inbounds ([80 x i8], [80 x i8]* @.str.5, i64 0, i64 0), i32 0, i32 1)
%38 = load i32*, i32** %1
%39 = load i8*, i8** %5
ret i32* %38
}

LLVM IR (-O2):
@PATH_MAX = global i32 0
@.str = [5 x i8] c"HOME\00"
@.str.1 = [14 x i8] c"%s/.cmdrec.db\00"
@.str.2 = [10 x i8] c"cmdrec.db\00"
@SQLITE_OPEN_READWRITE = global i32 0
@SQLITE_OPEN_CREATE = global i32 0
@stderr = global i32 0
@.str.3 = [38 x i8] c"sqlite3_open_v2(): error result = %d\0A\00"
@EXIT_FAILURE = global i32 0
@.str.4 = [132 x i8] c"CREATE TABLE cmdrec (file text primary key on conflict replace, cmd text, cwd text, args text, function text, pid text, ppid text);\00"
@.str.5 = [80 x i8] c"CREATE TABLE srcinc (header text primary key on conflict replace, source text);\00"
define i32* @database_init() {
%1 = alloca i32*
%2 = bitcast i32** %1 to i8*
%3 = load i32, i32* @PATH_MAX
%4 = zext i32 %3 to i64
%5 = tail call i8* @llvm.stacksave()
%6 = alloca i8, i64 %4
%7 = tail call i64 @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%8 = icmp eq i64 %7, 0
br i1 %8, label %12, label %9
9: ; preds = %0
%10 = inttoptr i64 %7 to i8*
%11 = call i32 @sprintf(i8* %6, i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i8* %10)
br label %14
12: ; preds = %0
%13 = call i32 @strcpy(i8* %6, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
br label %14
14: ; preds = %12, %9
%15 = load i32, i32* @SQLITE_OPEN_READWRITE
%16 = load i32, i32* @SQLITE_OPEN_CREATE
%17 = or i32 %16, %15
%18 = call i32 @sqlite3_open_v2(i8* %6, i32** %1, i32 %17, i32 0)
%19 = load i32*, i32** %1
%20 = icmp eq i32* %19, null
br i1 %20, label %21, label %26
21: ; preds = %14
%22 = load i32, i32* @stderr
%23 = call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.3, i64 0, i64 0), i32 %18)
%24 = load i32, i32* @EXIT_FAILURE
%25 = call i32 @exit(i32 %24)
unreachable
26: ; preds = %14
%27 = call i32 @database_sql_exec(i32* %19, i8* getelementptr inbounds ([132 x i8], [132 x i8]* @.str.4, i64 0, i64 0), i32 0, i32 1)
%28 = load i32*, i32** %1
%29 = call i32 @database_sql_exec(i32* %28, i8* getelementptr inbounds ([80 x i8], [80 x i8]* @.str.5, i64 0, i64 0), i32 0, i32 1)
%30 = load i32*, i32** %1
ret i32* %30
}