#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_DONE;
int SQLITE_OK;
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 /*<<< 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_update_host_port_hit(int ip_addr_ix, int the_port, int add_cnt,
                                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 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_update_host_port_hit]: %s",
           DB_LOCATION, sqlite3_errmsg(db));
    return 1;
  }
  snprintf(
      sql, 512,
      "UPDATE %s SET hit_count = ?1 WHERE host_ix = ?2 AND port_number = ?3",
      "hosts_ports_hits");
  sqlite3_prepare_v2(db, sql, strlen(sql), &stmt, NULL);
  sqlite3_bind_int(stmt, 1, add_cnt);
  sqlite3_bind_int(stmt, 2, ip_addr_ix);
  sqlite3_bind_int(stmt, 3, the_port);
  rc = sqlite3_step(stmt);
  if (rc != SQLITE_DONE) {
    sqlite3_finalize(stmt);
    sqlite3_close(db);
    return -1;
  }
  sqlite3_finalize(stmt);
  sqlite3_close(db);
  return 0;
}


x86 assembly (clang, -O0):
<sqlite_update_host_port_hit>:
push   %rbp
mov    %rsp,%rbp
sub    $0x570,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %rcx,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11f6 <sqlite_update_host_port_hit+0x46>
lea    -0x330(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    $0x200,%esi
lea    0xe83(%rip),%rdx
mov    $0x0,%al
call   1060 <snprintf@plt>
jmp    124a <sqlite_update_host_port_hit+0x9a>
lea    -0x120(%rbp),%rdi
mov    $0x100,%esi
call   1070 <getcwd@plt>
cmp    $0x0,%rax
jne    121d <sqlite_update_host_port_hit+0x6d>
movl   $0x1,-0x4(%rbp)
jmp    13e1 <sqlite_update_host_port_hit+0x231>
lea    -0x330(%rbp),%rdi
lea    -0x120(%rbp),%rcx
mov    $0x200,%esi
lea    0xdc9(%rip),%rdx
lea    0xdc7(%rip),%r8
mov    $0x0,%al
call   1060 <snprintf@plt>
jmp    124a <sqlite_update_host_port_hit+0x9a>
lea    -0x330(%rbp),%rdi
lea    -0x338(%rbp),%rsi
call   1030 <sqlite3_open@plt>
mov    %eax,-0x344(%rbp)
mov    -0x344(%rbp),%eax
mov    0x2d70(%rip),%rcx
cmp    (%rcx),%eax
je     12d1 <sqlite_update_host_port_hit+0x121>
mov    0x2d49(%rip),%rax
mov    (%rax),%eax
mov    0x2d60(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x55c(%rbp)
lea    -0x330(%rbp),%rax
mov    %rax,-0x558(%rbp)
mov    -0x338(%rbp),%rdi
call   1080 <sqlite3_errmsg@plt>
mov    -0x55c(%rbp),%edi
mov    -0x558(%rbp),%rdx
mov    %eax,%ecx
lea    0xd63(%rip),%rsi
call   10b0 <syslog@plt>
movl   $0x1,-0x4(%rbp)
jmp    13e1 <sqlite_update_host_port_hit+0x231>
lea    -0x550(%rbp),%rdi
mov    $0x200,%esi
lea    0xd8c(%rip),%rdx
lea    0xdca(%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   10d0 <sqlite3_prepare_v2@plt>
mov    -0x340(%rbp),%rdi
mov    -0x10(%rbp),%edx
mov    $0x1,%esi
call   1040 <sqlite3_bind_int@plt>
mov    -0x340(%rbp),%rdi
mov    -0x8(%rbp),%edx
mov    $0x2,%esi
call   1040 <sqlite3_bind_int@plt>
mov    -0x340(%rbp),%rdi
mov    -0xc(%rbp),%edx
mov    $0x3,%esi
call   1040 <sqlite3_bind_int@plt>
mov    -0x340(%rbp),%rdi
call   1090 <sqlite3_step@plt>
mov    %eax,-0x344(%rbp)
mov    -0x344(%rbp),%eax
mov    0x2c3a(%rip),%rcx
cmp    (%rcx),%eax
je     13c2 <sqlite_update_host_port_hit+0x212>
mov    -0x340(%rbp),%rdi
call   10c0 <sqlite3_finalize@plt>
mov    -0x338(%rbp),%rdi
call   10a0 <sqlite3_close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13e1 <sqlite_update_host_port_hit+0x231>
mov    -0x340(%rbp),%rdi
call   10c0 <sqlite3_finalize@plt>
mov    -0x338(%rbp),%rdi
call   10a0 <sqlite3_close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x570,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sqlite_update_host_port_hit>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x520,%rsp
mov    %edx,%r12d
mov    %esi,%r14d
mov    %edi,%r15d
test   %rcx,%rcx
je     11ea <sqlite_update_host_port_hit+0x3a>
lea    0xe99(%rip),%rdx
lea    0x110(%rsp),%rdi
mov    $0x200,%esi
xor    %eax,%eax
call   1060 <snprintf@plt>
jmp    1229 <sqlite_update_host_port_hit+0x79>
lea    0x10(%rsp),%rdi
mov    $0x100,%esi
call   1070 <getcwd@plt>
test   %rax,%rax
je     1324 <sqlite_update_host_port_hit+0x174>
lea    0xdf7(%rip),%rdx
lea    0xdf5(%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    0x2d9e(%rip),%rcx
cmp    (%rcx),%eax
jne    12f0 <sqlite_update_host_port_hit+0x140>
lea    0xe1f(%rip),%rdx
lea    0xe5d(%rip),%rcx
lea    0x320(%rsp),%rbp
mov    %rbp,%rdi
mov    $0x200,%esi
xor    %eax,%eax
call   1060 <snprintf@plt>
mov    0x8(%rsp),%rbx
mov    %rbp,%rdi
call   1050 <strlen@plt>
mov    %rsp,%rcx
mov    %rbx,%rdi
mov    %rbp,%rsi
mov    %eax,%edx
xor    %r8d,%r8d
call   10d0 <sqlite3_prepare_v2@plt>
mov    (%rsp),%rdi
mov    $0x1,%esi
mov    %r12d,%edx
call   1040 <sqlite3_bind_int@plt>
mov    (%rsp),%rdi
mov    $0x2,%esi
mov    %r15d,%edx
call   1040 <sqlite3_bind_int@plt>
mov    (%rsp),%rdi
mov    $0x3,%esi
mov    %r14d,%edx
call   1040 <sqlite3_bind_int@plt>
mov    (%rsp),%rdi
call   1090 <sqlite3_step@plt>
mov    0x2cfe(%rip),%rcx
xor    %ebx,%ebx
cmp    (%rcx),%eax
setne  %bl
mov    (%rsp),%rdi
call   10c0 <sqlite3_finalize@plt>
mov    0x8(%rsp),%rdi
call   10a0 <sqlite3_close@plt>
neg    %ebx
jmp    1329 <sqlite_update_host_port_hit+0x179>
mov    0x2cd1(%rip),%rax
mov    0x2cea(%rip),%rcx
mov    (%rcx),%ebx
or     (%rax),%ebx
mov    0x8(%rsp),%rdi
call   1080 <sqlite3_errmsg@plt>
lea    0xd10(%rip),%rsi
lea    0x110(%rsp),%rdx
mov    %ebx,%edi
mov    %eax,%ecx
call   10b0 <syslog@plt>
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 = [77 x i8] c"ERROR opening SQLite DB '%s' from function [sqlite_update_host_port_hit]: %s\00"
@.str.4 = [69 x i8] c"UPDATE %s SET hit_count = ?1 WHERE host_ix = ?2 AND port_number = ?3\00"
@.str.5 = [17 x i8] c"hosts_ports_hits\00"
@SQLITE_DONE = global i32 0
define i32 @sqlite_update_host_port_hit(i32 %0, i32 %1, i32 %2, i8* %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca [256 x i8]
%11 = alloca [513 x i8]
%12 = alloca i32*
%13 = alloca i32*
%14 = alloca i32
%15 = alloca [512 x i8]
store i32 %0, i32* %6
store i32 %1, i32* %7
store i32 %2, i32* %8
store i8* %3, i8** %9
%16 = load i8*, i8** %9
%17 = icmp ne i8* %16, null
br i1 %17, label %18, label %22
18: ; preds = %4
%19 = getelementptr inbounds [513 x i8], [513 x i8]* %11, i64 0, i64 0
%20 = load i8*, i8** %9
%21 = call i32 (i8*, i32, i8*, i8*, ...) @snprintf(i8* %19, i32 512, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %20)
br label %32
22: ; preds = %4
%23 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%24 = call i32* @getcwd(i8* %23, i32 256)
%25 = icmp eq i32* %24, null
br i1 %25, label %26, label %27
26: ; preds = %22
store i32 1, i32* %5
br label %78
27: ; preds = %22
%28 = getelementptr inbounds [513 x i8], [513 x i8]* %11, i64 0, i64 0
%29 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%30 = call i32 (i8*, i32, i8*, i8*, ...) @snprintf(i8* %28, i32 512, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %29, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0))
br label %31
31: ; preds = %27
br label %32
32: ; preds = %31, %18
%33 = getelementptr inbounds [513 x i8], [513 x i8]* %11, i64 0, i64 0
%34 = call i32 @sqlite3_open(i8* %33, i32** %12)
store i32 %34, i32* %14
%35 = load i32, i32* %14
%36 = load i32, i32* @SQLITE_OK
%37 = icmp ne i32 %35, %36
br i1 %37, label %38, label %46
38: ; preds = %32
%39 = load i32, i32* @LOG_INFO
%40 = load i32, i32* @LOG_LOCAL6
%41 = or i32 %39, %40
%42 = getelementptr inbounds [513 x i8], [513 x i8]* %11, i64 0, i64 0
%43 = load i32*, i32** %12
%44 = call i32 @sqlite3_errmsg(i32* %43)
%45 = call i32 @syslog(i32 %41, i8* getelementptr inbounds ([77 x i8], [77 x i8]* @.str.3, i64 0, i64 0), i8* %42, i32 %44)
store i32 1, i32* %5
br label %78
46: ; preds = %32
%47 = getelementptr inbounds [512 x i8], [512 x i8]* %15, i64 0, i64 0
%48 = call i32 (i8*, i32, i8*, i8*, ...) @snprintf(i8* %47, i32 512, i8* getelementptr inbounds ([69 x i8], [69 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0))
%49 = load i32*, i32** %12
%50 = getelementptr inbounds [512 x i8], [512 x i8]* %15, i64 0, i64 0
%51 = getelementptr inbounds [512 x i8], [512 x i8]* %15, i64 0, i64 0
%52 = call i32 @strlen(i8* %51)
%53 = call i32 @sqlite3_prepare_v2(i32* %49, i8* %50, i32 %52, i32** %13, i32* null)
%54 = load i32*, i32** %13
%55 = load i32, i32* %8
%56 = call i32 @sqlite3_bind_int(i32* %54, i32 1, i32 %55)
%57 = load i32*, i32** %13
%58 = load i32, i32* %6
%59 = call i32 @sqlite3_bind_int(i32* %57, i32 2, i32 %58)
%60 = load i32*, i32** %13
%61 = load i32, i32* %7
%62 = call i32 @sqlite3_bind_int(i32* %60, i32 3, i32 %61)
%63 = load i32*, i32** %13
%64 = call i32 @sqlite3_step(i32* %63)
store i32 %64, i32* %14
%65 = load i32, i32* %14
%66 = load i32, i32* @SQLITE_DONE
%67 = icmp ne i32 %65, %66
br i1 %67, label %68, label %73
68: ; preds = %46
%69 = load i32*, i32** %13
%70 = call i32 @sqlite3_finalize(i32* %69)
%71 = load i32*, i32** %12
%72 = call i32 @sqlite3_close(i32* %71)
store i32 -1, i32* %5
br label %78
73: ; preds = %46
%74 = load i32*, i32** %13
%75 = call i32 @sqlite3_finalize(i32* %74)
%76 = load i32*, i32** %12
%77 = call i32 @sqlite3_close(i32* %76)
store i32 0, i32* %5
br label %78
78: ; preds = %73, %68, %38, %26
%79 = load i32, i32* %5
ret i32 %79
}

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