#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*/ sql;

/* Variables and functions */
int SQLITE_ERROR;
int SQLITE_OK;
int SQLITE_ROW;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ sprintf(char *, char *, char *);
int /*<<< orphan*/ sqlite3_close(int /*<<< orphan*/ *);
char *sqlite3_errmsg(int /*<<< orphan*/ *);
int /*<<< orphan*/ sqlite3_finalize(int /*<<< orphan*/ *);
int sqlite3_open(char *, int /*<<< orphan*/ **);
int sqlite3_prepare(int /*<<< orphan*/ *, char *, int, int /*<<< orphan*/ **,
                    int /*<<< orphan*/);
int sqlite3_step(int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
int sql_select(char *domain) {
  sqlite3 *db;
  sqlite3_stmt *ppstmt = NULL;
  int rc;
  char sql[256] = {0};
  rc = sqlite3_open("/ailvgo/system/domain/domain.db", &db);
  if (rc == SQLITE_ERROR) {
    printf("open database failed!\n");
    sqlite3_close(db);
    return 0;
  }
  memset(sql, 0, sizeof(sql));
  sprintf(sql, "select * from domain_info where domain = '%s'", domain);
  rc = sqlite3_prepare(db, sql, -1, &ppstmt, 0);
  if (rc != SQLITE_OK) {
    fprintf(stderr, "SQL error:%s \n", sqlite3_errmsg(db));
    sqlite3_close(db);
    return 0;
  }
  rc = sqlite3_step(ppstmt);
  if (rc == SQLITE_ROW) {
    sqlite3_finalize(ppstmt);
    sqlite3_close(db);
    return 1;
  } else {
    sqlite3_finalize(ppstmt);
    sqlite3_close(db);
    return 0;
  }
}


x86 assembly (clang, -O0):
<sql_select>:
push   %rbp
mov    %rsp,%rbp
sub    $0x140,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x20(%rbp)
lea    -0x130(%rbp),%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1050 <memset@plt>
lea    0xe2f(%rip),%rdi
lea    -0x18(%rbp),%rsi
call   1030 <sqlite3_open@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    0x2df1(%rip),%rcx
cmp    (%rcx),%eax
jne    1210 <sql_select+0x70>
lea    0xe2a(%rip),%rdi
call   1040 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1090 <sqlite3_close@plt>
movl   $0x0,-0x4(%rbp)
jmp    1302 <sql_select+0x162>
lea    -0x130(%rbp),%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1050 <memset@plt>
lea    -0x130(%rbp),%rdi
mov    -0x10(%rbp),%rdx
lea    0xe02(%rip),%rsi
call   10c0 <sprintf@plt>
mov    -0x18(%rbp),%rdi
lea    -0x130(%rbp),%rsi
mov    $0xffffffff,%edx
lea    -0x20(%rbp),%rcx
xor    %r8d,%r8d
call   10a0 <sqlite3_prepare@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    0x2d85(%rip),%rcx
cmp    (%rcx),%eax
je     12ad <sql_select+0x10d>
mov    0x2d5e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x134(%rbp)
mov    -0x18(%rbp),%rdi
call   1060 <sqlite3_errmsg@plt>
mov    -0x134(%rbp),%edi
mov    %rax,%rdx
lea    0xdd2(%rip),%rsi
call   1080 <fprintf@plt>
mov    -0x18(%rbp),%rdi
call   1090 <sqlite3_close@plt>
movl   $0x0,-0x4(%rbp)
jmp    1302 <sql_select+0x162>
mov    -0x20(%rbp),%rdi
call   1070 <sqlite3_step@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    0x2d05(%rip),%rcx
cmp    (%rcx),%eax
jne    12e9 <sql_select+0x149>
mov    -0x20(%rbp),%rdi
call   10b0 <sqlite3_finalize@plt>
mov    -0x18(%rbp),%rdi
call   1090 <sqlite3_close@plt>
movl   $0x1,-0x4(%rbp)
jmp    1302 <sql_select+0x162>
mov    -0x20(%rbp),%rdi
call   10b0 <sqlite3_finalize@plt>
mov    -0x18(%rbp),%rdi
call   1090 <sqlite3_close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x140,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sql_select>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x118,%rsp
mov    %rdi,%r14
movq   $0x0,0x8(%rsp)
xorps  %xmm0,%xmm0
movaps %xmm0,0x100(%rsp)
movaps %xmm0,0xf0(%rsp)
movaps %xmm0,0xe0(%rsp)
movaps %xmm0,0xd0(%rsp)
movaps %xmm0,0xc0(%rsp)
movaps %xmm0,0xb0(%rsp)
movaps %xmm0,0xa0(%rsp)
movaps %xmm0,0x90(%rsp)
movaps %xmm0,0x80(%rsp)
movaps %xmm0,0x70(%rsp)
movaps %xmm0,0x60(%rsp)
movaps %xmm0,0x50(%rsp)
movaps %xmm0,0x40(%rsp)
movaps %xmm0,0x30(%rsp)
movaps %xmm0,0x20(%rsp)
movaps %xmm0,0x10(%rsp)
lea    0xdd2(%rip),%rdi
mov    %rsp,%rsi
call   1040 <sqlite3_open@plt>
mov    0x2d9b(%rip),%rcx
cmp    (%rcx),%eax
jne    125d <sql_select+0xbd>
lea    0xe15(%rip),%rdi
call   1030 <puts@plt>
mov    (%rsp),%rdi
call   1090 <sqlite3_close@plt>
xor    %ebx,%ebx
jmp    12f9 <sql_select+0x159>
lea    0x10(%rsp),%r15
xor    %ebx,%ebx
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1050 <memset@plt>
lea    0xda6(%rip),%rsi
mov    %r15,%rdi
mov    %r14,%rdx
call   10c0 <sprintf@plt>
mov    (%rsp),%rdi
lea    0x8(%rsp),%rcx
mov    %r15,%rsi
mov    $0xffffffff,%edx
xor    %r8d,%r8d
call   10a0 <sqlite3_prepare@plt>
mov    0x2d43(%rip),%rcx
cmp    (%rcx),%eax
jne    12cd <sql_select+0x12d>
mov    0x8(%rsp),%rdi
call   1070 <sqlite3_step@plt>
mov    0x2d0e(%rip),%rcx
xor    %ebx,%ebx
cmp    (%rcx),%eax
sete   %bl
mov    0x8(%rsp),%rdi
call   10b0 <sqlite3_finalize@plt>
jmp    12f0 <sql_select+0x150>
mov    0x2cfc(%rip),%rax
mov    (%rax),%ebp
mov    (%rsp),%rdi
call   1060 <sqlite3_errmsg@plt>
lea    0xd68(%rip),%rsi
mov    %ebp,%edi
mov    %rax,%rdx
call   1080 <fprintf@plt>
mov    (%rsp),%rdi
call   1090 <sqlite3_close@plt>
mov    %ebx,%eax
add    $0x118,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [32 x i8] c"/ailvgo/system/domain/domain.db\00"
@SQLITE_ERROR = global i32 0
@.str.1 = [23 x i8] c"open database failed!\0A\00"
@.str.2 = [46 x i8] c"select * from domain_info where domain = '%s'\00"
@SQLITE_OK = global i32 0
@stderr = global i32 0
@.str.3 = [15 x i8] c"SQL error:%s \0A\00"
@SQLITE_ROW = global i32 0
define i32 @sql_select(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i32*
%6 = alloca i32
%7 = alloca [256 x i8]
store i8* %0, i8** %3
store i32* null, i32** %5
%8 = bitcast [256 x i8]* %7 to i8*
%9 = call i32 @sqlite3_open(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i32** %4)
store i32 %9, i32* %6
%10 = load i32, i32* %6
%11 = load i32, i32* @SQLITE_ERROR
%12 = icmp eq i32 %10, %11
br i1 %12, label %13, label %17
13: ; preds = %1
%14 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
%15 = load i32*, i32** %4
%16 = call i32 @sqlite3_close(i32* %15)
store i32 0, i32* %2
br label %52
17: ; preds = %1
%18 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%19 = call i32 @memset(i8* %18, i32 0, i32 256)
%20 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%21 = load i8*, i8** %3
%22 = call i32 @sprintf(i8* %20, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.2, i64 0, i64 0), i8* %21)
%23 = load i32*, i32** %4
%24 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%25 = call i32 @sqlite3_prepare(i32* %23, i8* %24, i32 -1, i32** %5, i32 0)
store i32 %25, i32* %6
%26 = load i32, i32* %6
%27 = load i32, i32* @SQLITE_OK
%28 = icmp ne i32 %26, %27
br i1 %28, label %29, label %36
29: ; preds = %17
%30 = load i32, i32* @stderr
%31 = load i32*, i32** %4
%32 = call i8* @sqlite3_errmsg(i32* %31)
%33 = call i32 @fprintf(i32 %30, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i8* %32)
%34 = load i32*, i32** %4
%35 = call i32 @sqlite3_close(i32* %34)
store i32 0, i32* %2
br label %52
36: ; preds = %17
%37 = load i32*, i32** %5
%38 = call i32 @sqlite3_step(i32* %37)
store i32 %38, i32* %6
%39 = load i32, i32* %6
%40 = load i32, i32* @SQLITE_ROW
%41 = icmp eq i32 %39, %40
br i1 %41, label %42, label %47
42: ; preds = %36
%43 = load i32*, i32** %5
%44 = call i32 @sqlite3_finalize(i32* %43)
%45 = load i32*, i32** %4
%46 = call i32 @sqlite3_close(i32* %45)
store i32 1, i32* %2
br label %52
47: ; preds = %36
%48 = load i32*, i32** %5
%49 = call i32 @sqlite3_finalize(i32* %48)
%50 = load i32*, i32** %4
%51 = call i32 @sqlite3_close(i32* %50)
store i32 0, i32* %2
br label %52
52: ; preds = %47, %42, %29, %13
%53 = load i32, i32* %2
ret i32 %53
}

LLVM IR (-O2):
@.str = [32 x i8] c"/ailvgo/system/domain/domain.db\00"
@SQLITE_ERROR = global i32 0
@.str.2 = [46 x i8] c"select * from domain_info where domain = '%s'\00"
@SQLITE_OK = global i32 0
@stderr = global i32 0
@.str.3 = [15 x i8] c"SQL error:%s \0A\00"
@SQLITE_ROW = global i32 0
@str = [22 x i8] c"open database failed!\00"
define i32 @sql_select(i8* %0) {
%2 = alloca i32*
%3 = alloca i32*
%4 = alloca [256 x i8]
%5 = bitcast i32** %2 to i8*
%6 = bitcast i32** %3 to i8*
store i32* null, i32** %3
%7 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%8 = call i32 @sqlite3_open(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i32** %2)
%9 = load i32, i32* @SQLITE_ERROR
%10 = icmp eq i32 %8, %9
br i1 %10, label %11, label %15
11: ; preds = %1
%12 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str, i64 0, i64 0))
%13 = load i32*, i32** %2
%14 = call i32 @sqlite3_close(i32* %13)
br label %39
15: ; preds = %1
%16 = call i32 @memset(i8* %7, i32 0, i32 256)
%17 = call i32 @sprintf(i8* %7, i8* dereferenceable(1) getelementptr inbounds ([46 x i8], [46 x i8]* @.str.2, i64 0, i64 0), i8* %0)
%18 = load i32*, i32** %2
%19 = call i32 @sqlite3_prepare(i32* %18, i8* %7, i32 -1, i32** %3, i32 0)
%20 = load i32, i32* @SQLITE_OK
%21 = icmp eq i32 %19, %20
br i1 %21, label %29, label %22
22: ; preds = %15
%23 = load i32, i32* @stderr
%24 = load i32*, i32** %2
%25 = call i8* @sqlite3_errmsg(i32* %24)
%26 = call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i8* %25)
%27 = load i32*, i32** %2
%28 = call i32 @sqlite3_close(i32* %27)
br label %39
29: ; preds = %15
%30 = load i32*, i32** %3
%31 = call i32 @sqlite3_step(i32* %30)
%32 = load i32, i32* @SQLITE_ROW
%33 = icmp eq i32 %31, %32
%34 = load i32*, i32** %3
%35 = call i32 @sqlite3_finalize(i32* %34)
%36 = load i32*, i32** %2
%37 = call i32 @sqlite3_close(i32* %36)
%38 = zext i1 %33 to i32
br label %39
39: ; preds = %29, %22, %11
%40 = phi i32 [ 0, %11 ], [ 0, %22 ], [ %38, %29 ]
ret i32 %40
}