#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*/ 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_step(int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
int sql_step(sqlite3_stmt *stmt, int wait) {
  int rc;
  char looper[4] = {'|', '/', '-', '\\'};
  int looperc = 0;
  int waited = 0;
  while (1) {
    rc = sqlite3_step(stmt);
    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 getting results. "
                        "I've given up.\n");
        return rc;
      }
    } else {
      if (waited != 0)
        printf("\n\n");
      return rc;
    }
  }
}


x86 assembly (clang, -O0):
<sql_step>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    0xe9b(%rip),%eax
mov    %eax,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <sqlite3_step@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
mov    0x2e5c(%rip),%rcx
cmp    (%rcx),%eax
je     11a6 <sql_step+0x56>
mov    -0x18(%rbp),%eax
mov    0x2e32(%rip),%rcx
cmp    (%rcx),%eax
jne    1231 <sql_step+0xe1>
cmpl   $0x0,-0x14(%rbp)
je     120a <sql_step+0xba>
mov    0x2e11(%rip),%rax
mov    (%rax),%edi
mov    -0x24(%rbp),%edx
add    $0x1,%edx
mov    %edx,-0x24(%rbp)
movslq -0x20(%rbp),%rax
movsbl -0x1c(%rbp,%rax,1),%ecx
lea    0xe32(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    0x2de8(%rip),%rax
mov    (%rax),%edi
call   1060 <fflush@plt>
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
movslq -0x20(%rbp),%rax
add    $0x1,%rax
mov    %eax,-0x20(%rbp)
mov    $0x1,%edi
call   1070 <sleep@plt>
jmp    122c <sql_step+0xdc>
mov    0x2dc7(%rip),%rax
mov    (%rax),%edi
lea    0xe1f(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1257 <sql_step+0x107>
jmp    1252 <sql_step+0x102>
cmpl   $0x0,-0x24(%rbp)
je     1247 <sql_step+0xf7>
lea    0xe3a(%rip),%rdi
call   1030 <printf@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1257 <sql_step+0x107>
jmp    1176 <sql_step+0x26>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sql_step>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%r14d
mov    %rdi,%r15
call   1040 <sqlite3_step@plt>
mov    %eax,%ebp
mov    0x2e79(%rip),%rax
cmp    (%rax),%ebp
sete   %al
mov    0x2e55(%rip),%rcx
cmp    (%rcx),%ebp
sete   %cl
or     %al,%cl
cmp    $0x1,%cl
jne    1249 <sql_step+0xf9>
mov    %cl,0x7(%rsp)
test   %r14d,%r14d
je     1232 <sql_step+0xe2>
neg    %r14d
mov    $0x1,%ebx
mov    0x2e21(%rip),%r13
xor    %r12d,%r12d
jmp    11c7 <sql_step+0x77>
nopl   0x0(%rax)
add    $0x1,%r12
lea    (%r14,%rbx,1),%eax
add    $0x1,%eax
mov    %ebx,%ecx
add    $0x1,%ecx
mov    %ecx,%ebx
cmp    $0x1,%eax
je     1232 <sql_step+0xe2>
mov    0x0(%r13),%edi
movslq %r12d,%r12
lea    0xe2b(%rip),%rax
movsbl (%r12,%rax,1),%ecx
lea    0xe23(%rip),%rsi
mov    %ebx,%edx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    0x0(%r13),%edi
call   1060 <fflush@plt>
mov    $0x1,%edi
call   1070 <sleep@plt>
mov    %r15,%rdi
call   1040 <sqlite3_step@plt>
mov    %eax,%ebp
mov    0x2dda(%rip),%rax
cmp    (%rax),%ebp
je     11b0 <sql_step+0x60>
mov    0x2db7(%rip),%rax
cmp    (%rax),%ebp
je     11b0 <sql_step+0x60>
cmpb   $0x0,0x7(%rsp)
je     1249 <sql_step+0xf9>
lea    0xe4f(%rip),%rdi
call   1030 <puts@plt>
jmp    1249 <sql_step+0xf9>
mov    0x2d9f(%rip),%rax
mov    (%rax),%edi
lea    0xdf7(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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