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

/* Variables and functions */
int /*<<< orphan*/ fileExists(char *);
char *getMozProfilePath(int /*<<< orphan*/, char *);
scalar_t__ malloc(int);
int /*<<< orphan*/ printf(char *, char const *);
int /*<<< orphan*/ strcpy(char *, char *);
scalar_t__ strlen(char const *);
int /*<<< orphan*/ strncat(char *, char const *, int);
int /*<<< orphan*/ strncpy(char *, char *, int);
int /*<<< orphan*/ strstr(char *, char *);
char *getAlternativeFile(const char *filename, char *profilePath,
                         char *lastPath) {
  char *ret = (char *)malloc(500);
  unsigned int retSize = 0;
  if (profilePath != NULL && strlen(profilePath) > 0) {
    strncpy(ret, profilePath, 500 - retSize - 1);
    retSize += strlen(profilePath);
  } else {
    strcpy(ret, ".");
    retSize++;
  }
  strncat(ret, "/", 500 - retSize - 1);
  strncat(ret, filename, 500 - retSize - 2);
  retSize += strlen(filename) + 1;
  if (!fileExists(ret)) {
    if (profilePath != NULL && strlen(profilePath) > 0) {
      printf("[!] Could NOT use the path specified to search for %s\n",
             filename);
    }
    retSize = 0;
    if (lastPath && !strstr(lastPath, "/.thunderbird/")) {
      profilePath = getMozProfilePath(0, "/.thunderbird/");
    } else {
      profilePath = getMozProfilePath(0, "/.mozilla/firefox/");
    }
    if (profilePath != NULL) {
      strncpy(ret, profilePath, 500 - retSize - 1);
      retSize += strlen(profilePath);
      strncat(ret, "/", 500 - retSize - 1);
      strncat(ret, filename, 500 - retSize - 2);
    }
  }
  if (!fileExists(ret)) {
    strcpy(ret, "");
  }
  return ret;
}


x86 assembly (clang, -O0):
<getAlternativeFile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    $0x1f4,%edi
call   1090 <malloc@plt>
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1209 <getAlternativeFile+0x79>
mov    -0x10(%rbp),%rdi
call   1060 <strlen@plt>
cmp    $0x0,%rax
jle    1209 <getAlternativeFile+0x79>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    $0x1f4,%edx
sub    -0x24(%rbp),%edx
sub    $0x1,%edx
call   1030 <strncpy@plt>
mov    -0x10(%rbp),%rdi
call   1060 <strlen@plt>
mov    %rax,%rcx
mov    -0x24(%rbp),%eax
add    %rcx,%rax
mov    %eax,-0x24(%rbp)
jmp    1222 <getAlternativeFile+0x92>
mov    -0x20(%rbp),%rdi
lea    0xdec(%rip),%rsi
call   1040 <strcpy@plt>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%rdi
mov    $0x1f4,%edx
sub    -0x24(%rbp),%edx
sub    $0x1,%edx
lea    0xe0e(%rip),%rsi
call   1080 <strncat@plt>
mov    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    $0x1f4,%edx
sub    -0x24(%rbp),%edx
sub    $0x2,%edx
call   1080 <strncat@plt>
mov    -0x8(%rbp),%rdi
call   1060 <strlen@plt>
mov    %rax,%rcx
add    $0x1,%rcx
mov    -0x24(%rbp),%eax
add    %rcx,%rax
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%rdi
call   1050 <fileExists@plt>
cmp    $0x0,%eax
jne    1372 <getAlternativeFile+0x1e2>
cmpq   $0x0,-0x10(%rbp)
je     12ae <getAlternativeFile+0x11e>
mov    -0x10(%rbp),%rdi
call   1060 <strlen@plt>
cmp    $0x0,%rax
jle    12ae <getAlternativeFile+0x11e>
mov    -0x8(%rbp),%rsi
lea    0xd59(%rip),%rdi
call   1070 <printf@plt>
movl   $0x0,-0x24(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     12f0 <getAlternativeFile+0x160>
mov    -0x18(%rbp),%rdi
lea    0xd6e(%rip),%rsi
call   10b0 <strstr@plt>
cmp    $0x0,%eax
jne    12f0 <getAlternativeFile+0x160>
xor    %edi,%edi
lea    0xd57(%rip),%rsi
call   10a0 <getMozProfilePath@plt>
mov    %rax,-0x10(%rbp)
jmp    1302 <getAlternativeFile+0x172>
xor    %edi,%edi
lea    0xd4f(%rip),%rsi
call   10a0 <getMozProfilePath@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     136d <getAlternativeFile+0x1dd>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    $0x1f4,%edx
sub    -0x24(%rbp),%edx
sub    $0x1,%edx
call   1030 <strncpy@plt>
mov    -0x10(%rbp),%rdi
call   1060 <strlen@plt>
mov    %rax,%rcx
mov    -0x24(%rbp),%eax
add    %rcx,%rax
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%rdi
mov    $0x1f4,%edx
sub    -0x24(%rbp),%edx
sub    $0x1,%edx
lea    0xcf6(%rip),%rsi
call   1080 <strncat@plt>
mov    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    $0x1f4,%edx
sub    -0x24(%rbp),%edx
sub    $0x2,%edx
call   1080 <strncat@plt>
jmp    1372 <getAlternativeFile+0x1e2>
mov    -0x20(%rbp),%rdi
call   1050 <fileExists@plt>
cmp    $0x0,%eax
jne    1394 <getAlternativeFile+0x204>
mov    -0x20(%rbp),%rdi
lea    0xca9(%rip),%rsi
call   1040 <strcpy@plt>
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getAlternativeFile>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r15
mov    %rsi,%r12
mov    %rdi,%r14
mov    $0x1f4,%edi
call   1090 <malloc@plt>
mov    %rax,%r13
test   %r12,%r12
je     11e4 <getAlternativeFile+0x54>
mov    %r12,%rdi
call   1060 <strlen@plt>
test   %rax,%rax
jle    11e4 <getAlternativeFile+0x54>
mov    %r13,%rdi
mov    %r12,%rsi
mov    $0x1f3,%edx
call   1030 <strncpy@plt>
mov    %r12,%rdi
call   1060 <strlen@plt>
mov    $0x1f4,%ebx
sub    %eax,%ebx
jmp    11f8 <getAlternativeFile+0x68>
lea    0xe15(%rip),%rsi
mov    %r13,%rdi
call   1040 <strcpy@plt>
mov    $0x1f3,%ebx
lea    -0x1(%rbx),%edx
lea    0xe44(%rip),%rsi
mov    %r13,%rdi
call   1080 <strncat@plt>
add    $0xfffffffe,%ebx
mov    %r13,%rdi
mov    %r14,%rsi
mov    %ebx,%edx
call   1080 <strncat@plt>
mov    %r13,%rdi
call   1050 <fileExists@plt>
test   %eax,%eax
jne    12c4 <getAlternativeFile+0x134>
test   %r12,%r12
je     124b <getAlternativeFile+0xbb>
mov    %r12,%rdi
call   1060 <strlen@plt>
test   %rax,%rax
jle    124b <getAlternativeFile+0xbb>
lea    0xdbf(%rip),%rdi
mov    %r14,%rsi
call   1070 <printf@plt>
test   %r15,%r15
je     1266 <getAlternativeFile+0xd6>
lea    0xde2(%rip),%rbx
mov    %r15,%rdi
mov    %rbx,%rsi
call   10b0 <strstr@plt>
test   %eax,%eax
je     126d <getAlternativeFile+0xdd>
lea    0xddb(%rip),%rbx
xor    %edi,%edi
mov    %rbx,%rsi
call   10a0 <getMozProfilePath@plt>
test   %rax,%rax
je     12c4 <getAlternativeFile+0x134>
mov    %rax,%rbx
mov    $0x1f3,%ebp
mov    %r13,%rdi
mov    %rax,%rsi
mov    $0x1f3,%edx
call   1030 <strncpy@plt>
mov    %rbx,%rdi
call   1060 <strlen@plt>
mov    %rax,%rbx
sub    %ebx,%ebp
lea    0xd9e(%rip),%rsi
mov    %r13,%rdi
mov    %ebp,%edx
call   1080 <strncat@plt>
mov    $0x1f2,%edx
sub    %ebx,%edx
mov    %r13,%rdi
mov    %r14,%rsi
call   1080 <strncat@plt>
mov    %r13,%rdi
call   1050 <fileExists@plt>
test   %eax,%eax
jne    12df <getAlternativeFile+0x14f>
lea    0xd61(%rip),%rsi
mov    %r13,%rdi
call   1040 <strcpy@plt>
mov    %r13,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c".\00"
@.str.1 = [2 x i8] c"/\00"
@.str.2 = [55 x i8] c"[!] Could NOT use the path specified to search for %s\0A\00"
@.str.3 = [15 x i8] c"/.thunderbird/\00"
@.str.4 = [19 x i8] c"/.mozilla/firefox/\00"
@.str.5 = [1 x i8] zeroinitializer
define i8* @getAlternativeFile(i8* %0, i8* %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i8* %2, i8** %6
%9 = call i64 @malloc(i32 500)
%10 = inttoptr i64 %9 to i8*
store i8* %10, i8** %7
store i32 0, i32* %8
%11 = load i8*, i8** %5
%12 = icmp ne i8* %11, null
br i1 %12, label %13, label %30
13: ; preds = %3
%14 = load i8*, i8** %5
%15 = call i64 @strlen(i8* %14)
%16 = icmp sgt i64 %15, 0
br i1 %16, label %17, label %30
17: ; preds = %13
%18 = load i8*, i8** %7
%19 = load i8*, i8** %5
%20 = load i32, i32* %8
%21 = sub i32 500, %20
%22 = sub i32 %21, 1
%23 = call i32 @strncpy(i8* %18, i8* %19, i32 %22)
%24 = load i8*, i8** %5
%25 = call i64 @strlen(i8* %24)
%26 = load i32, i32* %8
%27 = zext i32 %26 to i64
%28 = add nsw i64 %27, %25
%29 = trunc i64 %28 to i32
store i32 %29, i32* %8
br label %35
30: ; preds = %13, %3
%31 = load i8*, i8** %7
%32 = call i32 @strcpy(i8* %31, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%33 = load i32, i32* %8
%34 = add i32 %33, 1
store i32 %34, i32* %8
br label %35
35: ; preds = %30, %17
%36 = load i8*, i8** %7
%37 = load i32, i32* %8
%38 = sub i32 500, %37
%39 = sub i32 %38, 1
%40 = call i32 @strncat(i8* %36, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 %39)
%41 = load i8*, i8** %7
%42 = load i8*, i8** %4
%43 = load i32, i32* %8
%44 = sub i32 500, %43
%45 = sub i32 %44, 2
%46 = call i32 @strncat(i8* %41, i8* %42, i32 %45)
%47 = load i8*, i8** %4
%48 = call i64 @strlen(i8* %47)
%49 = add nsw i64 %48, 1
%50 = load i32, i32* %8
%51 = zext i32 %50 to i64
%52 = add nsw i64 %51, %49
%53 = trunc i64 %52 to i32
store i32 %53, i32* %8
%54 = load i8*, i8** %7
%55 = call i32 @fileExists(i8* %54)
%56 = icmp ne i32 %55, 0
br i1 %56, label %106, label %57
57: ; preds = %35
%58 = load i8*, i8** %5
%59 = icmp ne i8* %58, null
br i1 %59, label %60, label %67
60: ; preds = %57
%61 = load i8*, i8** %5
%62 = call i64 @strlen(i8* %61)
%63 = icmp sgt i64 %62, 0
br i1 %63, label %64, label %67
64: ; preds = %60
%65 = load i8*, i8** %4
%66 = call i32 @printf(i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.2, i64 0, i64 0), i8* %65)
br label %67
67: ; preds = %64, %60, %57
store i32 0, i32* %8
%68 = load i8*, i8** %6
%69 = icmp ne i8* %68, null
br i1 %69, label %70, label %76
70: ; preds = %67
%71 = load i8*, i8** %6
%72 = call i32 @strstr(i8* %71, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
%73 = icmp ne i32 %72, 0
br i1 %73, label %76, label %74
74: ; preds = %70
%75 = call i8* @getMozProfilePath(i32 0, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
store i8* %75, i8** %5
br label %78
76: ; preds = %70, %67
%77 = call i8* @getMozProfilePath(i32 0, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0))
store i8* %77, i8** %5
br label %78
78: ; preds = %76, %74
%79 = load i8*, i8** %5
%80 = icmp ne i8* %79, null
br i1 %80, label %81, label %105
81: ; preds = %78
%82 = load i8*, i8** %7
%83 = load i8*, i8** %5
%84 = load i32, i32* %8
%85 = sub i32 500, %84
%86 = sub i32 %85, 1
%87 = call i32 @strncpy(i8* %82, i8* %83, i32 %86)
%88 = load i8*, i8** %5
%89 = call i64 @strlen(i8* %88)
%90 = load i32, i32* %8
%91 = zext i32 %90 to i64
%92 = add nsw i64 %91, %89
%93 = trunc i64 %92 to i32
store i32 %93, i32* %8
%94 = load i8*, i8** %7
%95 = load i32, i32* %8
%96 = sub i32 500, %95
%97 = sub i32 %96, 1
%98 = call i32 @strncat(i8* %94, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 %97)
%99 = load i8*, i8** %7
%100 = load i8*, i8** %4
%101 = load i32, i32* %8
%102 = sub i32 500, %101
%103 = sub i32 %102, 2
%104 = call i32 @strncat(i8* %99, i8* %100, i32 %103)
br label %105
105: ; preds = %81, %78
br label %106
106: ; preds = %105, %35
%107 = load i8*, i8** %7
%108 = call i32 @fileExists(i8* %107)
%109 = icmp ne i32 %108, 0
br i1 %109, label %113, label %110
110: ; preds = %106
%111 = load i8*, i8** %7
%112 = call i32 @strcpy(i8* %111, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.5, i64 0, i64 0))
br label %113
113: ; preds = %110, %106
%114 = load i8*, i8** %7
ret i8* %114
}

LLVM IR (-O2):
@.str = [2 x i8] c".\00"
@.str.1 = [2 x i8] c"/\00"
@.str.2 = [55 x i8] c"[!] Could NOT use the path specified to search for %s\0A\00"
@.str.3 = [15 x i8] c"/.thunderbird/\00"
@.str.4 = [19 x i8] c"/.mozilla/firefox/\00"
@.str.5 = [1 x i8] zeroinitializer
define i8* @getAlternativeFile(i8* %0, i8* %1, i8* %2) {
%4 = tail call i64 @malloc(i32 500)
%5 = inttoptr i64 %4 to i8*
%6 = icmp eq i8* %1, null
br i1 %6, label %15, label %7
7: ; preds = %3
%8 = tail call i64 @strlen(i8* %1)
%9 = icmp sgt i64 %8, 0
br i1 %9, label %10, label %15
10: ; preds = %7
%11 = tail call i32 @strncpy(i8* %5, i8* %1, i32 499)
%12 = tail call i64 @strlen(i8* %1)
%13 = trunc i64 %12 to i32
%14 = sub i32 500, %13
br label %17
15: ; preds = %7, %3
%16 = tail call i32 @strcpy(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
br label %17
17: ; preds = %15, %10
%18 = phi i32 [ %14, %10 ], [ 499, %15 ]
%19 = add i32 %18, -1
%20 = tail call i32 @strncat(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 %19)
%21 = add i32 %18, -2
%22 = tail call i32 @strncat(i8* %5, i8* %0, i32 %21)
%23 = tail call i32 @fileExists(i8* %5)
%24 = icmp eq i32 %23, 0
br i1 %24, label %25, label %49
25: ; preds = %17
br i1 %6, label %31, label %26
26: ; preds = %25
%27 = tail call i64 @strlen(i8* %1)
%28 = icmp sgt i64 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %26
%30 = tail call i32 @printf(i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.2, i64 0, i64 0), i8* %0)
br label %31
31: ; preds = %29, %26, %25
%32 = icmp eq i8* %2, null
br i1 %32, label %36, label %33
33: ; preds = %31
%34 = tail call i32 @strstr(i8* %2, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
%35 = icmp eq i32 %34, 0
br i1 %35, label %37, label %36
36: ; preds = %33, %31
br label %37
37: ; preds = %33, %36
%38 = phi i8* [ getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), %36 ], [ getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), %33 ]
%39 = tail call i8* @getMozProfilePath(i32 0, i8* %38)
%40 = icmp eq i8* %39, null
br i1 %40, label %49, label %41
41: ; preds = %37
%42 = tail call i32 @strncpy(i8* %5, i8* %39, i32 499)
%43 = tail call i64 @strlen(i8* %39)
%44 = trunc i64 %43 to i32
%45 = sub i32 499, %44
%46 = tail call i32 @strncat(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 %45)
%47 = sub i32 498, %44
%48 = tail call i32 @strncat(i8* %5, i8* %0, i32 %47)
br label %49
49: ; preds = %37, %41, %17
%50 = tail call i32 @fileExists(i8* %5)
%51 = icmp eq i32 %50, 0
br i1 %51, label %52, label %54
52: ; preds = %49
%53 = tail call i32 @strcpy(i8* %5, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.5, i64 0, i64 0))
br label %54
54: ; preds = %52, %49
ret i8* %5
}