#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*/ SQLITE_OPEN_READONLY;
int /*<<< orphan*/ db_close();
int /*<<< orphan*/ db_open();
int /*<<< orphan*/ import_db(int /*<<< orphan*/ **);
int /*<<< orphan*/ print_help(char *);
int /*<<< orphan*/ printf(char *, char *, ...);
int /*<<< orphan*/ sqlite3_close(int /*<<< orphan*/ *);
int sqlite3_open_v2(char *, int /*<<< orphan*/ **, int /*<<< orphan*/,
                    int /*<<< orphan*/ *);
int /*<<< orphan*/ validate_imported_db(int /*<<< orphan*/ *);
int main(int argc, char *argv[]) {
  sqlite3 *imp_db = NULL;
  if (argc < 2) {
    print_help(argv[0]);
    return -1;
  }
  int ret = sqlite3_open_v2(argv[1], &imp_db, SQLITE_OPEN_READONLY, NULL);
  if (ret != SQLITE_OK || imp_db == NULL) {
    printf("Could not open database %s: open returned %d\n", argv[1], ret);
    return -1;
  }
  if (!validate_imported_db(imp_db)) {
    printf("error: %s is not a valid OWL database\n", argv[1]);
    goto end;
  };
  db_open();
  import_db(&imp_db);
  sqlite3_close(imp_db);
  imp_db = NULL;
end:
  if (imp_db)
    sqlite3_close(imp_db);
  db_close();
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11c0 <main+0x40>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1090 <print_help@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1293 <main+0x113>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e19(%rip),%rax
mov    (%rax),%edx
lea    -0x18(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
call   10a0 <sqlite3_open_v2@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    0x2df5(%rip),%rcx
cmp    (%rcx),%eax
jne    11fe <main+0x7e>
cmpq   $0x0,-0x18(%rbp)
jne    1223 <main+0xa3>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
mov    -0x1c(%rbp),%edx
lea    0xdf0(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1293 <main+0x113>
mov    -0x18(%rbp),%rdi
call   1050 <validate_imported_db@plt>
cmp    $0x0,%eax
jne    1250 <main+0xd0>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xdea(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1271 <main+0xf1>
mov    $0x0,%al
call   1080 <db_open@plt>
lea    -0x18(%rbp),%rdi
call   1030 <import_db@plt>
mov    -0x18(%rbp),%rdi
call   1070 <sqlite3_close@plt>
movq   $0x0,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1285 <main+0x105>
mov    -0x18(%rbp),%rdi
call   1070 <sqlite3_close@plt>
mov    $0x0,%al
call   1060 <db_close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
movq   $0x0,0x8(%rsp)
cmp    $0x1,%edi
jg     11a8 <main+0x28>
mov    (%rbx),%rdi
call   1090 <print_help@plt>
mov    $0xffffffff,%ebx
jmp    124a <main+0xca>
mov    0x8(%rbx),%rdi
mov    0x2e35(%rip),%rax
mov    (%rax),%edx
lea    0x8(%rsp),%rsi
xor    %ecx,%ecx
call   10a0 <sqlite3_open_v2@plt>
mov    0x2e18(%rip),%rcx
cmp    (%rcx),%eax
jne    1205 <main+0x85>
mov    0x8(%rsp),%rdi
test   %rdi,%rdi
je     1205 <main+0x85>
call   1050 <validate_imported_db@plt>
test   %eax,%eax
je     1220 <main+0xa0>
xor    %eax,%eax
call   1080 <db_open@plt>
lea    0x8(%rsp),%rdi
call   1030 <import_db@plt>
mov    0x8(%rsp),%rdi
call   1070 <sqlite3_close@plt>
movq   $0x0,0x8(%rsp)
jmp    1241 <main+0xc1>
mov    0x8(%rbx),%rsi
lea    0xdf0(%rip),%rdi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0xffffffff,%ebx
jmp    124a <main+0xca>
mov    0x8(%rbx),%rsi
lea    0xe03(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x8(%rsp),%rdi
test   %rdi,%rdi
je     1241 <main+0xc1>
call   1070 <sqlite3_close@plt>
xor    %ebx,%ebx
xor    %eax,%eax
call   1060 <db_close@plt>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@SQLITE_OPEN_READONLY = global i32 0
@SQLITE_OK = global i32 0
@.str = [46 x i8] c"Could not open database %s: open returned %d\0A\00"
@.str.1 = [39 x i8] c"error: %s is not a valid OWL database\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32* null, i32** %6
%8 = load i32, i32* %4
%9 = icmp slt i32 %8, 2
br i1 %9, label %10, label %15
10: ; preds = %2
%11 = load i8**, i8*** %5
%12 = getelementptr inbounds i8*, i8** %11, i64 0
%13 = load i8*, i8** %12
%14 = call i32 @print_help(i8* %13)
store i32 -1, i32* %3
br label %55
15: ; preds = %2
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 1
%18 = load i8*, i8** %17
%19 = load i32, i32* @SQLITE_OPEN_READONLY
%20 = call i32 @sqlite3_open_v2(i8* %18, i32** %6, i32 %19, i32* null)
store i32 %20, i32* %7
%21 = load i32, i32* %7
%22 = load i32, i32* @SQLITE_OK
%23 = icmp ne i32 %21, %22
br i1 %23, label %27, label %24
24: ; preds = %15
%25 = load i32*, i32** %6
%26 = icmp eq i32* %25, null
br i1 %26, label %27, label %33
27: ; preds = %24, %15
%28 = load i8**, i8*** %5
%29 = getelementptr inbounds i8*, i8** %28, i64 1
%30 = load i8*, i8** %29
%31 = load i32, i32* %7
%32 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str, i64 0, i64 0), i8* %30, i32 %31)
store i32 -1, i32* %3
br label %55
33: ; preds = %24
%34 = load i32*, i32** %6
%35 = call i32 @validate_imported_db(i32* %34)
%36 = icmp ne i32 %35, 0
br i1 %36, label %42, label %37
37: ; preds = %33
%38 = load i8**, i8*** %5
%39 = getelementptr inbounds i8*, i8** %38, i64 1
%40 = load i8*, i8** %39
%41 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i8* %40)
br label %47
42: ; preds = %33
%43 = call i32 (...) @db_open()
%44 = call i32 @import_db(i32** %6)
%45 = load i32*, i32** %6
%46 = call i32 @sqlite3_close(i32* %45)
store i32* null, i32** %6
br label %47
47: ; preds = %42, %37
%48 = load i32*, i32** %6
%49 = icmp ne i32* %48, null
br i1 %49, label %50, label %53
50: ; preds = %47
%51 = load i32*, i32** %6
%52 = call i32 @sqlite3_close(i32* %51)
br label %53
53: ; preds = %50, %47
%54 = call i32 (...) @db_close()
store i32 0, i32* %3
br label %55
55: ; preds = %53, %27, %10
%56 = load i32, i32* %3
ret i32 %56
}

LLVM IR (-O2):
@SQLITE_OPEN_READONLY = global i32 0
@SQLITE_OK = global i32 0
@.str = [46 x i8] c"Could not open database %s: open returned %d\0A\00"
@.str.1 = [39 x i8] c"error: %s is not a valid OWL database\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32*
%4 = bitcast i32** %3 to i8*
store i32* null, i32** %3
%5 = icmp slt i32 %0, 2
br i1 %5, label %6, label %9
6: ; preds = %2
%7 = load i8*, i8** %1
%8 = tail call i32 @print_help(i8* %7)
br label %39
9: ; preds = %2
%10 = getelementptr inbounds i8*, i8** %1, i64 1
%11 = load i8*, i8** %10
%12 = load i32, i32* @SQLITE_OPEN_READONLY
%13 = call i32 @sqlite3_open_v2(i8* %11, i32** %3, i32 %12, i32* null)
%14 = load i32, i32* @SQLITE_OK
%15 = icmp ne i32 %13, %14
%16 = load i32*, i32** %3
%17 = icmp eq i32* %16, null
%18 = select i1 %15, i1 true, i1 %17
br i1 %18, label %19, label %22
19: ; preds = %9
%20 = load i8*, i8** %10
%21 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str, i64 0, i64 0), i8* %20, i32 %13)
br label %39
22: ; preds = %9
%23 = call i32 @validate_imported_db(i32* %16)
%24 = icmp eq i32 %23, 0
br i1 %24, label %30, label %25
25: ; preds = %22
%26 = call i32 (...) @db_open()
%27 = call i32 @import_db(i32** %3)
%28 = load i32*, i32** %3
%29 = call i32 @sqlite3_close(i32* %28)
store i32* null, i32** %3
br label %37
30: ; preds = %22
%31 = load i8*, i8** %10
%32 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i8* %31)
%33 = load i32*, i32** %3
%34 = icmp eq i32* %33, null
br i1 %34, label %37, label %35
35: ; preds = %30
%36 = call i32 @sqlite3_close(i32* %33)
br label %37
37: ; preds = %25, %35, %30
%38 = call i32 (...) @db_close()
br label %39
39: ; preds = %19, %37, %6
%40 = phi i32 [ -1, %6 ], [ -1, %19 ], [ 0, %37 ]
ret i32 %40
}