#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*/ cwd;

/* Variables and functions */
int LOG_INFO;
int LOG_LOCAL6;
int SQLITE_OK;
int SQLITE_ROW;
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ *getcwd(char *, int);
scalar_t__ malloc(int);
int /*<<< orphan*/ snprintf(char *, int, char *, char const *, ...);
int /*<<< orphan*/ sqlite3_bind_int(int /*<<< orphan*/ *, int, size_t);
int /*<<< orphan*/ sqlite3_close(int /*<<< orphan*/ *);
size_t sqlite3_column_int(int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ sqlite3_errmsg(int /*<<< orphan*/ *);
int /*<<< orphan*/ sqlite3_finalize(int /*<<< orphan*/ *);
int sqlite3_open(char *, int /*<<< orphan*/ **);
int /*<<< orphan*/ sqlite3_prepare_v2(int /*<<< orphan*/ *, char *,
                                      int /*<<< orphan*/, int /*<<< orphan*/ **,
                                      int /*<<< orphan*/ *);
int sqlite3_step(int /*<<< orphan*/ *);
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ syslog(int, char *, char *, int /*<<< orphan*/);
size_t sqlite_get_detected_hosts_row_ix_by_host_ix(size_t ip_addr_ix,
                                                   const char *db_loc) {
  char cwd[512 / 2];
  char DB_LOCATION[512 + 1];
  if (db_loc) {
    snprintf(DB_LOCATION, 512, "%s", db_loc);
  } else {
    if (getcwd(cwd, sizeof(cwd)) == NULL) {
      return 1;
    } else {
      snprintf(DB_LOCATION, 512, "%s%s", cwd, "/db/gargoyle_attack_detect.db");
    }
  }
  sqlite3 *db;
  sqlite3_stmt *stmt;
  int rc;
  size_t return_val = 0;
  char *sql = (char *)malloc(512);
  rc = sqlite3_open(DB_LOCATION, &db);
  if (rc != SQLITE_OK) {
    syslog(LOG_INFO | LOG_LOCAL6,
           "ERROR opening SQLite DB '%s' from function "
           "[sqlite_get_detected_hosts_row_ix_by_host_ix]: %s",
           DB_LOCATION, sqlite3_errmsg(db));
    free(sql);
    return 1;
  }
  snprintf(sql, 512, "SELECT ix FROM %s WHERE host_ix = ?1", "detected_hosts");
  sqlite3_prepare_v2(db, sql, strlen(sql), &stmt, NULL);
  sqlite3_bind_int(stmt, 1, ip_addr_ix);
  while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
    return_val = sqlite3_column_int(stmt, 0);
  }
  sqlite3_finalize(stmt);
  sqlite3_close(db);
  free(sql);
  return return_val;
}


x86 assembly (clang, -O0):
<sqlite_get_detected_hosts_row_ix_by_host_ix>:
push   %rbp
mov    %rsp,%rbp
sub    $0x380,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1221 <sqlite_get_detected_hosts_row_ix_by_host_ix+0x41>
lea    -0x330(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    $0x200,%esi
lea    0xe68(%rip),%rdx
mov    $0x0,%al
call   1070 <snprintf@plt>
jmp    1276 <sqlite_get_detected_hosts_row_ix_by_host_ix+0x96>
lea    -0x120(%rbp),%rdi
mov    $0x100,%esi
call   1080 <getcwd@plt>
cmp    $0x0,%rax
jne    1249 <sqlite_get_detected_hosts_row_ix_by_host_ix+0x69>
movq   $0x1,-0x8(%rbp)
jmp    140f <sqlite_get_detected_hosts_row_ix_by_host_ix+0x22f>
lea    -0x330(%rbp),%rdi
lea    -0x120(%rbp),%rcx
mov    $0x200,%esi
lea    0xd9d(%rip),%rdx
lea    0xd9b(%rip),%r8
mov    $0x0,%al
call   1070 <snprintf@plt>
jmp    1276 <sqlite_get_detected_hosts_row_ix_by_host_ix+0x96>
movq   $0x0,-0x350(%rbp)
mov    $0x200,%edi
call   10d0 <malloc@plt>
mov    %rax,-0x358(%rbp)
lea    -0x330(%rbp),%rdi
lea    -0x338(%rbp),%rsi
call   1040 <sqlite3_open@plt>
mov    %eax,-0x344(%rbp)
mov    -0x344(%rbp),%eax
mov    0x2d28(%rip),%rcx
cmp    (%rcx),%eax
je     1326 <sqlite_get_detected_hosts_row_ix_by_host_ix+0x146>
mov    0x2d01(%rip),%rax
mov    (%rax),%eax
mov    0x2d18(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x364(%rbp)
lea    -0x330(%rbp),%rax
mov    %rax,-0x360(%rbp)
mov    -0x338(%rbp),%rdi
call   10a0 <sqlite3_errmsg@plt>
mov    -0x364(%rbp),%edi
mov    -0x360(%rbp),%rdx
mov    %eax,%ecx
lea    0xd1b(%rip),%rsi
call   10e0 <syslog@plt>
mov    -0x358(%rbp),%rdi
call   1030 <free@plt>
movq   $0x1,-0x8(%rbp)
jmp    140f <sqlite_get_detected_hosts_row_ix_by_host_ix+0x22f>
mov    -0x358(%rbp),%rdi
mov    $0x200,%esi
lea    0xd47(%rip),%rdx
lea    0xd65(%rip),%rcx
mov    $0x0,%al
call   1070 <snprintf@plt>
mov    -0x338(%rbp),%rax
mov    %rax,-0x378(%rbp)
mov    -0x358(%rbp),%rax
mov    %rax,-0x370(%rbp)
mov    -0x358(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0x378(%rbp),%rdi
mov    -0x370(%rbp),%rsi
mov    %eax,%edx
lea    -0x340(%rbp),%rcx
xor    %eax,%eax
mov    %eax,%r8d
call   1100 <sqlite3_prepare_v2@plt>
mov    -0x340(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0x1,%esi
call   1050 <sqlite3_bind_int@plt>
mov    -0x340(%rbp),%rdi
call   10b0 <sqlite3_step@plt>
mov    %eax,-0x344(%rbp)
mov    0x2c12(%rip),%rcx
cmp    (%rcx),%eax
jne    13e0 <sqlite_get_detected_hosts_row_ix_by_host_ix+0x200>
mov    -0x340(%rbp),%rdi
xor    %esi,%esi
call   1090 <sqlite3_column_int@plt>
mov    %rax,-0x350(%rbp)
jmp    13a5 <sqlite_get_detected_hosts_row_ix_by_host_ix+0x1c5>
mov    -0x340(%rbp),%rdi
call   10f0 <sqlite3_finalize@plt>
mov    -0x338(%rbp),%rdi
call   10c0 <sqlite3_close@plt>
mov    -0x358(%rbp),%rdi
call   1030 <free@plt>
mov    -0x350(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x380,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sqlite_get_detected_hosts_row_ix_by_host_ix>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x318,%rsp
mov    %rdi,%r14
test   %rsi,%rsi
je     1216 <sqlite_get_detected_hosts_row_ix_by_host_ix+0x36>
mov    %rsi,%rcx
lea    0xe7d(%rip),%rdx
lea    0x110(%rsp),%rdi
mov    $0x200,%esi
xor    %eax,%eax
call   1070 <snprintf@plt>
jmp    1255 <sqlite_get_detected_hosts_row_ix_by_host_ix+0x75>
lea    0x10(%rsp),%rdi
mov    $0x100,%esi
call   1080 <getcwd@plt>
test   %rax,%rax
je     1374 <sqlite_get_detected_hosts_row_ix_by_host_ix+0x194>
lea    0xdcb(%rip),%rdx
lea    0xdc9(%rip),%r8
lea    0x110(%rsp),%rdi
lea    0x10(%rsp),%rcx
mov    $0x200,%esi
xor    %eax,%eax
call   1070 <snprintf@plt>
mov    $0x200,%edi
call   10d0 <malloc@plt>
mov    %rax,%r15
lea    0x110(%rsp),%rdi
lea    0x8(%rsp),%rsi
call   1040 <sqlite3_open@plt>
mov    0x2d65(%rip),%rcx
cmp    (%rcx),%eax
jne    1321 <sqlite_get_detected_hosts_row_ix_by_host_ix+0x141>
lea    0xdf6(%rip),%rdx
lea    0xe14(%rip),%rcx
xor    %ebx,%ebx
mov    %r15,%rdi
mov    $0x200,%esi
xor    %eax,%eax
call   1070 <snprintf@plt>
mov    0x8(%rsp),%r12
mov    %r15,%rdi
call   1060 <strlen@plt>
mov    %rsp,%rcx
mov    %r12,%rdi
mov    %r15,%rsi
mov    %eax,%edx
xor    %r8d,%r8d
call   1100 <sqlite3_prepare_v2@plt>
mov    (%rsp),%rdi
mov    $0x1,%esi
mov    %r14,%rdx
call   1050 <sqlite3_bind_int@plt>
mov    (%rsp),%rdi
call   10b0 <sqlite3_step@plt>
mov    0x2ced(%rip),%r14
cmp    (%r14),%eax
jne    130c <sqlite_get_detected_hosts_row_ix_by_host_ix+0x12c>
nopl   0x0(%rax,%rax,1)
mov    (%rsp),%rdi
xor    %esi,%esi
call   1090 <sqlite3_column_int@plt>
mov    %rax,%rbx
mov    (%rsp),%rdi
call   10b0 <sqlite3_step@plt>
cmp    (%r14),%eax
je     12f0 <sqlite_get_detected_hosts_row_ix_by_host_ix+0x110>
mov    (%rsp),%rdi
call   10f0 <sqlite3_finalize@plt>
mov    0x8(%rsp),%rdi
call   10c0 <sqlite3_close@plt>
jmp    135a <sqlite_get_detected_hosts_row_ix_by_host_ix+0x17a>
mov    0x2ca0(%rip),%rax
mov    0x2cb9(%rip),%rcx
mov    (%rcx),%ebx
or     (%rax),%ebx
mov    0x8(%rsp),%rdi
call   10a0 <sqlite3_errmsg@plt>
lea    0xcdf(%rip),%rsi
lea    0x110(%rsp),%rdx
mov    %ebx,%edi
mov    %eax,%ecx
call   10e0 <syslog@plt>
mov    $0x1,%ebx
mov    %r15,%rdi
call   1030 <free@plt>
mov    %rbx,%rax
add    $0x318,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    $0x1,%ebx
jmp    1362 <sqlite_get_detected_hosts_row_ix_by_host_ix+0x182>

LLVM IR (-O0):
@.str = [3 x i8] c"%s\00"
@.str.1 = [5 x i8] c"%s%s\00"
@.str.2 = [30 x i8] c"/db/gargoyle_attack_detect.db\00"
@SQLITE_OK = global i32 0
@LOG_INFO = global i32 0
@LOG_LOCAL6 = global i32 0
@.str.3 = [93 x i8] c"ERROR opening SQLite DB '%s' from function [sqlite_get_detected_hosts_row_ix_by_host_ix]: %s\00"
@.str.4 = [37 x i8] c"SELECT ix FROM %s WHERE host_ix = ?1\00"
@.str.5 = [15 x i8] c"detected_hosts\00"
@SQLITE_ROW = global i32 0
define i64 @sqlite_get_detected_hosts_row_ix_by_host_ix(i64 %0, i8* %1) {
%3 = alloca i64
%4 = alloca i64
%5 = alloca i8*
%6 = alloca [256 x i8]
%7 = alloca [513 x i8]
%8 = alloca i32*
%9 = alloca i32*
%10 = alloca i32
%11 = alloca i64
%12 = alloca i8*
store i64 %0, i64* %4
store i8* %1, i8** %5
%13 = load i8*, i8** %5
%14 = icmp ne i8* %13, null
br i1 %14, label %15, label %19
15: ; preds = %2
%16 = getelementptr inbounds [513 x i8], [513 x i8]* %7, i64 0, i64 0
%17 = load i8*, i8** %5
%18 = call i32 (i8*, i32, i8*, i8*, ...) @snprintf(i8* %16, i32 512, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %17)
br label %29
19: ; preds = %2
%20 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%21 = call i32* @getcwd(i8* %20, i32 256)
%22 = icmp eq i32* %21, null
br i1 %22, label %23, label %24
23: ; preds = %19
store i64 1, i64* %3
br label %74
24: ; preds = %19
%25 = getelementptr inbounds [513 x i8], [513 x i8]* %7, i64 0, i64 0
%26 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%27 = call i32 (i8*, i32, i8*, i8*, ...) @snprintf(i8* %25, i32 512, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %26, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0))
br label %28
28: ; preds = %24
br label %29
29: ; preds = %28, %15
store i64 0, i64* %11
%30 = call i64 @malloc(i32 512)
%31 = inttoptr i64 %30 to i8*
store i8* %31, i8** %12
%32 = getelementptr inbounds [513 x i8], [513 x i8]* %7, i64 0, i64 0
%33 = call i32 @sqlite3_open(i8* %32, i32** %8)
store i32 %33, i32* %10
%34 = load i32, i32* %10
%35 = load i32, i32* @SQLITE_OK
%36 = icmp ne i32 %34, %35
br i1 %36, label %37, label %47
37: ; preds = %29
%38 = load i32, i32* @LOG_INFO
%39 = load i32, i32* @LOG_LOCAL6
%40 = or i32 %38, %39
%41 = getelementptr inbounds [513 x i8], [513 x i8]* %7, i64 0, i64 0
%42 = load i32*, i32** %8
%43 = call i32 @sqlite3_errmsg(i32* %42)
%44 = call i32 @syslog(i32 %40, i8* getelementptr inbounds ([93 x i8], [93 x i8]* @.str.3, i64 0, i64 0), i8* %41, i32 %43)
%45 = load i8*, i8** %12
%46 = call i32 @free(i8* %45)
store i64 1, i64* %3
br label %74
47: ; preds = %29
%48 = load i8*, i8** %12
%49 = call i32 (i8*, i32, i8*, i8*, ...) @snprintf(i8* %48, i32 512, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0))
%50 = load i32*, i32** %8
%51 = load i8*, i8** %12
%52 = load i8*, i8** %12
%53 = call i32 @strlen(i8* %52)
%54 = call i32 @sqlite3_prepare_v2(i32* %50, i8* %51, i32 %53, i32** %9, i32* null)
%55 = load i32*, i32** %9
%56 = load i64, i64* %4
%57 = call i32 @sqlite3_bind_int(i32* %55, i32 1, i64 %56)
br label %58
58: ; preds = %63, %47
%59 = load i32*, i32** %9
%60 = call i32 @sqlite3_step(i32* %59)
store i32 %60, i32* %10
%61 = load i32, i32* @SQLITE_ROW
%62 = icmp eq i32 %60, %61
br i1 %62, label %63, label %66
63: ; preds = %58
%64 = load i32*, i32** %9
%65 = call i64 @sqlite3_column_int(i32* %64, i32 0)
store i64 %65, i64* %11
br label %58
66: ; preds = %58
%67 = load i32*, i32** %9
%68 = call i32 @sqlite3_finalize(i32* %67)
%69 = load i32*, i32** %8
%70 = call i32 @sqlite3_close(i32* %69)
%71 = load i8*, i8** %12
%72 = call i32 @free(i8* %71)
%73 = load i64, i64* %11
store i64 %73, i64* %3
br label %74
74: ; preds = %66, %37, %23
%75 = load i64, i64* %3
ret i64 %75
}

LLVM IR (-O2):
@.str = [3 x i8] c"%s\00"
@.str.1 = [5 x i8] c"%s%s\00"
@.str.2 = [30 x i8] c"/db/gargoyle_attack_detect.db\00"
@SQLITE_OK = global i32 0
@LOG_INFO = global i32 0
@LOG_LOCAL6 = global i32 0
@.str.3 = [93 x i8] c"ERROR opening SQLite DB '%s' from function [sqlite_get_detected_hosts_row_ix_by_host_ix]: %s\00"
@.str.4 = [37 x i8] c"SELECT ix FROM %s WHERE host_ix = ?1\00"
@.str.5 = [15 x i8] c"detected_hosts\00"
@SQLITE_ROW = global i32 0
define i64 @sqlite_get_detected_hosts_row_ix_by_host_ix(i64 %0, i8* %1) {
%3 = alloca [256 x i8]
%4 = alloca [513 x i8]
%5 = alloca i32*
%6 = alloca i32*
%7 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%8 = getelementptr inbounds [513 x i8], [513 x i8]* %4, i64 0, i64 0
%9 = icmp eq i8* %1, null
br i1 %9, label %12, label %10
10: ; preds = %2
%11 = call i32 (i8*, i32, i8*, i8*, ...) @snprintf(i8* %8, i32 512, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %1)
br label %17
12: ; preds = %2
%13 = call i32* @getcwd(i8* %7, i32 256)
%14 = icmp eq i32* %13, null
br i1 %14, label %59, label %15
15: ; preds = %12
%16 = call i32 (i8*, i32, i8*, i8*, ...) @snprintf(i8* %8, i32 512, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %7, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0))
br label %17
17: ; preds = %15, %10
%18 = bitcast i32** %5 to i8*
%19 = bitcast i32** %6 to i8*
%20 = call i64 @malloc(i32 512)
%21 = inttoptr i64 %20 to i8*
%22 = call i32 @sqlite3_open(i8* %8, i32** %5)
%23 = load i32, i32* @SQLITE_OK
%24 = icmp eq i32 %22, %23
br i1 %24, label %32, label %25
25: ; preds = %17
%26 = load i32, i32* @LOG_INFO
%27 = load i32, i32* @LOG_LOCAL6
%28 = or i32 %27, %26
%29 = load i32*, i32** %5
%30 = call i32 @sqlite3_errmsg(i32* %29)
%31 = call i32 @syslog(i32 %28, i8* getelementptr inbounds ([93 x i8], [93 x i8]* @.str.3, i64 0, i64 0), i8* %8, i32 %30)
br label %56
32: ; preds = %17
%33 = call i32 (i8*, i32, i8*, i8*, ...) @snprintf(i8* %21, i32 512, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0))
%34 = load i32*, i32** %5
%35 = call i32 @strlen(i8* %21)
%36 = call i32 @sqlite3_prepare_v2(i32* %34, i8* %21, i32 %35, i32** %6, i32* null)
%37 = load i32*, i32** %6
%38 = call i32 @sqlite3_bind_int(i32* %37, i32 1, i64 %0)
%39 = load i32*, i32** %6
%40 = call i32 @sqlite3_step(i32* %39)
%41 = load i32, i32* @SQLITE_ROW
%42 = icmp eq i32 %40, %41
br i1 %42, label %43, label %50
43: ; preds = %32, %43
%44 = load i32*, i32** %6
%45 = call i64 @sqlite3_column_int(i32* %44, i32 0)
%46 = load i32*, i32** %6
%47 = call i32 @sqlite3_step(i32* %46)
%48 = load i32, i32* @SQLITE_ROW
%49 = icmp eq i32 %47, %48
br i1 %49, label %43, label %50
50: ; preds = %43, %32
%51 = phi i64 [ 0, %32 ], [ %45, %43 ]
%52 = load i32*, i32** %6
%53 = call i32 @sqlite3_finalize(i32* %52)
%54 = load i32*, i32** %5
%55 = call i32 @sqlite3_close(i32* %54)
br label %56
56: ; preds = %50, %25
%57 = phi i64 [ 1, %25 ], [ %51, %50 ]
%58 = call i32 @free(i8* %21)
br label %59
59: ; preds = %12, %56
%60 = phi i64 [ %57, %56 ], [ 1, %12 ]
ret i64 %60
}