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

/* Variables and functions */
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
char *get_home();
char *getenv(char *);
int /*<<< orphan*/ perror(char *);
char *realloc(char *, int);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
char *xmalloc(int, char *);
char *xstrdup(char *, char *);
char *get_db_path() {
  char *path;
  FILE *fp;
  if ((path = getenv("TVEDB")) && path[0] != '\0') {
    return xstrdup(path, "get_db_path: xstrdup");
  }
  if ((path = get_home())) {
    path = realloc(path, (strlen(path) + 8 + 1) * sizeof(*path));
    if (!path) {
      perror("get_db_path: realloc");
      exit(-1);
    }
    strcat(path, "/.tve.db");
    fp = fopen(path, "r+b");
    if (fp) {
      fclose(fp);
      return path;
    }
  }
  if ((path = getenv("TVEROOT")) && path[0] != '\0') {
    path = xmalloc((strlen(path) + sizeof("/var/cache/tve.db")) * sizeof(*path),
                   "get_db_path");
    strcpy(path, getenv("TVEROOT"));
    strcat(path, "/var/cache/tve.db");
    return path;
  }
  return xstrdup("/var/cache/tve.db", "get_db_path: xstrdup");
}


x86 assembly (clang, -O0):
<get_db_path>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
lea    0xe31(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
je     120b <get_db_path+0x4b>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     120b <get_db_path+0x4b>
mov    -0x10(%rbp),%rdi
lea    0xe09(%rip),%rsi
call   1080 <xstrdup@plt>
mov    %rax,-0x8(%rbp)
jmp    135a <get_db_path+0x19a>
mov    $0x0,%al
call   10c0 <get_home@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
je     12b7 <get_db_path+0xf7>
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0x20(%rbp),%rdi
add    $0x8,%eax
add    $0x1,%eax
cltq
shl    $0x0,%rax
mov    %eax,%esi
call   1090 <realloc@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    126d <get_db_path+0xad>
lea    0xdbd(%rip),%rdi
call   10b0 <perror@plt>
mov    $0xffffffff,%edi
call   10e0 <exit@plt>
mov    -0x10(%rbp),%rdi
lea    0xdb8(%rip),%rsi
call   10d0 <strcat@plt>
mov    -0x10(%rbp),%rdi
lea    0xdb1(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     12b2 <get_db_path+0xf2>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    135a <get_db_path+0x19a>
jmp    12b7 <get_db_path+0xf7>
lea    0xd7f(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
je     1343 <get_db_path+0x183>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     1343 <get_db_path+0x183>
mov    -0x10(%rbp),%rdi
call   1060 <strlen@plt>
cltq
add    $0x12,%rax
shl    $0x0,%rax
mov    %eax,%edi
lea    0xd48(%rip),%rsi
call   1070 <xmalloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
lea    0xd28(%rip),%rdi
call   1030 <getenv@plt>
mov    -0x28(%rbp),%rdi
mov    %rax,%rsi
call   1040 <strcpy@plt>
mov    -0x10(%rbp),%rdi
lea    0xd20(%rip),%rsi
call   10d0 <strcat@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    135a <get_db_path+0x19a>
lea    0xd07(%rip),%rdi
lea    0xcb5(%rip),%rsi
call   1080 <xstrdup@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_db_path>:
push   %rbx
lea    0xe38(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     11e7 <get_db_path+0x27>
cmpb   $0x0,(%rax)
je     11e7 <get_db_path+0x27>
lea    0xe28(%rip),%rsi
mov    %rax,%rdi
pop    %rbx
jmp    1080 <xstrdup@plt>
xor    %eax,%eax
call   10c0 <get_home@plt>
test   %rax,%rax
je     1242 <get_db_path+0x82>
mov    %rax,%rbx
mov    %rax,%rdi
call   1060 <strlen@plt>
lea    0x9(%rax),%esi
mov    %rbx,%rdi
call   1090 <realloc@plt>
test   %rax,%rax
je     12b1 <get_db_path+0xf1>
mov    %rax,%rbx
lea    0xe14(%rip),%rsi
mov    %rax,%rdi
call   10d0 <strcat@plt>
lea    0xe0e(%rip),%rsi
mov    %rbx,%rdi
call   10a0 <fopen@plt>
test   %rax,%rax
je     1242 <get_db_path+0x82>
mov    %rax,%rdi
call   1050 <fclose@plt>
jmp    1298 <get_db_path+0xd8>
lea    0xdf4(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     129d <get_db_path+0xdd>
cmpb   $0x0,(%rax)
je     129d <get_db_path+0xdd>
mov    %rax,%rdi
call   1060 <strlen@plt>
lea    0x12(%rax),%edi
lea    0xddb(%rip),%rsi
call   1070 <xmalloc@plt>
mov    %rax,%rbx
lea    0xdc4(%rip),%rdi
call   1030 <getenv@plt>
mov    %rbx,%rdi
mov    %rax,%rsi
call   1040 <strcpy@plt>
lea    0xdc1(%rip),%rsi
mov    %rbx,%rdi
call   10d0 <strcat@plt>
mov    %rbx,%rax
pop    %rbx
ret
lea    0xdad(%rip),%rdi
lea    0xd5b(%rip),%rsi
pop    %rbx
jmp    1080 <xstrdup@plt>
lea    0xd63(%rip),%rdi
call   10b0 <perror@plt>
mov    $0xffffffff,%edi
call   10e0 <exit@plt>

LLVM IR (-O0):
@.str = [6 x i8] c"TVEDB\00"
@.str.1 = [21 x i8] c"get_db_path: xstrdup\00"
@.str.2 = [21 x i8] c"get_db_path: realloc\00"
@.str.3 = [9 x i8] c"/.tve.db\00"
@.str.4 = [4 x i8] c"r+b\00"
@.str.5 = [8 x i8] c"TVEROOT\00"
@.str.6 = [12 x i8] c"get_db_path\00"
@.str.7 = [18 x i8] c"/var/cache/tve.db\00"
define i8* @get_db_path() {
%1 = alloca i8*
%2 = alloca i8*
%3 = alloca i32*
%4 = call i8* @getenv(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
store i8* %4, i8** %2
%5 = icmp ne i8* %4, null
br i1 %5, label %6, label %15
6: ; preds = %0
%7 = load i8*, i8** %2
%8 = getelementptr inbounds i8, i8* %7, i64 0
%9 = load i8, i8* %8
%10 = sext i8 %9 to i32
%11 = icmp ne i32 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %6
%13 = load i8*, i8** %2
%14 = call i8* @xstrdup(i8* %13, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
store i8* %14, i8** %1
br label %70
15: ; preds = %6, %0
%16 = call i8* (...) @get_home()
store i8* %16, i8** %2
%17 = icmp ne i8* %16, null
br i1 %17, label %18, label %45
18: ; preds = %15
%19 = load i8*, i8** %2
%20 = load i8*, i8** %2
%21 = call i32 @strlen(i8* %20)
%22 = add nsw i32 %21, 8
%23 = add nsw i32 %22, 1
%24 = sext i32 %23 to i64
%25 = mul i64 %24, 1
%26 = trunc i64 %25 to i32
%27 = call i8* @realloc(i8* %19, i32 %26)
store i8* %27, i8** %2
%28 = load i8*, i8** %2
%29 = icmp ne i8* %28, null
br i1 %29, label %33, label %30
30: ; preds = %18
%31 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
%32 = call i32 @exit(i32 -1)
unreachable
33: ; preds = %18
%34 = load i8*, i8** %2
%35 = call i32 @strcat(i8* %34, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
%36 = load i8*, i8** %2
%37 = call i32* @fopen(i8* %36, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0))
store i32* %37, i32** %3
%38 = load i32*, i32** %3
%39 = icmp ne i32* %38, null
br i1 %39, label %40, label %44
40: ; preds = %33
%41 = load i32*, i32** %3
%42 = call i32 @fclose(i32* %41)
%43 = load i8*, i8** %2
store i8* %43, i8** %1
br label %70
44: ; preds = %33
br label %45
45: ; preds = %44, %15
%46 = call i8* @getenv(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0))
store i8* %46, i8** %2
%47 = icmp ne i8* %46, null
br i1 %47, label %48, label %68
48: ; preds = %45
%49 = load i8*, i8** %2
%50 = getelementptr inbounds i8, i8* %49, i64 0
%51 = load i8, i8* %50
%52 = sext i8 %51 to i32
%53 = icmp ne i32 %52, 0
br i1 %53, label %54, label %68
54: ; preds = %48
%55 = load i8*, i8** %2
%56 = call i32 @strlen(i8* %55)
%57 = sext i32 %56 to i64
%58 = add i64 %57, 18
%59 = mul i64 %58, 1
%60 = trunc i64 %59 to i32
%61 = call i8* @xmalloc(i32 %60, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.6, i64 0, i64 0))
store i8* %61, i8** %2
%62 = load i8*, i8** %2
%63 = call i8* @getenv(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0))
%64 = call i32 @strcpy(i8* %62, i8* %63)
%65 = load i8*, i8** %2
%66 = call i32 @strcat(i8* %65, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.7, i64 0, i64 0))
%67 = load i8*, i8** %2
store i8* %67, i8** %1
br label %70
68: ; preds = %48, %45
%69 = call i8* @xstrdup(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.7, i64 0, i64 0), i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
store i8* %69, i8** %1
br label %70
70: ; preds = %68, %54, %40, %12
%71 = load i8*, i8** %1
ret i8* %71
}

LLVM IR (-O2):
@.str = [6 x i8] c"TVEDB\00"
@.str.1 = [21 x i8] c"get_db_path: xstrdup\00"
@.str.2 = [21 x i8] c"get_db_path: realloc\00"
@.str.3 = [9 x i8] c"/.tve.db\00"
@.str.4 = [4 x i8] c"r+b\00"
@.str.5 = [8 x i8] c"TVEROOT\00"
@.str.6 = [12 x i8] c"get_db_path\00"
@.str.7 = [18 x i8] c"/var/cache/tve.db\00"
define i8* @get_db_path() {
%1 = tail call i8* @getenv(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%2 = icmp eq i8* %1, null
br i1 %2, label %8, label %3
3: ; preds = %0
%4 = load i8, i8* %1
%5 = icmp eq i8 %4, 0
br i1 %5, label %8, label %6
6: ; preds = %3
%7 = tail call i8* @xstrdup(i8* %1, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
br label %40
8: ; preds = %3, %0
%9 = tail call i8* (...) @get_home()
%10 = icmp eq i8* %9, null
br i1 %10, label %25, label %11
11: ; preds = %8
%12 = tail call i32 @strlen(i8* %9)
%13 = add nsw i32 %12, 9
%14 = tail call i8* @realloc(i8* %9, i32 %13)
%15 = icmp eq i8* %14, null
br i1 %15, label %16, label %19
16: ; preds = %11
%17 = tail call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
%18 = tail call i32 @exit(i32 -1)
unreachable
19: ; preds = %11
%20 = tail call i32 @strcat(i8* %14, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
%21 = tail call i32* @fopen(i8* %14, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0))
%22 = icmp eq i32* %21, null
br i1 %22, label %25, label %23
23: ; preds = %19
%24 = tail call i32 @fclose(i32* %21)
br label %40
25: ; preds = %19, %8
%26 = tail call i8* @getenv(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0))
%27 = icmp eq i8* %26, null
br i1 %27, label %38, label %28
28: ; preds = %25
%29 = load i8, i8* %26
%30 = icmp eq i8 %29, 0
br i1 %30, label %38, label %31
31: ; preds = %28
%32 = tail call i32 @strlen(i8* %26)
%33 = add i32 %32, 18
%34 = tail call i8* @xmalloc(i32 %33, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.6, i64 0, i64 0))
%35 = tail call i8* @getenv(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0))
%36 = tail call i32 @strcpy(i8* %34, i8* %35)
%37 = tail call i32 @strcat(i8* %34, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.7, i64 0, i64 0))
br label %40
38: ; preds = %28, %25
%39 = tail call i8* @xstrdup(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.7, i64 0, i64 0), i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
br label %40
40: ; preds = %38, %31, %23, %6
%41 = phi i8* [ %7, %6 ], [ %14, %23 ], [ %34, %31 ], [ %39, %38 ]
ret i8* %41
}