#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_handle {
  int dummy;
};

/* Variables and functions */
scalar_t__ asprintf(char **, char *, char const *, char const *);
int db_exec(struct db_handle *, char *, int /*<<< orphan*/ *,
            int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
int db_attach(struct db_handle *h, const char *file, const char *name) {
  char *sql;
  int ret;
  if (asprintf(&sql, "ATTACH DATABASE %s AS %s", file, name) < 0)
    return -1;
  ret = db_exec(h, sql, NULL, NULL);
  free(sql);
  return ret;
}


x86 assembly (clang, -O0):
<db_attach>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
lea    -0x28(%rbp),%rdi
lea    0xea9(%rip),%rsi
call   1040 <asprintf@plt>
cmp    $0x0,%rax
jge    1172 <db_attach+0x42>
movl   $0xffffffff,-0x4(%rbp)
jmp    1198 <db_attach+0x68>
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
mov    %rcx,%rdx
call   1050 <db_exec@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<db_attach>:
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%rcx
mov    %rsi,%rdx
mov    %rdi,%rbx
lea    0xebb(%rip),%rsi
lea    0x8(%rsp),%rdi
call   1040 <asprintf@plt>
test   %rax,%rax
js     1173 <db_attach+0x43>
mov    0x8(%rsp),%rsi
mov    %rbx,%rdi
xor    %edx,%edx
xor    %ecx,%ecx
call   1050 <db_exec@plt>
mov    %eax,%ebx
mov    0x8(%rsp),%rdi
call   1030 <free@plt>
jmp    1178 <db_attach+0x48>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.db_handle = type { i32 }
@.str = [25 x i8] c"ATTACH DATABASE %s AS %s\00"
define i32 @db_attach(%struct.db_handle* %0, i8* %1, i8* %2) {
%4 = alloca i32
%5 = alloca %struct.db_handle*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
store %struct.db_handle* %0, %struct.db_handle** %5
store i8* %1, i8** %6
store i8* %2, i8** %7
%10 = load i8*, i8** %6
%11 = load i8*, i8** %7
%12 = call i64 @asprintf(i8** %8, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* %10, i8* %11)
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %15
14: ; preds = %3
store i32 -1, i32* %4
br label %22
15: ; preds = %3
%16 = load %struct.db_handle*, %struct.db_handle** %5
%17 = load i8*, i8** %8
%18 = call i32 @db_exec(%struct.db_handle* %16, i8* %17, i32* null, i32* null)
store i32 %18, i32* %9
%19 = load i8*, i8** %8
%20 = call i32 @free(i8* %19)
%21 = load i32, i32* %9
store i32 %21, i32* %4
br label %22
22: ; preds = %15, %14
%23 = load i32, i32* %4
ret i32 %23
}

LLVM IR (-O2):
%struct.db_handle = type { i32 }
@.str = [25 x i8] c"ATTACH DATABASE %s AS %s\00"
define i32 @db_attach(%struct.db_handle* %0, i8* %1, i8* %2) {
%4 = alloca i8*
%5 = bitcast i8** %4 to i8*
%6 = call i64 @asprintf(i8** %4, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* %1, i8* %2)
%7 = icmp slt i64 %6, 0
br i1 %7, label %13, label %8
8: ; preds = %3
%9 = load i8*, i8** %4
%10 = call i32 @db_exec(%struct.db_handle* %0, i8* %9, i32* null, i32* null)
%11 = load i8*, i8** %4
%12 = call i32 @free(i8* %11)
br label %13
13: ; preds = %3, %8
%14 = phi i32 [ %10, %8 ], [ -1, %3 ]
ret i32 %14
}