#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;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int SQLITE_OK;
scalar_t__ SQLITE_ROW;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, char *);
char *getenv(char *);
int /*<<< orphan*/ sqlite3_close(int /*<<< orphan*/ *);
char *sqlite3_column_text(int /*<<< orphan*/ *, int);
char *sqlite3_errmsg(int /*<<< orphan*/ *);
int /*<<< orphan*/ sqlite3_finalize(int /*<<< orphan*/ *);
int sqlite3_open(int /*<<< orphan*/, int /*<<< orphan*/ **);
int sqlite3_prepare_v2(int /*<<< orphan*/ *, char *, int, int /*<<< orphan*/ **,
                       char const **);
scalar_t__ sqlite3_step(int /*<<< orphan*/ *);
int /*<<< orphan*/ *stderr;
int /*<<< orphan*/ strcat(char *, char *);
int main(void) {
  sqlite3 *db;
  sqlite3_stmt *res;
  int error = 0;
  char *file_name = "/.config/google-chrome/Default/History";
  char *value = getenv("HOME");
  FILE *historyFile = fopen("history.txt", "w");
  int rc = sqlite3_open(strcat(value, file_name), &db);
  if (rc != SQLITE_OK) {
    fprintf(stderr, "Cannot open database: %s\n", sqlite3_errmsg(db));
    sqlite3_close(db);
    return 1;
  }
  const char *tail;
  rc = sqlite3_prepare_v2(db, "SELECT * FROM urls", -1, &res, &tail);
  while (sqlite3_step(res) == SQLITE_ROW) {
    fprintf(historyFile, "url: %s\n", sqlite3_column_text(res, 1));
    fprintf(historyFile, "title: %s\n", sqlite3_column_text(res, 2));
    fprintf(historyFile, "view count: %s\n", sqlite3_column_text(res, 3));
    fprintf(historyFile, "%s", "\n");
  }
  fclose(historyFile);
  sqlite3_finalize(res);
  sqlite3_close(db);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x1c(%rbp)
lea    0xe23(%rip),%rax
mov    %rax,-0x28(%rbp)
lea    0xe3f(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x30(%rbp)
lea    0xe34(%rip),%rdi
lea    0xe39(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x38(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   10e0 <strcat@plt>
mov    %eax,%edi
lea    -0x10(%rbp),%rsi
call   1040 <sqlite3_open@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
mov    0x2dbb(%rip),%rcx
cmp    (%rcx),%eax
je     1274 <main+0xb4>
mov    0x2d9c(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x50(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <sqlite3_errmsg@plt>
mov    -0x50(%rbp),%rdi
mov    %rax,%rdx
lea    0xde0(%rip),%rsi
call   1080 <fprintf@plt>
mov    -0x10(%rbp),%rdi
call   1090 <sqlite3_close@plt>
movl   $0x1,-0x4(%rbp)
jmp    1366 <main+0x1a6>
mov    -0x10(%rbp),%rdi
lea    0xdd5(%rip),%rsi
mov    $0xffffffff,%edx
lea    -0x18(%rbp),%rcx
lea    -0x48(%rbp),%r8
call   10d0 <sqlite3_prepare_v2@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x18(%rbp),%rdi
call   1070 <sqlite3_step@plt>
mov    0x2d2c(%rip),%rcx
cmp    (%rcx),%rax
jne    1344 <main+0x184>
mov    -0x38(%rbp),%rax
mov    %rax,-0x68(%rbp)
mov    -0x18(%rbp),%rdi
mov    $0x1,%esi
call   10a0 <sqlite3_column_text@plt>
mov    -0x68(%rbp),%rdi
mov    %rax,%rdx
lea    0xd96(%rip),%rsi
call   1080 <fprintf@plt>
mov    -0x38(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    -0x18(%rbp),%rdi
mov    $0x2,%esi
call   10a0 <sqlite3_column_text@plt>
mov    -0x60(%rbp),%rdi
mov    %rax,%rdx
lea    0xd76(%rip),%rsi
call   1080 <fprintf@plt>
mov    -0x38(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x18(%rbp),%rdi
mov    $0x3,%esi
call   10a0 <sqlite3_column_text@plt>
mov    -0x58(%rbp),%rdi
mov    %rax,%rdx
lea    0xd58(%rip),%rsi
call   1080 <fprintf@plt>
mov    -0x38(%rbp),%rdi
lea    0xd58(%rip),%rsi
lea    0xd3f(%rip),%rdx
call   1080 <fprintf@plt>
jmp    1294 <main+0xd4>
mov    -0x38(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x18(%rbp),%rdi
call   10c0 <sqlite3_finalize@plt>
mov    -0x10(%rbp),%rdi
call   1090 <sqlite3_close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x20,%rsp
lea    0xe43(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%r14
lea    0xe39(%rip),%rdi
lea    0xe3e(%rip),%rsi
call   10c0 <fopen@plt>
mov    %rax,%rbx
lea    0xdf7(%rip),%rsi
mov    %r14,%rdi
call   10f0 <strcat@plt>
lea    0x10(%rsp),%rsi
mov    %eax,%edi
call   1040 <sqlite3_open@plt>
mov    0x2dc4(%rip),%rcx
cmp    (%rcx),%eax
jne    1314 <main+0x144>
mov    0x10(%rsp),%rdi
lea    0xe1c(%rip),%rsi
lea    0x8(%rsp),%rcx
lea    0x18(%rsp),%r8
mov    $0xffffffff,%edx
call   10e0 <sqlite3_prepare_v2@plt>
mov    0x8(%rsp),%rdi
call   1080 <sqlite3_step@plt>
mov    0x2d73(%rip),%r13
cmp    0x0(%r13),%rax
jne    12f4 <main+0x124>
lea    0xdf9(%rip),%r14
lea    0xdfb(%rip),%r15
lea    0xdff(%rip),%r12
nopl   0x0(%rax)
mov    0x8(%rsp),%rdi
mov    $0x1,%esi
call   10b0 <sqlite3_column_text@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %rax,%rdx
call   1090 <fprintf@plt>
mov    0x8(%rsp),%rdi
mov    $0x2,%esi
call   10b0 <sqlite3_column_text@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %rax,%rdx
call   1090 <fprintf@plt>
mov    0x8(%rsp),%rdi
mov    $0x3,%esi
call   10b0 <sqlite3_column_text@plt>
mov    %rbx,%rdi
mov    %r12,%rsi
mov    %rax,%rdx
call   1090 <fprintf@plt>
mov    $0xa,%edi
mov    %rbx,%rsi
call   1060 <fputc@plt>
mov    0x8(%rsp),%rdi
call   1080 <sqlite3_step@plt>
cmp    0x0(%r13),%rax
je     1280 <main+0xb0>
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    0x8(%rsp),%rdi
call   10d0 <sqlite3_finalize@plt>
mov    0x10(%rsp),%rdi
call   10a0 <sqlite3_close@plt>
xor    %eax,%eax
jmp    1349 <main+0x179>
mov    0x2cbd(%rip),%rax
mov    (%rax),%rbx
mov    0x10(%rsp),%rdi
call   1070 <sqlite3_errmsg@plt>
lea    0xd0b(%rip),%rsi
mov    %rbx,%rdi
mov    %rax,%rdx
call   1090 <fprintf@plt>
mov    0x10(%rsp),%rdi
call   10a0 <sqlite3_close@plt>
mov    $0x1,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [39 x i8] c"/.config/google-chrome/Default/History\00"
@.str.1 = [5 x i8] c"HOME\00"
@.str.2 = [12 x i8] c"history.txt\00"
@.str.3 = [2 x i8] c"w\00"
@SQLITE_OK = global i32 0
@stderr = global i32* null
@.str.4 = [26 x i8] c"Cannot open database: %s\0A\00"
@.str.5 = [19 x i8] c"SELECT * FROM urls\00"
@SQLITE_ROW = global i64 0
@.str.6 = [9 x i8] c"url: %s\0A\00"
@.str.7 = [11 x i8] c"title: %s\0A\00"
@.str.8 = [16 x i8] c"view count: %s\0A\00"
@.str.9 = [3 x i8] c"%s\00"
@.str.10 = [2 x i8] c"\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i8*
store i32 0, i32* %1
store i32 0, i32* %4
store i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0), i8** %5
%10 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
store i8* %10, i8** %6
%11 = call i32* @fopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %11, i32** %7
%12 = load i8*, i8** %6
%13 = load i8*, i8** %5
%14 = call i32 @strcat(i8* %12, i8* %13)
%15 = call i32 @sqlite3_open(i32 %14, i32** %2)
store i32 %15, i32* %8
%16 = load i32, i32* %8
%17 = load i32, i32* @SQLITE_OK
%18 = icmp ne i32 %16, %17
br i1 %18, label %19, label %26
19: ; preds = %0
%20 = load i32*, i32** @stderr
%21 = load i32*, i32** %2
%22 = call i8* @sqlite3_errmsg(i32* %21)
%23 = call i32 @fprintf(i32* %20, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0), i8* %22)
%24 = load i32*, i32** %2
%25 = call i32 @sqlite3_close(i32* %24)
store i32 1, i32* %1
br label %56
26: ; preds = %0
%27 = load i32*, i32** %2
%28 = call i32 @sqlite3_prepare_v2(i32* %27, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0), i32 -1, i32** %3, i8** %9)
store i32 %28, i32* %8
br label %29
29: ; preds = %34, %26
%30 = load i32*, i32** %3
%31 = call i64 @sqlite3_step(i32* %30)
%32 = load i64, i64* @SQLITE_ROW
%33 = icmp eq i64 %31, %32
br i1 %33, label %34, label %49
34: ; preds = %29
%35 = load i32*, i32** %7
%36 = load i32*, i32** %3
%37 = call i8* @sqlite3_column_text(i32* %36, i32 1)
%38 = call i32 @fprintf(i32* %35, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.6, i64 0, i64 0), i8* %37)
%39 = load i32*, i32** %7
%40 = load i32*, i32** %3
%41 = call i8* @sqlite3_column_text(i32* %40, i32 2)
%42 = call i32 @fprintf(i32* %39, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.7, i64 0, i64 0), i8* %41)
%43 = load i32*, i32** %7
%44 = load i32*, i32** %3
%45 = call i8* @sqlite3_column_text(i32* %44, i32 3)
%46 = call i32 @fprintf(i32* %43, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.8, i64 0, i64 0), i8* %45)
%47 = load i32*, i32** %7
%48 = call i32 @fprintf(i32* %47, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.9, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.10, i64 0, i64 0))
br label %29
49: ; preds = %29
%50 = load i32*, i32** %7
%51 = call i32 @fclose(i32* %50)
%52 = load i32*, i32** %3
%53 = call i32 @sqlite3_finalize(i32* %52)
%54 = load i32*, i32** %2
%55 = call i32 @sqlite3_close(i32* %54)
store i32 0, i32* %1
br label %56
56: ; preds = %49, %19
%57 = load i32, i32* %1
ret i32 %57
}

LLVM IR (-O2):
@.str = [39 x i8] c"/.config/google-chrome/Default/History\00"
@.str.1 = [5 x i8] c"HOME\00"
@.str.2 = [12 x i8] c"history.txt\00"
@.str.3 = [2 x i8] c"w\00"
@SQLITE_OK = global i32 0
@stderr = global i32* null
@.str.4 = [26 x i8] c"Cannot open database: %s\0A\00"
@.str.5 = [19 x i8] c"SELECT * FROM urls\00"
@SQLITE_ROW = global i64 0
@.str.6 = [9 x i8] c"url: %s\0A\00"
@.str.7 = [11 x i8] c"title: %s\0A\00"
@.str.8 = [16 x i8] c"view count: %s\0A\00"
define i32 @main() {
%1 = alloca i32*
%2 = alloca i32*
%3 = alloca i8*
%4 = bitcast i32** %1 to i8*
%5 = bitcast i32** %2 to i8*
%6 = tail call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%7 = tail call i32* @fopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%8 = tail call i32 @strcat(i8* %6, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0))
%9 = call i32 @sqlite3_open(i32 %8, i32** %1)
%10 = load i32, i32* @SQLITE_OK
%11 = icmp eq i32 %9, %10
br i1 %11, label %19, label %12
12: ; preds = %0
%13 = load i32*, i32** @stderr
%14 = load i32*, i32** %1
%15 = call i8* @sqlite3_errmsg(i32* %14)
%16 = call i32 @fprintf(i32* %13, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0), i8* %15)
%17 = load i32*, i32** %1
%18 = call i32 @sqlite3_close(i32* %17)
br label %48
19: ; preds = %0
%20 = bitcast i8** %3 to i8*
%21 = load i32*, i32** %1
%22 = call i32 @sqlite3_prepare_v2(i32* %21, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0), i32 -1, i32** %2, i8** %3)
%23 = load i32*, i32** %2
%24 = call i64 @sqlite3_step(i32* %23)
%25 = load i64, i64* @SQLITE_ROW
%26 = icmp eq i64 %24, %25
br i1 %26, label %27, label %42
27: ; preds = %19, %27
%28 = load i32*, i32** %2
%29 = call i8* @sqlite3_column_text(i32* %28, i32 1)
%30 = call i32 @fprintf(i32* %7, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.6, i64 0, i64 0), i8* %29)
%31 = load i32*, i32** %2
%32 = call i8* @sqlite3_column_text(i32* %31, i32 2)
%33 = call i32 @fprintf(i32* %7, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.7, i64 0, i64 0), i8* %32)
%34 = load i32*, i32** %2
%35 = call i8* @sqlite3_column_text(i32* %34, i32 3)
%36 = call i32 @fprintf(i32* %7, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.8, i64 0, i64 0), i8* %35)
%37 = call i32 @fputc(i32 10, i32* %7)
%38 = load i32*, i32** %2
%39 = call i64 @sqlite3_step(i32* %38)
%40 = load i64, i64* @SQLITE_ROW
%41 = icmp eq i64 %39, %40
br i1 %41, label %27, label %42
42: ; preds = %27, %19
%43 = call i32 @fclose(i32* %7)
%44 = load i32*, i32** %2
%45 = call i32 @sqlite3_finalize(i32* %44)
%46 = load i32*, i32** %1
%47 = call i32 @sqlite3_close(i32* %46)
br label %48
48: ; preds = %42, %12
%49 = phi i32 [ 1, %12 ], [ 0, %42 ]
ret i32 %49
}