#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 /*<<< orphan*/ sqlite3_close(int /*<<< orphan*/ *);
int sqlite3_column_count(int /*<<< orphan*/ *);
char *sqlite3_column_text(int /*<<< orphan*/ *, int);
char *sqlite3_errmsg(int /*<<< orphan*/ *);
int /*<<< orphan*/ sqlite3_finalize(int /*<<< orphan*/ *);
int sqlite3_open(char *, int /*<<< orphan*/ **);
int sqlite3_prepare_v2(int /*<<< orphan*/ *, char *, int, int /*<<< orphan*/ **,
                       int /*<<< orphan*/);
int sqlite3_step(int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
int main(int argc, char **argv) {
  sqlite3 *db;
  sqlite3_stmt *stmt;
  int rc;
  if (argc != 2) {
    fprintf(stderr, "Usage: %s <database file>\n", argv[0]);
    return (1);
  }
  rc = sqlite3_open(argv[1], &db);
  if (rc) {
    fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
    sqlite3_close(db);
    return (1);
  }
  char *sql_stmt = "select Country "
                   "from "
                   "(select distinct ai.Country, count(*) as number "
                   "from airlines ai, routes r, airports ap "
                   "where r.Airline_ID = ai.Airline_ID and "
                   "(r.Source_airport_ID = ap.Airport_ID or "
                   "r.Destination_airport_ID = ap.Airport_ID) and "
                   "ap.Country = 'Canada' and "
                   "ai.Country != '' and "
                   "ai.Country!= '\\N' "
                   "group by ai.Name order by count(*) desc) "
                   "group by Country order by SUM(number) desc limit 10;";
  rc = sqlite3_prepare_v2(db, sql_stmt, -1, &stmt, 0);
  if (rc != SQLITE_OK) {
    fprintf(stderr, "Preparation failed: %s\n", sqlite3_errmsg(db));
    sqlite3_close(db);
    return 1;
  }
  while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
    int col;
    for (col = 0; col < sqlite3_column_count(stmt) - 1; col++) {
      printf("%s|", sqlite3_column_text(stmt, col));
    }
    printf("%s", sqlite3_column_text(stmt, col));
    printf("\n");
  }
  sqlite3_finalize(stmt);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11e8 <main+0x48>
mov    0x2e11(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe29(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1368 <main+0x1c8>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    -0x18(%rbp),%rsi
call   1030 <sqlite3_open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     1242 <main+0xa2>
mov    0x2dcb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <sqlite3_errmsg@plt>
mov    -0x38(%rbp),%edi
mov    %rax,%rdx
lea    0xdf3(%rip),%rsi
call   1070 <fprintf@plt>
mov    -0x18(%rbp),%rdi
call   1080 <sqlite3_close@plt>
movl   $0x1,-0x4(%rbp)
jmp    1368 <main+0x1c8>
lea    0xdeb(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    $0xffffffff,%edx
lea    -0x20(%rbp),%rcx
xor    %r8d,%r8d
call   10c0 <sqlite3_prepare_v2@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    0x2d75(%rip),%rcx
cmp    (%rcx),%eax
je     12b7 <main+0x117>
mov    0x2d56(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <sqlite3_errmsg@plt>
mov    -0x3c(%rbp),%edi
mov    %rax,%rdx
lea    0xf1f(%rip),%rsi
call   1070 <fprintf@plt>
mov    -0x18(%rbp),%rdi
call   1080 <sqlite3_close@plt>
movl   $0x1,-0x4(%rbp)
jmp    1368 <main+0x1c8>
jmp    12bc <main+0x11c>
mov    -0x20(%rbp),%rdi
call   1060 <sqlite3_step@plt>
mov    %eax,-0x24(%rbp)
mov    0x2d01(%rip),%rcx
cmp    (%rcx),%eax
jne    1358 <main+0x1b8>
movl   $0x0,-0x34(%rbp)
mov    -0x34(%rbp),%eax
mov    %eax,-0x40(%rbp)
mov    -0x20(%rbp),%rdi
call   1090 <sqlite3_column_count@plt>
mov    %eax,%ecx
mov    -0x40(%rbp),%eax
sub    $0x1,%ecx
cmp    %ecx,%eax
jge    1328 <main+0x188>
mov    -0x20(%rbp),%rdi
mov    -0x34(%rbp),%esi
call   10a0 <sqlite3_column_text@plt>
mov    %rax,%rsi
lea    0xec1(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    12de <main+0x13e>
mov    -0x20(%rbp),%rdi
mov    -0x34(%rbp),%esi
call   10a0 <sqlite3_column_text@plt>
mov    %rax,%rsi
lea    0xe9a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xccd(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12bc <main+0x11c>
mov    -0x20(%rbp),%rdi
call   10b0 <sqlite3_finalize@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
cmp    $0x2,%edi
jne    11f0 <main+0x40>
mov    0x8(%rsi),%rdi
lea    0x10(%rsp),%rsi
call   1040 <sqlite3_open@plt>
test   %eax,%eax
je     120d <main+0x5d>
mov    0x2e00(%rip),%rax
mov    (%rax),%ebx
mov    0x10(%rsp),%rdi
call   1060 <sqlite3_errmsg@plt>
lea    0xe30(%rip),%rsi
jmp    1309 <main+0x159>
mov    0x2de1(%rip),%rax
mov    (%rax),%edi
mov    (%rsi),%rdx
lea    0xdfd(%rip),%rsi
call   1080 <fprintf@plt>
jmp    131d <main+0x16d>
mov    0x10(%rsp),%rdi
lea    0xe1b(%rip),%rsi
lea    0x8(%rsp),%rcx
mov    $0xffffffff,%edx
xor    %r8d,%r8d
call   10d0 <sqlite3_prepare_v2@plt>
mov    0x2db6(%rip),%rcx
cmp    (%rcx),%eax
jne    12ef <main+0x13f>
mov    0x8(%rsp),%rdi
call   1070 <sqlite3_step@plt>
mov    0x2d85(%rip),%r15
cmp    (%r15),%eax
jne    132d <main+0x17d>
lea    0xf79(%rip),%rbx
lea    0xf76(%rip),%r14
jmp    12a6 <main+0xf6>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    0x8(%rsp),%rdi
mov    %ebp,%esi
call   10b0 <sqlite3_column_text@plt>
mov    %r14,%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    0x8(%rsp),%rdi
call   1070 <sqlite3_step@plt>
cmp    (%r15),%eax
jne    132d <main+0x17d>
mov    0x8(%rsp),%rdi
call   10a0 <sqlite3_column_count@plt>
mov    $0x0,%ebp
cmp    $0x2,%eax
jl     1270 <main+0xc0>
xor    %ebp,%ebp
nopl   0x0(%rax)
mov    0x8(%rsp),%rdi
mov    %ebp,%esi
call   10b0 <sqlite3_column_text@plt>
mov    %rbx,%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
add    $0x1,%ebp
mov    0x8(%rsp),%rdi
call   10a0 <sqlite3_column_count@plt>
add    $0xffffffff,%eax
cmp    %eax,%ebp
jl     12c0 <main+0x110>
jmp    1270 <main+0xc0>
mov    0x2ce2(%rip),%rax
mov    (%rax),%ebx
mov    0x10(%rsp),%rdi
call   1060 <sqlite3_errmsg@plt>
lea    0xeb3(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1080 <fprintf@plt>
mov    0x10(%rsp),%rdi
call   1090 <sqlite3_close@plt>
mov    $0x1,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x8(%rsp),%rdi
call   10c0 <sqlite3_finalize@plt>
xor    %eax,%eax
jmp    1322 <main+0x172>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [27 x i8] c"Usage: %s <database file>\0A\00"
@.str.1 = [25 x i8] c"Can't open database: %s\0A\00"
@.str.2 = [392 x i8] c"select Country from (select distinct ai.Country, count(*) as number from airlines ai, routes r, airports ap where r.Airline_ID = ai.Airline_ID and (r.Source_airport_ID = ap.Airport_ID or r.Destination_airport_ID = ap.Airport_ID) and ap.Country = 'Canada' and ai.Country != '' and ai.Country!= '\\N' group by ai.Name order by count(*) desc) group by Country order by SUM(number) desc limit 10;\00"
@SQLITE_OK = global i32 0
@.str.3 = [24 x i8] c"Preparation failed: %s\0A\00"
@SQLITE_ROW = global i32 0
@.str.4 = [4 x i8] c"%s|\00"
@.str.5 = [3 x i8] c"%s\00"
@.str.6 = [2 x i8] c"\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* %4
%12 = icmp ne i32 %11, 2
br i1 %12, label %13, label %19
13: ; preds = %2
%14 = load i32, i32* @stderr
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 0
%17 = load i8*, i8** %16
%18 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %17)
store i32 1, i32* %3
br label %77
19: ; preds = %2
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i64 1
%22 = load i8*, i8** %21
%23 = call i32 @sqlite3_open(i8* %22, i32** %6)
store i32 %23, i32* %8
%24 = load i32, i32* %8
%25 = icmp ne i32 %24, 0
br i1 %25, label %26, label %33
26: ; preds = %19
%27 = load i32, i32* @stderr
%28 = load i32*, i32** %6
%29 = call i8* @sqlite3_errmsg(i32* %28)
%30 = call i32 @fprintf(i32 %27, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %29)
%31 = load i32*, i32** %6
%32 = call i32 @sqlite3_close(i32* %31)
store i32 1, i32* %3
br label %77
33: ; preds = %19
store i8* getelementptr inbounds ([392 x i8], [392 x i8]* @.str.2, i64 0, i64 0), i8** %9
%34 = load i32*, i32** %6
%35 = load i8*, i8** %9
%36 = call i32 @sqlite3_prepare_v2(i32* %34, i8* %35, i32 -1, i32** %7, i32 0)
store i32 %36, i32* %8
%37 = load i32, i32* %8
%38 = load i32, i32* @SQLITE_OK
%39 = icmp ne i32 %37, %38
br i1 %39, label %40, label %47
40: ; preds = %33
%41 = load i32, i32* @stderr
%42 = load i32*, i32** %6
%43 = call i8* @sqlite3_errmsg(i32* %42)
%44 = call i32 @fprintf(i32 %41, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i8* %43)
%45 = load i32*, i32** %6
%46 = call i32 @sqlite3_close(i32* %45)
store i32 1, i32* %3
br label %77
47: ; preds = %33
br label %48
48: ; preds = %68, %47
%49 = load i32*, i32** %7
%50 = call i32 @sqlite3_step(i32* %49)
store i32 %50, i32* %8
%51 = load i32, i32* @SQLITE_ROW
%52 = icmp eq i32 %50, %51
br i1 %52, label %53, label %74
53: ; preds = %48
store i32 0, i32* %10
br label %54
54: ; preds = %65, %53
%55 = load i32, i32* %10
%56 = load i32*, i32** %7
%57 = call i32 @sqlite3_column_count(i32* %56)
%58 = sub nsw i32 %57, 1
%59 = icmp slt i32 %55, %58
br i1 %59, label %60, label %68
60: ; preds = %54
%61 = load i32*, i32** %7
%62 = load i32, i32* %10
%63 = call i8* @sqlite3_column_text(i32* %61, i32 %62)
%64 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i8* %63)
br label %65
65: ; preds = %60
%66 = load i32, i32* %10
%67 = add nsw i32 %66, 1
store i32 %67, i32* %10
br label %54
68: ; preds = %54
%69 = load i32*, i32** %7
%70 = load i32, i32* %10
%71 = call i8* @sqlite3_column_text(i32* %69, i32 %70)
%72 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i8* %71)
%73 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0))
br label %48
74: ; preds = %48
%75 = load i32*, i32** %7
%76 = call i32 @sqlite3_finalize(i32* %75)
store i32 0, i32* %3
br label %77
77: ; preds = %74, %40, %26, %13
%78 = load i32, i32* %3
ret i32 %78
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [27 x i8] c"Usage: %s <database file>\0A\00"
@.str.1 = [25 x i8] c"Can't open database: %s\0A\00"
@.str.2 = [392 x i8] c"select Country from (select distinct ai.Country, count(*) as number from airlines ai, routes r, airports ap where r.Airline_ID = ai.Airline_ID and (r.Source_airport_ID = ap.Airport_ID or r.Destination_airport_ID = ap.Airport_ID) and ap.Country = 'Canada' and ai.Country != '' and ai.Country!= '\\N' group by ai.Name order by count(*) desc) group by Country order by SUM(number) desc limit 10;\00"
@SQLITE_OK = global i32 0
@.str.3 = [24 x i8] c"Preparation failed: %s\0A\00"
@SQLITE_ROW = global i32 0
@.str.4 = [4 x i8] c"%s|\00"
@.str.5 = [3 x i8] c"%s\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32*
%4 = alloca i32*
%5 = bitcast i32** %3 to i8*
%6 = bitcast i32** %4 to i8*
%7 = icmp eq i32 %0, 2
br i1 %7, label %12, label %8
8: ; preds = %2
%9 = load i32, i32* @stderr
%10 = load i8*, i8** %1
%11 = tail call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %10)
br label %68
12: ; preds = %2
%13 = getelementptr inbounds i8*, i8** %1, i64 1
%14 = load i8*, i8** %13
%15 = call i32 @sqlite3_open(i8* %14, i32** %3)
%16 = icmp eq i32 %15, 0
br i1 %16, label %24, label %17
17: ; preds = %12
%18 = load i32, i32* @stderr
%19 = load i32*, i32** %3
%20 = call i8* @sqlite3_errmsg(i32* %19)
%21 = call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %20)
%22 = load i32*, i32** %3
%23 = call i32 @sqlite3_close(i32* %22)
br label %68
24: ; preds = %12
%25 = load i32*, i32** %3
%26 = call i32 @sqlite3_prepare_v2(i32* %25, i8* getelementptr inbounds ([392 x i8], [392 x i8]* @.str.2, i64 0, i64 0), i32 -1, i32** %4, i32 0)
%27 = load i32, i32* @SQLITE_OK
%28 = icmp eq i32 %26, %27
br i1 %28, label %29, label %34
29: ; preds = %24
%30 = load i32*, i32** %4
%31 = call i32 @sqlite3_step(i32* %30)
%32 = load i32, i32* @SQLITE_ROW
%33 = icmp eq i32 %31, %32
br i1 %33, label %41, label %65
34: ; preds = %24
%35 = load i32, i32* @stderr
%36 = load i32*, i32** %3
%37 = call i8* @sqlite3_errmsg(i32* %36)
%38 = call i32 @fprintf(i32 %35, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i8* %37)
%39 = load i32*, i32** %3
%40 = call i32 @sqlite3_close(i32* %39)
br label %68
41: ; preds = %29, %55
%42 = load i32*, i32** %4
%43 = call i32 @sqlite3_column_count(i32* %42)
%44 = icmp sgt i32 %43, 1
br i1 %44, label %45, label %55
45: ; preds = %41, %45
%46 = phi i32 [ %50, %45 ], [ 0, %41 ]
%47 = load i32*, i32** %4
%48 = call i8* @sqlite3_column_text(i32* %47, i32 %46)
%49 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i8* %48)
%50 = add nuw nsw i32 %46, 1
%51 = load i32*, i32** %4
%52 = call i32 @sqlite3_column_count(i32* %51)
%53 = add nsw i32 %52, -1
%54 = icmp slt i32 %50, %53
br i1 %54, label %45, label %55
55: ; preds = %45, %41
%56 = phi i32 [ 0, %41 ], [ %50, %45 ]
%57 = load i32*, i32** %4
%58 = call i8* @sqlite3_column_text(i32* %57, i32 %56)
%59 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i8* %58)
%60 = call i32 @putchar(i32 10)
%61 = load i32*, i32** %4
%62 = call i32 @sqlite3_step(i32* %61)
%63 = load i32, i32* @SQLITE_ROW
%64 = icmp eq i32 %62, %63
br i1 %64, label %41, label %65
65: ; preds = %55, %29
%66 = load i32*, i32** %4
%67 = call i32 @sqlite3_finalize(i32* %66)
br label %68
68: ; preds = %34, %65, %17, %8
%69 = phi i32 [ 1, %8 ], [ 1, %17 ], [ 1, %34 ], [ 0, %65 ]
ret i32 %69
}