#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*/ memcpy(char *, char *, size_t);
int /*<<< orphan*/ snprintf(char *, int, char *, ...);
int /*<<< orphan*/ sqlite3_bind_int(int /*<<< orphan*/ *, int, int);
int /*<<< orphan*/ sqlite3_close(int /*<<< orphan*/ *);
int sqlite3_column_int(int /*<<< orphan*/ *, int);
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,
                                      int /*<<< orphan*/ **,
                                      int /*<<< orphan*/ *);
int sqlite3_step(int /*<<< orphan*/ *);
int strlen(char *);
int /*<<< orphan*/ strncat(char *, char *, int);
int /*<<< orphan*/ syslog(int, char *, char *, int /*<<< orphan*/);
int sqlite_get_one_host_all_ports(int ip_addr_ix, char *dst, size_t sz_dst,
                                  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;
  char *final_set;
  final_set = (char *)malloc(2097152);
  char *l_buf;
  l_buf = (char *)malloc(60);
  char *sql;
  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_one_host_all_ports]: %s",
           DB_LOCATION, sqlite3_errmsg(db));
    free(l_buf);
    free(sql);
    free(final_set);
    return 1;
  }
  snprintf(sql, 512, "SELECT * 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);
  *final_set = 0;
  while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
    snprintf(l_buf, 60, "%d:%d:%d:%d>", sqlite3_column_int(stmt, 0),
             sqlite3_column_int(stmt, 1), sqlite3_column_int(stmt, 2),
             sqlite3_column_int(stmt, 3));
    strncat(final_set, l_buf, 2097152 - strlen(final_set) - 1);
  }
  size_t final_set_len = strlen(final_set);
  final_set[final_set_len] = '\0';
  sqlite3_finalize(stmt);
  sqlite3_close(db);
  if (final_set_len + 1 > sz_dst) {
    free(l_buf);
    free(sql);
    free(final_set);
    return 1;
  }
  memcpy(dst, final_set, final_set_len + 1);
  free(l_buf);
  free(sql);
  free(final_set);
  return 0;
}


x86 assembly (clang, -O0):
<sqlite_get_one_host_all_ports>:
push   %rbp
mov    %rsp,%rbp
sub    $0x3c0,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1248 <sqlite_get_one_host_all_ports+0x48>
lea    -0x330(%rbp),%rdi
mov    -0x20(%rbp),%rcx
mov    $0x200,%esi
lea    0xe33(%rip),%rdx
mov    $0x0,%al
call   1070 <snprintf@plt>
jmp    129c <sqlite_get_one_host_all_ports+0x9c>
lea    -0x120(%rbp),%rdi
mov    $0x100,%esi
call   1080 <getcwd@plt>
cmp    $0x0,%rax
jne    126f <sqlite_get_one_host_all_ports+0x6f>
movl   $0x1,-0x4(%rbp)
jmp    15c9 <sqlite_get_one_host_all_ports+0x3c9>
lea    -0x330(%rbp),%rdi
lea    -0x120(%rbp),%rcx
mov    $0x200,%esi
lea    0xd77(%rip),%rdx
lea    0xd75(%rip),%r8
mov    $0x0,%al
call   1070 <snprintf@plt>
jmp    129c <sqlite_get_one_host_all_ports+0x9c>
mov    $0x200000,%edi
call   10f0 <malloc@plt>
mov    %rax,-0x350(%rbp)
mov    $0x3c,%edi
call   10f0 <malloc@plt>
mov    %rax,-0x358(%rbp)
mov    $0x200,%edi
call   10f0 <malloc@plt>
mov    %rax,-0x360(%rbp)
lea    -0x330(%rbp),%rdi
lea    -0x338(%rbp),%rsi
call   1040 <sqlite3_open@plt>
mov    %eax,-0x344(%rbp)
mov    -0x344(%rbp),%eax
mov    0x2ceb(%rip),%rcx
cmp    (%rcx),%eax
je     137a <sqlite_get_one_host_all_ports+0x17a>
mov    0x2cc4(%rip),%rax
mov    (%rax),%eax
mov    0x2cdb(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x374(%rbp)
lea    -0x330(%rbp),%rax
mov    %rax,-0x370(%rbp)
mov    -0x338(%rbp),%rdi
call   10b0 <sqlite3_errmsg@plt>
mov    -0x374(%rbp),%edi
mov    -0x370(%rbp),%rdx
mov    %eax,%ecx
lea    0xcde(%rip),%rsi
call   1100 <syslog@plt>
mov    -0x358(%rbp),%rdi
call   1030 <free@plt>
mov    -0x360(%rbp),%rdi
call   1030 <free@plt>
mov    -0x350(%rbp),%rdi
call   1030 <free@plt>
movl   $0x1,-0x4(%rbp)
jmp    15c9 <sqlite_get_one_host_all_ports+0x3c9>
mov    -0x360(%rbp),%rdi
mov    $0x200,%esi
lea    0xce5(%rip),%rdx
lea    0xd02(%rip),%rcx
mov    $0x0,%al
call   1070 <snprintf@plt>
mov    -0x338(%rbp),%rax
mov    %rax,-0x388(%rbp)
mov    -0x360(%rbp),%rax
mov    %rax,-0x380(%rbp)
mov    -0x360(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0x388(%rbp),%rdi
mov    -0x380(%rbp),%rsi
mov    %eax,%edx
lea    -0x340(%rbp),%rcx
xor    %eax,%eax
mov    %eax,%r8d
call   1120 <sqlite3_prepare_v2@plt>
mov    -0x340(%rbp),%rdi
mov    -0x8(%rbp),%edx
mov    $0x1,%esi
call   1050 <sqlite3_bind_int@plt>
mov    -0x350(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x340(%rbp),%rdi
call   10c0 <sqlite3_step@plt>
mov    %eax,-0x344(%rbp)
mov    0x2bb5(%rip),%rcx
cmp    (%rcx),%eax
jne    14ff <sqlite_get_one_host_all_ports+0x2ff>
mov    -0x358(%rbp),%rax
mov    %rax,-0x3b0(%rbp)
mov    -0x340(%rbp),%rdi
xor    %esi,%esi
call   10a0 <sqlite3_column_int@plt>
mov    %eax,-0x3a4(%rbp)
mov    -0x340(%rbp),%rdi
mov    $0x1,%esi
call   10a0 <sqlite3_column_int@plt>
mov    %eax,-0x3a0(%rbp)
mov    -0x340(%rbp),%rdi
mov    $0x2,%esi
call   10a0 <sqlite3_column_int@plt>
mov    %eax,-0x39c(%rbp)
mov    -0x340(%rbp),%rdi
mov    $0x3,%esi
call   10a0 <sqlite3_column_int@plt>
mov    -0x3b0(%rbp),%rdi
mov    -0x3a4(%rbp),%ecx
mov    -0x3a0(%rbp),%r8d
mov    -0x39c(%rbp),%r9d
mov    $0x3c,%esi
lea    0xbfc(%rip),%rdx
mov    %eax,(%rsp)
mov    $0x0,%al
call   1070 <snprintf@plt>
mov    -0x350(%rbp),%rax
mov    %rax,-0x398(%rbp)
mov    -0x358(%rbp),%rax
mov    %rax,-0x390(%rbp)
mov    -0x350(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0x398(%rbp),%rdi
mov    -0x390(%rbp),%rsi
mov    $0x200000,%edx
sub    %eax,%edx
sub    $0x1,%edx
call   1090 <strncat@plt>
jmp    1402 <sqlite_get_one_host_all_ports+0x202>
mov    -0x350(%rbp),%rdi
call   1060 <strlen@plt>
cltq
mov    %rax,-0x368(%rbp)
mov    -0x350(%rbp),%rax
mov    -0x368(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x340(%rbp),%rdi
call   1110 <sqlite3_finalize@plt>
mov    -0x338(%rbp),%rdi
call   10e0 <sqlite3_close@plt>
mov    -0x368(%rbp),%rax
add    $0x1,%rax
cmp    -0x18(%rbp),%rax
jbe    1583 <sqlite_get_one_host_all_ports+0x383>
mov    -0x358(%rbp),%rdi
call   1030 <free@plt>
mov    -0x360(%rbp),%rdi
call   1030 <free@plt>
mov    -0x350(%rbp),%rdi
call   1030 <free@plt>
movl   $0x1,-0x4(%rbp)
jmp    15c9 <sqlite_get_one_host_all_ports+0x3c9>
mov    -0x10(%rbp),%rdi
mov    -0x350(%rbp),%rsi
mov    -0x368(%rbp),%rdx
add    $0x1,%rdx
call   10d0 <memcpy@plt>
mov    -0x358(%rbp),%rdi
call   1030 <free@plt>
mov    -0x360(%rbp),%rdi
call   1030 <free@plt>
mov    -0x350(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x3c0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sqlite_get_one_host_all_ports>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x338,%rsp
mov    %rdx,%r14
mov    %rsi,%rbp
mov    %edi,%r15d
test   %rcx,%rcx
je     123c <sqlite_get_one_host_all_ports+0x3c>
lea    0xe49(%rip),%rdx
lea    0x130(%rsp),%rdi
mov    $0x200,%esi
xor    %eax,%eax
call   1070 <snprintf@plt>
jmp    127b <sqlite_get_one_host_all_ports+0x7b>
lea    0x30(%rsp),%rdi
mov    $0x100,%esi
call   1080 <getcwd@plt>
test   %rax,%rax
je     146a <sqlite_get_one_host_all_ports+0x26a>
lea    0xda5(%rip),%rdx
lea    0xda3(%rip),%r8
lea    0x130(%rsp),%rdi
lea    0x30(%rsp),%rcx
mov    $0x200,%esi
xor    %eax,%eax
call   1070 <snprintf@plt>
mov    $0x200000,%edi
call   10f0 <malloc@plt>
mov    %rax,%r13
mov    $0x3c,%edi
call   10f0 <malloc@plt>
mov    %rax,%rbx
mov    $0x200,%edi
call   10f0 <malloc@plt>
mov    %rax,%r12
lea    0x130(%rsp),%rdi
lea    0x10(%rsp),%rsi
call   1040 <sqlite3_open@plt>
mov    0x2d25(%rip),%rcx
cmp    (%rcx),%eax
jne    141e <sqlite_get_one_host_all_ports+0x21e>
mov    %r14,0x28(%rsp)
mov    %rbp,0x20(%rsp)
lea    0xd9e(%rip),%rdx
lea    0xdbb(%rip),%rcx
mov    %r12,%rdi
mov    $0x200,%esi
xor    %eax,%eax
call   1070 <snprintf@plt>
mov    0x10(%rsp),%r14
mov    %r12,%rdi
call   1060 <strlen@plt>
lea    0x8(%rsp),%rcx
mov    %r14,%rdi
mov    %r12,0x18(%rsp)
mov    %r12,%rsi
mov    %eax,%edx
xor    %r8d,%r8d
call   1120 <sqlite3_prepare_v2@plt>
mov    0x8(%rsp),%rdi
mov    $0x1,%esi
mov    %r15d,%edx
call   1050 <sqlite3_bind_int@plt>
movb   $0x0,0x0(%r13)
mov    0x8(%rsp),%rdi
call   10c0 <sqlite3_step@plt>
mov    0x2c97(%rip),%r14
cmp    (%r14),%eax
jne    13df <sqlite_get_one_host_all_ports+0x1df>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    0x8(%rsp),%rdi
xor    %esi,%esi
call   10a0 <sqlite3_column_int@plt>
mov    %eax,%ebp
mov    0x8(%rsp),%rdi
mov    $0x1,%esi
call   10a0 <sqlite3_column_int@plt>
mov    %eax,%r15d
mov    0x8(%rsp),%rdi
mov    $0x2,%esi
call   10a0 <sqlite3_column_int@plt>
mov    %eax,%r12d
mov    0x8(%rsp),%rdi
mov    $0x3,%esi
call   10a0 <sqlite3_column_int@plt>
mov    %eax,(%rsp)
mov    %rbx,%rdi
mov    $0x3c,%esi
lea    0xd04(%rip),%rdx
mov    %ebp,%ecx
mov    %r15d,%r8d
mov    %r12d,%r9d
xor    %eax,%eax
call   1070 <snprintf@plt>
mov    %r13,%rdi
call   1060 <strlen@plt>
mov    $0x1fffff,%edx
sub    %eax,%edx
mov    %r13,%rdi
mov    %rbx,%rsi
call   1090 <strncat@plt>
mov    0x8(%rsp),%rdi
call   10c0 <sqlite3_step@plt>
cmp    (%r14),%eax
je     1350 <sqlite_get_one_host_all_ports+0x150>
mov    %r13,%rdi
call   1060 <strlen@plt>
movslq %eax,%rbp
movb   $0x0,0x0(%r13,%rbp,1)
mov    0x8(%rsp),%rdi
call   1110 <sqlite3_finalize@plt>
mov    0x10(%rsp),%rdi
call   10e0 <sqlite3_close@plt>
add    $0x1,%rbp
cmp    0x28(%rsp),%rbp
jbe    1471 <sqlite_get_one_host_all_ports+0x271>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    0x18(%rsp),%rdi
jmp    145d <sqlite_get_one_host_all_ports+0x25d>
mov    0x2ba3(%rip),%rax
mov    0x2bbc(%rip),%rcx
mov    (%rcx),%ebp
or     (%rax),%ebp
mov    0x10(%rsp),%rdi
call   10b0 <sqlite3_errmsg@plt>
lea    0xbe2(%rip),%rsi
lea    0x130(%rsp),%rdx
mov    %ebp,%edi
mov    %eax,%ecx
call   1100 <syslog@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r12,%rdi
call   1030 <free@plt>
mov    %r13,%rdi
call   1030 <free@plt>
mov    $0x1,%eax
jmp    149d <sqlite_get_one_host_all_ports+0x29d>
mov    0x20(%rsp),%rdi
mov    %r13,%rsi
mov    %rbp,%rdx
call   10d0 <memcpy@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    0x18(%rsp),%rdi
call   1030 <free@plt>
mov    %r13,%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x338,%rsp
pop    %rbx
pop    %r12
pop    %r13
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 = [79 x i8] c"ERROR opening SQLite DB '%s' from function [sqlite_get_one_host_all_ports]: %s\00"
@.str.4 = [36 x i8] c"SELECT * FROM %s WHERE host_ix = ?1\00"
@.str.5 = [17 x i8] c"hosts_ports_hits\00"
@SQLITE_ROW = global i32 0
@.str.6 = [13 x i8] c"%d:%d:%d:%d>\00"
define i32 @sqlite_get_one_host_all_ports(i32 %0, i8* %1, i64 %2, i8* %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i8*
%10 = alloca [256 x i8]
%11 = alloca [513 x i8]
%12 = alloca i32*
%13 = alloca i32*
%14 = alloca i32
%15 = alloca i8*
%16 = alloca i8*
%17 = alloca i8*
%18 = alloca i64
store i32 %0, i32* %6
store i8* %1, i8** %7
store i64 %2, i64* %8
store i8* %3, i8** %9
%19 = load i8*, i8** %9
%20 = icmp ne i8* %19, null
br i1 %20, label %21, label %25
21: ; preds = %4
%22 = getelementptr inbounds [513 x i8], [513 x i8]* %11, i64 0, i64 0
%23 = load i8*, i8** %9
%24 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %22, i32 512, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %23)
br label %35
25: ; preds = %4
%26 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%27 = call i32* @getcwd(i8* %26, i32 256)
%28 = icmp eq i32* %27, null
br i1 %28, label %29, label %30
29: ; preds = %25
store i32 1, i32* %5
br label %130
30: ; preds = %25
%31 = getelementptr inbounds [513 x i8], [513 x i8]* %11, i64 0, i64 0
%32 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%33 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %31, i32 512, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %32, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0))
br label %34
34: ; preds = %30
br label %35
35: ; preds = %34, %21
%36 = call i64 @malloc(i32 2097152)
%37 = inttoptr i64 %36 to i8*
store i8* %37, i8** %15
%38 = call i64 @malloc(i32 60)
%39 = inttoptr i64 %38 to i8*
store i8* %39, i8** %16
%40 = call i64 @malloc(i32 512)
%41 = inttoptr i64 %40 to i8*
store i8* %41, i8** %17
%42 = getelementptr inbounds [513 x i8], [513 x i8]* %11, i64 0, i64 0
%43 = call i32 @sqlite3_open(i8* %42, i32** %12)
store i32 %43, i32* %14
%44 = load i32, i32* %14
%45 = load i32, i32* @SQLITE_OK
%46 = icmp ne i32 %44, %45
br i1 %46, label %47, label %61
47: ; preds = %35
%48 = load i32, i32* @LOG_INFO
%49 = load i32, i32* @LOG_LOCAL6
%50 = or i32 %48, %49
%51 = getelementptr inbounds [513 x i8], [513 x i8]* %11, i64 0, i64 0
%52 = load i32*, i32** %12
%53 = call i32 @sqlite3_errmsg(i32* %52)
%54 = call i32 @syslog(i32 %50, i8* getelementptr inbounds ([79 x i8], [79 x i8]* @.str.3, i64 0, i64 0), i8* %51, i32 %53)
%55 = load i8*, i8** %16
%56 = call i32 @free(i8* %55)
%57 = load i8*, i8** %17
%58 = call i32 @free(i8* %57)
%59 = load i8*, i8** %15
%60 = call i32 @free(i8* %59)
store i32 1, i32* %5
br label %130
61: ; preds = %35
%62 = load i8*, i8** %17
%63 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %62, i32 512, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0))
%64 = load i32*, i32** %12
%65 = load i8*, i8** %17
%66 = load i8*, i8** %17
%67 = call i32 @strlen(i8* %66)
%68 = call i32 @sqlite3_prepare_v2(i32* %64, i8* %65, i32 %67, i32** %13, i32* null)
%69 = load i32*, i32** %13
%70 = load i32, i32* %6
%71 = call i32 @sqlite3_bind_int(i32* %69, i32 1, i32 %70)
%72 = load i8*, i8** %15
store i8 0, i8* %72
br label %73
73: ; preds = %78, %61
%74 = load i32*, i32** %13
%75 = call i32 @sqlite3_step(i32* %74)
store i32 %75, i32* %14
%76 = load i32, i32* @SQLITE_ROW
%77 = icmp eq i32 %75, %76
br i1 %77, label %78, label %96
78: ; preds = %73
%79 = load i8*, i8** %16
%80 = load i32*, i32** %13
%81 = call i32 @sqlite3_column_int(i32* %80, i32 0)
%82 = load i32*, i32** %13
%83 = call i32 @sqlite3_column_int(i32* %82, i32 1)
%84 = load i32*, i32** %13
%85 = call i32 @sqlite3_column_int(i32* %84, i32 2)
%86 = load i32*, i32** %13
%87 = call i32 @sqlite3_column_int(i32* %86, i32 3)
%88 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %79, i32 60, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.6, i64 0, i64 0), i32 %81, i32 %83, i32 %85, i32 %87)
%89 = load i8*, i8** %15
%90 = load i8*, i8** %16
%91 = load i8*, i8** %15
%92 = call i32 @strlen(i8* %91)
%93 = sub nsw i32 2097152, %92
%94 = sub nsw i32 %93, 1
%95 = call i32 @strncat(i8* %89, i8* %90, i32 %94)
br label %73
96: ; preds = %73
%97 = load i8*, i8** %15
%98 = call i32 @strlen(i8* %97)
%99 = sext i32 %98 to i64
store i64 %99, i64* %18
%100 = load i8*, i8** %15
%101 = load i64, i64* %18
%102 = getelementptr inbounds i8, i8* %100, i64 %101
store i8 0, i8* %102
%103 = load i32*, i32** %13
%104 = call i32 @sqlite3_finalize(i32* %103)
%105 = load i32*, i32** %12
%106 = call i32 @sqlite3_close(i32* %105)
%107 = load i64, i64* %18
%108 = add i64 %107, 1
%109 = load i64, i64* %8
%110 = icmp ugt i64 %108, %109
br i1 %110, label %111, label %118
111: ; preds = %96
%112 = load i8*, i8** %16
%113 = call i32 @free(i8* %112)
%114 = load i8*, i8** %17
%115 = call i32 @free(i8* %114)
%116 = load i8*, i8** %15
%117 = call i32 @free(i8* %116)
store i32 1, i32* %5
br label %130
118: ; preds = %96
%119 = load i8*, i8** %7
%120 = load i8*, i8** %15
%121 = load i64, i64* %18
%122 = add i64 %121, 1
%123 = call i32 @memcpy(i8* %119, i8* %120, i64 %122)
%124 = load i8*, i8** %16
%125 = call i32 @free(i8* %124)
%126 = load i8*, i8** %17
%127 = call i32 @free(i8* %126)
%128 = load i8*, i8** %15
%129 = call i32 @free(i8* %128)
store i32 0, i32* %5
br label %130
130: ; preds = %118, %111, %47, %29
%131 = load i32, i32* %5
ret i32 %131
}

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 = [79 x i8] c"ERROR opening SQLite DB '%s' from function [sqlite_get_one_host_all_ports]: %s\00"
@.str.4 = [36 x i8] c"SELECT * FROM %s WHERE host_ix = ?1\00"
@.str.5 = [17 x i8] c"hosts_ports_hits\00"
@SQLITE_ROW = global i32 0
@.str.6 = [13 x i8] c"%d:%d:%d:%d>\00"
define i32 @sqlite_get_one_host_all_ports(i32 %0, i8* %1, i64 %2, i8* %3) {
%5 = alloca [256 x i8]
%6 = alloca [513 x i8]
%7 = alloca i32*
%8 = alloca i32*
%9 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%10 = getelementptr inbounds [513 x i8], [513 x i8]* %6, i64 0, i64 0
%11 = icmp eq i8* %3, null
br i1 %11, label %14, label %12
12: ; preds = %4
%13 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %10, i32 512, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %3)
br label %19
14: ; preds = %4
%15 = call i32* @getcwd(i8* %9, i32 256)
%16 = icmp eq i32* %15, null
br i1 %16, label %90, label %17
17: ; preds = %14
%18 = call i32 (i8*, i32, 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** %7 to i8*
%21 = bitcast i32** %8 to i8*
%22 = call i64 @malloc(i32 2097152)
%23 = inttoptr i64 %22 to i8*
%24 = call i64 @malloc(i32 60)
%25 = inttoptr i64 %24 to i8*
%26 = call i64 @malloc(i32 512)
%27 = inttoptr i64 %26 to i8*
%28 = call i32 @sqlite3_open(i8* %10, i32** %7)
%29 = load i32, i32* @SQLITE_OK
%30 = icmp eq i32 %28, %29
br i1 %30, label %41, label %31
31: ; preds = %19
%32 = load i32, i32* @LOG_INFO
%33 = load i32, i32* @LOG_LOCAL6
%34 = or i32 %33, %32
%35 = load i32*, i32** %7
%36 = call i32 @sqlite3_errmsg(i32* %35)
%37 = call i32 @syslog(i32 %34, i8* getelementptr inbounds ([79 x i8], [79 x i8]* @.str.3, i64 0, i64 0), i8* %10, i32 %36)
%38 = call i32 @free(i8* %25)
%39 = call i32 @free(i8* %27)
%40 = call i32 @free(i8* %23)
br label %88
41: ; preds = %19
%42 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %27, i32 512, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0))
%43 = load i32*, i32** %7
%44 = call i32 @strlen(i8* %27)
%45 = call i32 @sqlite3_prepare_v2(i32* %43, i8* %27, i32 %44, i32** %8, i32* null)
%46 = load i32*, i32** %8
%47 = call i32 @sqlite3_bind_int(i32* %46, i32 1, i32 %0)
store i8 0, i8* %23
%48 = load i32*, i32** %8
%49 = call i32 @sqlite3_step(i32* %48)
%50 = load i32, i32* @SQLITE_ROW
%51 = icmp eq i32 %49, %50
br i1 %51, label %52, label %69
52: ; preds = %41, %52
%53 = load i32*, i32** %8
%54 = call i32 @sqlite3_column_int(i32* %53, i32 0)
%55 = load i32*, i32** %8
%56 = call i32 @sqlite3_column_int(i32* %55, i32 1)
%57 = load i32*, i32** %8
%58 = call i32 @sqlite3_column_int(i32* %57, i32 2)
%59 = load i32*, i32** %8
%60 = call i32 @sqlite3_column_int(i32* %59, i32 3)
%61 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %25, i32 60, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.6, i64 0, i64 0), i32 %54, i32 %56, i32 %58, i32 %60)
%62 = call i32 @strlen(i8* %23)
%63 = sub i32 2097151, %62
%64 = call i32 @strncat(i8* %23, i8* %25, i32 %63)
%65 = load i32*, i32** %8
%66 = call i32 @sqlite3_step(i32* %65)
%67 = load i32, i32* @SQLITE_ROW
%68 = icmp eq i32 %66, %67
br i1 %68, label %52, label %69
69: ; preds = %52, %41
%70 = call i32 @strlen(i8* %23)
%71 = sext i32 %70 to i64
%72 = getelementptr inbounds i8, i8* %23, i64 %71
store i8 0, i8* %72
%73 = load i32*, i32** %8
%74 = call i32 @sqlite3_finalize(i32* %73)
%75 = load i32*, i32** %7
%76 = call i32 @sqlite3_close(i32* %75)
%77 = add nsw i64 %71, 1
%78 = icmp ugt i64 %77, %2
br i1 %78, label %79, label %83
79: ; preds = %69
%80 = call i32 @free(i8* %25)
%81 = call i32 @free(i8* %27)
%82 = call i32 @free(i8* %23)
br label %88
83: ; preds = %69
%84 = call i32 @memcpy(i8* %1, i8* %23, i64 %77)
%85 = call i32 @free(i8* %25)
%86 = call i32 @free(i8* %27)
%87 = call i32 @free(i8* %23)
br label %88
88: ; preds = %79, %83, %31
%89 = phi i32 [ 1, %31 ], [ 1, %79 ], [ 0, %83 ]
br label %90
90: ; preds = %14, %88
%91 = phi i32 [ %89, %88 ], [ 1, %14 ]
ret i32 %91
}