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

/* Variables and functions */
int SQLITE_BUSY;
int SQLITE_LOCKED;
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ sleep(int);
int sqlite3_prepare_v2(int /*<<< orphan*/ *, char const *, int,
                       int /*<<< orphan*/ **, int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
int sql_prepare(sqlite3 *db, const char *sql, sqlite3_stmt **ppStmt, int wait) {
  int rc;
  char looper[4] = {'|', '/', '-', '\\'};
  int looperc = 0;
  int waited = 0;
  while (1) {
    rc = sqlite3_prepare_v2(db, sql, -1, ppStmt, NULL);
    if (rc == SQLITE_LOCKED || rc == SQLITE_BUSY) {
      if (wait != 0) {
        fprintf(stdout, "Database is locked or busy. Waiting %is ... %1c    \r",
                ++waited, looper[looperc]);
        fflush(stdout);
        wait--;
        looperc = looperc + 1 % sizeof(looper);
        sleep(1);
      } else {
        fprintf(stderr, "Database was locked or busy while creating statement. "
                        "I've given up.\n");
        return rc;
      }
    } else {
      if (waited != 0)
        printf("\n\n");
      return rc;
    }
  }
}


x86 assembly (clang, -O0):
<sql_prepare>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
mov    0xe93(%rip),%eax
mov    %eax,-0x2c(%rbp)
movl   $0x0,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rcx
mov    $0xffffffff,%edx
xor    %eax,%eax
mov    %eax,%r8d
call   1060 <sqlite3_prepare_v2@plt>
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%eax
mov    0x2e42(%rip),%rcx
cmp    (%rcx),%eax
je     11c0 <sql_prepare+0x70>
mov    -0x28(%rbp),%eax
mov    0x2e18(%rip),%rcx
cmp    (%rcx),%eax
jne    124b <sql_prepare+0xfb>
cmpl   $0x0,-0x24(%rbp)
je     1224 <sql_prepare+0xd4>
mov    0x2df7(%rip),%rax
mov    (%rax),%edi
mov    -0x34(%rbp),%edx
add    $0x1,%edx
mov    %edx,-0x34(%rbp)
movslq -0x30(%rbp),%rax
movsbl -0x2c(%rbp,%rax,1),%ecx
lea    0xe18(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    0x2dce(%rip),%rax
mov    (%rax),%edi
call   1050 <fflush@plt>
mov    -0x24(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x24(%rbp)
movslq -0x30(%rbp),%rax
add    $0x1,%rax
mov    %eax,-0x30(%rbp)
mov    $0x1,%edi
call   1070 <sleep@plt>
jmp    1246 <sql_prepare+0xf6>
mov    0x2dad(%rip),%rax
mov    (%rax),%edi
lea    0xe05(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1271 <sql_prepare+0x121>
jmp    126c <sql_prepare+0x11c>
cmpl   $0x0,-0x34(%rbp)
je     1261 <sql_prepare+0x111>
lea    0xe23(%rip),%rdi
call   1030 <printf@plt>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1271 <sql_prepare+0x121>
jmp    117e <sql_prepare+0x2e>
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sql_prepare>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %ecx,%ebx
mov    %rdx,%rcx
mov    %rdi,%r13
mov    %rsi,0x8(%rsp)
mov    $0xffffffff,%edx
mov    %rcx,0x10(%rsp)
xor    %r8d,%r8d
call   1060 <sqlite3_prepare_v2@plt>
mov    %eax,%ebp
mov    0x2e62(%rip),%rax
cmp    (%rax),%ebp
sete   %al
mov    0x2e3e(%rip),%rcx
cmp    (%rcx),%ebp
sete   %cl
or     %al,%cl
cmp    $0x1,%cl
jne    1279 <sql_prepare+0x129>
mov    %cl,0x7(%rsp)
test   %ebx,%ebx
je     1262 <sql_prepare+0x112>
mov    %rbx,%r12
neg    %r12d
mov    $0x1,%ebx
mov    0x2e08(%rip),%r14
xor    %r15d,%r15d
jmp    11e7 <sql_prepare+0x97>
cs nopw 0x0(%rax,%rax,1)
nop
add    $0x1,%r15
lea    (%r12,%rbx,1),%eax
add    $0x1,%eax
mov    %ebx,%ecx
add    $0x1,%ecx
mov    %ecx,%ebx
cmp    $0x1,%eax
je     1262 <sql_prepare+0x112>
mov    (%r14),%edi
movslq %r15d,%r15
lea    0xe0c(%rip),%rax
movsbl (%r15,%rax,1),%ecx
lea    0xe04(%rip),%rsi
mov    %ebx,%edx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    (%r14),%edi
call   1050 <fflush@plt>
mov    $0x1,%edi
call   1070 <sleep@plt>
mov    %r13,%rdi
mov    0x8(%rsp),%rsi
mov    $0xffffffff,%edx
mov    0x10(%rsp),%rcx
xor    %r8d,%r8d
call   1060 <sqlite3_prepare_v2@plt>
mov    %eax,%ebp
mov    0x2daa(%rip),%rax
cmp    (%rax),%ebp
je     11d0 <sql_prepare+0x80>
mov    0x2d87(%rip),%rax
cmp    (%rax),%ebp
je     11d0 <sql_prepare+0x80>
cmpb   $0x0,0x7(%rsp)
je     1279 <sql_prepare+0x129>
lea    0xe22(%rip),%rdi
call   1030 <puts@plt>
jmp    1279 <sql_prepare+0x129>
mov    0x2d6f(%rip),%rax
mov    (%rax),%edi
lea    0xdc7(%rip),%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@__const.sql_prepare.looper = [4 x i8] c"|/-\\"
@SQLITE_LOCKED = global i32 0
@SQLITE_BUSY = global i32 0
@stdout = global i32 0
@.str = [53 x i8] c"Database is locked or busy. Waiting %is ... %1c \0D\00"
@stderr = global i32 0
@.str.1 = [70 x i8] c"Database was locked or busy while creating statement. I've given up.\0A\00"
@.str.2 = [3 x i8] c"\0A\0A\00"
define i32 @sql_prepare(i32* %0, i8* %1, i32** %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i8*
%8 = alloca i32**
%9 = alloca i32
%10 = alloca i32
%11 = alloca [4 x i8]
%12 = alloca i32
%13 = alloca i32
store i32* %0, i32** %6
store i8* %1, i8** %7
store i32** %2, i32*** %8
store i32 %3, i32* %9
%14 = bitcast [4 x i8]* %11 to i8*
store i32 0, i32* %12
store i32 0, i32* %13
br label %15
15: ; preds = %4, %61
%16 = load i32*, i32** %6
%17 = load i8*, i8** %7
%18 = load i32**, i32*** %8
%19 = call i32 @sqlite3_prepare_v2(i32* %16, i8* %17, i32 -1, i32** %18, i32* null)
store i32 %19, i32* %10
%20 = load i32, i32* %10
%21 = load i32, i32* @SQLITE_LOCKED
%22 = icmp eq i32 %20, %21
br i1 %22, label %27, label %23
23: ; preds = %15
%24 = load i32, i32* %10
%25 = load i32, i32* @SQLITE_BUSY
%26 = icmp eq i32 %24, %25
br i1 %26, label %27, label %54
27: ; preds = %23, %15
%28 = load i32, i32* %9
%29 = icmp ne i32 %28, 0
br i1 %29, label %30, label %49
30: ; preds = %27
%31 = load i32, i32* @stdout
%32 = load i32, i32* %13
%33 = add nsw i32 %32, 1
store i32 %33, i32* %13
%34 = load i32, i32* %12
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds [4 x i8], [4 x i8]* %11, i64 0, i64 %35
%37 = load i8, i8* %36
%38 = sext i8 %37 to i32
%39 = call i32 (i32, i8*, ...) @fprintf(i32 %31, i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str, i64 0, i64 0), i32 %33, i32 %38)
%40 = load i32, i32* @stdout
%41 = call i32 @fflush(i32 %40)
%42 = load i32, i32* %9
%43 = add nsw i32 %42, -1
store i32 %43, i32* %9
%44 = load i32, i32* %12
%45 = sext i32 %44 to i64
%46 = add i64 %45, 1
%47 = trunc i64 %46 to i32
store i32 %47, i32* %12
%48 = call i32 @sleep(i32 1)
br label %53
49: ; preds = %27
%50 = load i32, i32* @stderr
%51 = call i32 (i32, i8*, ...) @fprintf(i32 %50, i8* getelementptr inbounds ([70 x i8], [70 x i8]* @.str.1, i64 0, i64 0))
%52 = load i32, i32* %10
store i32 %52, i32* %5
br label %62
53: ; preds = %30
br label %61
54: ; preds = %23
%55 = load i32, i32* %13
%56 = icmp ne i32 %55, 0
br i1 %56, label %57, label %59
57: ; preds = %54
%58 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
br label %59
59: ; preds = %57, %54
%60 = load i32, i32* %10
store i32 %60, i32* %5
br label %62
61: ; preds = %53
br label %15
62: ; preds = %59, %49
%63 = load i32, i32* %5
ret i32 %63
}

LLVM IR (-O2):
@__const.sql_prepare.looper = [4 x i8] c"|/-\\"
@SQLITE_LOCKED = global i32 0
@SQLITE_BUSY = global i32 0
@stdout = global i32 0
@.str = [53 x i8] c"Database is locked or busy. Waiting %is ... %1c \0D\00"
@stderr = global i32 0
@.str.1 = [70 x i8] c"Database was locked or busy while creating statement. I've given up.\0A\00"
@str = [2 x i8] c"\0A\00"
define i32 @sql_prepare(i32* %0, i8* %1, i32** %2, i32 %3) {
%5 = tail call i32 @sqlite3_prepare_v2(i32* %0, i8* %1, i32 -1, i32** %2, i32* null)
%6 = load i32, i32* @SQLITE_LOCKED
%7 = icmp eq i32 %5, %6
%8 = load i32, i32* @SQLITE_BUSY
%9 = icmp eq i32 %5, %8
%10 = select i1 %7, i1 true, i1 %9
br i1 %10, label %11, label %45
11: ; preds = %4
%12 = icmp eq i32 %3, 0
br i1 %12, label %38, label %16
13: ; preds = %16
%14 = add nsw i64 %23, 1
%15 = icmp eq i32 %30, 0
br i1 %15, label %38, label %16
16: ; preds = %11, %13
%17 = phi i32 [ %30, %13 ], [ %3, %11 ]
%18 = phi i64 [ %14, %13 ], [ 0, %11 ]
%19 = phi i32 [ %21, %13 ], [ 0, %11 ]
%20 = load i32, i32* @stdout
%21 = add nuw nsw i32 %19, 1
%22 = shl i64 %18, 32
%23 = ashr exact i64 %22, 32
%24 = getelementptr inbounds [4 x i8], [4 x i8]* @__const.sql_prepare.looper, i64 0, i64 %23
%25 = load i8, i8* %24
%26 = sext i8 %25 to i32
%27 = tail call i32 (i32, i8*, ...) @fprintf(i32 %20, i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str, i64 0, i64 0), i32 %21, i32 %26)
%28 = load i32, i32* @stdout
%29 = tail call i32 @fflush(i32 %28)
%30 = add nsw i32 %17, -1
%31 = tail call i32 @sleep(i32 1)
%32 = tail call i32 @sqlite3_prepare_v2(i32* %0, i8* %1, i32 -1, i32** %2, i32* null)
%33 = load i32, i32* @SQLITE_LOCKED
%34 = icmp eq i32 %32, %33
%35 = load i32, i32* @SQLITE_BUSY
%36 = icmp eq i32 %32, %35
%37 = select i1 %34, i1 true, i1 %36
br i1 %37, label %13, label %42
38: ; preds = %13, %11
%39 = phi i32 [ %5, %11 ], [ %32, %13 ]
%40 = load i32, i32* @stderr
%41 = tail call i32 (i32, i8*, ...) @fprintf(i32 %40, i8* getelementptr inbounds ([70 x i8], [70 x i8]* @.str.1, i64 0, i64 0))
br label %45
42: ; preds = %16
br i1 %10, label %43, label %45
43: ; preds = %42
%44 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([2 x i8], [2 x i8]* @str, i64 0, i64 0))
br label %45
45: ; preds = %4, %42, %43, %38
%46 = phi i32 [ %32, %42 ], [ %32, %43 ], [ %39, %38 ], [ %5, %4 ]
ret i32 %46
}