#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*/ *);
scalar_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*/);
int sqlite_get_one_host_hit_count_all_ports(int ip_addr_ix,
                                            const char *db_loc) {
  int return_val;
  return_val = 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_one_host_hit_count_all_ports]: %s",
           DB_LOCATION, sqlite3_errmsg(db));
    return 1;
  }
  snprintf(sql, 512, "SELECT hit_count FROM %s WHERE host_ix = ?1",
           "hosts_ports_hits");
  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);
  return return_val;
}


x86 assembly (clang, -O0):
<sqlite_get_one_host_hit_count_all_ports>:
push   %rbp
mov    %rsp,%rbp
sub    $0x570,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1207 <sqlite_get_one_host_hit_count_all_ports+0x47>
lea    -0x330(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x200,%esi
lea    0xe7e(%rip),%rdx
mov    $0x0,%al
call   1060 <snprintf@plt>
jmp    125b <sqlite_get_one_host_hit_count_all_ports+0x9b>
lea    -0x120(%rbp),%rdi
mov    $0x100,%esi
call   1070 <getcwd@plt>
cmp    $0x0,%rax
jne    122e <sqlite_get_one_host_hit_count_all_ports+0x6e>
movl   $0x1,-0x4(%rbp)
jmp    13bf <sqlite_get_one_host_hit_count_all_ports+0x1ff>
lea    -0x330(%rbp),%rdi
lea    -0x120(%rbp),%rcx
mov    $0x200,%esi
lea    0xdb8(%rip),%rdx
lea    0xdb6(%rip),%r8
mov    $0x0,%al
call   1060 <snprintf@plt>
jmp    125b <sqlite_get_one_host_hit_count_all_ports+0x9b>
lea    -0x330(%rbp),%rdi
lea    -0x338(%rbp),%rsi
call   1030 <sqlite3_open@plt>
mov    %eax,-0x344(%rbp)
mov    -0x344(%rbp),%eax
mov    0x2d5f(%rip),%rcx
cmp    (%rcx),%eax
je     12e2 <sqlite_get_one_host_hit_count_all_ports+0x122>
mov    0x2d38(%rip),%rax
mov    (%rax),%eax
mov    0x2d4f(%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    0xd52(%rip),%rsi
call   10c0 <syslog@plt>
movl   $0x1,-0x4(%rbp)
jmp    13bf <sqlite_get_one_host_hit_count_all_ports+0x1ff>
lea    -0x550(%rbp),%rdi
mov    $0x200,%esi
lea    0xd87(%rip),%rdx
lea    0xdac(%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
call   10a0 <sqlite3_step@plt>
mov    %eax,-0x344(%rbp)
mov    0x2c57(%rip),%rcx
cmp    (%rcx),%eax
jne    13a1 <sqlite_get_one_host_hit_count_all_ports+0x1e1>
mov    -0x340(%rbp),%rdi
xor    %esi,%esi
call   1080 <sqlite3_column_int@plt>
mov    %rax,%rcx
movslq -0x14(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x14(%rbp)
jmp    1360 <sqlite_get_one_host_hit_count_all_ports+0x1a0>
mov    -0x340(%rbp),%rdi
call   10d0 <sqlite3_finalize@plt>
mov    -0x338(%rbp),%rdi
call   10b0 <sqlite3_close@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x570,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sqlite_get_one_host_hit_count_all_ports>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x528,%rsp
mov    %edi,%r14d
test   %rsi,%rsi
je     11f5 <sqlite_get_one_host_hit_count_all_ports+0x35>
mov    %rsi,%rcx
lea    0xe9a(%rip),%rdx
lea    0x110(%rsp),%rdi
mov    $0x200,%esi
xor    %eax,%eax
call   1060 <snprintf@plt>
jmp    1234 <sqlite_get_one_host_hit_count_all_ports+0x74>
lea    0x10(%rsp),%rdi
mov    $0x100,%esi
call   1070 <getcwd@plt>
test   %rax,%rax
je     1334 <sqlite_get_one_host_hit_count_all_ports+0x174>
lea    0xdec(%rip),%rdx
lea    0xdea(%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    0x2d93(%rip),%rcx
cmp    (%rcx),%eax
jne    1300 <sqlite_get_one_host_hit_count_all_ports+0x140>
lea    0xe20(%rip),%rdx
lea    0xe45(%rip),%rcx
xor    %ebx,%ebx
lea    0x320(%rsp),%r15
mov    %r15,%rdi
mov    $0x200,%esi
xor    %eax,%eax
call   1060 <snprintf@plt>
mov    0x8(%rsp),%rbp
mov    %r15,%rdi
call   1050 <strlen@plt>
mov    %rsp,%rcx
mov    %rbp,%rdi
mov    %r15,%rsi
mov    %eax,%edx
xor    %r8d,%r8d
call   10e0 <sqlite3_prepare_v2@plt>
mov    (%rsp),%rdi
mov    $0x1,%esi
mov    %r14d,%edx
call   1040 <sqlite3_bind_int@plt>
mov    (%rsp),%rdi
call   10a0 <sqlite3_step@plt>
mov    0x2d13(%rip),%rbp
cmp    0x0(%rbp),%eax
jne    12eb <sqlite_get_one_host_hit_count_all_ports+0x12b>
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    (%rsp),%rdi
xor    %esi,%esi
call   1080 <sqlite3_column_int@plt>
add    %eax,%ebx
mov    (%rsp),%rdi
call   10a0 <sqlite3_step@plt>
cmp    0x0(%rbp),%eax
je     12d0 <sqlite_get_one_host_hit_count_all_ports+0x110>
mov    (%rsp),%rdi
call   10d0 <sqlite3_finalize@plt>
mov    0x8(%rsp),%rdi
call   10b0 <sqlite3_close@plt>
jmp    1339 <sqlite_get_one_host_hit_count_all_ports+0x179>
mov    0x2cc1(%rip),%rax
mov    0x2cda(%rip),%rcx
mov    (%rcx),%ebx
or     (%rax),%ebx
mov    0x8(%rsp),%rdi
call   1090 <sqlite3_errmsg@plt>
lea    0xd00(%rip),%rsi
lea    0x110(%rsp),%rdx
mov    %ebx,%edi
mov    %eax,%ecx
call   10c0 <syslog@plt>
mov    $0x1,%ebx
mov    %ebx,%eax
add    $0x528,%rsp
pop    %rbx
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 = [89 x i8] c"ERROR opening SQLite DB '%s' from function [sqlite_get_one_host_hit_count_all_ports]: %s\00"
@.str.4 = [44 x i8] c"SELECT hit_count FROM %s WHERE host_ix = ?1\00"
@.str.5 = [17 x i8] c"hosts_ports_hits\00"
@SQLITE_ROW = global i32 0
define i32 @sqlite_get_one_host_hit_count_all_ports(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca [256 x i8]
%8 = alloca [513 x i8]
%9 = alloca i32*
%10 = alloca i32*
%11 = alloca i32
%12 = alloca [512 x i8]
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32 0, i32* %6
%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]* %8, 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]* %7, 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 i32 1, i32* %3
br label %72
24: ; preds = %19
%25 = getelementptr inbounds [513 x i8], [513 x i8]* %8, i64 0, i64 0
%26 = getelementptr inbounds [256 x i8], [256 x i8]* %7, 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
%30 = getelementptr inbounds [513 x i8], [513 x i8]* %8, i64 0, i64 0
%31 = call i32 @sqlite3_open(i8* %30, i32** %9)
store i32 %31, i32* %11
%32 = load i32, i32* %11
%33 = load i32, i32* @SQLITE_OK
%34 = icmp ne i32 %32, %33
br i1 %34, label %35, label %43
35: ; preds = %29
%36 = load i32, i32* @LOG_INFO
%37 = load i32, i32* @LOG_LOCAL6
%38 = or i32 %36, %37
%39 = getelementptr inbounds [513 x i8], [513 x i8]* %8, i64 0, i64 0
%40 = load i32*, i32** %9
%41 = call i32 @sqlite3_errmsg(i32* %40)
%42 = call i32 @syslog(i32 %38, i8* getelementptr inbounds ([89 x i8], [89 x i8]* @.str.3, i64 0, i64 0), i8* %39, i32 %41)
store i32 1, i32* %3
br label %72
43: ; preds = %29
%44 = getelementptr inbounds [512 x i8], [512 x i8]* %12, i64 0, i64 0
%45 = call i32 (i8*, i32, i8*, i8*, ...) @snprintf(i8* %44, i32 512, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0))
%46 = load i32*, i32** %9
%47 = getelementptr inbounds [512 x i8], [512 x i8]* %12, i64 0, i64 0
%48 = getelementptr inbounds [512 x i8], [512 x i8]* %12, i64 0, i64 0
%49 = call i32 @strlen(i8* %48)
%50 = call i32 @sqlite3_prepare_v2(i32* %46, i8* %47, i32 %49, i32** %10, i32* null)
%51 = load i32*, i32** %10
%52 = load i32, i32* %4
%53 = call i32 @sqlite3_bind_int(i32* %51, i32 1, i32 %52)
br label %54
54: ; preds = %59, %43
%55 = load i32*, i32** %10
%56 = call i32 @sqlite3_step(i32* %55)
store i32 %56, i32* %11
%57 = load i32, i32* @SQLITE_ROW
%58 = icmp eq i32 %56, %57
br i1 %58, label %59, label %66
59: ; preds = %54
%60 = load i32*, i32** %10
%61 = call i64 @sqlite3_column_int(i32* %60, i32 0)
%62 = load i32, i32* %6
%63 = sext i32 %62 to i64
%64 = add nsw i64 %63, %61
%65 = trunc i64 %64 to i32
store i32 %65, i32* %6
br label %54
66: ; preds = %54
%67 = load i32*, i32** %10
%68 = call i32 @sqlite3_finalize(i32* %67)
%69 = load i32*, i32** %9
%70 = call i32 @sqlite3_close(i32* %69)
%71 = load i32, i32* %6
store i32 %71, i32* %3
br label %72
72: ; preds = %66, %35, %23
%73 = load i32, i32* %3
ret i32 %73
}

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