#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 */
char *getenv(char *);
char *safeRealloc(char *, int);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
scalar_t__ strncmp(char *, char *, int);
int /*<<< orphan*/ strncpy(char *, char *, int);
char *strrchr(char *, char);
char *utilExecutablePath();
char *utilExpandPath(char *ipath) {
  static char *opath = 0;
  static int opathSize = 0;
  char *prefix = 0;
  int prefixLength = 0;
  int length;
  if (strncmp(ipath, "~/", 2) == 0) {
    prefix = getenv("HOME");
    if (prefix)
      prefixLength = strlen(prefix);
  } else if (strncmp(ipath, "%/", 2) == 0) {
    prefix = utilExecutablePath();
    if (prefix)
      prefixLength = strrchr(prefix, '/') - prefix;
  }
  if (!prefix)
    return ipath;
  length = prefixLength + strlen(ipath);
  if (opathSize < length) {
    opathSize = length;
    opath = safeRealloc(opath, opathSize * sizeof(char));
  }
  strncpy(opath, prefix, prefixLength);
  opath[prefixLength] = '/';
  strcpy(opath + prefixLength + 1, ipath + 2);
  return opath;
}


x86 assembly (clang, -O0):
<utilExpandPath>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe5a(%rip),%rsi
mov    $0x2,%edx
call   1050 <strncmp@plt>
cmp    $0x0,%rax
jne    11e6 <utilExpandPath+0x66>
lea    0xe42(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11e1 <utilExpandPath+0x61>
mov    -0x18(%rbp),%rdi
call   1080 <strlen@plt>
mov    %eax,-0x1c(%rbp)
jmp    123d <utilExpandPath+0xbd>
mov    -0x10(%rbp),%rdi
lea    0xe17(%rip),%rsi
mov    $0x2,%edx
call   1050 <strncmp@plt>
cmp    $0x0,%rax
jne    1238 <utilExpandPath+0xb8>
mov    $0x0,%al
call   10a0 <utilExecutablePath@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1233 <utilExpandPath+0xb3>
mov    -0x18(%rbp),%rdi
mov    $0x2f,%esi
call   1090 <strrchr@plt>
mov    -0x18(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,-0x1c(%rbp)
jmp    1238 <utilExpandPath+0xb8>
jmp    123d <utilExpandPath+0xbd>
cmpq   $0x0,-0x18(%rbp)
jne    1255 <utilExpandPath+0xd5>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12f2 <utilExpandPath+0x172>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
call   1080 <strlen@plt>
mov    %eax,%ecx
mov    -0x24(%rbp),%eax
add    %ecx,%eax
mov    %eax,-0x20(%rbp)
mov    0x2dfc(%rip),%eax
cmp    -0x20(%rbp),%eax
jge    12a6 <utilExpandPath+0x126>
mov    -0x20(%rbp),%eax
mov    %eax,0x2dea(%rip)
mov    0x2ddb(%rip),%rdi
movslq 0x2ddc(%rip),%rax
shl    $0x0,%rax
mov    %eax,%esi
call   1070 <safeRealloc@plt>
mov    %rax,0x2dc2(%rip)
mov    0x2dbb(%rip),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1040 <strncpy@plt>
mov    0x2da8(%rip),%rax
movslq -0x1c(%rbp),%rcx
movb   $0x2f,(%rax,%rcx,1)
mov    0x2d99(%rip),%rdi
movslq -0x1c(%rbp),%rax
add    %rax,%rdi
add    $0x1,%rdi
mov    -0x10(%rbp),%rsi
add    $0x2,%rsi
call   1060 <strcpy@plt>
mov    0x2d7a(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<utilExpandPath>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%rbx
lea    0xe71(%rip),%rsi
mov    $0x2,%edx
call   1050 <strncmp@plt>
test   %rax,%rax
je     11e3 <utilExpandPath+0x63>
lea    0xe63(%rip),%rsi
mov    %rbx,%rdi
mov    $0x2,%edx
call   1050 <strncmp@plt>
test   %rax,%rax
jne    1270 <utilExpandPath+0xf0>
xor    %eax,%eax
call   10a0 <utilExecutablePath@plt>
test   %rax,%rax
je     1270 <utilExpandPath+0xf0>
mov    %rax,%r14
mov    %rax,%rdi
mov    $0x2f,%esi
call   1090 <strrchr@plt>
mov    %rax,%r15
sub    %r14d,%r15d
jmp    1202 <utilExpandPath+0x82>
lea    0xe19(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     1270 <utilExpandPath+0xf0>
mov    %rax,%r14
mov    %rax,%rdi
call   1080 <strlen@plt>
mov    %eax,%r15d
mov    %rbx,%rdi
call   1080 <strlen@plt>
add    %r15d,%eax
cmp    %eax,0x2e5d(%rip)
jge    1235 <utilExpandPath+0xb5>
mov    %eax,0x2e55(%rip)
mov    0x2e46(%rip),%rdi
mov    %eax,%esi
call   1070 <safeRealloc@plt>
mov    %rax,%rdi
mov    %rax,0x2e35(%rip)
jmp    123c <utilExpandPath+0xbc>
mov    0x2e2c(%rip),%rdi
mov    %r14,%rsi
mov    %r15d,%edx
call   1040 <strncpy@plt>
mov    0x2e1a(%rip),%rax
movslq %r15d,%rcx
movb   $0x2f,(%rax,%rcx,1)
lea    (%rax,%rcx,1),%rdi
add    $0x1,%rdi
add    $0x2,%rbx
mov    %rbx,%rsi
call   1060 <strcpy@plt>
mov    0x2df8(%rip),%rbx
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@utilExpandPath.opath = internal global i8* null
@utilExpandPath.opathSize = internal global i32 0
@.str = [3 x i8] c"~/\00"
@.str.1 = [5 x i8] c"HOME\00"
@.str.2 = [3 x i8] c"%/\00"
define i8* @utilExpandPath(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %3
store i8* null, i8** %4
store i32 0, i32* %5
%7 = load i8*, i8** %3
%8 = call i64 @strncmp(i8* %7, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 2)
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %18
10: ; preds = %1
%11 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
store i8* %11, i8** %4
%12 = load i8*, i8** %4
%13 = icmp ne i8* %12, null
br i1 %13, label %14, label %17
14: ; preds = %10
%15 = load i8*, i8** %4
%16 = call i32 @strlen(i8* %15)
store i32 %16, i32* %5
br label %17
17: ; preds = %14, %10
br label %36
18: ; preds = %1
%19 = load i8*, i8** %3
%20 = call i64 @strncmp(i8* %19, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 2)
%21 = icmp eq i64 %20, 0
br i1 %21, label %22, label %35
22: ; preds = %18
%23 = call i8* (...) @utilExecutablePath()
store i8* %23, i8** %4
%24 = load i8*, i8** %4
%25 = icmp ne i8* %24, null
br i1 %25, label %26, label %34
26: ; preds = %22
%27 = load i8*, i8** %4
%28 = call i8* @strrchr(i8* %27, i8 signext 47)
%29 = load i8*, i8** %4
%30 = ptrtoint i8* %28 to i64
%31 = ptrtoint i8* %29 to i64
%32 = sub i64 %30, %31
%33 = trunc i64 %32 to i32
store i32 %33, i32* %5
br label %34
34: ; preds = %26, %22
br label %35
35: ; preds = %34, %18
br label %36
36: ; preds = %35, %17
%37 = load i8*, i8** %4
%38 = icmp ne i8* %37, null
br i1 %38, label %41, label %39
39: ; preds = %36
%40 = load i8*, i8** %3
store i8* %40, i8** %2
br label %75
41: ; preds = %36
%42 = load i32, i32* %5
%43 = load i8*, i8** %3
%44 = call i32 @strlen(i8* %43)
%45 = add nsw i32 %42, %44
store i32 %45, i32* %6
%46 = load i32, i32* @utilExpandPath.opathSize
%47 = load i32, i32* %6
%48 = icmp slt i32 %46, %47
br i1 %48, label %49, label %57
49: ; preds = %41
%50 = load i32, i32* %6
store i32 %50, i32* @utilExpandPath.opathSize
%51 = load i8*, i8** @utilExpandPath.opath
%52 = load i32, i32* @utilExpandPath.opathSize
%53 = sext i32 %52 to i64
%54 = mul i64 %53, 1
%55 = trunc i64 %54 to i32
%56 = call i8* @safeRealloc(i8* %51, i32 %55)
store i8* %56, i8** @utilExpandPath.opath
br label %57
57: ; preds = %49, %41
%58 = load i8*, i8** @utilExpandPath.opath
%59 = load i8*, i8** %4
%60 = load i32, i32* %5
%61 = call i32 @strncpy(i8* %58, i8* %59, i32 %60)
%62 = load i8*, i8** @utilExpandPath.opath
%63 = load i32, i32* %5
%64 = sext i32 %63 to i64
%65 = getelementptr inbounds i8, i8* %62, i64 %64
store i8 47, i8* %65
%66 = load i8*, i8** @utilExpandPath.opath
%67 = load i32, i32* %5
%68 = sext i32 %67 to i64
%69 = getelementptr inbounds i8, i8* %66, i64 %68
%70 = getelementptr inbounds i8, i8* %69, i64 1
%71 = load i8*, i8** %3
%72 = getelementptr inbounds i8, i8* %71, i64 2
%73 = call i32 @strcpy(i8* %70, i8* %72)
%74 = load i8*, i8** @utilExpandPath.opath
store i8* %74, i8** %2
br label %75
75: ; preds = %57, %39
%76 = load i8*, i8** %2
ret i8* %76
}

LLVM IR (-O2):
@utilExpandPath.opath = internal unnamed_addr global i8* null
@utilExpandPath.opathSize = internal unnamed_addr global i32 0
@.str = [3 x i8] c"~/\00"
@.str.1 = [5 x i8] c"HOME\00"
@.str.2 = [3 x i8] c"%/\00"
define i8* @utilExpandPath(i8* %0) {
%2 = tail call i64 @strncmp(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 2)
%3 = icmp eq i64 %2, 0
br i1 %3, label %4, label %9
4: ; preds = %1
%5 = tail call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%6 = icmp eq i8* %5, null
br i1 %6, label %43, label %7
7: ; preds = %4
%8 = tail call i32 @strlen(i8* %5)
br label %21
9: ; preds = %1
%10 = tail call i64 @strncmp(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 2)
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %43
12: ; preds = %9
%13 = tail call i8* (...) @utilExecutablePath()
%14 = icmp eq i8* %13, null
br i1 %14, label %43, label %15
15: ; preds = %12
%16 = tail call i8* @strrchr(i8* %13, i8 signext 47)
%17 = ptrtoint i8* %16 to i64
%18 = ptrtoint i8* %13 to i64
%19 = sub i64 %17, %18
%20 = trunc i64 %19 to i32
br label %21
21: ; preds = %7, %15
%22 = phi i8* [ %5, %7 ], [ %13, %15 ]
%23 = phi i32 [ %8, %7 ], [ %20, %15 ]
%24 = tail call i32 @strlen(i8* %0)
%25 = add nsw i32 %24, %23
%26 = load i32, i32* @utilExpandPath.opathSize
%27 = icmp slt i32 %26, %25
br i1 %27, label %30, label %28
28: ; preds = %21
%29 = load i8*, i8** @utilExpandPath.opath
br label %33
30: ; preds = %21
store i32 %25, i32* @utilExpandPath.opathSize
%31 = load i8*, i8** @utilExpandPath.opath
%32 = tail call i8* @safeRealloc(i8* %31, i32 %25)
store i8* %32, i8** @utilExpandPath.opath
br label %33
33: ; preds = %28, %30
%34 = phi i8* [ %29, %28 ], [ %32, %30 ]
%35 = tail call i32 @strncpy(i8* %34, i8* %22, i32 %23)
%36 = load i8*, i8** @utilExpandPath.opath
%37 = sext i32 %23 to i64
%38 = getelementptr inbounds i8, i8* %36, i64 %37
store i8 47, i8* %38
%39 = getelementptr inbounds i8, i8* %38, i64 1
%40 = getelementptr inbounds i8, i8* %0, i64 2
%41 = tail call i32 @strcpy(i8* %39, i8* %40)
%42 = load i8*, i8** @utilExpandPath.opath
br label %43
43: ; preds = %9, %12, %4, %33
%44 = phi i8* [ %42, %33 ], [ %0, %4 ], [ %0, %12 ], [ %0, %9 ]
ret i8* %44
}