#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 */
scalar_t__ malloc(int);
long sqlite3_column_int(int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ sqlite3_finalize(int /*<<< orphan*/ *);
int /*<<< orphan*/ sqlite3_prepare_v2(int /*<<< orphan*/ *, char const *, int,
                                      int /*<<< orphan*/ **,
                                      int /*<<< orphan*/ *);
int /*<<< orphan*/ sqlite3_step(int /*<<< orphan*/ *);
long *db_array_retrieval(sqlite3 *db, const char *sql, unsigned n) {
  sqlite3_stmt *stmt;
  sqlite3_prepare_v2(db, sql, -1, &stmt, NULL);
  sqlite3_step(stmt);
  long *arr = (long *)malloc(sizeof(long) * n);
  unsigned i;
  for (i = 0; i < n; i++) {
    arr[i] = sqlite3_column_int(stmt, 0);
    sqlite3_step(stmt);
  }
  sqlite3_finalize(stmt);
  return arr;
}


x86 assembly (clang, -O0):
<db_array_retrieval>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    $0xffffffff,%edx
lea    -0x20(%rbp),%rcx
xor    %eax,%eax
mov    %eax,%r8d
call   1070 <sqlite3_prepare_v2@plt>
mov    -0x20(%rbp),%rdi
call   1040 <sqlite3_step@plt>
mov    -0x14(%rbp),%eax
shl    $0x3,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jae    11dc <db_array_retrieval+0x8c>
mov    -0x20(%rbp),%rdi
xor    %esi,%esi
call   1030 <sqlite3_column_int@plt>
mov    %rax,%rdx
mov    -0x28(%rbp),%rax
mov    -0x2c(%rbp),%ecx
mov    %rdx,(%rax,%rcx,8)
mov    -0x20(%rbp),%rdi
call   1040 <sqlite3_step@plt>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    11a0 <db_array_retrieval+0x50>
mov    -0x20(%rbp),%rdi
call   1060 <sqlite3_finalize@plt>
mov    -0x28(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<db_array_retrieval>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edx,%r14d
lea    0x8(%rsp),%rcx
mov    $0xffffffff,%edx
xor    %r8d,%r8d
call   1070 <sqlite3_prepare_v2@plt>
mov    0x8(%rsp),%rdi
call   1040 <sqlite3_step@plt>
lea    0x0(,%r14,8),%edi
call   1050 <malloc@plt>
mov    %rax,%r15
test   %r14d,%r14d
je     11c3 <db_array_retrieval+0x73>
mov    %r14d,%r14d
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    0x8(%rsp),%rdi
xor    %esi,%esi
call   1030 <sqlite3_column_int@plt>
mov    %rax,(%r15,%rbx,8)
mov    0x8(%rsp),%rdi
call   1040 <sqlite3_step@plt>
add    $0x1,%rbx
cmp    %rbx,%r14
jne    11a0 <db_array_retrieval+0x50>
mov    0x8(%rsp),%rdi
call   1060 <sqlite3_finalize@plt>
mov    %r15,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i64* @db_array_retrieval(i32* %0, i8* %1, i32 %2) {
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i64*
%9 = alloca i32
store i32* %0, i32** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%10 = load i32*, i32** %4
%11 = load i8*, i8** %5
%12 = call i32 @sqlite3_prepare_v2(i32* %10, i8* %11, i32 -1, i32** %7, i32* null)
%13 = load i32*, i32** %7
%14 = call i32 @sqlite3_step(i32* %13)
%15 = load i32, i32* %6
%16 = zext i32 %15 to i64
%17 = mul i64 8, %16
%18 = trunc i64 %17 to i32
%19 = call i64 @malloc(i32 %18)
%20 = inttoptr i64 %19 to i64*
store i64* %20, i64** %8
store i32 0, i32* %9
br label %21
21: ; preds = %34, %3
%22 = load i32, i32* %9
%23 = load i32, i32* %6
%24 = icmp ult i32 %22, %23
br i1 %24, label %25, label %37
25: ; preds = %21
%26 = load i32*, i32** %7
%27 = call i64 @sqlite3_column_int(i32* %26, i32 0)
%28 = load i64*, i64** %8
%29 = load i32, i32* %9
%30 = zext i32 %29 to i64
%31 = getelementptr inbounds i64, i64* %28, i64 %30
store i64 %27, i64* %31
%32 = load i32*, i32** %7
%33 = call i32 @sqlite3_step(i32* %32)
br label %34
34: ; preds = %25
%35 = load i32, i32* %9
%36 = add i32 %35, 1
store i32 %36, i32* %9
br label %21
37: ; preds = %21
%38 = load i32*, i32** %7
%39 = call i32 @sqlite3_finalize(i32* %38)
%40 = load i64*, i64** %8
ret i64* %40
}

LLVM IR (-O2):
define i64* @db_array_retrieval(i32* %0, i8* %1, i32 %2) {
%4 = alloca i32*
%5 = bitcast i32** %4 to i8*
%6 = call i32 @sqlite3_prepare_v2(i32* %0, i8* %1, i32 -1, i32** %4, i32* null)
%7 = load i32*, i32** %4
%8 = call i32 @sqlite3_step(i32* %7)
%9 = shl i32 %2, 3
%10 = call i64 @malloc(i32 %9)
%11 = inttoptr i64 %10 to i64*
%12 = icmp eq i32 %2, 0
br i1 %12, label %24, label %13
13: ; preds = %3
%14 = zext i32 %2 to i64
br label %15
15: ; preds = %13, %15
%16 = phi i64 [ 0, %13 ], [ %22, %15 ]
%17 = load i32*, i32** %4
%18 = call i64 @sqlite3_column_int(i32* %17, i32 0)
%19 = getelementptr inbounds i64, i64* %11, i64 %16
store i64 %18, i64* %19
%20 = load i32*, i32** %4
%21 = call i32 @sqlite3_step(i32* %20)
%22 = add nuw nsw i64 %16, 1
%23 = icmp eq i64 %22, %14
br i1 %23, label %24, label %15
24: ; preds = %15, %3
%25 = load i32*, i32** %4
%26 = call i32 @sqlite3_finalize(i32* %25)
ret i64* %11
}