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

/* Variables and functions */
long db_retrieval(int /*<<< orphan*/ *, char *);
int /*<<< orphan*/ free(char *);
char *sql_generate(char const *, char const *, ...);
long db_total_posts(sqlite3 *db, const char *board_id, const long id) {
  static const char *const sql[] = {
      "SELECT MAX(id) FROM posts WHERE board_id = \"%s\";",
      "SELECT COUNT(*) FROM posts "
      "WHERE board_id = \"%s\" AND parent_id = %ld;"};
  unsigned opt = (id < 0) ? 0 : 1;
  const char *stmt = sql[opt];
  char *cmd =
      (!opt) ? sql_generate(stmt, board_id) : sql_generate(stmt, board_id, id);
  long post_count = db_retrieval(db, cmd);
  free(cmd);
  return post_count;
}


x86 assembly (clang, -O0):
<db_total_posts>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x18(%rbp),%rdx
mov    $0x1,%eax
xor    %ecx,%ecx
cmp    $0x0,%rdx
cmovl  %ecx,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,%ecx
lea    0x2cab(%rip),%rax
mov    (%rax,%rcx,8),%rax
mov    %rax,-0x28(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    118f <db_total_posts+0x5f>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    $0x0,%al
call   1040 <sql_generate@plt>
mov    %rax,-0x40(%rbp)
jmp    11a6 <db_total_posts+0x76>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    $0x0,%al
call   1040 <sql_generate@plt>
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   1050 <db_retrieval@plt>
mov    %rax,-0x38(%rbp)
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x38(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<db_total_posts>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
mov    %rdx,%rax
shr    $0x3f,%rax
mov    %eax,%ecx
xor    $0x1,%ecx
lea    0x2cc6(%rip),%rdi
mov    (%rdi,%rcx,8),%rdi
test   %rax,%rax
jne    115c <db_total_posts+0x2c>
xor    %eax,%eax
call   1040 <sql_generate@plt>
jmp    1163 <db_total_posts+0x33>
xor    %eax,%eax
call   1040 <sql_generate@plt>
mov    %rax,%rbx
mov    %r14,%rdi
mov    %rax,%rsi
call   1050 <db_retrieval@plt>
mov    %rax,%r14
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@db_total_posts.sql = internal constant [2 x i8*] [i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str, i32 0, i32 0), i8* getelementptr inbounds ([70 x i8], [70 x i8]* @.str.1, i32 0, i32 0)]
@.str = [49 x i8] c"SELECT MAX(id) FROM posts WHERE board_id = \22%s\22;\00"
@.str.1 = [70 x i8] c"SELECT COUNT(*) FROM posts WHERE board_id = \22%s\22 AND parent_id = %ld;\00"
define i64 @db_total_posts(i32* %0, i8* %1, i64 %2) {
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i64
store i32* %0, i32** %4
store i8* %1, i8** %5
store i64 %2, i64* %6
%11 = load i64, i64* %6
%12 = icmp slt i64 %11, 0
%13 = zext i1 %12 to i64
%14 = select i1 %12, i32 0, i32 1
store i32 %14, i32* %7
%15 = load i32, i32* %7
%16 = zext i32 %15 to i64
%17 = getelementptr inbounds [2 x i8*], [2 x i8*]* @db_total_posts.sql, i64 0, i64 %16
%18 = load i8*, i8** %17
store i8* %18, i8** %8
%19 = load i32, i32* %7
%20 = icmp ne i32 %19, 0
br i1 %20, label %25, label %21
21: ; preds = %3
%22 = load i8*, i8** %8
%23 = load i8*, i8** %5
%24 = call i8* (i8*, i8*, ...) @sql_generate(i8* %22, i8* %23)
br label %30
25: ; preds = %3
%26 = load i8*, i8** %8
%27 = load i8*, i8** %5
%28 = load i64, i64* %6
%29 = call i8* (i8*, i8*, ...) @sql_generate(i8* %26, i8* %27, i64 %28)
br label %30
30: ; preds = %25, %21
%31 = phi i8* [ %24, %21 ], [ %29, %25 ]
store i8* %31, i8** %9
%32 = load i32*, i32** %4
%33 = load i8*, i8** %9
%34 = call i64 @db_retrieval(i32* %32, i8* %33)
store i64 %34, i64* %10
%35 = load i8*, i8** %9
%36 = call i32 @free(i8* %35)
%37 = load i64, i64* %10
ret i64 %37
}

LLVM IR (-O2):
@db_total_posts.sql = internal unnamed_addr constant [2 x i8*] [i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str, i32 0, i32 0), i8* getelementptr inbounds ([70 x i8], [70 x i8]* @.str.1, i32 0, i32 0)]
@.str = [49 x i8] c"SELECT MAX(id) FROM posts WHERE board_id = \22%s\22;\00"
@.str.1 = [70 x i8] c"SELECT COUNT(*) FROM posts WHERE board_id = \22%s\22 AND parent_id = %ld;\00"
define i64 @db_total_posts(i32* %0, i8* %1, i64 %2) {
%4 = lshr i64 %2, 63
%5 = trunc i64 %4 to i32
%6 = xor i32 %5, 1
%7 = zext i32 %6 to i64
%8 = getelementptr inbounds [2 x i8*], [2 x i8*]* @db_total_posts.sql, i64 0, i64 %7
%9 = load i8*, i8** %8
%10 = icmp eq i32 %6, 0
br i1 %10, label %11, label %13
11: ; preds = %3
%12 = tail call i8* (i8*, i8*, ...) @sql_generate(i8* %9, i8* %1)
br label %15
13: ; preds = %3
%14 = tail call i8* (i8*, i8*, ...) @sql_generate(i8* %9, i8* %1, i64 %2)
br label %15
15: ; preds = %13, %11
%16 = phi i8* [ %12, %11 ], [ %14, %13 ]
%17 = tail call i64 @db_retrieval(i32* %0, i8* %16)
%18 = tail call i32 @free(i8* %16)
ret i64 %17
}