#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;

/* Variables and functions */
int SQLITE_DONE;
int SQLITE_ROW;
int sqlite3_step(int /*<<< orphan*/ *);
int count_rows(sqlite3_stmt *stmt) {
  int count = 0, rc;
  while ((rc = sqlite3_step(stmt)) != SQLITE_DONE) {
    if (rc == SQLITE_ROW)
      count++;
    else
      return -1;
  }
  return count;
}


x86 assembly (clang, -O0):
<count_rows>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <sqlite3_step@plt>
mov    %eax,-0x18(%rbp)
mov    0x2eaa(%rip),%rcx
cmp    (%rcx),%eax
je     116f <count_rows+0x5f>
mov    -0x18(%rbp),%eax
mov    0x2e90(%rip),%rcx
cmp    (%rcx),%eax
jne    115e <count_rows+0x4e>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    116a <count_rows+0x5a>
movl   $0xffffffff,-0x4(%rbp)
jmp    1175 <count_rows+0x65>
jmp    1123 <count_rows+0x13>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<count_rows>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
call   1030 <sqlite3_step@plt>
mov    0x2eba(%rip),%r14
xor    %ebp,%ebp
cmp    (%r14),%eax
je     115c <count_rows+0x4c>
xor    %ebp,%ebp
mov    0x2ea2(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
cmp    (%r15),%eax
jne    1157 <count_rows+0x47>
add    $0x1,%ebp
mov    %rbx,%rdi
call   1030 <sqlite3_step@plt>
cmp    (%r14),%eax
jne    1140 <count_rows+0x30>
jmp    115c <count_rows+0x4c>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@SQLITE_DONE = global i32 0
@SQLITE_ROW = global i32 0
define i32 @count_rows(i32* %0) {
%2 = alloca i32
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i32
store i32* %0, i32** %3
store i32 0, i32* %4
br label %6
6: ; preds = %19, %1
%7 = load i32*, i32** %3
%8 = call i32 @sqlite3_step(i32* %7)
store i32 %8, i32* %5
%9 = load i32, i32* @SQLITE_DONE
%10 = icmp ne i32 %8, %9
br i1 %10, label %11, label %20
11: ; preds = %6
%12 = load i32, i32* %5
%13 = load i32, i32* @SQLITE_ROW
%14 = icmp eq i32 %12, %13
br i1 %14, label %15, label %18
15: ; preds = %11
%16 = load i32, i32* %4
%17 = add nsw i32 %16, 1
store i32 %17, i32* %4
br label %19
18: ; preds = %11
store i32 -1, i32* %2
br label %22
19: ; preds = %15
br label %6
20: ; preds = %6
%21 = load i32, i32* %4
store i32 %21, i32* %2
br label %22
22: ; preds = %20, %18
%23 = load i32, i32* %2
ret i32 %23
}

LLVM IR (-O2):
@SQLITE_DONE = global i32 0
@SQLITE_ROW = global i32 0
define i32 @count_rows(i32* %0) {
%2 = tail call i32 @sqlite3_step(i32* %0)
%3 = load i32, i32* @SQLITE_DONE
%4 = icmp eq i32 %2, %3
br i1 %4, label %15, label %5
5: ; preds = %1, %10
%6 = phi i32 [ %12, %10 ], [ %2, %1 ]
%7 = phi i32 [ %11, %10 ], [ 0, %1 ]
%8 = load i32, i32* @SQLITE_ROW
%9 = icmp eq i32 %6, %8
br i1 %9, label %10, label %15
10: ; preds = %5
%11 = add nuw nsw i32 %7, 1
%12 = tail call i32 @sqlite3_step(i32* %0)
%13 = load i32, i32* @SQLITE_DONE
%14 = icmp eq i32 %12, %13
br i1 %14, label %15, label %5
15: ; preds = %5, %10, %1
%16 = phi i32 [ 0, %1 ], [ %11, %10 ], [ -1, %5 ]
ret i32 %16
}