#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*/ *getcwd(char *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, char const *, ...);
int /*<<< orphan*/ sqlite3_bind_int(int /*<<< orphan*/ *, int, int);
int /*<<< orphan*/ sqlite3_close(int /*<<< orphan*/ *);
int 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*/);
int sqlite_get_host_port_hit(int ip_addr_ix, int the_port, const char *db_loc) {
  int ret;
  ret = 0;
  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;
  char sql[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_host_port_hit]: %s",
           DB_LOCATION, sqlite3_errmsg(db));
    return -1;
  }
  snprintf(sql, 512,
           "SELECT hit_count FROM %s WHERE host_ix = ?1 AND port_number = ?2",
           "hosts_ports_hits");
  sqlite3_prepare_v2(db, sql, strlen(sql), &stmt, NULL);
  sqlite3_bind_int(stmt, 1, ip_addr_ix);
  sqlite3_bind_int(stmt, 2, the_port);
  while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
    ret = sqlite3_column_int(stmt, 0);
  }
  sqlite3_finalize(stmt);
  sqlite3_close(db);
  return ret;
}


x86 assembly (clang, -O0):
<sqlite_get_host_port_hit>:
push   %rbp
mov    %rsp,%rbp
sub    $0x570,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     120a <sqlite_get_host_port_hit+0x4a>
lea    -0x330(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    $0x200,%esi
lea    0xe6c(%rip),%rdx
mov    $0x0,%al
call   1060 <snprintf@plt>
jmp    125e <sqlite_get_host_port_hit+0x9e>
lea    -0x120(%rbp),%rdi
mov    $0x100,%esi
call   1070 <getcwd@plt>
cmp    $0x0,%rax
jne    1231 <sqlite_get_host_port_hit+0x71>
movl   $0x1,-0x4(%rbp)
jmp    13cc <sqlite_get_host_port_hit+0x20c>
lea    -0x330(%rbp),%rdi
lea    -0x120(%rbp),%rcx
mov    $0x200,%esi
lea    0xdb5(%rip),%rdx
lea    0xdb3(%rip),%r8
mov    $0x0,%al
call   1060 <snprintf@plt>
jmp    125e <sqlite_get_host_port_hit+0x9e>
lea    -0x330(%rbp),%rdi
lea    -0x338(%rbp),%rsi
call   1030 <sqlite3_open@plt>
mov    %eax,-0x344(%rbp)
mov    -0x344(%rbp),%eax
mov    0x2d5c(%rip),%rcx
cmp    (%rcx),%eax
je     12e5 <sqlite_get_host_port_hit+0x125>
mov    0x2d35(%rip),%rax
mov    (%rax),%eax
mov    0x2d4c(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x55c(%rbp)
lea    -0x330(%rbp),%rax
mov    %rax,-0x558(%rbp)
mov    -0x338(%rbp),%rdi
call   1090 <sqlite3_errmsg@plt>
mov    -0x55c(%rbp),%edi
mov    -0x558(%rbp),%rdx
mov    %eax,%ecx
lea    0xd4f(%rip),%rsi
call   10c0 <syslog@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13cc <sqlite_get_host_port_hit+0x20c>
lea    -0x550(%rbp),%rdi
mov    $0x200,%esi
lea    0xd75(%rip),%rdx
lea    0xdaf(%rip),%rcx
mov    $0x0,%al
call   1060 <snprintf@plt>
mov    -0x338(%rbp),%rax
mov    %rax,-0x570(%rbp)
lea    -0x550(%rbp),%rax
mov    %rax,-0x568(%rbp)
lea    -0x550(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x570(%rbp),%rdi
mov    -0x568(%rbp),%rsi
mov    %eax,%edx
lea    -0x340(%rbp),%rcx
xor    %eax,%eax
mov    %eax,%r8d
call   10e0 <sqlite3_prepare_v2@plt>
mov    -0x340(%rbp),%rdi
mov    -0x8(%rbp),%edx
mov    $0x1,%esi
call   1040 <sqlite3_bind_int@plt>
mov    -0x340(%rbp),%rdi
mov    -0xc(%rbp),%edx
mov    $0x2,%esi
call   1040 <sqlite3_bind_int@plt>
mov    -0x340(%rbp),%rdi
call   10a0 <sqlite3_step@plt>
mov    %eax,-0x344(%rbp)
mov    0x2c40(%rip),%rcx
cmp    (%rcx),%eax
jne    13ae <sqlite_get_host_port_hit+0x1ee>
mov    -0x340(%rbp),%rdi
xor    %esi,%esi
call   1080 <sqlite3_column_int@plt>
mov    %eax,-0x1c(%rbp)
jmp    1377 <sqlite_get_host_port_hit+0x1b7>
mov    -0x340(%rbp),%rdi
call   10d0 <sqlite3_finalize@plt>
mov    -0x338(%rbp),%rdi
call   10b0 <sqlite3_close@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x570,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sqlite_get_host_port_hit>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x520,%rsp
mov    %esi,%r14d
mov    %edi,%r15d
test   %rdx,%rdx
je     11fa <sqlite_get_host_port_hit+0x3a>
mov    %rdx,%rcx
lea    0xe86(%rip),%rdx
lea    0x110(%rsp),%rdi
mov    $0x200,%esi
xor    %eax,%eax
call   1060 <snprintf@plt>
jmp    1239 <sqlite_get_host_port_hit+0x79>
lea    0x10(%rsp),%rdi
mov    $0x100,%esi
call   1070 <getcwd@plt>
test   %rax,%rax
je     134b <sqlite_get_host_port_hit+0x18b>
lea    0xde7(%rip),%rdx
lea    0xde5(%rip),%r8
lea    0x110(%rsp),%rdi
lea    0x10(%rsp),%rcx
mov    $0x200,%esi
xor    %eax,%eax
call   1060 <snprintf@plt>
lea    0x110(%rsp),%rdi
lea    0x8(%rsp),%rsi
call   1030 <sqlite3_open@plt>
mov    0x2d8e(%rip),%rcx
cmp    (%rcx),%eax
jne    1310 <sqlite_get_host_port_hit+0x150>
lea    0xe0c(%rip),%rdx
lea    0xe46(%rip),%rcx
xor    %ebx,%ebx
lea    0x320(%rsp),%r12
mov    %r12,%rdi
mov    $0x200,%esi
xor    %eax,%eax
call   1060 <snprintf@plt>
mov    0x8(%rsp),%rbp
mov    %r12,%rdi
call   1050 <strlen@plt>
mov    %rsp,%rcx
mov    %rbp,%rdi
mov    %r12,%rsi
mov    %eax,%edx
xor    %r8d,%r8d
call   10e0 <sqlite3_prepare_v2@plt>
mov    (%rsp),%rdi
mov    $0x1,%esi
mov    %r15d,%edx
call   1040 <sqlite3_bind_int@plt>
mov    (%rsp),%rdi
mov    $0x2,%esi
mov    %r14d,%edx
call   1040 <sqlite3_bind_int@plt>
mov    (%rsp),%rdi
call   10a0 <sqlite3_step@plt>
mov    0x2cfd(%rip),%rbp
cmp    0x0(%rbp),%eax
jne    12fb <sqlite_get_host_port_hit+0x13b>
nopl   0x0(%rax,%rax,1)
mov    (%rsp),%rdi
xor    %esi,%esi
call   1080 <sqlite3_column_int@plt>
mov    %eax,%ebx
mov    (%rsp),%rdi
call   10a0 <sqlite3_step@plt>
cmp    0x0(%rbp),%eax
je     12e0 <sqlite_get_host_port_hit+0x120>
mov    (%rsp),%rdi
call   10d0 <sqlite3_finalize@plt>
mov    0x8(%rsp),%rdi
call   10b0 <sqlite3_close@plt>
jmp    1350 <sqlite_get_host_port_hit+0x190>
mov    0x2cb1(%rip),%rax
mov    0x2cca(%rip),%rcx
mov    (%rcx),%ebx
or     (%rax),%ebx
mov    0x8(%rsp),%rdi
call   1090 <sqlite3_errmsg@plt>
lea    0xcf0(%rip),%rsi
lea    0x110(%rsp),%rdx
mov    %ebx,%edi
mov    %eax,%ecx
call   10c0 <syslog@plt>
mov    $0xffffffff,%ebx
jmp    1350 <sqlite_get_host_port_hit+0x190>
mov    $0x1,%ebx
mov    %ebx,%eax
add    $0x520,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

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