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

/* Variables and functions */
int SQLITE_OK;
scalar_t__ SQLITE_ROW;
int /*<<< orphan*/ SQLITE_STATIC;
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ db_fill_channel_from_row(int /*<<< orphan*/ *,
                                            struct db_channel *,
                                            int /*<<< orphan*/);
int sqlite3_bind_text(int /*<<< orphan*/ *, int, char const *, int,
                      int /*<<< orphan*/);
int /*<<< orphan*/ sqlite3_finalize(int /*<<< orphan*/ *);
int sqlite3_prepare_v2(int /*<<< orphan*/ *, char const *, int /*<<< orphan*/,
                       int /*<<< orphan*/ **, int /*<<< orphan*/ *);
int /*<<< orphan*/ sqlite3_reset(int /*<<< orphan*/ *);
scalar_t__ sqlite3_step(int /*<<< orphan*/ *);
int /*<<< orphan*/ strlen(char const *);
int db_get_channel_by_name(sqlite3 *db, const char *name,
                           struct db_channel *out) {
  static sqlite3_stmt *stmt;
  static const char query[] = "SELECT * FROM channel WHERE name = ?;";
  int ret;
  do {
    if (db == NULL) {
      if (stmt != NULL) {
        sqlite3_finalize(stmt);
        stmt = NULL;
      }
      return 0;
    }
  } while (0);
  do {
    if (stmt == NULL) {
      ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
      assert(ret == SQLITE_OK);
    }
  } while (0);
  sqlite3_reset(stmt);
  ret = sqlite3_bind_text(stmt, 1, name, -1, SQLITE_STATIC);
  assert(ret == SQLITE_OK);
  if (sqlite3_step(stmt) != SQLITE_ROW)
    return 1;
  db_fill_channel_from_row(stmt, out, 0);
  return 0;
}


x86 assembly (clang, -O0):
<db_get_channel_by_name>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11d0 <db_get_channel_by_name+0x50>
cmpq   $0x0,0x2ec1(%rip)
je     11c4 <db_get_channel_by_name+0x44>
mov    0x2eb4(%rip),%rdi
call   1090 <sqlite3_finalize@plt>
movq   $0x0,0x2ea4(%rip)
movl   $0x0,-0x4(%rbp)
jmp    12c8 <db_get_channel_by_name+0x148>
jmp    11d5 <db_get_channel_by_name+0x55>
jmp    11da <db_get_channel_by_name+0x5a>
cmpq   $0x0,0x2e86(%rip)
jne    1236 <db_get_channel_by_name+0xb6>
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
lea    0xe09(%rip),%rdi
call   1040 <strlen@plt>
mov    -0x30(%rbp),%rdi
mov    %eax,%edx
lea    0xdf7(%rip),%rsi
lea    0x2e58(%rip),%rcx
xor    %eax,%eax
mov    %eax,%r8d
call   10a0 <sqlite3_prepare_v2@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    0x2dc1(%rip),%rcx
cmp    (%rcx),%eax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
jmp    123b <db_get_channel_by_name+0xbb>
mov    0x2e26(%rip),%rdi
call   1030 <sqlite3_reset@plt>
mov    0x2e1a(%rip),%rdi
mov    -0x18(%rbp),%rdx
mov    0x2d7f(%rip),%rax
mov    (%rax),%r8d
mov    $0x1,%esi
mov    $0xffffffff,%ecx
call   1080 <sqlite3_bind_text@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    0x2d70(%rip),%rcx
cmp    (%rcx),%eax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    0x2dda(%rip),%rdi
call   1060 <sqlite3_step@plt>
mov    0x2d36(%rip),%rcx
cmp    (%rcx),%rax
je     12af <db_get_channel_by_name+0x12f>
movl   $0x1,-0x4(%rbp)
jmp    12c8 <db_get_channel_by_name+0x148>
mov    0x2db2(%rip),%rdi
mov    -0x20(%rbp),%rsi
xor    %edx,%edx
call   1070 <db_fill_channel_from_row@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<db_get_channel_by_name>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbp
mov    0x2ed7(%rip),%rdi
test   %rbp,%rbp
je     1254 <db_get_channel_by_name+0xd4>
mov    %rdx,%r14
mov    %rsi,%rbx
test   %rdi,%rdi
jne    11e6 <db_get_channel_by_name+0x66>
lea    0xe54(%rip),%r15
mov    %r15,%rdi
call   1040 <strlen@plt>
lea    0x2ead(%rip),%rcx
mov    %rbp,%rdi
mov    %r15,%rsi
mov    %eax,%edx
xor    %r8d,%r8d
call   10a0 <sqlite3_prepare_v2@plt>
mov    0x2e16(%rip),%rcx
xor    %edi,%edi
cmp    (%rcx),%eax
sete   %dil
call   1050 <assert@plt>
mov    0x2e82(%rip),%rdi
call   1030 <sqlite3_reset@plt>
mov    0x2e76(%rip),%rdi
mov    0x2ddf(%rip),%rax
mov    (%rax),%r8d
mov    $0x1,%ebp
mov    $0x1,%esi
mov    %rbx,%rdx
mov    $0xffffffff,%ecx
call   1080 <sqlite3_bind_text@plt>
mov    0x2dce(%rip),%rcx
xor    %edi,%edi
cmp    (%rcx),%eax
sete   %dil
call   1050 <assert@plt>
mov    0x2e3a(%rip),%rdi
call   1060 <sqlite3_step@plt>
mov    0x2d96(%rip),%rcx
cmp    (%rcx),%rax
jne    126b <db_get_channel_by_name+0xeb>
mov    0x2e22(%rip),%rdi
xor    %ebp,%ebp
mov    %r14,%rsi
xor    %edx,%edx
call   1070 <db_fill_channel_from_row@plt>
jmp    126b <db_get_channel_by_name+0xeb>
xor    %ebp,%ebp
test   %rdi,%rdi
je     126b <db_get_channel_by_name+0xeb>
call   1090 <sqlite3_finalize@plt>
movq   $0x0,0x2dfd(%rip)
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.db_channel = type { i32 }
@db_get_channel_by_name.stmt = internal global i32* null
@db_get_channel_by_name.query = internal constant [38 x i8] c"SELECT * FROM channel WHERE name = ?;\00"
@SQLITE_OK = global i32 0
@SQLITE_STATIC = global i32 0
@SQLITE_ROW = global i64 0
define i32 @db_get_channel_by_name(i32* %0, i8* %1, %struct.db_channel* %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i8*
%7 = alloca %struct.db_channel*
%8 = alloca i32
store i32* %0, i32** %5
store i8* %1, i8** %6
store %struct.db_channel* %2, %struct.db_channel** %7
br label %9
9: ; preds = %3
%10 = load i32*, i32** %5
%11 = icmp eq i32* %10, null
br i1 %11, label %12, label %19
12: ; preds = %9
%13 = load i32*, i32** @db_get_channel_by_name.stmt
%14 = icmp ne i32* %13, null
br i1 %14, label %15, label %18
15: ; preds = %12
%16 = load i32*, i32** @db_get_channel_by_name.stmt
%17 = call i32 @sqlite3_finalize(i32* %16)
store i32* null, i32** @db_get_channel_by_name.stmt
br label %18
18: ; preds = %15, %12
store i32 0, i32* %4
br label %55
19: ; preds = %9
br label %20
20: ; preds = %19
br label %21
21: ; preds = %20
%22 = load i32*, i32** @db_get_channel_by_name.stmt
%23 = icmp eq i32* %22, null
br i1 %23, label %24, label %33
24: ; preds = %21
%25 = load i32*, i32** %5
%26 = call i32 @strlen(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @db_get_channel_by_name.query, i64 0, i64 0))
%27 = call i32 @sqlite3_prepare_v2(i32* %25, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @db_get_channel_by_name.query, i64 0, i64 0), i32 %26, i32** @db_get_channel_by_name.stmt, i32* null)
store i32 %27, i32* %8
%28 = load i32, i32* %8
%29 = load i32, i32* @SQLITE_OK
%30 = icmp eq i32 %28, %29
%31 = zext i1 %30 to i32
%32 = call i32 @assert(i32 %31)
br label %33
33: ; preds = %24, %21
br label %34
34: ; preds = %33
%35 = load i32*, i32** @db_get_channel_by_name.stmt
%36 = call i32 @sqlite3_reset(i32* %35)
%37 = load i32*, i32** @db_get_channel_by_name.stmt
%38 = load i8*, i8** %6
%39 = load i32, i32* @SQLITE_STATIC
%40 = call i32 @sqlite3_bind_text(i32* %37, i32 1, i8* %38, i32 -1, i32 %39)
store i32 %40, i32* %8
%41 = load i32, i32* %8
%42 = load i32, i32* @SQLITE_OK
%43 = icmp eq i32 %41, %42
%44 = zext i1 %43 to i32
%45 = call i32 @assert(i32 %44)
%46 = load i32*, i32** @db_get_channel_by_name.stmt
%47 = call i64 @sqlite3_step(i32* %46)
%48 = load i64, i64* @SQLITE_ROW
%49 = icmp ne i64 %47, %48
br i1 %49, label %50, label %51
50: ; preds = %34
store i32 1, i32* %4
br label %55
51: ; preds = %34
%52 = load i32*, i32** @db_get_channel_by_name.stmt
%53 = load %struct.db_channel*, %struct.db_channel** %7
%54 = call i32 @db_fill_channel_from_row(i32* %52, %struct.db_channel* %53, i32 0)
store i32 0, i32* %4
br label %55
55: ; preds = %51, %50, %18
%56 = load i32, i32* %4
ret i32 %56
}

LLVM IR (-O2):
%struct.db_channel = type { i32 }
@db_get_channel_by_name.stmt = internal global i32* null
@db_get_channel_by_name.query = internal constant [38 x i8] c"SELECT * FROM channel WHERE name = ?;\00"
@SQLITE_OK = global i32 0
@SQLITE_STATIC = global i32 0
@SQLITE_ROW = global i64 0
define i32 @db_get_channel_by_name(i32* %0, i8* %1, %struct.db_channel* %2) {
%4 = icmp eq i32* %0, null
%5 = load i32*, i32** @db_get_channel_by_name.stmt
%6 = icmp eq i32* %5, null
br i1 %4, label %7, label %10
7: ; preds = %3
br i1 %6, label %36, label %8
8: ; preds = %7
%9 = tail call i32 @sqlite3_finalize(i32* %5)
store i32* null, i32** @db_get_channel_by_name.stmt
br label %36
10: ; preds = %3
br i1 %6, label %11, label %19
11: ; preds = %10
%12 = tail call i32 @strlen(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @db_get_channel_by_name.query, i64 0, i64 0))
%13 = tail call i32 @sqlite3_prepare_v2(i32* %0, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @db_get_channel_by_name.query, i64 0, i64 0), i32 %12, i32** @db_get_channel_by_name.stmt, i32* null)
%14 = load i32, i32* @SQLITE_OK
%15 = icmp eq i32 %13, %14
%16 = zext i1 %15 to i32
%17 = tail call i32 @assert(i32 %16)
%18 = load i32*, i32** @db_get_channel_by_name.stmt
br label %19
19: ; preds = %11, %10
%20 = phi i32* [ %18, %11 ], [ %5, %10 ]
%21 = tail call i32 @sqlite3_reset(i32* %20)
%22 = load i32*, i32** @db_get_channel_by_name.stmt
%23 = load i32, i32* @SQLITE_STATIC
%24 = tail call i32 @sqlite3_bind_text(i32* %22, i32 1, i8* %1, i32 -1, i32 %23)
%25 = load i32, i32* @SQLITE_OK
%26 = icmp eq i32 %24, %25
%27 = zext i1 %26 to i32
%28 = tail call i32 @assert(i32 %27)
%29 = load i32*, i32** @db_get_channel_by_name.stmt
%30 = tail call i64 @sqlite3_step(i32* %29)
%31 = load i64, i64* @SQLITE_ROW
%32 = icmp eq i64 %30, %31
br i1 %32, label %33, label %36
33: ; preds = %19
%34 = load i32*, i32** @db_get_channel_by_name.stmt
%35 = tail call i32 @db_fill_channel_from_row(i32* %34, %struct.db_channel* %2, i32 0)
br label %36
36: ; preds = %19, %7, %8, %33
%37 = phi i32 [ 0, %33 ], [ 0, %8 ], [ 0, %7 ], [ 1, %19 ]
ret i32 %37
}