#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_DONE;
int SQLITE_ROW;
int matoi(char *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ snprintf(char *, int, char *);
scalar_t__ sqlite3_column_text(int /*<<< orphan*/ *, int /*<<< orphan*/);
int sqlite3_prepare_v2(int /*<<< orphan*/ *, char *, int, int /*<<< orphan*/ **,
                       int /*<<< orphan*/);
int sqlite3_step(int /*<<< orphan*/ *);
int getindex(sqlite3 *db, const int verb) {
  int ret = 0;
  char sql[512];
  sqlite3_stmt *stmt;
  snprintf(sql, 512, "SELECT COUNT(*) FROM id;");
  int dbop = sqlite3_prepare_v2(db, sql, -1, &stmt, 0);
  if (verb > 1)
    printf("Query: %s\n", sql);
  while ((dbop = sqlite3_step(stmt)) != SQLITE_DONE) {
    if (dbop == SQLITE_ROW)
      ret = matoi((char *)sqlite3_column_text(stmt, 0));
  }
  return ret;
}


x86 assembly (clang, -O0):
<getindex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x220,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
lea    -0x210(%rbp),%rdi
mov    $0x200,%esi
lea    0xe74(%rip),%rdx
call   1040 <snprintf@plt>
mov    -0x8(%rbp),%rdi
lea    -0x210(%rbp),%rsi
mov    $0xffffffff,%edx
lea    -0x218(%rbp),%rcx
xor    %r8d,%r8d
call   1080 <sqlite3_prepare_v2@plt>
mov    %eax,-0x21c(%rbp)
cmpl   $0x1,-0xc(%rbp)
jle    11d3 <getindex+0x73>
lea    -0x210(%rbp),%rsi
lea    0xe4b(%rip),%rdi
call   1030 <printf@plt>
jmp    11d8 <getindex+0x78>
mov    -0x218(%rbp),%rdi
call   1050 <sqlite3_step@plt>
mov    %eax,-0x21c(%rbp)
mov    0x2def(%rip),%rcx
cmp    (%rcx),%eax
je     122c <getindex+0xcc>
mov    -0x21c(%rbp),%eax
mov    0x2dd2(%rip),%rcx
cmp    (%rcx),%eax
jne    1227 <getindex+0xc7>
mov    -0x218(%rbp),%rdi
xor    %esi,%esi
call   1060 <sqlite3_column_text@plt>
mov    %rax,%rdi
call   1070 <matoi@plt>
mov    %eax,-0x10(%rbp)
jmp    11d8 <getindex+0x78>
mov    -0x10(%rbp),%eax
add    $0x220,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getindex>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x210,%rsp
mov    %esi,%r14d
mov    %rdi,%rbp
lea    0xe84(%rip),%rdx
lea    0x10(%rsp),%r15
mov    %r15,%rdi
mov    $0x200,%esi
call   1040 <snprintf@plt>
lea    0x8(%rsp),%rcx
xor    %r12d,%r12d
mov    %rbp,%rdi
mov    %r15,%rsi
mov    $0xffffffff,%edx
xor    %r8d,%r8d
call   1080 <sqlite3_prepare_v2@plt>
cmp    $0x2,%r14d
jl     11c0 <getindex+0x60>
lea    0xe63(%rip),%rdi
lea    0x10(%rsp),%rsi
call   1030 <printf@plt>
mov    0x8(%rsp),%rdi
call   1050 <sqlite3_step@plt>
mov    0x2e0f(%rip),%rbp
cmp    0x0(%rbp),%eax
jne    11e9 <getindex+0x89>
mov    %r12d,%eax
add    $0x210,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %r12d,%r12d
mov    0x2de5(%rip),%rbx
jmp    120f <getindex+0xaf>
cs nopw 0x0(%rax,%rax,1)
nop
mov    0x8(%rsp),%rdi
call   1050 <sqlite3_step@plt>
cmp    0x0(%rbp),%eax
je     11d6 <getindex+0x76>
cmp    (%rbx),%eax
jne    1200 <getindex+0xa0>
mov    0x8(%rsp),%rdi
xor    %esi,%esi
call   1060 <sqlite3_column_text@plt>
mov    %rax,%rdi
call   1070 <matoi@plt>
mov    %eax,%r12d
jmp    1200 <getindex+0xa0>

LLVM IR (-O0):
@.str = [25 x i8] c"SELECT COUNT(*) FROM id;\00"
@.str.1 = [11 x i8] c"Query: %s\0A\00"
@SQLITE_DONE = global i32 0
@SQLITE_ROW = global i32 0
define i32 @getindex(i32* %0, i32 %1) {
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i32
%6 = alloca [512 x i8]
%7 = alloca i32*
%8 = alloca i32
store i32* %0, i32** %3
store i32 %1, i32* %4
store i32 0, i32* %5
%9 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%10 = call i32 @snprintf(i8* %9, i32 512, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
%11 = load i32*, i32** %3
%12 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%13 = call i32 @sqlite3_prepare_v2(i32* %11, i8* %12, i32 -1, i32** %7, i32 0)
store i32 %13, i32* %8
%14 = load i32, i32* %4
%15 = icmp sgt i32 %14, 1
br i1 %15, label %16, label %19
16: ; preds = %2
%17 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%18 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i8* %17)
br label %19
19: ; preds = %16, %2
br label %20
20: ; preds = %34, %19
%21 = load i32*, i32** %7
%22 = call i32 @sqlite3_step(i32* %21)
store i32 %22, i32* %8
%23 = load i32, i32* @SQLITE_DONE
%24 = icmp ne i32 %22, %23
br i1 %24, label %25, label %35
25: ; preds = %20
%26 = load i32, i32* %8
%27 = load i32, i32* @SQLITE_ROW
%28 = icmp eq i32 %26, %27
br i1 %28, label %29, label %34
29: ; preds = %25
%30 = load i32*, i32** %7
%31 = call i64 @sqlite3_column_text(i32* %30, i32 0)
%32 = inttoptr i64 %31 to i8*
%33 = call i32 @matoi(i8* %32)
store i32 %33, i32* %5
br label %34
34: ; preds = %29, %25
br label %20
35: ; preds = %20
%36 = load i32, i32* %5
ret i32 %36
}

LLVM IR (-O2):
@.str = [25 x i8] c"SELECT COUNT(*) FROM id;\00"
@.str.1 = [11 x i8] c"Query: %s\0A\00"
@SQLITE_DONE = global i32 0
@SQLITE_ROW = global i32 0
define i32 @getindex(i32* %0, i32 %1) {
%3 = alloca [512 x i8]
%4 = alloca i32*
%5 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%6 = bitcast i32** %4 to i8*
%7 = call i32 @snprintf(i8* %5, i32 512, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
%8 = call i32 @sqlite3_prepare_v2(i32* %0, i8* %5, i32 -1, i32** %4, i32 0)
%9 = icmp sgt i32 %1, 1
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i8* %5)
br label %12
12: ; preds = %10, %2
%13 = load i32*, i32** %4
%14 = call i32 @sqlite3_step(i32* %13)
%15 = load i32, i32* @SQLITE_DONE
%16 = icmp eq i32 %14, %15
br i1 %16, label %33, label %17
17: ; preds = %12, %27
%18 = phi i32 [ %30, %27 ], [ %14, %12 ]
%19 = phi i32 [ %28, %27 ], [ 0, %12 ]
%20 = load i32, i32* @SQLITE_ROW
%21 = icmp eq i32 %18, %20
br i1 %21, label %22, label %27
22: ; preds = %17
%23 = load i32*, i32** %4
%24 = call i64 @sqlite3_column_text(i32* %23, i32 0)
%25 = inttoptr i64 %24 to i8*
%26 = call i32 @matoi(i8* %25)
br label %27
27: ; preds = %22, %17
%28 = phi i32 [ %26, %22 ], [ %19, %17 ]
%29 = load i32*, i32** %4
%30 = call i32 @sqlite3_step(i32* %29)
%31 = load i32, i32* @SQLITE_DONE
%32 = icmp eq i32 %30, %31
br i1 %32, label %33, label %17
33: ; preds = %27, %12
%34 = phi i32 [ 0, %12 ], [ %28, %27 ]
ret i32 %34
}