#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 */
int SQLITE_OK;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ sprintf(char *, char *, char const *);
int /*<<< orphan*/ sqlite3_free_table(char **);
int sqlite3_get_table(int /*<<< orphan*/ *, char *, char ***, int *, int *,
                      char **);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcpy(char *, char *);
int sqlite_find_user(sqlite3 *db, const char *name, char *passwd) {
  int ret = 0;
  char sql[512];
  char **resultp;
  char *errmsg;
  int row, column;
  sprintf(sql, "select password from users where name='%s';", name);
  ret = sqlite3_get_table(db, sql, &resultp, &row, &column, &errmsg);
  if (ret != SQLITE_OK) {
    fprintf(stderr, "Fail to sqlite3_get_table : %s\n", errmsg);
    ret = -1;
    goto exit;
  }
  if (row >= 1) {
    strcpy(passwd, resultp[1]);
  }
  ret = row;
  sqlite3_free_table(resultp);
exit:
  return ret;
}


x86 assembly (clang, -O0):
<sqlite_find_user>:
push   %rbp
mov    %rsp,%rbp
sub    $0x240,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
lea    -0x220(%rbp),%rdi
mov    -0x10(%rbp),%rdx
lea    0xe80(%rip),%rsi
call   1070 <sprintf@plt>
mov    -0x8(%rbp),%rdi
lea    -0x220(%rbp),%rsi
lea    -0x228(%rbp),%rdx
lea    -0x234(%rbp),%rcx
lea    -0x238(%rbp),%r8
lea    -0x230(%rbp),%r9
call   1040 <sqlite3_get_table@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    0x2e2a(%rip),%rcx
cmp    (%rcx),%eax
je     11ee <sqlite_find_user+0x9e>
mov    0x2e0b(%rip),%rax
mov    (%rax),%edi
mov    -0x230(%rbp),%rdx
lea    0xe4f(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x1c(%rbp)
jmp    1224 <sqlite_find_user+0xd4>
cmpl   $0x1,-0x234(%rbp)
jl     120f <sqlite_find_user+0xbf>
mov    -0x18(%rbp),%rdi
mov    -0x228(%rbp),%rax
mov    0x8(%rax),%rsi
call   1030 <strcpy@plt>
mov    -0x234(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x228(%rbp),%rdi
call   1060 <sqlite3_free_table@plt>
mov    -0x1c(%rbp),%eax
add    $0x240,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sqlite_find_user>:
push   %rbp
push   %r14
push   %rbx
sub    $0x220,%rsp
mov    %rdx,%r14
mov    %rsi,%rdx
mov    %rdi,%rbp
lea    0xe95(%rip),%rsi
lea    0x20(%rsp),%rbx
mov    %rbx,%rdi
call   1070 <sprintf@plt>
lea    0x8(%rsp),%rdx
lea    0x4(%rsp),%rcx
lea    0x1c(%rsp),%r8
lea    0x10(%rsp),%r9
mov    %rbp,%rdi
mov    %rbx,%rsi
call   1040 <sqlite3_get_table@plt>
mov    0x2e4a(%rip),%rcx
cmp    (%rcx),%eax
jne    11cb <sqlite_find_user+0x7b>
mov    0x4(%rsp),%ebp
test   %ebp,%ebp
jle    11bf <sqlite_find_user+0x6f>
mov    0x8(%rsp),%rax
mov    0x8(%rax),%rsi
mov    %r14,%rdi
call   1030 <strcpy@plt>
mov    0x4(%rsp),%ebp
mov    0x8(%rsp),%rdi
call   1060 <sqlite3_free_table@plt>
jmp    11ea <sqlite_find_user+0x9a>
mov    0x2e06(%rip),%rax
mov    (%rax),%edi
mov    0x10(%rsp),%rdx
lea    0xe4c(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x220,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [44 x i8] c"select password from users where name='%s';\00"
@SQLITE_OK = global i32 0
@stderr = global i32 0
@.str.1 = [32 x i8] c"Fail to sqlite3_get_table : %s\0A\00"
define i32 @sqlite_find_user(i32* %0, i8* %1, i8* %2) {
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca [512 x i8]
%9 = alloca i8**
%10 = alloca i8*
%11 = alloca i32
%12 = alloca i32
store i32* %0, i32** %4
store i8* %1, i8** %5
store i8* %2, i8** %6
store i32 0, i32* %7
%13 = getelementptr inbounds [512 x i8], [512 x i8]* %8, i64 0, i64 0
%14 = load i8*, i8** %5
%15 = call i32 @sprintf(i8* %13, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0), i8* %14)
%16 = load i32*, i32** %4
%17 = getelementptr inbounds [512 x i8], [512 x i8]* %8, i64 0, i64 0
%18 = call i32 @sqlite3_get_table(i32* %16, i8* %17, i8*** %9, i32* %11, i32* %12, i8** %10)
store i32 %18, i32* %7
%19 = load i32, i32* %7
%20 = load i32, i32* @SQLITE_OK
%21 = icmp ne i32 %19, %20
br i1 %21, label %22, label %26
22: ; preds = %3
%23 = load i32, i32* @stderr
%24 = load i8*, i8** %10
%25 = call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i8* %24)
store i32 -1, i32* %7
br label %39
26: ; preds = %3
%27 = load i32, i32* %11
%28 = icmp sge i32 %27, 1
br i1 %28, label %29, label %35
29: ; preds = %26
%30 = load i8*, i8** %6
%31 = load i8**, i8*** %9
%32 = getelementptr inbounds i8*, i8** %31, i64 1
%33 = load i8*, i8** %32
%34 = call i32 @strcpy(i8* %30, i8* %33)
br label %35
35: ; preds = %29, %26
%36 = load i32, i32* %11
store i32 %36, i32* %7
%37 = load i8**, i8*** %9
%38 = call i32 @sqlite3_free_table(i8** %37)
br label %39
39: ; preds = %35, %22
%40 = load i32, i32* %7
ret i32 %40
}

LLVM IR (-O2):
@.str = [44 x i8] c"select password from users where name='%s';\00"
@SQLITE_OK = global i32 0
@stderr = global i32 0
@.str.1 = [32 x i8] c"Fail to sqlite3_get_table : %s\0A\00"
define i32 @sqlite_find_user(i32* %0, i8* %1, i8* %2) {
%4 = alloca [512 x i8]
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%10 = bitcast i8*** %5 to i8*
%11 = bitcast i8** %6 to i8*
%12 = bitcast i32* %7 to i8*
%13 = bitcast i32* %8 to i8*
%14 = call i32 @sprintf(i8* %9, i8* dereferenceable(1) getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0), i8* %1)
%15 = call i32 @sqlite3_get_table(i32* %0, i8* %9, i8*** %5, i32* %7, i32* %8, i8** %6)
%16 = load i32, i32* @SQLITE_OK
%17 = icmp eq i32 %15, %16
br i1 %17, label %22, label %18
18: ; preds = %3
%19 = load i32, i32* @stderr
%20 = load i8*, i8** %6
%21 = call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i8* %20)
br label %35
22: ; preds = %3
%23 = load i32, i32* %7
%24 = icmp sgt i32 %23, 0
br i1 %24, label %25, label %31
25: ; preds = %22
%26 = load i8**, i8*** %5
%27 = getelementptr inbounds i8*, i8** %26, i64 1
%28 = load i8*, i8** %27
%29 = call i32 @strcpy(i8* %2, i8* %28)
%30 = load i32, i32* %7
br label %31
31: ; preds = %25, %22
%32 = phi i32 [ %30, %25 ], [ %23, %22 ]
%33 = load i8**, i8*** %5
%34 = call i32 @sqlite3_free_table(i8** %33)
br label %35
35: ; preds = %31, %18
%36 = phi i32 [ -1, %18 ], [ %32, %31 ]
ret i32 %36
}