#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;

/* Variables and functions */
int SQLITE_OK;
int SQLITE_ROW;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ printf(char *, ...);
int sqlite3_column_count(int /*<<< orphan*/ *);
scalar_t__ sqlite3_column_int(int /*<<< orphan*/ *, int);
char *sqlite3_column_name(int /*<<< orphan*/ *, int);
scalar_t__ sqlite3_column_text(int /*<<< orphan*/ *, int);
int sqlite3_column_type(int /*<<< orphan*/ *, int);
char *sqlite3_errmsg(int /*<<< orphan*/ *);
int /*<<< orphan*/ sqlite3_finalize(int /*<<< orphan*/ *);
int sqlite3_prepare_v2(int /*<<< orphan*/ *, char *, int, int /*<<< orphan*/ **,
                       int /*<<< orphan*/);
int sqlite3_step(int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
scalar_t__ strlen(char *);
int query(sqlite3 *db, sqlite3_stmt *stmt) {
  char *selectSql = "select * from student order by id desc limit 0,10;";
  int res = sqlite3_prepare_v2(db, selectSql, (int)strlen(selectSql), &stmt, 0);
  if (res != SQLITE_OK) {
    printf("sqlite3_prepare_v2 error\n");
    fprintf(stderr, "prepare error %s\n", sqlite3_errmsg(db));
    return -1;
  }
  res = sqlite3_step(stmt);
  int count = sqlite3_column_count(stmt);
  printf("count = %d\n", count);
  int i;
  for (i = 0; i < count; i++) {
    printf("column name:%s - type %d\n", sqlite3_column_name(stmt, i),
           sqlite3_column_type(stmt, i));
  }
  unsigned int id;
  const char *name;
  unsigned int age;
  const char *address;
  while (res == SQLITE_ROW) {
    id = (unsigned int)sqlite3_column_int(stmt, 0);
    name = (char *)sqlite3_column_text(stmt, 1);
    age = (unsigned int)sqlite3_column_int(stmt, 2);
    address = (char *)sqlite3_column_text(stmt, 3);
    printf("id = %d,name = %s,age = %d,address = %s\n", id, name, age, address);
    res = sqlite3_step(stmt);
  }
  if (stmt) {
    sqlite3_finalize(stmt);
    printf("query sqlite3_finalize\n");
  }
  return res;
}


x86 assembly (clang, -O0):
<query>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
lea    0xe29(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x58(%rbp),%rdi
mov    -0x50(%rbp),%rsi
mov    %eax,%edx
lea    -0x18(%rbp),%rcx
xor    %r8d,%r8d
call   10e0 <sqlite3_prepare_v2@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    0x2dd1(%rip),%rcx
cmp    (%rcx),%eax
je     1260 <query+0xa0>
lea    0xe0d(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2da4(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x5c(%rbp)
mov    -0x10(%rbp),%rdi
call   1070 <sqlite3_errmsg@plt>
mov    -0x5c(%rbp),%edi
mov    %rax,%rdx
lea    0xdfe(%rip),%rsi
call   10a0 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1389 <query+0x1c9>
mov    -0x18(%rbp),%rdi
call   1090 <sqlite3_step@plt>
mov    %eax,-0x24(%rbp)
mov    -0x18(%rbp),%rdi
call   10b0 <sqlite3_column_count@plt>
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%esi
lea    0xddd(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x28(%rbp),%eax
jge    12da <query+0x11a>
mov    -0x18(%rbp),%rdi
mov    -0x2c(%rbp),%esi
call   1030 <sqlite3_column_name@plt>
mov    %rax,-0x68(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x2c(%rbp),%esi
call   1080 <sqlite3_column_type@plt>
mov    -0x68(%rbp),%rsi
mov    %eax,%edx
lea    0xda6(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    1290 <query+0xd0>
jmp    12df <query+0x11f>
mov    -0x24(%rbp),%eax
mov    0x2ce7(%rip),%rcx
cmp    (%rcx),%eax
jne    1361 <query+0x1a1>
mov    -0x18(%rbp),%rdi
xor    %esi,%esi
call   1060 <sqlite3_column_int@plt>
mov    %eax,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
mov    $0x1,%esi
call   10c0 <sqlite3_column_text@plt>
mov    %rax,-0x38(%rbp)
mov    -0x18(%rbp),%rdi
mov    $0x2,%esi
call   1060 <sqlite3_column_int@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x18(%rbp),%rdi
mov    $0x3,%esi
call   10c0 <sqlite3_column_text@plt>
mov    %rax,-0x48(%rbp)
mov    -0x30(%rbp),%esi
mov    -0x38(%rbp),%rdx
mov    -0x3c(%rbp),%ecx
mov    -0x48(%rbp),%r8
lea    0xd3c(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1090 <sqlite3_step@plt>
mov    %eax,-0x24(%rbp)
jmp    12df <query+0x11f>
cmpq   $0x0,-0x18(%rbp)
je     1383 <query+0x1c3>
mov    -0x18(%rbp),%rdi
call   10d0 <sqlite3_finalize@plt>
lea    0xd32(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<query>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    %rsi,(%rsp)
lea    0xe27(%rip),%rsi
mov    %rsp,%rcx
mov    $0x32,%edx
xor    %r8d,%r8d
call   10e0 <sqlite3_prepare_v2@plt>
mov    0x2df8(%rip),%rcx
cmp    (%rcx),%eax
jne    1301 <query+0x141>
mov    (%rsp),%rdi
call   1090 <sqlite3_step@plt>
mov    %eax,%r12d
mov    (%rsp),%rdi
call   10b0 <sqlite3_column_count@plt>
mov    %eax,%r15d
lea    0xe2e(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
test   %r15d,%r15d
jle    1260 <query+0xa0>
lea    0xe25(%rip),%r14
xor    %ebp,%ebp
xchg   %ax,%ax
mov    (%rsp),%rdi
mov    %ebp,%esi
call   1040 <sqlite3_column_name@plt>
mov    %rax,%rbx
mov    (%rsp),%rdi
mov    %ebp,%esi
call   1080 <sqlite3_column_type@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1050 <printf@plt>
add    $0x1,%ebp
cmp    %ebp,%r15d
jne    1230 <query+0x70>
mov    0x2d69(%rip),%r13
cmp    0x0(%r13),%r12d
jne    12e5 <query+0x125>
lea    0xdf7(%rip),%r14
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    (%rsp),%rdi
xor    %esi,%esi
call   1060 <sqlite3_column_int@plt>
mov    %rax,%r15
mov    (%rsp),%rdi
mov    $0x1,%esi
call   10c0 <sqlite3_column_text@plt>
mov    %rax,%rbp
mov    (%rsp),%rdi
mov    $0x2,%esi
call   1060 <sqlite3_column_int@plt>
mov    %rax,%rbx
mov    (%rsp),%rdi
mov    $0x3,%esi
call   10c0 <sqlite3_column_text@plt>
mov    %r14,%rdi
mov    %r15d,%esi
mov    %rbp,%rdx
mov    %ebx,%ecx
mov    %rax,%r8
xor    %eax,%eax
call   1050 <printf@plt>
mov    (%rsp),%rdi
call   1090 <sqlite3_step@plt>
cmp    0x0(%r13),%eax
je     1280 <query+0xc0>
mov    %eax,%r12d
mov    (%rsp),%rdi
test   %rdi,%rdi
je     1335 <query+0x175>
call   10d0 <sqlite3_finalize@plt>
lea    0xd9a(%rip),%rdi
call   1030 <puts@plt>
jmp    1335 <query+0x175>
lea    0xda3(%rip),%rdi
call   1030 <puts@plt>
mov    0x2cc4(%rip),%rax
mov    (%rax),%ebp
mov    %rbx,%rdi
call   1070 <sqlite3_errmsg@plt>
lea    0xd0e(%rip),%rsi
mov    %ebp,%edi
mov    %rax,%rdx
call   10a0 <fprintf@plt>
mov    $0xffffffff,%r12d
mov    %r12d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [51 x i8] c"select * from student order by id desc limit 0,10;\00"
@SQLITE_OK = global i32 0
@.str.1 = [26 x i8] c"sqlite3_prepare_v2 error\0A\00"
@stderr = global i32 0
@.str.2 = [18 x i8] c"prepare error %s\0A\00"
@.str.3 = [12 x i8] c"count = %d\0A\00"
@.str.4 = [26 x i8] c"column name:%s - type %d\0A\00"
@SQLITE_ROW = global i32 0
@.str.5 = [41 x i8] c"id = %d,name = %s,age = %d,address = %s\0A\00"
@.str.6 = [24 x i8] c"query sqlite3_finalize\0A\00"
define i32 @query(i32* %0, i32* %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i32*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i32
%13 = alloca i8*
store i32* %0, i32** %4
store i32* %1, i32** %5
store i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str, i64 0, i64 0), i8** %6
%14 = load i32*, i32** %4
%15 = load i8*, i8** %6
%16 = load i8*, i8** %6
%17 = call i64 @strlen(i8* %16)
%18 = trunc i64 %17 to i32
%19 = call i32 @sqlite3_prepare_v2(i32* %14, i8* %15, i32 %18, i32** %5, i32 0)
store i32 %19, i32* %7
%20 = load i32, i32* %7
%21 = load i32, i32* @SQLITE_OK
%22 = icmp ne i32 %20, %21
br i1 %22, label %23, label %29
23: ; preds = %2
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
%25 = load i32, i32* @stderr
%26 = load i32*, i32** %4
%27 = call i8* @sqlite3_errmsg(i32* %26)
%28 = call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i8* %27)
store i32 -1, i32* %3
br label %85
29: ; preds = %2
%30 = load i32*, i32** %5
%31 = call i32 @sqlite3_step(i32* %30)
store i32 %31, i32* %7
%32 = load i32*, i32** %5
%33 = call i32 @sqlite3_column_count(i32* %32)
store i32 %33, i32* %8
%34 = load i32, i32* %8
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i32 %34)
store i32 0, i32* %9
br label %36
36: ; preds = %48, %29
%37 = load i32, i32* %9
%38 = load i32, i32* %8
%39 = icmp slt i32 %37, %38
br i1 %39, label %40, label %51
40: ; preds = %36
%41 = load i32*, i32** %5
%42 = load i32, i32* %9
%43 = call i8* @sqlite3_column_name(i32* %41, i32 %42)
%44 = load i32*, i32** %5
%45 = load i32, i32* %9
%46 = call i32 @sqlite3_column_type(i32* %44, i32 %45)
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0), i8* %43, i32 %46)
br label %48
48: ; preds = %40
%49 = load i32, i32* %9
%50 = add nsw i32 %49, 1
store i32 %50, i32* %9
br label %36
51: ; preds = %36
br label %52
52: ; preds = %56, %51
%53 = load i32, i32* %7
%54 = load i32, i32* @SQLITE_ROW
%55 = icmp eq i32 %53, %54
br i1 %55, label %56, label %76
56: ; preds = %52
%57 = load i32*, i32** %5
%58 = call i64 @sqlite3_column_int(i32* %57, i32 0)
%59 = trunc i64 %58 to i32
store i32 %59, i32* %10
%60 = load i32*, i32** %5
%61 = call i64 @sqlite3_column_text(i32* %60, i32 1)
%62 = inttoptr i64 %61 to i8*
store i8* %62, i8** %11
%63 = load i32*, i32** %5
%64 = call i64 @sqlite3_column_int(i32* %63, i32 2)
%65 = trunc i64 %64 to i32
store i32 %65, i32* %12
%66 = load i32*, i32** %5
%67 = call i64 @sqlite3_column_text(i32* %66, i32 3)
%68 = inttoptr i64 %67 to i8*
store i8* %68, i8** %13
%69 = load i32, i32* %10
%70 = load i8*, i8** %11
%71 = load i32, i32* %12
%72 = load i8*, i8** %13
%73 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.5, i64 0, i64 0), i32 %69, i8* %70, i32 %71, i8* %72)
%74 = load i32*, i32** %5
%75 = call i32 @sqlite3_step(i32* %74)
store i32 %75, i32* %7
br label %52
76: ; preds = %52
%77 = load i32*, i32** %5
%78 = icmp ne i32* %77, null
br i1 %78, label %79, label %83
79: ; preds = %76
%80 = load i32*, i32** %5
%81 = call i32 @sqlite3_finalize(i32* %80)
%82 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.6, i64 0, i64 0))
br label %83
83: ; preds = %79, %76
%84 = load i32, i32* %7
store i32 %84, i32* %3
br label %85
85: ; preds = %83, %23
%86 = load i32, i32* %3
ret i32 %86
}

LLVM IR (-O2):
@.str = [51 x i8] c"select * from student order by id desc limit 0,10;\00"
@SQLITE_OK = global i32 0
@stderr = global i32 0
@.str.2 = [18 x i8] c"prepare error %s\0A\00"
@.str.3 = [12 x i8] c"count = %d\0A\00"
@.str.4 = [26 x i8] c"column name:%s - type %d\0A\00"
@SQLITE_ROW = global i32 0
@.str.5 = [41 x i8] c"id = %d,name = %s,age = %d,address = %s\0A\00"
@str = [23 x i8] c"query sqlite3_finalize\00"
@str.7 = [25 x i8] c"sqlite3_prepare_v2 error\00"
define i32 @query(i32* %0, i32* %1) {
%3 = alloca i32*
store i32* %1, i32** %3
%4 = call i32 @sqlite3_prepare_v2(i32* %0, i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str, i64 0, i64 0), i32 50, i32** %3, i32 0)
%5 = load i32, i32* @SQLITE_OK
%6 = icmp eq i32 %4, %5
br i1 %6, label %12, label %7
7: ; preds = %2
%8 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str.7, i64 0, i64 0))
%9 = load i32, i32* @stderr
%10 = call i8* @sqlite3_errmsg(i32* %0)
%11 = call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i8* %10)
br label %56
12: ; preds = %2
%13 = load i32*, i32** %3
%14 = call i32 @sqlite3_step(i32* %13)
%15 = load i32*, i32** %3
%16 = call i32 @sqlite3_column_count(i32* %15)
%17 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i32 %16)
%18 = icmp sgt i32 %16, 0
br i1 %18, label %22, label %19
19: ; preds = %22, %12
%20 = load i32, i32* @SQLITE_ROW
%21 = icmp eq i32 %14, %20
br i1 %21, label %31, label %49
22: ; preds = %12, %22
%23 = phi i32 [ %29, %22 ], [ 0, %12 ]
%24 = load i32*, i32** %3
%25 = call i8* @sqlite3_column_name(i32* %24, i32 %23)
%26 = load i32*, i32** %3
%27 = call i32 @sqlite3_column_type(i32* %26, i32 %23)
%28 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0), i8* %25, i32 %27)
%29 = add nuw nsw i32 %23, 1
%30 = icmp eq i32 %29, %16
br i1 %30, label %19, label %22
31: ; preds = %19, %31
%32 = load i32*, i32** %3
%33 = call i64 @sqlite3_column_int(i32* %32, i32 0)
%34 = trunc i64 %33 to i32
%35 = load i32*, i32** %3
%36 = call i64 @sqlite3_column_text(i32* %35, i32 1)
%37 = inttoptr i64 %36 to i8*
%38 = load i32*, i32** %3
%39 = call i64 @sqlite3_column_int(i32* %38, i32 2)
%40 = trunc i64 %39 to i32
%41 = load i32*, i32** %3
%42 = call i64 @sqlite3_column_text(i32* %41, i32 3)
%43 = inttoptr i64 %42 to i8*
%44 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @.str.5, i64 0, i64 0), i32 %34, i8* %37, i32 %40, i8* %43)
%45 = load i32*, i32** %3
%46 = call i32 @sqlite3_step(i32* %45)
%47 = load i32, i32* @SQLITE_ROW
%48 = icmp eq i32 %46, %47
br i1 %48, label %31, label %49
49: ; preds = %31, %19
%50 = phi i32 [ %14, %19 ], [ %46, %31 ]
%51 = load i32*, i32** %3
%52 = icmp eq i32* %51, null
br i1 %52, label %56, label %53
53: ; preds = %49
%54 = call i32 @sqlite3_finalize(i32* %51)
%55 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str, i64 0, i64 0))
br label %56
56: ; preds = %49, %53, %7
%57 = phi i32 [ -1, %7 ], [ %50, %53 ], [ %50, %49 ]
ret i32 %57
}