#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 Get_Full_Names(int /*<<< orphan*/ *, int /*<<< orphan*/ *, char **, int);
int Query_Max_Number_Of_Names(int /*<<< orphan*/ *, int /*<<< orphan*/ *);
char **calloc(int, int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ sqlite3_close(int /*<<< orphan*/ *);
char *sqlite3_errmsg(int /*<<< orphan*/ *);
int /*<<< orphan*/ sqlite3_finalize(int /*<<< orphan*/ *);
int sqlite3_open(char *, int /*<<< orphan*/ **);
int /*<<< orphan*/ stderr;
int main() {
  sqlite3 *db;
  sqlite3_stmt *stmt = NULL;
  int rv;
  rv = sqlite3_open("NamesDatabase.db", &db);
  if (rv) {
    fprintf(stderr, "Failed to open database %s\n", sqlite3_errmsg(db));
    sqlite3_close(db);
    return -1;
  }
  int maxSize = Query_Max_Number_Of_Names(db, stmt);
  if (maxSize <= 0) {
    fprintf(stderr, "Database contains no names!\n");
    sqlite3_close(db);
    return -1;
  }
  char **fullnames = calloc(5500000, 26);
  if (5500000 > maxSize) {
    rv = Get_Full_Names(db, stmt, fullnames, maxSize);
  } else {
    rv = Get_Full_Names(db, stmt, fullnames, 5500000);
  }
  if (rv == -1) {
    fprintf(stderr, "Failed to get names\n");
    sqlite3_close(db);
    return -1;
  }
  sqlite3_finalize(stmt);
  sqlite3_close(db);
  printf("Completed generation of %d random names\n", 5500000);
  for (int i = 0; i < 5500000; i++) {
    free(fullnames[i]);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
movq   $0x0,-0x18(%rbp)
lea    0xe42(%rip),%rdi
lea    -0x10(%rbp),%rsi
call   1040 <sqlite3_open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     1212 <main+0x72>
mov    0x2e05(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <sqlite3_errmsg@plt>
mov    -0x30(%rbp),%edi
mov    %rax,%rdx
lea    0xe1b(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    -0x10(%rbp),%rdi
call   1090 <sqlite3_close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1341 <main+0x1a1>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   10c0 <Query_Max_Number_Of_Names@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jg     1258 <main+0xb8>
mov    0x2dad(%rip),%rax
mov    (%rax),%edi
lea    0xdf1(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    -0x10(%rbp),%rdi
call   1090 <sqlite3_close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1341 <main+0x1a1>
mov    $0x53ec60,%edi
mov    $0x1a,%esi
call   1070 <calloc@plt>
mov    %rax,-0x28(%rbp)
mov    $0x53ec60,%eax
cmp    -0x20(%rbp),%eax
jle    1295 <main+0xf5>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x28(%rbp),%rdx
mov    -0x20(%rbp),%ecx
call   10a0 <Get_Full_Names@plt>
mov    %eax,-0x1c(%rbp)
jmp    12ae <main+0x10e>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x28(%rbp),%rdx
mov    $0x53ec60,%ecx
call   10a0 <Get_Full_Names@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    12e4 <main+0x144>
mov    0x2d21(%rip),%rax
mov    (%rax),%edi
lea    0xd82(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    -0x10(%rbp),%rdi
call   1090 <sqlite3_close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1341 <main+0x1a1>
mov    -0x18(%rbp),%rdi
call   10b0 <sqlite3_finalize@plt>
mov    -0x10(%rbp),%rdi
call   1090 <sqlite3_close@plt>
lea    0xd62(%rip),%rdi
mov    $0x53ec60,%esi
call   1050 <printf@plt>
movl   $0x0,-0x2c(%rbp)
cmpl   $0x53ec60,-0x2c(%rbp)
jge    133a <main+0x19a>
mov    -0x28(%rbp),%rax
movslq -0x2c(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
call   1030 <free@plt>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    130e <main+0x16e>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
lea    0xe51(%rip),%rdi
lea    0x8(%rsp),%rsi
call   1040 <sqlite3_open@plt>
test   %eax,%eax
je     11e8 <main+0x48>
mov    0x2e1c(%rip),%rax
mov    (%rax),%ebx
mov    0x8(%rsp),%rdi
call   1060 <sqlite3_errmsg@plt>
lea    0xe3a(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1080 <fprintf@plt>
jmp    12a1 <main+0x101>
mov    0x8(%rsp),%rdi
xor    %esi,%esi
call   10c0 <Query_Max_Number_Of_Names@plt>
test   %eax,%eax
jle    1278 <main+0xd8>
mov    %eax,%ebp
mov    $0x53ec60,%r14d
mov    $0x53ec60,%edi
mov    $0x1a,%esi
call   1070 <calloc@plt>
mov    %rax,%rbx
cmp    $0x53ec60,%ebp
mov    0x8(%rsp),%rdi
cmovb  %ebp,%r14d
xor    %esi,%esi
mov    %rax,%rdx
mov    %r14d,%ecx
call   10a0 <Get_Full_Names@plt>
cmp    $0xffffffff,%eax
je     128a <main+0xea>
xor    %r14d,%r14d
xor    %edi,%edi
call   10b0 <sqlite3_finalize@plt>
mov    0x8(%rsp),%rdi
call   1090 <sqlite3_close@plt>
lea    0xe0d(%rip),%rdi
mov    $0x53ec60,%esi
call   1050 <printf@plt>
xor    %ebp,%ebp
xchg   %ax,%ax
mov    (%rbx,%rbp,8),%rdi
call   1030 <free@plt>
add    $0x1,%rbp
cmp    $0x53ec60,%rbp
jne    1260 <main+0xc0>
jmp    12b1 <main+0x111>
mov    0x2d61(%rip),%rax
mov    (%rax),%edi
lea    0xda5(%rip),%rsi
jmp    129a <main+0xfa>
mov    0x2d4f(%rip),%rax
mov    (%rax),%edi
lea    0xdb0(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    0x8(%rsp),%rdi
call   1090 <sqlite3_close@plt>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [17 x i8] c"NamesDatabase.db\00"
@stderr = global i32 0
@.str.1 = [28 x i8] c"Failed to open database %s\0A\00"
@.str.2 = [29 x i8] c"Database contains no names!\0A\00"
@.str.3 = [21 x i8] c"Failed to get names\0A\00"
@.str.4 = [41 x i8] c"Completed generation of %d random names\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8**
%7 = alloca i32
store i32 0, i32* %1
store i32* null, i32** %3
%8 = call i32 @sqlite3_open(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32** %2)
store i32 %8, i32* %4
%9 = load i32, i32* %4
%10 = icmp ne i32 %9, 0
br i1 %10, label %11, label %18
11: ; preds = %0
%12 = load i32, i32* @stderr
%13 = load i32*, i32** %2
%14 = call i8* @sqlite3_errmsg(i32* %13)
%15 = call i32 (i32, i8*, ...) @fprintf(i32 %12, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i8* %14)
%16 = load i32*, i32** %2
%17 = call i32 @sqlite3_close(i32* %16)
store i32 -1, i32* %1
br label %72
18: ; preds = %0
%19 = load i32*, i32** %2
%20 = load i32*, i32** %3
%21 = call i32 @Query_Max_Number_Of_Names(i32* %19, i32* %20)
store i32 %21, i32* %5
%22 = load i32, i32* %5
%23 = icmp sle i32 %22, 0
br i1 %23, label %24, label %29
24: ; preds = %18
%25 = load i32, i32* @stderr
%26 = call i32 (i32, i8*, ...) @fprintf(i32 %25, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0))
%27 = load i32*, i32** %2
%28 = call i32 @sqlite3_close(i32* %27)
store i32 -1, i32* %1
br label %72
29: ; preds = %18
%30 = call i8** @calloc(i32 5500000, i32 26)
store i8** %30, i8*** %6
%31 = load i32, i32* %5
%32 = icmp sgt i32 5500000, %31
br i1 %32, label %33, label %39
33: ; preds = %29
%34 = load i32*, i32** %2
%35 = load i32*, i32** %3
%36 = load i8**, i8*** %6
%37 = load i32, i32* %5
%38 = call i32 @Get_Full_Names(i32* %34, i32* %35, i8** %36, i32 %37)
store i32 %38, i32* %4
br label %44
39: ; preds = %29
%40 = load i32*, i32** %2
%41 = load i32*, i32** %3
%42 = load i8**, i8*** %6
%43 = call i32 @Get_Full_Names(i32* %40, i32* %41, i8** %42, i32 5500000)
store i32 %43, i32* %4
br label %44
44: ; preds = %39, %33
%45 = load i32, i32* %4
%46 = icmp eq i32 %45, -1
br i1 %46, label %47, label %52
47: ; preds = %44
%48 = load i32, i32* @stderr
%49 = call i32 (i32, i8*, ...) @fprintf(i32 %48, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
%50 = load i32*, i32** %2
%51 = call i32 @sqlite3_close(i32* %50)
store i32 -1, i32* %1
br label %72
52: ; preds = %44
%53 = load i32*, i32** %3
%54 = call i32 @sqlite3_finalize(i32* %53)
%55 = load i32*, i32** %2
%56 = call i32 @sqlite3_close(i32* %55)
%57 = call i32 @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.4, i64 0, i64 0), i32 5500000)
store i32 0, i32* %7
br label %58
58: ; preds = %68, %52
%59 = load i32, i32* %7
%60 = icmp slt i32 %59, 5500000
br i1 %60, label %61, label %71
61: ; preds = %58
%62 = load i8**, i8*** %6
%63 = load i32, i32* %7
%64 = sext i32 %63 to i64
%65 = getelementptr inbounds i8*, i8** %62, i64 %64
%66 = load i8*, i8** %65
%67 = call i32 @free(i8* %66)
br label %68
68: ; preds = %61
%69 = load i32, i32* %7
%70 = add nsw i32 %69, 1
store i32 %70, i32* %7
br label %58
71: ; preds = %58
store i32 0, i32* %1
br label %72
72: ; preds = %71, %47, %24, %11
%73 = load i32, i32* %1
ret i32 %73
}

LLVM IR (-O2):
@.str = [17 x i8] c"NamesDatabase.db\00"
@stderr = global i32 0
@.str.1 = [28 x i8] c"Failed to open database %s\0A\00"
@.str.2 = [29 x i8] c"Database contains no names!\0A\00"
@.str.3 = [21 x i8] c"Failed to get names\0A\00"
@.str.4 = [41 x i8] c"Completed generation of %d random names\0A\00"
define i32 @main() {
%1 = alloca i32*
%2 = bitcast i32** %1 to i8*
%3 = call i32 @sqlite3_open(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32** %1)
%4 = icmp eq i32 %3, 0
br i1 %4, label %10, label %5
5: ; preds = %0
%6 = load i32, i32* @stderr
%7 = load i32*, i32** %1
%8 = call i8* @sqlite3_errmsg(i32* %7)
%9 = call i32 (i32, i8*, ...) @fprintf(i32 %6, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i8* %8)
br label %39
10: ; preds = %0
%11 = load i32*, i32** %1
%12 = call i32 @Query_Max_Number_Of_Names(i32* %11, i32* null)
%13 = icmp slt i32 %12, 1
br i1 %13, label %14, label %17
14: ; preds = %10
%15 = load i32, i32* @stderr
%16 = call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0))
br label %39
17: ; preds = %10
%18 = call i8** @calloc(i32 5500000, i32 26)
%19 = icmp ult i32 %12, 5500000
%20 = load i32*, i32** %1
%21 = select i1 %19, i32 %12, i32 5500000
%22 = call i32 @Get_Full_Names(i32* %20, i32* null, i8** %18, i32 %21)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %27
24: ; preds = %17
%25 = load i32, i32* @stderr
%26 = call i32 (i32, i8*, ...) @fprintf(i32 %25, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
br label %39
27: ; preds = %17
%28 = call i32 @sqlite3_finalize(i32* null)
%29 = load i32*, i32** %1
%30 = call i32 @sqlite3_close(i32* %29)
%31 = call i32 @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.4, i64 0, i64 0), i32 5500000)
br label %32
32: ; preds = %27, %32
%33 = phi i64 [ 0, %27 ], [ %37, %32 ]
%34 = getelementptr inbounds i8*, i8** %18, i64 %33
%35 = load i8*, i8** %34
%36 = call i32 @free(i8* %35)
%37 = add nuw nsw i64 %33, 1
%38 = icmp eq i64 %37, 5500000
br i1 %38, label %42, label %32
39: ; preds = %5, %24, %14
%40 = load i32*, i32** %1
%41 = call i32 @sqlite3_close(i32* %40)
br label %42
42: ; preds = %32, %39
%43 = phi i32 [ -1, %39 ], [ 0, %32 ]
ret i32 %43
}