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

/* Variables and functions */
int SQLITE_BUSY;
int SQLITE_LOCKED;
int SQLITE_OK;
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ sleep(int);
int sqlite3_exec(int /*<<< orphan*/ *, char const *, void *, void *, char **);
int /*<<< orphan*/ sqlite3_free(char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
int sql_exec_cb(sqlite3 *db, const char *sql, void *callback, void *cb_arg) {
  int rc;
  char *zErrMsg = 0;
  char looper[4] = {'|', '/', '-', '\\'};
  int looperc = 0;
  int waited = 0;
  while (1) {
    rc = sqlite3_exec(db, sql, callback, cb_arg, &zErrMsg);
    if (rc == SQLITE_LOCKED || rc == SQLITE_BUSY) {
      fprintf(stdout, "Database is locked or busy. Waiting %is ... %1c    \r",
              ++waited, looper[looperc++ % sizeof(looper)]);
      fflush(stdout);
      sleep(1);
    } else {
      if (rc != SQLITE_OK) {
        fprintf(stderr, "SQL error. %s\n", zErrMsg);
        sqlite3_free(zErrMsg);
      }
      if (waited != 0)
        printf("\n\n");
      return rc;
    }
  }
}


x86 assembly (clang, -O0):
<sql_exec_cb>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
movq   $0x0,-0x30(%rbp)
mov    0xe7a(%rip),%eax
mov    %eax,-0x34(%rbp)
movl   $0x0,-0x38(%rbp)
movl   $0x0,-0x3c(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
lea    -0x30(%rbp),%r8
call   1030 <sqlite3_exec@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    0x2e23(%rip),%rcx
cmp    (%rcx),%eax
je     11d7 <sql_exec_cb+0x77>
mov    -0x24(%rbp),%eax
mov    0x2df9(%rip),%rcx
cmp    (%rcx),%eax
jne    122a <sql_exec_cb+0xca>
mov    0x2de2(%rip),%rax
mov    (%rax),%edi
mov    -0x3c(%rbp),%edx
add    $0x1,%edx
mov    %edx,-0x3c(%rbp)
mov    -0x38(%rbp),%eax
mov    %eax,%ecx
add    $0x1,%ecx
mov    %ecx,-0x38(%rbp)
cltq
and    $0x3,%rax
movsbl -0x34(%rbp,%rax,1),%ecx
lea    0xdfe(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    0x2dac(%rip),%rax
mov    (%rax),%edi
call   1060 <fflush@plt>
mov    $0x1,%edi
call   1080 <sleep@plt>
jmp    127f <sql_exec_cb+0x11f>
mov    -0x24(%rbp),%eax
mov    0x2db4(%rip),%rcx
cmp    (%rcx),%eax
je     1260 <sql_exec_cb+0x100>
mov    0x2d8d(%rip),%rax
mov    (%rax),%edi
mov    -0x30(%rbp),%rdx
lea    0xde9(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x30(%rbp),%rdi
call   1070 <sqlite3_free@plt>
cmpl   $0x0,-0x3c(%rbp)
je     1276 <sql_exec_cb+0x116>
lea    0xdd7(%rip),%rdi
call   1040 <printf@plt>
mov    -0x24(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret
jmp    1197 <sql_exec_cb+0x37>

x86 assembly (clang, -O2):
<sql_exec_cb>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%rbx
movq   $0x0,0x8(%rsp)
lea    0x8(%rsp),%r8
mov    %rsi,0x10(%rsp)
mov    %rdx,%r12
mov    %rcx,%r15
call   1040 <sqlite3_exec@plt>
mov    %eax,%ebp
mov    0x2e48(%rip),%rax
cmp    (%rax),%ebp
sete   %al
mov    0x2e24(%rip),%rcx
cmp    (%rcx),%ebp
sete   %r14b
or     %al,%r14b
cmp    $0x1,%r14b
jne    1240 <sql_exec_cb+0xe0>
mov    %r14b,0x7(%rsp)
mov    0x2dfd(%rip),%r14
xor    %r13d,%r13d
cs nopw 0x0(%rax,%rax,1)
mov    (%r14),%edi
mov    %r13d,%eax
add    $0x1,%r13d
and    $0x3,%eax
lea    0xe1c(%rip),%rcx
movsbl (%rax,%rcx,1),%ecx
lea    0xe15(%rip),%rsi
mov    %r13d,%edx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    (%r14),%edi
call   1060 <fflush@plt>
mov    $0x1,%edi
call   1080 <sleep@plt>
mov    %rbx,%rdi
mov    0x10(%rsp),%rsi
mov    %r12,%rdx
mov    %r15,%rcx
lea    0x8(%rsp),%r8
call   1040 <sqlite3_exec@plt>
mov    %eax,%ebp
mov    0x2db4(%rip),%rax
cmp    (%rax),%ebp
je     11d0 <sql_exec_cb+0x70>
mov    0x2d91(%rip),%rax
cmp    (%rax),%ebp
je     11d0 <sql_exec_cb+0x70>
mov    0x7(%rsp),%r14b
mov    0x2da1(%rip),%rax
cmp    (%rax),%ebp
je     1271 <sql_exec_cb+0x111>
mov    0x2d7e(%rip),%rax
mov    (%rax),%edi
mov    0x8(%rsp),%rdx
lea    0xdd9(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    0x8(%rsp),%rdi
call   1070 <sqlite3_free@plt>
test   %r14b,%r14b
je     1282 <sql_exec_cb+0x122>
lea    0xdc9(%rip),%rdi
call   1030 <puts@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_exec_cb.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"
@SQLITE_OK = global i32 0
@stderr = global i32 0
@.str.1 = [15 x i8] c"SQL error. %s\0A\00"
@.str.2 = [3 x i8] c"\0A\0A\00"
define i32 @sql_exec_cb(i32* %0, i8* %1, i8* %2, i8* %3) {
%5 = alloca i32*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i8*
%11 = alloca [4 x i8]
%12 = alloca i32
%13 = alloca i32
store i32* %0, i32** %5
store i8* %1, i8** %6
store i8* %2, i8** %7
store i8* %3, i8** %8
store i8* null, i8** %10
%14 = bitcast [4 x i8]* %11 to i8*
store i32 0, i32* %12
store i32 0, i32* %13
br label %15
15: ; preds = %4, %60
%16 = load i32*, i32** %5
%17 = load i8*, i8** %6
%18 = load i8*, i8** %7
%19 = load i8*, i8** %8
%20 = call i32 @sqlite3_exec(i32* %16, i8* %17, i8* %18, i8* %19, i8** %10)
store i32 %20, i32* %9
%21 = load i32, i32* %9
%22 = load i32, i32* @SQLITE_LOCKED
%23 = icmp eq i32 %21, %22
br i1 %23, label %28, label %24
24: ; preds = %15
%25 = load i32, i32* %9
%26 = load i32, i32* @SQLITE_BUSY
%27 = icmp eq i32 %25, %26
br i1 %27, label %28, label %43
28: ; preds = %24, %15
%29 = load i32, i32* @stdout
%30 = load i32, i32* %13
%31 = add nsw i32 %30, 1
store i32 %31, i32* %13
%32 = load i32, i32* %12
%33 = add nsw i32 %32, 1
store i32 %33, i32* %12
%34 = sext i32 %32 to i64
%35 = urem i64 %34, 4
%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 %29, i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str, i64 0, i64 0), i32 %31, i32 %38)
%40 = load i32, i32* @stdout
%41 = call i32 @fflush(i32 %40)
%42 = call i32 @sleep(i32 1)
br label %60
43: ; preds = %24
%44 = load i32, i32* %9
%45 = load i32, i32* @SQLITE_OK
%46 = icmp ne i32 %44, %45
br i1 %46, label %47, label %53
47: ; preds = %43
%48 = load i32, i32* @stderr
%49 = load i8*, i8** %10
%50 = call i32 (i32, i8*, ...) @fprintf(i32 %48, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8* %49)
%51 = load i8*, i8** %10
%52 = call i32 @sqlite3_free(i8* %51)
br label %53
53: ; preds = %47, %43
%54 = load i32, i32* %13
%55 = icmp ne i32 %54, 0
br i1 %55, label %56, label %58
56: ; preds = %53
%57 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
br label %58
58: ; preds = %56, %53
%59 = load i32, i32* %9
ret i32 %59
60: ; preds = %28
br label %15
}

LLVM IR (-O2):
@__const.sql_exec_cb.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"
@SQLITE_OK = global i32 0
@stderr = global i32 0
@.str.1 = [15 x i8] c"SQL error. %s\0A\00"
@str = [2 x i8] c"\0A\00"
define i32 @sql_exec_cb(i32* %0, i8* %1, i8* %2, i8* %3) {
%5 = alloca i8*
%6 = bitcast i8** %5 to i8*
store i8* null, i8** %5
%7 = call i32 @sqlite3_exec(i32* %0, i8* %1, i8* %2, i8* %3, i8** %5)
%8 = load i32, i32* @SQLITE_LOCKED
%9 = icmp eq i32 %7, %8
%10 = load i32, i32* @SQLITE_BUSY
%11 = icmp eq i32 %7, %10
%12 = select i1 %9, i1 true, i1 %11
br i1 %12, label %13, label %32
13: ; preds = %4, %13
%14 = phi i32 [ %16, %13 ], [ 0, %4 ]
%15 = load i32, i32* @stdout
%16 = add nuw nsw i32 %14, 1
%17 = and i32 %14, 3
%18 = zext i32 %17 to i64
%19 = getelementptr inbounds [4 x i8], [4 x i8]* @__const.sql_exec_cb.looper, i64 0, i64 %18
%20 = load i8, i8* %19
%21 = sext i8 %20 to i32
%22 = call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str, i64 0, i64 0), i32 %16, i32 %21)
%23 = load i32, i32* @stdout
%24 = call i32 @fflush(i32 %23)
%25 = call i32 @sleep(i32 1)
%26 = call i32 @sqlite3_exec(i32* %0, i8* %1, i8* %2, i8* %3, i8** %5)
%27 = load i32, i32* @SQLITE_LOCKED
%28 = icmp eq i32 %26, %27
%29 = load i32, i32* @SQLITE_BUSY
%30 = icmp eq i32 %26, %29
%31 = select i1 %28, i1 true, i1 %30
br i1 %31, label %13, label %32
32: ; preds = %13, %4
%33 = phi i32 [ %7, %4 ], [ %26, %13 ]
%34 = load i32, i32* @SQLITE_OK
%35 = icmp eq i32 %33, %34
br i1 %35, label %42, label %36
36: ; preds = %32
%37 = load i32, i32* @stderr
%38 = load i8*, i8** %5
%39 = call i32 (i32, i8*, ...) @fprintf(i32 %37, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8* %38)
%40 = load i8*, i8** %5
%41 = call i32 @sqlite3_free(i8* %40)
br label %42
42: ; preds = %36, %32
br i1 %12, label %43, label %45
43: ; preds = %42
%44 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([2 x i8], [2 x i8]* @str, i64 0, i64 0))
br label %45
45: ; preds = %43, %42
ret i32 %33
}