#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;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
scalar_t__ exec(int /*<<< orphan*/ *, char *);
scalar_t__ quiet_exec(int /*<<< orphan*/ *, char *);
int /*<<< orphan*/ snprintf(char *, int, char *, ...);
int perf_test(sqlite3 *db, int virt) {
  char buf[4096];
  unsigned int i;
  if (virt) {
    snprintf(buf, sizeof(buf),
             "CREATE VIRTUAL TABLE perf USING redis (localhost:6379, prefix,\n"
             "   timestamp          INTEGER,\n"
             "   idx          INTEGER\n"
             ");\n");
  } else {
    snprintf(buf, sizeof(buf),
             "CREATE TABLE perf (\n"
             "   timestamp          INTEGER,\n"
             "   idx          INTEGER\n"
             ");\n");
  }
  if (exec(db, buf))
    return 1;
  exec(db, "delete from perf");
  for (i = 0; i < 10000; ++i) {
    snprintf(buf, sizeof(buf),
             "insert into perf "
             "(timestamp, idx) "
             "values (strftime('%%s','now'), %u)",
             i);
    if (quiet_exec(db, buf))
      return 1;
  }
  exec(db, "select * from perf where idx = 50");
  exec(db, "select * from perf where idx < 50 limit 10");
  exec(db, "select * from perf where idx <= 50 limit 10");
  if (virt)
    exec(db,
         "select max(timestamp) - min(timestamp) as virt_duration from perf");
  else
    exec(db,
         "select max(timestamp) - min(timestamp) as sql_duration from perf");
  exec(db, "DROP TABLE perf");
  return 0;
}


x86 assembly (clang, -O0):
<perf_test>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1030,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     116b <perf_test+0x3b>
lea    -0x1020(%rbp),%rdi
mov    $0x1000,%esi
lea    0xea1(%rip),%rdx
mov    $0x0,%al
call   1040 <snprintf@plt>
jmp    1185 <perf_test+0x55>
lea    -0x1020(%rbp),%rdi
mov    $0x1000,%esi
lea    0xefc(%rip),%rdx
mov    $0x0,%al
call   1040 <snprintf@plt>
mov    -0x10(%rbp),%rdi
lea    -0x1020(%rbp),%rsi
call   1050 <exec@plt>
cmp    $0x0,%rax
je     11ab <perf_test+0x7b>
movl   $0x1,-0x4(%rbp)
jmp    12aa <perf_test+0x17a>
mov    -0x10(%rbp),%rdi
lea    0xf13(%rip),%rsi
call   1050 <exec@plt>
movl   $0x0,-0x1024(%rbp)
cmpl   $0x2710,-0x1024(%rbp)
jae    1234 <perf_test+0x104>
lea    -0x1020(%rbp),%rdi
mov    -0x1024(%rbp),%ecx
mov    $0x1000,%esi
lea    0xeec(%rip),%rdx
mov    $0x0,%al
call   1040 <snprintf@plt>
mov    -0x10(%rbp),%rdi
lea    -0x1020(%rbp),%rsi
call   1030 <quiet_exec@plt>
cmp    $0x0,%rax
je     121b <perf_test+0xeb>
movl   $0x1,-0x4(%rbp)
jmp    12aa <perf_test+0x17a>
jmp    1220 <perf_test+0xf0>
mov    -0x1024(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1024(%rbp)
jmp    11c5 <perf_test+0x95>
mov    -0x10(%rbp),%rdi
lea    0xee0(%rip),%rsi
call   1050 <exec@plt>
mov    -0x10(%rbp),%rdi
lea    0xef2(%rip),%rsi
call   1050 <exec@plt>
mov    -0x10(%rbp),%rdi
lea    0xf0d(%rip),%rsi
call   1050 <exec@plt>
cmpl   $0x0,-0x14(%rbp)
je     1283 <perf_test+0x153>
mov    -0x10(%rbp),%rdi
lea    0xf1f(%rip),%rsi
call   1050 <exec@plt>
jmp    1293 <perf_test+0x163>
mov    -0x10(%rbp),%rdi
lea    0xf4c(%rip),%rsi
call   1050 <exec@plt>
mov    -0x10(%rbp),%rdi
lea    0xf7d(%rip),%rsi
call   1050 <exec@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<perf_test>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1008,%rsp
mov    %esi,%r14d
mov    %rdi,%r13
test   %esi,%esi
lea    0xf2a(%rip),%rax
lea    0xea9(%rip),%rdx
cmove  %rax,%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x1000,%esi
xor    %eax,%eax
call   1040 <snprintf@plt>
mov    %r13,%rdi
mov    %rbx,%rsi
call   1050 <exec@plt>
mov    $0x1,%r15d
test   %rax,%rax
jne    123b <perf_test+0x10b>
lea    0xf3b(%rip),%rsi
mov    %r13,%rdi
call   1050 <exec@plt>
lea    0xf3d(%rip),%r12
mov    %rsp,%rbp
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %rbp,%rdi
mov    $0x1000,%esi
mov    %r12,%rdx
mov    %ebx,%ecx
xor    %eax,%eax
call   1040 <snprintf@plt>
mov    %r13,%rdi
mov    %rbp,%rsi
call   1030 <quiet_exec@plt>
test   %rax,%rax
jne    123b <perf_test+0x10b>
add    $0x1,%ebx
cmp    $0x2710,%ebx
jne    11b0 <perf_test+0x80>
lea    0xf39(%rip),%rsi
mov    %r13,%rdi
call   1050 <exec@plt>
lea    0xf4c(%rip),%rsi
mov    %r13,%rdi
call   1050 <exec@plt>
lea    0xf68(%rip),%rsi
mov    %r13,%rdi
call   1050 <exec@plt>
test   %r14d,%r14d
lea    0xfc4(%rip),%rax
lea    0xf7b(%rip),%rsi
cmove  %rax,%rsi
mov    %r13,%rdi
call   1050 <exec@plt>
lea    0xfeb(%rip),%rsi
mov    %r13,%rdi
call   1050 <exec@plt>
xor    %r15d,%r15d
mov    %r15d,%eax
add    $0x1008,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [122 x i8] c"CREATE VIRTUAL TABLE perf USING redis (localhost:6379, prefix,\0A timestamp INTEGER,\0A idx INTEGER\0A);\0A\00"
@.str.1 = [79 x i8] c"CREATE TABLE perf (\0A timestamp INTEGER,\0A idx INTEGER\0A);\0A\00"
@.str.2 = [17 x i8] c"delete from perf\00"
@.str.3 = [69 x i8] c"insert into perf (timestamp, idx) values (strftime('%%s','now'), %u)\00"
@.str.4 = [34 x i8] c"select * from perf where idx = 50\00"
@.str.5 = [43 x i8] c"select * from perf where idx < 50 limit 10\00"
@.str.6 = [44 x i8] c"select * from perf where idx <= 50 limit 10\00"
@.str.7 = [66 x i8] c"select max(timestamp) - min(timestamp) as virt_duration from perf\00"
@.str.8 = [65 x i8] c"select max(timestamp) - min(timestamp) as sql_duration from perf\00"
@.str.9 = [16 x i8] c"DROP TABLE perf\00"
define i32 @perf_test(i32* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i32
%6 = alloca [4096 x i8]
%7 = alloca i32
store i32* %0, i32** %4
store i32 %1, i32* %5
%8 = load i32, i32* %5
%9 = icmp ne i32 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %2
%11 = getelementptr inbounds [4096 x i8], [4096 x i8]* %6, i64 0, i64 0
%12 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %11, i32 4096, i8* getelementptr inbounds ([122 x i8], [122 x i8]* @.str, i64 0, i64 0))
br label %16
13: ; preds = %2
%14 = getelementptr inbounds [4096 x i8], [4096 x i8]* %6, i64 0, i64 0
%15 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %14, i32 4096, i8* getelementptr inbounds ([79 x i8], [79 x i8]* @.str.1, i64 0, i64 0))
br label %16
16: ; preds = %13, %10
%17 = load i32*, i32** %4
%18 = getelementptr inbounds [4096 x i8], [4096 x i8]* %6, i64 0, i64 0
%19 = call i64 @exec(i32* %17, i8* %18)
%20 = icmp ne i64 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %16
store i32 1, i32* %3
br label %59
22: ; preds = %16
%23 = load i32*, i32** %4
%24 = call i64 @exec(i32* %23, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %7
br label %25
25: ; preds = %38, %22
%26 = load i32, i32* %7
%27 = icmp ult i32 %26, 10000
br i1 %27, label %28, label %41
28: ; preds = %25
%29 = getelementptr inbounds [4096 x i8], [4096 x i8]* %6, i64 0, i64 0
%30 = load i32, i32* %7
%31 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %29, i32 4096, i8* getelementptr inbounds ([69 x i8], [69 x i8]* @.str.3, i64 0, i64 0), i32 %30)
%32 = load i32*, i32** %4
%33 = getelementptr inbounds [4096 x i8], [4096 x i8]* %6, i64 0, i64 0
%34 = call i64 @quiet_exec(i32* %32, i8* %33)
%35 = icmp ne i64 %34, 0
br i1 %35, label %36, label %37
36: ; preds = %28
store i32 1, i32* %3
br label %59
37: ; preds = %28
br label %38
38: ; preds = %37
%39 = load i32, i32* %7
%40 = add i32 %39, 1
store i32 %40, i32* %7
br label %25
41: ; preds = %25
%42 = load i32*, i32** %4
%43 = call i64 @exec(i32* %42, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.4, i64 0, i64 0))
%44 = load i32*, i32** %4
%45 = call i64 @exec(i32* %44, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.5, i64 0, i64 0))
%46 = load i32*, i32** %4
%47 = call i64 @exec(i32* %46, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.6, i64 0, i64 0))
%48 = load i32, i32* %5
%49 = icmp ne i32 %48, 0
br i1 %49, label %50, label %53
50: ; preds = %41
%51 = load i32*, i32** %4
%52 = call i64 @exec(i32* %51, i8* getelementptr inbounds ([66 x i8], [66 x i8]* @.str.7, i64 0, i64 0))
br label %56
53: ; preds = %41
%54 = load i32*, i32** %4
%55 = call i64 @exec(i32* %54, i8* getelementptr inbounds ([65 x i8], [65 x i8]* @.str.8, i64 0, i64 0))
br label %56
56: ; preds = %53, %50
%57 = load i32*, i32** %4
%58 = call i64 @exec(i32* %57, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.9, i64 0, i64 0))
store i32 0, i32* %3
br label %59
59: ; preds = %56, %36, %21
%60 = load i32, i32* %3
ret i32 %60
}

LLVM IR (-O2):
@.str = [122 x i8] c"CREATE VIRTUAL TABLE perf USING redis (localhost:6379, prefix,\0A timestamp INTEGER,\0A idx INTEGER\0A);\0A\00"
@.str.1 = [79 x i8] c"CREATE TABLE perf (\0A timestamp INTEGER,\0A idx INTEGER\0A);\0A\00"
@.str.2 = [17 x i8] c"delete from perf\00"
@.str.3 = [69 x i8] c"insert into perf (timestamp, idx) values (strftime('%%s','now'), %u)\00"
@.str.4 = [34 x i8] c"select * from perf where idx = 50\00"
@.str.5 = [43 x i8] c"select * from perf where idx < 50 limit 10\00"
@.str.6 = [44 x i8] c"select * from perf where idx <= 50 limit 10\00"
@.str.7 = [66 x i8] c"select max(timestamp) - min(timestamp) as virt_duration from perf\00"
@.str.8 = [65 x i8] c"select max(timestamp) - min(timestamp) as sql_duration from perf\00"
@.str.9 = [16 x i8] c"DROP TABLE perf\00"
define i32 @perf_test(i32* %0, i32 %1) {
%3 = alloca [4096 x i8]
%4 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%5 = icmp eq i32 %1, 0
%6 = select i1 %5, i8* getelementptr inbounds ([79 x i8], [79 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([122 x i8], [122 x i8]* @.str, i64 0, i64 0)
%7 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %4, i32 4096, i8* %6)
%8 = call i64 @exec(i32* %0, i8* %4)
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %27
10: ; preds = %2
%11 = call i64 @exec(i32* %0, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
br label %15
12: ; preds = %15
%13 = add nuw nsw i32 %16, 1
%14 = icmp eq i32 %13, 10000
br i1 %14, label %20, label %15
15: ; preds = %10, %12
%16 = phi i32 [ 0, %10 ], [ %13, %12 ]
%17 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %4, i32 4096, i8* getelementptr inbounds ([69 x i8], [69 x i8]* @.str.3, i64 0, i64 0), i32 %16)
%18 = call i64 @quiet_exec(i32* %0, i8* %4)
%19 = icmp eq i64 %18, 0
br i1 %19, label %12, label %27
20: ; preds = %12
%21 = call i64 @exec(i32* %0, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.4, i64 0, i64 0))
%22 = call i64 @exec(i32* %0, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.5, i64 0, i64 0))
%23 = call i64 @exec(i32* %0, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.6, i64 0, i64 0))
%24 = select i1 %5, i8* getelementptr inbounds ([65 x i8], [65 x i8]* @.str.8, i64 0, i64 0), i8* getelementptr inbounds ([66 x i8], [66 x i8]* @.str.7, i64 0, i64 0)
%25 = call i64 @exec(i32* %0, i8* %24)
%26 = call i64 @exec(i32* %0, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.9, i64 0, i64 0))
br label %27
27: ; preds = %15, %2, %20
%28 = phi i32 [ 0, %20 ], [ 1, %2 ], [ 1, %15 ]
ret i32 %28
}