#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 /*<<< orphan*/ finishedStmt(int /*<<< orphan*/ *);
int /*<<< orphan*/ *getStmt(char *);
int /*<<< orphan*/ logMsg(int, char *, int);
int sqlite3_column_int(int /*<<< orphan*/ *, int /*<<< orphan*/);
int sqlite3_step(int /*<<< orphan*/ *);
int getNextId(char *sql) {
  sqlite3_stmt *stmtNextId = getStmt(sql);
  int rc;
  rc = sqlite3_step(stmtNextId);
  if (rc == SQLITE_ROW) {
    int maxId = sqlite3_column_int(stmtNextId, 0);
    finishedStmt(stmtNextId);
    return maxId + 1;
  } else {
    finishedStmt(stmtNextId);
    if (rc == SQLITE_DONE) {
      return 1;
    } else {
      logMsg(4, "stmtNextId failed: %d", rc);
      return -1;
    }
  }
}


x86 assembly (clang, -O0):
<getNextId>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <getStmt@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <sqlite3_step@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    0x2e59(%rip),%rcx
cmp    (%rcx),%eax
jne    11ac <getNextId+0x5c>
mov    -0x18(%rbp),%rdi
xor    %esi,%esi
call   1030 <sqlite3_column_int@plt>
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1070 <finishedStmt@plt>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4(%rbp)
jmp    11ee <getNextId+0x9e>
mov    -0x18(%rbp),%rdi
call   1070 <finishedStmt@plt>
mov    -0x1c(%rbp),%eax
mov    0x2e21(%rip),%rcx
cmp    (%rcx),%eax
jne    11d3 <getNextId+0x83>
movl   $0x1,-0x4(%rbp)
jmp    11ee <getNextId+0x9e>
mov    -0x1c(%rbp),%edx
mov    $0x4,%edi
lea    0xe1e(%rip),%rsi
call   1040 <logMsg@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getNextId>:
push   %rbp
push   %r14
push   %rbx
call   1060 <getStmt@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1050 <sqlite3_step@plt>
mov    %eax,%r14d
mov    0x2e6a(%rip),%rax
cmp    (%rax),%r14d
jne    118c <getNextId+0x3c>
mov    %rbx,%rdi
xor    %esi,%esi
call   1030 <sqlite3_column_int@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1070 <finishedStmt@plt>
add    $0x1,%ebp
jmp    11be <getNextId+0x6e>
mov    %rbx,%rdi
call   1070 <finishedStmt@plt>
mov    0x2e45(%rip),%rax
mov    $0x1,%ebp
cmp    (%rax),%r14d
je     11be <getNextId+0x6e>
lea    0xe54(%rip),%rsi
mov    $0x4,%edi
mov    %r14d,%edx
call   1040 <logMsg@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@SQLITE_ROW = global i32 0
@SQLITE_DONE = global i32 0
@.str = [22 x i8] c"stmtNextId failed: %d\00"
define i32 @getNextId(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %3
%7 = load i8*, i8** %3
%8 = call i32* @getStmt(i8* %7)
store i32* %8, i32** %4
%9 = load i32*, i32** %4
%10 = call i32 @sqlite3_step(i32* %9)
store i32 %10, i32* %5
%11 = load i32, i32* %5
%12 = load i32, i32* @SQLITE_ROW
%13 = icmp eq i32 %11, %12
br i1 %13, label %14, label %21
14: ; preds = %1
%15 = load i32*, i32** %4
%16 = call i32 @sqlite3_column_int(i32* %15, i32 0)
store i32 %16, i32* %6
%17 = load i32*, i32** %4
%18 = call i32 @finishedStmt(i32* %17)
%19 = load i32, i32* %6
%20 = add nsw i32 %19, 1
store i32 %20, i32* %2
br label %31
21: ; preds = %1
%22 = load i32*, i32** %4
%23 = call i32 @finishedStmt(i32* %22)
%24 = load i32, i32* %5
%25 = load i32, i32* @SQLITE_DONE
%26 = icmp eq i32 %24, %25
br i1 %26, label %27, label %28
27: ; preds = %21
store i32 1, i32* %2
br label %31
28: ; preds = %21
%29 = load i32, i32* %5
%30 = call i32 @logMsg(i32 4, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i32 %29)
store i32 -1, i32* %2
br label %31
31: ; preds = %28, %27, %14
%32 = load i32, i32* %2
ret i32 %32
}

LLVM IR (-O2):
@SQLITE_ROW = global i32 0
@SQLITE_DONE = global i32 0
@.str = [22 x i8] c"stmtNextId failed: %d\00"
define i32 @getNextId(i8* %0) {
%2 = tail call i32* @getStmt(i8* %0)
%3 = tail call i32 @sqlite3_step(i32* %2)
%4 = load i32, i32* @SQLITE_ROW
%5 = icmp eq i32 %3, %4
br i1 %5, label %6, label %10
6: ; preds = %1
%7 = tail call i32 @sqlite3_column_int(i32* %2, i32 0)
%8 = tail call i32 @finishedStmt(i32* %2)
%9 = add nsw i32 %7, 1
br label %16
10: ; preds = %1
%11 = tail call i32 @finishedStmt(i32* %2)
%12 = load i32, i32* @SQLITE_DONE
%13 = icmp eq i32 %3, %12
br i1 %13, label %16, label %14
14: ; preds = %10
%15 = tail call i32 @logMsg(i32 4, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i32 %3)
br label %16
16: ; preds = %10, %14, %6
%17 = phi i32 [ %9, %6 ], [ -1, %14 ], [ 1, %10 ]
ret i32 %17
}