#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 */
struct Alert {
  int dummy;
};
typedef int /*<<< orphan*/ sqlite3_stmt;

/* Variables and functions */
int SQLITE_DONE;
int SQLITE_ROW;
struct Alert *alertForRow(int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                          int /*<<< orphan*/ *);
int /*<<< orphan*/ appendAlert(struct Alert **, struct Alert *);
int /*<<< orphan*/ beginTrans(int /*<<< orphan*/);
int /*<<< orphan*/ commitTrans();
int /*<<< orphan*/ finishedStmt(int /*<<< orphan*/ *);
int /*<<< orphan*/ *getStmt(char *);
int /*<<< orphan*/ logMsg(int, char *, int);
int sqlite3_step(int /*<<< orphan*/ *);
struct Alert *getAlerts() {
  sqlite3_stmt *stmtSelectAlerts =
      getStmt("SELECT id, name, active, bound, direction, amount FROM alert;");
  sqlite3_stmt *stmtSelectInterval =
      getStmt("SELECT yr, mn, dy, wk, hr FROM interval WHERE id=?;");
  sqlite3_stmt *stmtSelectIntervalIdsForAlert =
      getStmt("SELECT interval_id FROM alert_interval WHERE alert_id=?");
  struct Alert *result = NULL;
  struct Alert *thisAlert = NULL;
  int rc;
  beginTrans(0);
  while ((rc = sqlite3_step(stmtSelectAlerts)) == SQLITE_ROW) {
    thisAlert = alertForRow(stmtSelectAlerts, stmtSelectInterval,
                            stmtSelectIntervalIdsForAlert);
    appendAlert(&result, thisAlert);
  }
  commitTrans();
  finishedStmt(stmtSelectIntervalIdsForAlert);
  finishedStmt(stmtSelectInterval);
  finishedStmt(stmtSelectAlerts);
  if (rc != SQLITE_DONE) {
    logMsg(4, "stmtSelectAlerts failed: %d.", rc);
  }
  return result;
}


x86 assembly (clang, -O0):
<getAlerts>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
lea    0xe71(%rip),%rdi
call   1090 <getStmt@plt>
mov    %rax,-0x8(%rbp)
lea    0xe9f(%rip),%rdi
call   1090 <getStmt@plt>
mov    %rax,-0x10(%rbp)
lea    0xec3(%rip),%rdi
call   1090 <getStmt@plt>
mov    %rax,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
xor    %edi,%edi
call   1040 <beginTrans@plt>
mov    -0x8(%rbp),%rdi
call   1060 <sqlite3_step@plt>
mov    %eax,-0x2c(%rbp)
mov    0x2df6(%rip),%rcx
cmp    (%rcx),%eax
jne    1211 <getAlerts+0x91>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1080 <alertForRow@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rsi
lea    -0x20(%rbp),%rdi
call   1070 <appendAlert@plt>
jmp    11cf <getAlerts+0x4f>
mov    $0x0,%al
call   1030 <commitTrans@plt>
mov    -0x18(%rbp),%rdi
call   10a0 <finishedStmt@plt>
mov    -0x10(%rbp),%rdi
call   10a0 <finishedStmt@plt>
mov    -0x8(%rbp),%rdi
call   10a0 <finishedStmt@plt>
mov    -0x2c(%rbp),%eax
mov    0x2da3(%rip),%rcx
cmp    (%rcx),%eax
je     1259 <getAlerts+0xd9>
mov    -0x2c(%rbp),%edx
mov    $0x4,%edi
lea    0xe56(%rip),%rsi
call   1050 <logMsg@plt>
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getAlerts>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
lea    0xe6e(%rip),%rdi
call   1090 <getStmt@plt>
mov    %rax,%rbx
lea    0xe9d(%rip),%rdi
call   1090 <getStmt@plt>
mov    %rax,%r14
lea    0xec2(%rip),%rdi
call   1090 <getStmt@plt>
mov    %rax,%r15
movq   $0x0,(%rsp)
xor    %edi,%edi
call   1040 <beginTrans@plt>
mov    %rbx,%rdi
call   1060 <sqlite3_step@plt>
mov    %eax,%ebp
mov    0x2e00(%rip),%r13
cmp    0x0(%r13),%eax
jne    1219 <getAlerts+0x99>
mov    %rsp,%r12
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
call   1080 <alertForRow@plt>
mov    %r12,%rdi
mov    %rax,%rsi
call   1070 <appendAlert@plt>
mov    %rbx,%rdi
call   1060 <sqlite3_step@plt>
cmp    0x0(%r13),%eax
je     11f0 <getAlerts+0x70>
mov    %eax,%ebp
xor    %eax,%eax
call   1030 <commitTrans@plt>
mov    %r15,%rdi
call   10a0 <finishedStmt@plt>
mov    %r14,%rdi
call   10a0 <finishedStmt@plt>
mov    %rbx,%rdi
call   10a0 <finishedStmt@plt>
mov    0x2da1(%rip),%rax
cmp    (%rax),%ebp
je     1256 <getAlerts+0xd6>
lea    0xe60(%rip),%rsi
mov    $0x4,%edi
mov    %ebp,%edx
call   1050 <logMsg@plt>
mov    (%rsp),%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.Alert = type { i32 }
@.str = [62 x i8] c"SELECT id, name, active, bound, direction, amount FROM alert;\00"
@.str.1 = [52 x i8] c"SELECT yr, mn, dy, wk, hr FROM interval WHERE id=?;\00"
@.str.2 = [56 x i8] c"SELECT interval_id FROM alert_interval WHERE alert_id=?\00"
@SQLITE_ROW = global i32 0
@SQLITE_DONE = global i32 0
@.str.3 = [29 x i8] c"stmtSelectAlerts failed: %d.\00"
define %struct.Alert* @getAlerts() {
%1 = alloca i32*
%2 = alloca i32*
%3 = alloca i32*
%4 = alloca %struct.Alert*
%5 = alloca %struct.Alert*
%6 = alloca i32
%7 = call i32* @getStmt(i8* getelementptr inbounds ([62 x i8], [62 x i8]* @.str, i64 0, i64 0))
store i32* %7, i32** %1
%8 = call i32* @getStmt(i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.1, i64 0, i64 0))
store i32* %8, i32** %2
%9 = call i32* @getStmt(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.2, i64 0, i64 0))
store i32* %9, i32** %3
store %struct.Alert* null, %struct.Alert** %4
store %struct.Alert* null, %struct.Alert** %5
%10 = call i32 @beginTrans(i32 0)
br label %11
11: ; preds = %16, %0
%12 = load i32*, i32** %1
%13 = call i32 @sqlite3_step(i32* %12)
store i32 %13, i32* %6
%14 = load i32, i32* @SQLITE_ROW
%15 = icmp eq i32 %13, %14
br i1 %15, label %16, label %23
16: ; preds = %11
%17 = load i32*, i32** %1
%18 = load i32*, i32** %2
%19 = load i32*, i32** %3
%20 = call %struct.Alert* @alertForRow(i32* %17, i32* %18, i32* %19)
store %struct.Alert* %20, %struct.Alert** %5
%21 = load %struct.Alert*, %struct.Alert** %5
%22 = call i32 @appendAlert(%struct.Alert** %4, %struct.Alert* %21)
br label %11
23: ; preds = %11
%24 = call i32 (...) @commitTrans()
%25 = load i32*, i32** %3
%26 = call i32 @finishedStmt(i32* %25)
%27 = load i32*, i32** %2
%28 = call i32 @finishedStmt(i32* %27)
%29 = load i32*, i32** %1
%30 = call i32 @finishedStmt(i32* %29)
%31 = load i32, i32* %6
%32 = load i32, i32* @SQLITE_DONE
%33 = icmp ne i32 %31, %32
br i1 %33, label %34, label %37
34: ; preds = %23
%35 = load i32, i32* %6
%36 = call i32 @logMsg(i32 4, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i32 %35)
br label %37
37: ; preds = %34, %23
%38 = load %struct.Alert*, %struct.Alert** %4
ret %struct.Alert* %38
}

LLVM IR (-O2):
%struct.Alert = type { i32 }
@.str = [62 x i8] c"SELECT id, name, active, bound, direction, amount FROM alert;\00"
@.str.1 = [52 x i8] c"SELECT yr, mn, dy, wk, hr FROM interval WHERE id=?;\00"
@.str.2 = [56 x i8] c"SELECT interval_id FROM alert_interval WHERE alert_id=?\00"
@SQLITE_ROW = global i32 0
@SQLITE_DONE = global i32 0
@.str.3 = [29 x i8] c"stmtSelectAlerts failed: %d.\00"
define %struct.Alert* @getAlerts() {
%1 = alloca %struct.Alert*
%2 = tail call i32* @getStmt(i8* getelementptr inbounds ([62 x i8], [62 x i8]* @.str, i64 0, i64 0))
%3 = tail call i32* @getStmt(i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.1, i64 0, i64 0))
%4 = tail call i32* @getStmt(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.2, i64 0, i64 0))
%5 = bitcast %struct.Alert** %1 to i8*
store %struct.Alert* null, %struct.Alert** %1
%6 = tail call i32 @beginTrans(i32 0)
%7 = call i32 @sqlite3_step(i32* %2)
%8 = load i32, i32* @SQLITE_ROW
%9 = icmp eq i32 %7, %8
br i1 %9, label %10, label %16
10: ; preds = %0, %10
%11 = call %struct.Alert* @alertForRow(i32* %2, i32* %3, i32* %4)
%12 = call i32 @appendAlert(%struct.Alert** %1, %struct.Alert* %11)
%13 = call i32 @sqlite3_step(i32* %2)
%14 = load i32, i32* @SQLITE_ROW
%15 = icmp eq i32 %13, %14
br i1 %15, label %10, label %16
16: ; preds = %10, %0
%17 = phi i32 [ %7, %0 ], [ %13, %10 ]
%18 = call i32 (...) @commitTrans()
%19 = call i32 @finishedStmt(i32* %4)
%20 = call i32 @finishedStmt(i32* %3)
%21 = call i32 @finishedStmt(i32* %2)
%22 = load i32, i32* @SQLITE_DONE
%23 = icmp eq i32 %17, %22
br i1 %23, label %26, label %24
24: ; preds = %16
%25 = call i32 @logMsg(i32 4, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i32 %17)
br label %26
26: ; preds = %24, %16
%27 = load %struct.Alert*, %struct.Alert** %1
ret %struct.Alert* %27
}