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

/* Variables and functions */
int /*<<< orphan*/ chmod(char *, int);
int /*<<< orphan*/ file_copy(char *, char *, struct stat);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ join_strings(char **, int, char *, char *, char *);
int /*<<< orphan*/ rename(char *, char *);
int /*<<< orphan*/ sqlite3_close(int /*<<< orphan*/ *);
int /*<<< orphan*/ stat(char *, struct stat *);
int /*<<< orphan*/ unlink(char *);
int close_db(sqlite3 **db, char *db_filename, char *db_temp_filename,
             char *path) {
  char *new_filename;
  struct stat db_stats;
  unlink(db_filename);
  rename(db_temp_filename, db_filename);
  chmod(db_filename, 0777);
  join_strings(&new_filename, 3, path, "/", "mnemon.db");
  stat(db_filename, &db_stats);
  file_copy(db_filename, new_filename, db_stats);
  free(new_filename);
  sqlite3_close(*db);
  return 0;
}


x86 assembly (clang, -O0):
<close_db>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <unlink@plt>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   10a0 <rename@plt>
mov    -0x10(%rbp),%rdi
mov    $0x1ff,%esi
call   1080 <chmod@plt>
mov    -0x20(%rbp),%rdx
lea    -0x28(%rbp),%rdi
mov    $0x3,%esi
lea    0xe30(%rip),%rcx
lea    0xe2b(%rip),%r8
call   1090 <join_strings@plt>
mov    -0x10(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1050 <stat@plt>
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x30(%rbp),%edx
call   1070 <file_copy@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
call   1060 <sqlite3_close@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<close_db>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rcx,%r15
mov    %rdx,%r12
mov    %rsi,%rbx
mov    %rdi,%r14
mov    %rsi,%rdi
call   1040 <unlink@plt>
mov    %r12,%rdi
mov    %rbx,%rsi
call   10a0 <rename@plt>
mov    %rbx,%rdi
mov    $0x1ff,%esi
call   1080 <chmod@plt>
lea    0xe42(%rip),%rcx
lea    0xe3d(%rip),%r8
lea    0x10(%rsp),%rdi
mov    $0x3,%esi
mov    %r15,%rdx
call   1090 <join_strings@plt>
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
call   1050 <stat@plt>
mov    0x10(%rsp),%rsi
mov    0x8(%rsp),%edx
mov    %rbx,%rdi
call   1070 <file_copy@plt>
mov    0x10(%rsp),%rdi
call   1030 <free@plt>
mov    (%r14),%rdi
call   1060 <sqlite3_close@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [2 x i8] c"/\00"
@.str.1 = [10 x i8] c"mnemon.db\00"
define i32 @close_db(i32** %0, i8* %1, i8* %2, i8* %3) {
%5 = alloca i32**
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca %struct.stat
store i32** %0, i32*** %5
store i8* %1, i8** %6
store i8* %2, i8** %7
store i8* %3, i8** %8
%11 = load i8*, i8** %6
%12 = call i32 @unlink(i8* %11)
%13 = load i8*, i8** %7
%14 = load i8*, i8** %6
%15 = call i32 @rename(i8* %13, i8* %14)
%16 = load i8*, i8** %6
%17 = call i32 @chmod(i8* %16, i32 511)
%18 = load i8*, i8** %8
%19 = call i32 @join_strings(i8** %9, i32 3, i8* %18, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%20 = load i8*, i8** %6
%21 = call i32 @stat(i8* %20, %struct.stat* %10)
%22 = load i8*, i8** %6
%23 = load i8*, i8** %9
%24 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 0
%25 = load i32, i32* %24
%26 = call i32 @file_copy(i8* %22, i8* %23, i32 %25)
%27 = load i8*, i8** %9
%28 = call i32 @free(i8* %27)
%29 = load i32**, i32*** %5
%30 = load i32*, i32** %29
%31 = call i32 @sqlite3_close(i32* %30)
ret i32 0
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [2 x i8] c"/\00"
@.str.1 = [10 x i8] c"mnemon.db\00"
define i32 @close_db(i32** nocapture %0, i8* %1, i8* nocapture %2, i8* %3) {
%5 = alloca i8*
%6 = alloca %struct.stat
%7 = bitcast i8** %5 to i8*
%8 = bitcast %struct.stat* %6 to i8*
%9 = tail call i32 @unlink(i8* %1)
%10 = tail call i32 @rename(i8* %2, i8* %1)
%11 = tail call i32 @chmod(i8* %1, i32 511)
%12 = call i32 @join_strings(i8** %5, i32 3, i8* %3, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%13 = call i32 @stat(i8* %1, %struct.stat* %6)
%14 = load i8*, i8** %5
%15 = getelementptr inbounds %struct.stat, %struct.stat* %6, i64 0, i32 0
%16 = load i32, i32* %15
%17 = call i32 @file_copy(i8* %1, i8* %14, i32 %16)
%18 = load i8*, i8** %5
%19 = call i32 @free(i8* %18)
%20 = load i32*, i32** %0
%21 = call i32 @sqlite3_close(i32* %20)
ret i32 0
}