#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;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int SQLITE_ERROR;
int SQLITE_OK;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ fscanf(int /*<<< orphan*/, char *, char *);
int sqlite3_open(char const *, int /*<<< orphan*/ **);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ stdout;
char *strerror(int /*<<< orphan*/);
sqlite3 *_db_connect(const char *dbName) {
  sqlite3 *db;
  int rc = 0;
  char rsp = ' ';
  FILE *dbfile = NULL;
  rc = sqlite3_open(dbName, &db);
  if (rc == SQLITE_OK) {
    fprintf(stdout, "Successful result: database opened \n");
    return db;
  } else if (rc == SQLITE_ERROR) {
    fprintf(stderr, "SQL error or missing database \n");
    fprintf(stderr, "Do you want to create a new database? [Y/N]: ");
    fscanf(stdin, "%c", &rsp);
    if (rsp == 'Y' || rsp == 'y') {
      dbfile = fopen(dbName, "w");
      fprintf(stdout, "Database file was created with name: %s\n", dbName);
      fclose(dbfile);
      rc = sqlite3_open(dbName, &db);
      return db;
    } else {
      fprintf(stderr, "File not created!\n");
      return NULL;
    }
  } else {
    fprintf(stderr, "%s\n", strerror(errno));
    return NULL;
  }
}


x86 assembly (clang, -O0):
<_db_connect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x1c(%rbp)
movb   $0x20,-0x1d(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1030 <sqlite3_open@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    0x2e4f(%rip),%rcx
cmp    (%rcx),%eax
jne    11c5 <_db_connect+0x65>
mov    0x2e10(%rip),%rax
mov    (%rax),%edi
lea    0xe4f(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12e7 <_db_connect+0x187>
mov    -0x1c(%rbp),%eax
mov    0x2e01(%rip),%rcx
cmp    (%rcx),%eax
jne    12b1 <_db_connect+0x151>
mov    0x2dea(%rip),%rax
mov    (%rax),%edi
lea    0xe3e(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    0x2dd3(%rip),%rax
mov    (%rax),%edi
lea    0xe47(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    0x2dd4(%rip),%rax
mov    (%rax),%edi
lea    0xe5e(%rip),%rsi
lea    -0x1d(%rbp),%rdx
call   1050 <fscanf@plt>
movsbl -0x1d(%rbp),%eax
cmp    $0x59,%eax
je     1238 <_db_connect+0xd8>
movsbl -0x1d(%rbp),%eax
cmp    $0x79,%eax
jne    128d <_db_connect+0x12d>
mov    -0x10(%rbp),%rdi
lea    0xe33(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x28(%rbp)
mov    0x2d65(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe18(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1030 <sqlite3_open@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12e7 <_db_connect+0x187>
mov    0x2d34(%rip),%rax
mov    (%rax),%edi
lea    0xe04(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    12e7 <_db_connect+0x187>
mov    0x2d10(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2cfc(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x2c(%rbp),%edi
mov    %rax,%rdx
lea    0xdc5(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_db_connect>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
movb   $0x20,0x7(%rsp)
lea    0x8(%rsp),%rsi
call   1030 <sqlite3_open@plt>
mov    0x2e67(%rip),%rcx
cmp    (%rcx),%eax
jne    11a6 <_db_connect+0x46>
mov    0x2e2c(%rip),%rax
mov    (%rax),%edi
lea    0xe6b(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    0x8(%rsp),%rbx
jmp    1284 <_db_connect+0x124>
mov    0x2e23(%rip),%rcx
mov    0x2e14(%rip),%r14
mov    (%r14),%ebp
cmp    (%rcx),%eax
jne    124c <_db_connect+0xec>
lea    0xe5f(%rip),%rsi
mov    %ebp,%edi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    (%r14),%edi
lea    0xe6c(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    0x2df9(%rip),%rax
mov    (%rax),%edi
lea    0xe83(%rip),%rsi
lea    0x7(%rsp),%rdx
call   1050 <fscanf@plt>
mov    0x7(%rsp),%al
and    $0xdf,%al
cmp    $0x59,%al
jne    1271 <_db_connect+0x111>
lea    0xe6b(%rip),%rsi
mov    %rbx,%rdi
call   1070 <fopen@plt>
mov    %rax,%rbp
mov    0x2d9b(%rip),%rax
mov    (%rax),%edi
lea    0xe52(%rip),%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    %rbp,%rdi
call   1040 <fclose@plt>
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
call   1030 <sqlite3_open@plt>
mov    0x8(%rsp),%rbx
jmp    1284 <_db_connect+0x124>
mov    0x2d6d(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xe3c(%rip),%rsi
xor    %ebx,%ebx
mov    %ebp,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
jmp    1284 <_db_connect+0x124>
mov    (%r14),%edi
lea    0xe26(%rip),%rsi
xor    %ebx,%ebx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    %rbx,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@SQLITE_OK = global i32 0
@stdout = global i32 0
@.str = [37 x i8] c"Successful result: database opened \0A\00"
@SQLITE_ERROR = global i32 0
@stderr = global i32 0
@.str.1 = [32 x i8] c"SQL error or missing database \0A\00"
@.str.2 = [46 x i8] c"Do you want to create a new database? [Y/N]: \00"
@stdin = global i32 0
@.str.3 = [3 x i8] c"%c\00"
@.str.4 = [2 x i8] c"w\00"
@.str.5 = [41 x i8] c"Database file was created with name: %s\0A\00"
@.str.6 = [19 x i8] c"File not created!\0A\00"
@.str.7 = [4 x i8] c"%s\0A\00"
@errno = global i32 0
define i32* @_db_connect(i8* %0) {
%2 = alloca i32*
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i8
%7 = alloca i32*
store i8* %0, i8** %3
store i32 0, i32* %5
store i8 32, i8* %6
store i32* null, i32** %7
%8 = load i8*, i8** %3
%9 = call i32 @sqlite3_open(i8* %8, i32** %4)
store i32 %9, i32* %5
%10 = load i32, i32* %5
%11 = load i32, i32* @SQLITE_OK
%12 = icmp eq i32 %10, %11
br i1 %12, label %13, label %17
13: ; preds = %1
%14 = load i32, i32* @stdout
%15 = call i32 (i32, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0))
%16 = load i32*, i32** %4
store i32* %16, i32** %2
br label %54
17: ; preds = %1
%18 = load i32, i32* %5
%19 = load i32, i32* @SQLITE_ERROR
%20 = icmp eq i32 %18, %19
br i1 %20, label %21, label %49
21: ; preds = %17
%22 = load i32, i32* @stderr
%23 = call i32 (i32, i8*, ...) @fprintf(i32 %22, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0))
%24 = load i32, i32* @stderr
%25 = call i32 (i32, i8*, ...) @fprintf(i32 %24, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.2, i64 0, i64 0))
%26 = load i32, i32* @stdin
%27 = call i32 @fscanf(i32 %26, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %6)
%28 = load i8, i8* %6
%29 = sext i8 %28 to i32
%30 = icmp eq i32 %29, 89
br i1 %30, label %35, label %31
31: ; preds = %21
%32 = load i8, i8* %6
%33 = sext i8 %32 to i32
%34 = icmp eq i32 %33, 121
br i1 %34, label %35, label %46
35: ; preds = %31, %21
%36 = load i8*, i8** %3
%37 = call i32* @fopen(i8* %36, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
store i32* %37, i32** %7
%38 = load i32, i32* @stdout
%39 = load i8*, i8** %3
%40 = call i32 (i32, i8*, ...) @fprintf(i32 %38, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.5, i64 0, i64 0), i8* %39)
%41 = load i32*, i32** %7
%42 = call i32 @fclose(i32* %41)
%43 = load i8*, i8** %3
%44 = call i32 @sqlite3_open(i8* %43, i32** %4)
store i32 %44, i32* %5
%45 = load i32*, i32** %4
store i32* %45, i32** %2
br label %54
46: ; preds = %31
%47 = load i32, i32* @stderr
%48 = call i32 (i32, i8*, ...) @fprintf(i32 %47, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.6, i64 0, i64 0))
store i32* null, i32** %2
br label %54
49: ; preds = %17
%50 = load i32, i32* @stderr
%51 = load i32, i32* @errno
%52 = call i8* @strerror(i32 %51)
%53 = call i32 (i32, i8*, ...) @fprintf(i32 %50, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.7, i64 0, i64 0), i8* %52)
store i32* null, i32** %2
br label %54
54: ; preds = %49, %46, %35, %13
%55 = load i32*, i32** %2
ret i32* %55
}

LLVM IR (-O2):
@SQLITE_OK = global i32 0
@stdout = global i32 0
@.str = [37 x i8] c"Successful result: database opened \0A\00"
@SQLITE_ERROR = global i32 0
@stderr = global i32 0
@.str.1 = [32 x i8] c"SQL error or missing database \0A\00"
@.str.2 = [46 x i8] c"Do you want to create a new database? [Y/N]: \00"
@stdin = global i32 0
@.str.3 = [3 x i8] c"%c\00"
@.str.4 = [2 x i8] c"w\00"
@.str.5 = [41 x i8] c"Database file was created with name: %s\0A\00"
@.str.6 = [19 x i8] c"File not created!\0A\00"
@.str.7 = [4 x i8] c"%s\0A\00"
@errno = global i32 0
define i32* @_db_connect(i8* %0) {
%2 = alloca i32*
%3 = alloca i8
%4 = bitcast i32** %2 to i8*
store i8 32, i8* %3
%5 = call i32 @sqlite3_open(i8* %0, i32** %2)
%6 = load i32, i32* @SQLITE_OK
%7 = icmp eq i32 %5, %6
br i1 %7, label %8, label %12
8: ; preds = %1
%9 = load i32, i32* @stdout
%10 = call i32 (i32, i8*, ...) @fprintf(i32 %9, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0))
%11 = load i32*, i32** %2
br label %39
12: ; preds = %1
%13 = load i32, i32* @SQLITE_ERROR
%14 = icmp eq i32 %5, %13
%15 = load i32, i32* @stderr
br i1 %14, label %16, label %35
16: ; preds = %12
%17 = call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0))
%18 = load i32, i32* @stderr
%19 = call i32 (i32, i8*, ...) @fprintf(i32 %18, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.2, i64 0, i64 0))
%20 = load i32, i32* @stdin
%21 = call i32 @fscanf(i32 %20, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %3)
%22 = load i8, i8* %3
%23 = and i8 %22, -33
%24 = icmp eq i8 %23, 89
br i1 %24, label %25, label %32
25: ; preds = %16
%26 = call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%27 = load i32, i32* @stdout
%28 = call i32 (i32, i8*, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.5, i64 0, i64 0), i8* %0)
%29 = call i32 @fclose(i32* %26)
%30 = call i32 @sqlite3_open(i8* %0, i32** %2)
%31 = load i32*, i32** %2
br label %39
32: ; preds = %16
%33 = load i32, i32* @stderr
%34 = call i32 (i32, i8*, ...) @fprintf(i32 %33, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.6, i64 0, i64 0))
br label %39
35: ; preds = %12
%36 = load i32, i32* @errno
%37 = call i8* @strerror(i32 %36)
%38 = call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.7, i64 0, i64 0), i8* %37)
br label %39
39: ; preds = %35, %32, %25, %8
%40 = phi i32* [ %11, %8 ], [ %31, %25 ], [ null, %32 ], [ null, %35 ]
ret i32* %40
}