#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_bind_text(int /*<<< orphan*/ *, int, char const *,
                                     int, int /*<<< orphan*/);
int /*<<< orphan*/ sqlite3_close(int /*<<< orphan*/ *);
int /*<<< orphan*/ sqlite3_errmsg(int /*<<< orphan*/ *);
int /*<<< orphan*/ sqlite3_finalize(int /*<<< orphan*/ *);
int sqlite3_last_insert_rowid(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*/);
scalar_t__ time(int /*<<< orphan*/ *);
int sqlite_add_host(const char *the_ip, 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");
    }
  }
  int ret;
  int now;
  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_add_host]: %s",
           DB_LOCATION, sqlite3_errmsg(db));
    return 1;
  }
  snprintf(sql, 512,
           "INSERT INTO %s (host,first_seen,last_seen) VALUES (?1,?2,?3)",
           "hosts_table");
  sqlite3_prepare_v2(db, sql, strlen(sql), &stmt, NULL);
  sqlite3_bind_text(stmt, 1, the_ip, -1, 0);
  now = (int)time(NULL);
  sqlite3_bind_int(stmt, 2, now);
  sqlite3_bind_int(stmt, 3, now);
  rc = sqlite3_step(stmt);
  if (rc != SQLITE_DONE) {
    sqlite3_finalize(stmt);
    sqlite3_close(db);
    return -1;
  }
  ret = sqlite3_last_insert_rowid(db);
  sqlite3_finalize(stmt);
  sqlite3_close(db);
  return ret;
}


x86 assembly (clang, -O0):
<sqlite_add_host>:
push   %rbp
mov    %rsp,%rbp
sub    $0x570,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1221 <sqlite_add_host+0x41>
lea    -0x330(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    $0x200,%esi
lea    0xe4c(%rip),%rdx
mov    $0x0,%al
call   1060 <snprintf@plt>
jmp    1275 <sqlite_add_host+0x95>
lea    -0x120(%rbp),%rdi
mov    $0x100,%esi
call   1070 <getcwd@plt>
cmp    $0x0,%rax
jne    1248 <sqlite_add_host+0x68>
movl   $0x1,-0x4(%rbp)
jmp    143e <sqlite_add_host+0x25e>
lea    -0x330(%rbp),%rdi
lea    -0x120(%rbp),%rcx
mov    $0x200,%esi
lea    0xd9e(%rip),%rdx
lea    0xd9c(%rip),%r8
mov    $0x0,%al
call   1060 <snprintf@plt>
jmp    1275 <sqlite_add_host+0x95>
lea    -0x330(%rbp),%rdi
lea    -0x340(%rbp),%rsi
call   1030 <sqlite3_open@plt>
mov    %eax,-0x34c(%rbp)
mov    -0x34c(%rbp),%eax
mov    0x2d45(%rip),%rcx
cmp    (%rcx),%eax
je     12fc <sqlite_add_host+0x11c>
mov    0x2d1e(%rip),%rax
mov    (%rax),%eax
mov    0x2d35(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x55c(%rbp)
lea    -0x330(%rbp),%rax
mov    %rax,-0x558(%rbp)
mov    -0x340(%rbp),%rdi
call   1080 <sqlite3_errmsg@plt>
mov    -0x55c(%rbp),%edi
mov    -0x558(%rbp),%rdx
mov    %eax,%ecx
lea    0xd38(%rip),%rsi
call   10c0 <syslog@plt>
movl   $0x1,-0x4(%rbp)
jmp    143e <sqlite_add_host+0x25e>
lea    -0x550(%rbp),%rdi
mov    $0x200,%esi
lea    0xd55(%rip),%rdx
lea    0xd8b(%rip),%rcx
mov    $0x0,%al
call   1060 <snprintf@plt>
mov    -0x340(%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    -0x348(%rbp),%rcx
xor    %eax,%eax
mov    %eax,%r8d
call   10f0 <sqlite3_prepare_v2@plt>
mov    -0x348(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0x1,%esi
mov    $0xffffffff,%ecx
xor    %r8d,%r8d
call   10d0 <sqlite3_bind_text@plt>
xor    %eax,%eax
mov    %eax,%edi
call   10b0 <time@plt>
mov    %eax,-0x338(%rbp)
mov    -0x348(%rbp),%rdi
mov    -0x338(%rbp),%edx
mov    $0x2,%esi
call   1040 <sqlite3_bind_int@plt>
mov    -0x348(%rbp),%rdi
mov    -0x338(%rbp),%edx
mov    $0x3,%esi
call   1040 <sqlite3_bind_int@plt>
mov    -0x348(%rbp),%rdi
call   1090 <sqlite3_step@plt>
mov    %eax,-0x34c(%rbp)
mov    -0x34c(%rbp),%eax
mov    0x2bf1(%rip),%rcx
cmp    (%rcx),%eax
je     140b <sqlite_add_host+0x22b>
mov    -0x348(%rbp),%rdi
call   10e0 <sqlite3_finalize@plt>
mov    -0x340(%rbp),%rdi
call   10a0 <sqlite3_close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    143e <sqlite_add_host+0x25e>
mov    -0x340(%rbp),%rdi
call   1100 <sqlite3_last_insert_rowid@plt>
mov    %eax,-0x334(%rbp)
mov    -0x348(%rbp),%rdi
call   10e0 <sqlite3_finalize@plt>
mov    -0x340(%rbp),%rdi
call   10a0 <sqlite3_close@plt>
mov    -0x334(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x570,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sqlite_add_host>:
push   %rbp
push   %r14
push   %rbx
sub    $0x520,%rsp
mov    %rdi,%r14
test   %rsi,%rsi
je     1213 <sqlite_add_host+0x33>
mov    %rsi,%rcx
lea    0xe64(%rip),%rdx
lea    0x110(%rsp),%rdi
mov    $0x200,%esi
xor    %eax,%eax
call   1060 <snprintf@plt>
jmp    1252 <sqlite_add_host+0x72>
lea    0x10(%rsp),%rdi
mov    $0x100,%esi
call   1070 <getcwd@plt>
test   %rax,%rax
je     1369 <sqlite_add_host+0x189>
lea    0xdce(%rip),%rdx
lea    0xdcc(%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    0x2d75(%rip),%rcx
cmp    (%rcx),%eax
jne    1335 <sqlite_add_host+0x155>
lea    0xdea(%rip),%rdx
lea    0xe20(%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   10f0 <sqlite3_prepare_v2@plt>
mov    (%rsp),%rdi
mov    $0xffffffff,%ebp
mov    $0x1,%esi
mov    %r14,%rdx
mov    $0xffffffff,%ecx
xor    %r8d,%r8d
call   10d0 <sqlite3_bind_text@plt>
xor    %edi,%edi
call   10b0 <time@plt>
mov    %rax,%rbx
mov    (%rsp),%rdi
mov    $0x2,%esi
mov    %ebx,%edx
call   1040 <sqlite3_bind_int@plt>
mov    (%rsp),%rdi
mov    $0x3,%esi
mov    %ebx,%edx
call   1040 <sqlite3_bind_int@plt>
mov    (%rsp),%rdi
call   1090 <sqlite3_step@plt>
mov    0x2cc0(%rip),%rcx
cmp    (%rcx),%eax
jne    1320 <sqlite_add_host+0x140>
mov    0x8(%rsp),%rdi
call   1100 <sqlite3_last_insert_rowid@plt>
mov    %eax,%ebp
mov    (%rsp),%rdi
call   10e0 <sqlite3_finalize@plt>
mov    0x8(%rsp),%rdi
call   10a0 <sqlite3_close@plt>
jmp    136e <sqlite_add_host+0x18e>
mov    0x2c8c(%rip),%rax
mov    0x2ca5(%rip),%rcx
mov    (%rcx),%ebx
or     (%rax),%ebx
mov    0x8(%rsp),%rdi
call   1080 <sqlite3_errmsg@plt>
lea    0xccb(%rip),%rsi
lea    0x110(%rsp),%rdx
mov    %ebx,%edi
mov    %eax,%ecx
call   10c0 <syslog@plt>
mov    $0x1,%ebp
mov    %ebp,%eax
add    $0x520,%rsp
pop    %rbx
pop    %r14
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 = [65 x i8] c"ERROR opening SQLite DB '%s' from function [sqlite_add_host]: %s\00"
@.str.4 = [61 x i8] c"INSERT INTO %s (host,first_seen,last_seen) VALUES (?1,?2,?3)\00"
@.str.5 = [12 x i8] c"hosts_table\00"
@SQLITE_DONE = global i32 0
define i32 @sqlite_add_host(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca [256 x i8]
%7 = alloca [513 x i8]
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32*
%11 = alloca i32*
%12 = alloca i32
%13 = alloca [512 x i8]
store i8* %0, i8** %4
store i8* %1, i8** %5
%14 = load i8*, i8** %5
%15 = icmp ne i8* %14, null
br i1 %15, label %16, label %20
16: ; preds = %2
%17 = getelementptr inbounds [513 x i8], [513 x i8]* %7, i64 0, i64 0
%18 = load i8*, i8** %5
%19 = call i32 (i8*, i32, i8*, i8*, ...) @snprintf(i8* %17, i32 512, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %18)
br label %30
20: ; preds = %2
%21 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%22 = call i32* @getcwd(i8* %21, i32 256)
%23 = icmp eq i32* %22, null
br i1 %23, label %24, label %25
24: ; preds = %20
store i32 1, i32* %3
br label %81
25: ; preds = %20
%26 = getelementptr inbounds [513 x i8], [513 x i8]* %7, i64 0, i64 0
%27 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%28 = call i32 (i8*, i32, i8*, i8*, ...) @snprintf(i8* %26, i32 512, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %27, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0))
br label %29
29: ; preds = %25
br label %30
30: ; preds = %29, %16
%31 = getelementptr inbounds [513 x i8], [513 x i8]* %7, i64 0, i64 0
%32 = call i32 @sqlite3_open(i8* %31, i32** %10)
store i32 %32, i32* %12
%33 = load i32, i32* %12
%34 = load i32, i32* @SQLITE_OK
%35 = icmp ne i32 %33, %34
br i1 %35, label %36, label %44
36: ; preds = %30
%37 = load i32, i32* @LOG_INFO
%38 = load i32, i32* @LOG_LOCAL6
%39 = or i32 %37, %38
%40 = getelementptr inbounds [513 x i8], [513 x i8]* %7, i64 0, i64 0
%41 = load i32*, i32** %10
%42 = call i32 @sqlite3_errmsg(i32* %41)
%43 = call i32 @syslog(i32 %39, i8* getelementptr inbounds ([65 x i8], [65 x i8]* @.str.3, i64 0, i64 0), i8* %40, i32 %42)
store i32 1, i32* %3
br label %81
44: ; preds = %30
%45 = getelementptr inbounds [512 x i8], [512 x i8]* %13, i64 0, i64 0
%46 = call i32 (i8*, i32, i8*, i8*, ...) @snprintf(i8* %45, i32 512, i8* getelementptr inbounds ([61 x i8], [61 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0))
%47 = load i32*, i32** %10
%48 = getelementptr inbounds [512 x i8], [512 x i8]* %13, i64 0, i64 0
%49 = getelementptr inbounds [512 x i8], [512 x i8]* %13, i64 0, i64 0
%50 = call i32 @strlen(i8* %49)
%51 = call i32 @sqlite3_prepare_v2(i32* %47, i8* %48, i32 %50, i32** %11, i32* null)
%52 = load i32*, i32** %11
%53 = load i8*, i8** %4
%54 = call i32 @sqlite3_bind_text(i32* %52, i32 1, i8* %53, i32 -1, i32 0)
%55 = call i64 @time(i32* null)
%56 = trunc i64 %55 to i32
store i32 %56, i32* %9
%57 = load i32*, i32** %11
%58 = load i32, i32* %9
%59 = call i32 @sqlite3_bind_int(i32* %57, i32 2, i32 %58)
%60 = load i32*, i32** %11
%61 = load i32, i32* %9
%62 = call i32 @sqlite3_bind_int(i32* %60, i32 3, i32 %61)
%63 = load i32*, i32** %11
%64 = call i32 @sqlite3_step(i32* %63)
store i32 %64, i32* %12
%65 = load i32, i32* %12
%66 = load i32, i32* @SQLITE_DONE
%67 = icmp ne i32 %65, %66
br i1 %67, label %68, label %73
68: ; preds = %44
%69 = load i32*, i32** %11
%70 = call i32 @sqlite3_finalize(i32* %69)
%71 = load i32*, i32** %10
%72 = call i32 @sqlite3_close(i32* %71)
store i32 -1, i32* %3
br label %81
73: ; preds = %44
%74 = load i32*, i32** %10
%75 = call i32 @sqlite3_last_insert_rowid(i32* %74)
store i32 %75, i32* %8
%76 = load i32*, i32** %11
%77 = call i32 @sqlite3_finalize(i32* %76)
%78 = load i32*, i32** %10
%79 = call i32 @sqlite3_close(i32* %78)
%80 = load i32, i32* %8
store i32 %80, i32* %3
br label %81
81: ; preds = %73, %68, %36, %24
%82 = load i32, i32* %3
ret i32 %82
}

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 = [65 x i8] c"ERROR opening SQLite DB '%s' from function [sqlite_add_host]: %s\00"
@.str.4 = [61 x i8] c"INSERT INTO %s (host,first_seen,last_seen) VALUES (?1,?2,?3)\00"
@.str.5 = [12 x i8] c"hosts_table\00"
@SQLITE_DONE = global i32 0
define i32 @sqlite_add_host(i8* %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 %63, 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 ([65 x i8], [65 x i8]* @.str.3, i64 0, i64 0), i8* %9, i32 %30)
br label %61
32: ; preds = %18
%33 = call i32 (i8*, i32, i8*, i8*, ...) @snprintf(i8* %21, i32 512, i8* getelementptr inbounds ([61 x i8], [61 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([12 x i8], [12 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_text(i32* %37, i32 1, i8* %0, i32 -1, i32 0)
%39 = call i64 @time(i32* null)
%40 = trunc i64 %39 to i32
%41 = load i32*, i32** %6
%42 = call i32 @sqlite3_bind_int(i32* %41, i32 2, i32 %40)
%43 = load i32*, i32** %6
%44 = call i32 @sqlite3_bind_int(i32* %43, i32 3, i32 %40)
%45 = load i32*, i32** %6
%46 = call i32 @sqlite3_step(i32* %45)
%47 = load i32, i32* @SQLITE_DONE
%48 = icmp eq i32 %46, %47
br i1 %48, label %54, label %49
49: ; preds = %32
%50 = load i32*, i32** %6
%51 = call i32 @sqlite3_finalize(i32* %50)
%52 = load i32*, i32** %5
%53 = call i32 @sqlite3_close(i32* %52)
br label %61
54: ; preds = %32
%55 = load i32*, i32** %5
%56 = call i32 @sqlite3_last_insert_rowid(i32* %55)
%57 = load i32*, i32** %6
%58 = call i32 @sqlite3_finalize(i32* %57)
%59 = load i32*, i32** %5
%60 = call i32 @sqlite3_close(i32* %59)
br label %61
61: ; preds = %54, %49, %25
%62 = phi i32 [ 1, %25 ], [ -1, %49 ], [ %56, %54 ]
br label %63
63: ; preds = %13, %61
%64 = phi i32 [ %62, %61 ], [ 1, %13 ]
ret i32 %64
}