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

/* Variables and functions */
int SQLITE_DONE;
int SQLITE_ROW;
int /*<<< orphan*/ printf(char *, ...);
int sqlite3_column_count(int /*<<< orphan*/ *);
char *sqlite3_column_name(int /*<<< orphan*/ *, int);
scalar_t__ sqlite3_column_text(int /*<<< orphan*/ *, int);
int sqlite3_step(int /*<<< orphan*/ *);
int db_print_query(int *retval, sqlite3_stmt **stmt) {
  int cols = sqlite3_column_count(*stmt);
  while (1) {
    *retval = sqlite3_step(*stmt);
    if (*retval == SQLITE_ROW) {
      for (int col = 0; col < cols; col++) {
        const char *val = (const char *)sqlite3_column_text(*stmt, col);
        printf("%s = %s\t", sqlite3_column_name(*stmt, col), val);
      }
      printf("\n");
    } else if (*retval == SQLITE_DONE) {
      printf("All rows fetched\n");
      break;
    } else {
      printf("Some error encountered\n");
      return -1;
    }
  }
}


x86 assembly (clang, -O0):
<db_print_query>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
call   1060 <sqlite3_column_count@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
call   1050 <sqlite3_step@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    0x2e48(%rip),%rcx
cmp    (%rcx),%eax
jne    1203 <db_print_query+0xb3>
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    11f0 <db_print_query+0xa0>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
mov    -0x20(%rbp),%esi
call   1070 <sqlite3_column_text@plt>
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
mov    -0x20(%rbp),%esi
call   1030 <sqlite3_column_name@plt>
mov    %rax,%rsi
mov    -0x28(%rbp),%rdx
lea    0xe25(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    119f <db_print_query+0x4f>
lea    0xe22(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1245 <db_print_query+0xf5>
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    0x2dd0(%rip),%rcx
cmp    (%rcx),%eax
jne    122b <db_print_query+0xdb>
lea    0xdea(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    124a <db_print_query+0xfa>
lea    0xde9(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    124a <db_print_query+0xfa>
jmp    116f <db_print_query+0x1f>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<db_print_query>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r13
mov    %rdi,%r14
mov    (%rsi),%rdi
call   1080 <sqlite3_column_count@plt>
mov    %eax,%r12d
mov    0x0(%r13),%rdi
call   1070 <sqlite3_step@plt>
mov    %eax,(%r14)
mov    0x2e39(%rip),%rcx
cmp    (%rcx),%eax
jne    1213 <db_print_query+0xa3>
lea    0xe56(%rip),%r15
jmp    11d1 <db_print_query+0x61>
nopl   0x0(%rax)
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    0x0(%r13),%rdi
call   1070 <sqlite3_step@plt>
mov    %eax,(%r14)
mov    0x2e0b(%rip),%rcx
cmp    (%rcx),%eax
jne    1213 <db_print_query+0xa3>
test   %r12d,%r12d
jle    11b0 <db_print_query+0x40>
xor    %ebp,%ebp
nopl   0x0(%rax,%rax,1)
mov    0x0(%r13),%rdi
mov    %ebp,%esi
call   1090 <sqlite3_column_text@plt>
mov    %rax,%rbx
mov    0x0(%r13),%rdi
mov    %ebp,%esi
call   1050 <sqlite3_column_name@plt>
mov    %r15,%rdi
mov    %rax,%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1060 <printf@plt>
add    $0x1,%ebp
cmp    %ebp,%r12d
jne    11e0 <db_print_query+0x70>
jmp    11b0 <db_print_query+0x40>
mov    0x2dc6(%rip),%rcx
cmp    (%rcx),%eax
lea    0xdfd(%rip),%rax
lea    0xddf(%rip),%rdi
cmove  %rax,%rdi
call   1040 <puts@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@SQLITE_ROW = global i32 0
@.str = [9 x i8] c"%s = %s\09\00"
@.str.1 = [2 x i8] c"\0A\00"
@SQLITE_DONE = global i32 0
@.str.2 = [18 x i8] c"All rows fetched\0A\00"
@.str.3 = [24 x i8] c"Some error encountered\0A\00"
define i32 @db_print_query(i32* %0, i32** %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i32**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
store i32* %0, i32** %4
store i32** %1, i32*** %5
%9 = load i32**, i32*** %5
%10 = load i32*, i32** %9
%11 = call i32 @sqlite3_column_count(i32* %10)
store i32 %11, i32* %6
br label %12
12: ; preds = %2, %52
%13 = load i32**, i32*** %5
%14 = load i32*, i32** %13
%15 = call i32 @sqlite3_step(i32* %14)
%16 = load i32*, i32** %4
store i32 %15, i32* %16
%17 = load i32*, i32** %4
%18 = load i32, i32* %17
%19 = load i32, i32* @SQLITE_ROW
%20 = icmp eq i32 %18, %19
br i1 %20, label %21, label %43
21: ; preds = %12
store i32 0, i32* %7
br label %22
22: ; preds = %38, %21
%23 = load i32, i32* %7
%24 = load i32, i32* %6
%25 = icmp slt i32 %23, %24
br i1 %25, label %26, label %41
26: ; preds = %22
%27 = load i32**, i32*** %5
%28 = load i32*, i32** %27
%29 = load i32, i32* %7
%30 = call i64 @sqlite3_column_text(i32* %28, i32 %29)
%31 = inttoptr i64 %30 to i8*
store i8* %31, i8** %8
%32 = load i32**, i32*** %5
%33 = load i32*, i32** %32
%34 = load i32, i32* %7
%35 = call i8* @sqlite3_column_name(i32* %33, i32 %34)
%36 = load i8*, i8** %8
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* %35, i8* %36)
br label %38
38: ; preds = %26
%39 = load i32, i32* %7
%40 = add nsw i32 %39, 1
store i32 %40, i32* %7
br label %22
41: ; preds = %22
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
br label %52
43: ; preds = %12
%44 = load i32*, i32** %4
%45 = load i32, i32* %44
%46 = load i32, i32* @SQLITE_DONE
%47 = icmp eq i32 %45, %46
br i1 %47, label %48, label %50
48: ; preds = %43
%49 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
br label %53
50: ; preds = %43
%51 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %3
br label %53
52: ; preds = %41
br label %12
53: ; preds = %50, %48
%54 = load i32, i32* %3
ret i32 %54
}

LLVM IR (-O2):
@SQLITE_ROW = global i32 0
@.str = [9 x i8] c"%s = %s\09\00"
@SQLITE_DONE = global i32 0
@str = [23 x i8] c"Some error encountered\00"
@str.4 = [17 x i8] c"All rows fetched\00"
define i32 @db_print_query(i32* nocapture writeonly %0, i32** nocapture %1) {
%3 = load i32*, i32** %1
%4 = tail call i32 @sqlite3_column_count(i32* %3)
%5 = load i32*, i32** %1
%6 = tail call i32 @sqlite3_step(i32* %5)
store i32 %6, i32* %0
%7 = load i32, i32* @SQLITE_ROW
%8 = icmp eq i32 %6, %7
br i1 %8, label %9, label %28
9: ; preds = %2
%10 = icmp sgt i32 %4, 0
br label %11
11: ; preds = %9, %12
br i1 %10, label %18, label %12
12: ; preds = %18, %11
%13 = tail call i32 @putchar(i32 10)
%14 = load i32*, i32** %1
%15 = tail call i32 @sqlite3_step(i32* %14)
store i32 %15, i32* %0
%16 = load i32, i32* @SQLITE_ROW
%17 = icmp eq i32 %15, %16
br i1 %17, label %11, label %28
18: ; preds = %11, %18
%19 = phi i32 [ %26, %18 ], [ 0, %11 ]
%20 = load i32*, i32** %1
%21 = tail call i64 @sqlite3_column_text(i32* %20, i32 %19)
%22 = inttoptr i64 %21 to i8*
%23 = load i32*, i32** %1
%24 = tail call i8* @sqlite3_column_name(i32* %23, i32 %19)
%25 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* %24, i8* %22)
%26 = add nuw nsw i32 %19, 1
%27 = icmp eq i32 %26, %4
br i1 %27, label %12, label %18
28: ; preds = %12, %2
%29 = phi i32 [ %6, %2 ], [ %15, %12 ]
%30 = load i32, i32* @SQLITE_DONE
%31 = icmp eq i32 %29, %30
%32 = select i1 %31, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @str.4, i64 0, i64 0), i8* getelementptr inbounds ([23 x i8], [23 x i8]* @str, i64 0, i64 0)
%33 = tail call i32 @puts(i8* dereferenceable(1) %32)
ret i32 -1
}