#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 passwd {
  char const *pw_dir;
};

/* Variables and functions */
int /*<<< orphan*/ free(char *);
char *getcwd(int /*<<< orphan*/ *, int /*<<< orphan*/);
void *getenv(char *);
struct passwd *getpwuid(int /*<<< orphan*/);
int /*<<< orphan*/ getuid();
char const *mpw_str(char *, char const *, char const *);
char *mpw_strdup(char const *);
char *strstr(char *, char *);
const char *mpw_path(const char *prefix, const char *extension) {
  char *homeDir = NULL;
  if (!homeDir)
    if ((homeDir = getenv("HOME")))
      homeDir = mpw_strdup(homeDir);
  if (!homeDir)
    if ((homeDir = getenv("USERPROFILE")))
      homeDir = mpw_strdup(homeDir);
  if (!homeDir) {
    const char *homeDrive = getenv("HOMEDRIVE"), *homePath = getenv("HOMEPATH");
    if (homeDrive && homePath)
      homeDir = mpw_strdup(mpw_str("%s%s", homeDrive, homePath));
  }
  if (!homeDir) {
    struct passwd *passwd = getpwuid(getuid());
    if (passwd)
      homeDir = mpw_strdup(passwd->pw_dir);
  }
  if (!homeDir)
    homeDir = getcwd(NULL, 0);
  char *path = mpw_strdup(mpw_str("%s.%s", prefix, extension));
  for (char *slash; (slash = strstr(path, "/")); *slash = '_')
    ;
  if (homeDir) {
    const char *homePath = mpw_str("%s/.mpw.d/%s", homeDir, path);
    free(homeDir);
    free(path);
    if (homePath)
      path = mpw_strdup(homePath);
  }
  return path;
}


x86 assembly (clang, -O0):
<mpw_path>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11cf <mpw_path+0x4f>
lea    0xe56(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     11ca <mpw_path+0x4a>
mov    -0x18(%rbp),%rdi
call   1050 <mpw_strdup@plt>
mov    %rax,-0x18(%rbp)
jmp    11cf <mpw_path+0x4f>
cmpq   $0x0,-0x18(%rbp)
jne    1206 <mpw_path+0x86>
lea    0xe24(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     1201 <mpw_path+0x81>
mov    -0x18(%rbp),%rdi
call   1050 <mpw_strdup@plt>
mov    %rax,-0x18(%rbp)
jmp    1206 <mpw_path+0x86>
cmpq   $0x0,-0x18(%rbp)
jne    126c <mpw_path+0xec>
lea    0xdf9(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x20(%rbp)
lea    0xdf3(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1267 <mpw_path+0xe7>
cmpq   $0x0,-0x28(%rbp)
je     1267 <mpw_path+0xe7>
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rdx
lea    0xdce(%rip),%rdi
call   1060 <mpw_str@plt>
mov    %rax,%rdi
call   1050 <mpw_strdup@plt>
mov    %rax,-0x18(%rbp)
jmp    126c <mpw_path+0xec>
cmpq   $0x0,-0x18(%rbp)
jne    12a9 <mpw_path+0x129>
mov    $0x0,%al
call   1080 <getuid@plt>
mov    %eax,%edi
call   1070 <getpwuid@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     12a4 <mpw_path+0x124>
mov    -0x30(%rbp),%rax
mov    (%rax),%rdi
call   1050 <mpw_strdup@plt>
mov    %rax,-0x18(%rbp)
jmp    12a9 <mpw_path+0x129>
cmpq   $0x0,-0x18(%rbp)
jne    12c3 <mpw_path+0x143>
xor    %eax,%eax
mov    %eax,%edi
xor    %esi,%esi
call   1090 <getcwd@plt>
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rsi
mov    -0x10(%rbp),%rdx
lea    0xd57(%rip),%rdi
call   1060 <mpw_str@plt>
mov    %rax,%rdi
call   1050 <mpw_strdup@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
lea    0xd41(%rip),%rsi
call   10a0 <strstr@plt>
mov    %rax,-0x40(%rbp)
cmp    $0x0,%rax
je     1312 <mpw_path+0x192>
jmp    1306 <mpw_path+0x186>
mov    -0x40(%rbp),%rax
movb   $0x5f,(%rax)
jmp    12e3 <mpw_path+0x163>
cmpq   $0x0,-0x18(%rbp)
je     1364 <mpw_path+0x1e4>
mov    -0x18(%rbp),%rsi
mov    -0x38(%rbp),%rdx
lea    0xd05(%rip),%rdi
call   1060 <mpw_str@plt>
mov    %rax,-0x48(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <free@plt>
mov    -0x38(%rbp),%rdi
call   1040 <free@plt>
cmpq   $0x0,-0x48(%rbp)
je     135f <mpw_path+0x1df>
mov    -0x48(%rbp),%rdi
call   1050 <mpw_strdup@plt>
mov    %rax,-0x38(%rbp)
jmp    1364 <mpw_path+0x1e4>
mov    -0x38(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mpw_path>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r15
lea    0xe6b(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     11b3 <mpw_path+0x33>
mov    %rax,%rdi
call   1050 <mpw_strdup@plt>
mov    %rax,%r12
test   %rax,%rax
jne    124a <mpw_path+0xca>
lea    0xe4b(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     11d4 <mpw_path+0x54>
mov    %rax,%rdi
call   1050 <mpw_strdup@plt>
mov    %rax,%r12
test   %rax,%rax
jne    124a <mpw_path+0xca>
lea    0xe36(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%rbx
lea    0xe31(%rip),%rdi
call   1030 <getenv@plt>
test   %rbx,%rbx
je     121b <mpw_path+0x9b>
test   %rax,%rax
je     121b <mpw_path+0x9b>
lea    0xe24(%rip),%rdi
mov    %rbx,%rsi
mov    %rax,%rdx
call   1060 <mpw_str@plt>
mov    %rax,%rdi
call   1050 <mpw_strdup@plt>
mov    %rax,%r12
test   %rax,%rax
jne    124a <mpw_path+0xca>
xor    %eax,%eax
call   1080 <getuid@plt>
mov    %eax,%edi
call   1070 <getpwuid@plt>
test   %rax,%rax
je     123e <mpw_path+0xbe>
mov    (%rax),%rdi
call   1050 <mpw_strdup@plt>
mov    %rax,%r12
test   %rax,%rax
jne    124a <mpw_path+0xca>
xor    %edi,%edi
xor    %esi,%esi
call   10a0 <getcwd@plt>
mov    %rax,%r12
lea    0xdd8(%rip),%rdi
mov    %r15,%rsi
mov    %r14,%rdx
call   1060 <mpw_str@plt>
mov    %rax,%rdi
call   1050 <mpw_strdup@plt>
mov    %rax,%rbx
mov    %rax,%rdi
nopw   0x0(%rax,%rax,1)
mov    $0x2f,%esi
call   1090 <strchr@plt>
test   %rax,%rax
je     1287 <mpw_path+0x107>
movb   $0x5f,(%rax)
mov    %rbx,%rdi
jmp    1270 <mpw_path+0xf0>
test   %r12,%r12
je     12c9 <mpw_path+0x149>
lea    0xd9c(%rip),%rdi
mov    %r12,%rsi
mov    %rbx,%rdx
call   1060 <mpw_str@plt>
mov    %rax,%r14
mov    %r12,%rdi
call   1040 <free@plt>
mov    %rbx,%rdi
call   1040 <free@plt>
test   %r14,%r14
je     12c9 <mpw_path+0x149>
mov    %r14,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1050 <mpw_strdup@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.passwd = type { i8* }
@.str = [5 x i8] c"HOME\00"
@.str.1 = [12 x i8] c"USERPROFILE\00"
@.str.2 = [10 x i8] c"HOMEDRIVE\00"
@.str.3 = [9 x i8] c"HOMEPATH\00"
@.str.4 = [5 x i8] c"%s%s\00"
@.str.5 = [6 x i8] c"%s.%s\00"
@.str.6 = [2 x i8] c"/\00"
@.str.7 = [13 x i8] c"%s/.mpw.d/%s\00"
define i8* @mpw_path(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca %struct.passwd*
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i8*
store i8* %0, i8** %3
store i8* %1, i8** %4
store i8* null, i8** %5
%12 = load i8*, i8** %5
%13 = icmp ne i8* %12, null
br i1 %13, label %21, label %14
14: ; preds = %2
%15 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i8* %15, i8** %5
%16 = icmp ne i8* %15, null
br i1 %16, label %17, label %20
17: ; preds = %14
%18 = load i8*, i8** %5
%19 = call i8* @mpw_strdup(i8* %18)
store i8* %19, i8** %5
br label %20
20: ; preds = %17, %14
br label %21
21: ; preds = %20, %2
%22 = load i8*, i8** %5
%23 = icmp ne i8* %22, null
br i1 %23, label %31, label %24
24: ; preds = %21
%25 = call i8* @getenv(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
store i8* %25, i8** %5
%26 = icmp ne i8* %25, null
br i1 %26, label %27, label %30
27: ; preds = %24
%28 = load i8*, i8** %5
%29 = call i8* @mpw_strdup(i8* %28)
store i8* %29, i8** %5
br label %30
30: ; preds = %27, %24
br label %31
31: ; preds = %30, %21
%32 = load i8*, i8** %5
%33 = icmp ne i8* %32, null
br i1 %33, label %48, label %34
34: ; preds = %31
%35 = call i8* @getenv(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
store i8* %35, i8** %6
%36 = call i8* @getenv(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
store i8* %36, i8** %7
%37 = load i8*, i8** %6
%38 = icmp ne i8* %37, null
br i1 %38, label %39, label %47
39: ; preds = %34
%40 = load i8*, i8** %7
%41 = icmp ne i8* %40, null
br i1 %41, label %42, label %47
42: ; preds = %39
%43 = load i8*, i8** %6
%44 = load i8*, i8** %7
%45 = call i8* @mpw_str(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i8* %43, i8* %44)
%46 = call i8* @mpw_strdup(i8* %45)
store i8* %46, i8** %5
br label %47
47: ; preds = %42, %39, %34
br label %48
48: ; preds = %47, %31
%49 = load i8*, i8** %5
%50 = icmp ne i8* %49, null
br i1 %50, label %62, label %51
51: ; preds = %48
%52 = call i32 (...) @getuid()
%53 = call %struct.passwd* @getpwuid(i32 %52)
store %struct.passwd* %53, %struct.passwd** %8
%54 = load %struct.passwd*, %struct.passwd** %8
%55 = icmp ne %struct.passwd* %54, null
br i1 %55, label %56, label %61
56: ; preds = %51
%57 = load %struct.passwd*, %struct.passwd** %8
%58 = getelementptr inbounds %struct.passwd, %struct.passwd* %57, i32 0, i32 0
%59 = load i8*, i8** %58
%60 = call i8* @mpw_strdup(i8* %59)
store i8* %60, i8** %5
br label %61
61: ; preds = %56, %51
br label %62
62: ; preds = %61, %48
%63 = load i8*, i8** %5
%64 = icmp ne i8* %63, null
br i1 %64, label %67, label %65
65: ; preds = %62
%66 = call i8* @getcwd(i32* null, i32 0)
store i8* %66, i8** %5
br label %67
67: ; preds = %65, %62
%68 = load i8*, i8** %3
%69 = load i8*, i8** %4
%70 = call i8* @mpw_str(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0), i8* %68, i8* %69)
%71 = call i8* @mpw_strdup(i8* %70)
store i8* %71, i8** %9
br label %72
72: ; preds = %77, %67
%73 = load i8*, i8** %9
%74 = call i8* @strstr(i8* %73, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0))
store i8* %74, i8** %10
%75 = icmp ne i8* %74, null
br i1 %75, label %76, label %79
76: ; preds = %72
br label %77
77: ; preds = %76
%78 = load i8*, i8** %10
store i8 95, i8* %78
br label %72
79: ; preds = %72
%80 = load i8*, i8** %5
%81 = icmp ne i8* %80, null
br i1 %81, label %82, label %96
82: ; preds = %79
%83 = load i8*, i8** %5
%84 = load i8*, i8** %9
%85 = call i8* @mpw_str(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.7, i64 0, i64 0), i8* %83, i8* %84)
store i8* %85, i8** %11
%86 = load i8*, i8** %5
%87 = call i32 @free(i8* %86)
%88 = load i8*, i8** %9
%89 = call i32 @free(i8* %88)
%90 = load i8*, i8** %11
%91 = icmp ne i8* %90, null
br i1 %91, label %92, label %95
92: ; preds = %82
%93 = load i8*, i8** %11
%94 = call i8* @mpw_strdup(i8* %93)
store i8* %94, i8** %9
br label %95
95: ; preds = %92, %82
br label %96
96: ; preds = %95, %79
%97 = load i8*, i8** %9
ret i8* %97
}

LLVM IR (-O2):
%struct.passwd = type { i8* }
@.str = [5 x i8] c"HOME\00"
@.str.1 = [12 x i8] c"USERPROFILE\00"
@.str.2 = [10 x i8] c"HOMEDRIVE\00"
@.str.3 = [9 x i8] c"HOMEPATH\00"
@.str.4 = [5 x i8] c"%s%s\00"
@.str.5 = [6 x i8] c"%s.%s\00"
@.str.7 = [13 x i8] c"%s/.mpw.d/%s\00"
define i8* @mpw_path(i8* %0, i8* %1) {
%3 = tail call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i8* %3, null
br i1 %4, label %8, label %5
5: ; preds = %2
%6 = tail call i8* @mpw_strdup(i8* %3)
%7 = icmp eq i8* %6, null
br i1 %7, label %8, label %35
8: ; preds = %2, %5
%9 = tail call i8* @getenv(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%10 = icmp eq i8* %9, null
br i1 %10, label %14, label %11
11: ; preds = %8
%12 = tail call i8* @mpw_strdup(i8* %9)
%13 = icmp eq i8* %12, null
br i1 %13, label %14, label %35
14: ; preds = %8, %11
%15 = tail call i8* @getenv(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
%16 = tail call i8* @getenv(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
%17 = icmp ne i8* %15, null
%18 = icmp ne i8* %16, null
%19 = and i1 %17, %18
br i1 %19, label %20, label %24
20: ; preds = %14
%21 = tail call i8* @mpw_str(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i8* %15, i8* %16)
%22 = tail call i8* @mpw_strdup(i8* %21)
%23 = icmp eq i8* %22, null
br i1 %23, label %24, label %35
24: ; preds = %14, %20
%25 = tail call i32 (...) @getuid()
%26 = tail call %struct.passwd* @getpwuid(i32 %25)
%27 = icmp eq %struct.passwd* %26, null
br i1 %27, label %33, label %28
28: ; preds = %24
%29 = getelementptr inbounds %struct.passwd, %struct.passwd* %26, i64 0, i32 0
%30 = load i8*, i8** %29
%31 = tail call i8* @mpw_strdup(i8* %30)
%32 = icmp eq i8* %31, null
br i1 %32, label %33, label %35
33: ; preds = %24, %28
%34 = tail call i8* @getcwd(i32* null, i32 0)
br label %35
35: ; preds = %5, %11, %20, %33, %28
%36 = phi i8* [ %31, %28 ], [ %34, %33 ], [ %22, %20 ], [ %12, %11 ], [ %6, %5 ]
%37 = tail call i8* @mpw_str(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0), i8* %0, i8* %1)
%38 = tail call i8* @mpw_strdup(i8* %37)
%39 = tail call i8* @strchr(i8* dereferenceable(1) %38, i32 47)
%40 = icmp eq i8* %39, null
br i1 %40, label %41, label %43
41: ; preds = %43, %35
%42 = icmp eq i8* %36, null
br i1 %42, label %54, label %47
43: ; preds = %35, %43
%44 = phi i8* [ %45, %43 ], [ %39, %35 ]
store i8 95, i8* %44
%45 = tail call i8* @strchr(i8* dereferenceable(1) %38, i32 47)
%46 = icmp eq i8* %45, null
br i1 %46, label %41, label %43
47: ; preds = %41
%48 = tail call i8* @mpw_str(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.7, i64 0, i64 0), i8* %36, i8* %38)
%49 = tail call i32 @free(i8* %36)
%50 = tail call i32 @free(i8* %38)
%51 = icmp eq i8* %48, null
br i1 %51, label %54, label %52
52: ; preds = %47
%53 = tail call i8* @mpw_strdup(i8* %48)
br label %54
54: ; preds = %47, %52, %41
%55 = phi i8* [ %38, %41 ], [ %53, %52 ], [ %38, %47 ]
ret i8* %55
}