#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 *home_dir(char *, int);
int /*<<< orphan*/ perish(char *);
int /*<<< orphan*/ strcat(char *, char *);
char *strchr(char *, char);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
char *expand_path(char *path, char *expanded) {
  if (!path || *path != '~') {
    return path;
  } else {
    char *next = path + 1;
    if (*next == '/') {
      strcpy(expanded, getenv("HOME"));
      strcat(expanded, next);
    } else {
      char *slash = strchr(next, '/');
      if (!slash) {
        slash = next + strlen(next);
      }
      char *home = home_dir(next, slash - next);
      if (!home) {
        perish(path);
      } else {
        strcpy(expanded, home);
        strcat(expanded, slash);
      }
    }
  }
  return expanded;
}


x86 assembly (clang, -O0):
<expand_path>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     119b <expand_path+0x2b>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x7e,%eax
je     11a8 <expand_path+0x38>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1293 <expand_path+0x123>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
jne    11f6 <expand_path+0x86>
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
lea    0xe2d(%rip),%rdi
call   1030 <getenv@plt>
mov    -0x38(%rbp),%rdi
mov    %rax,%rsi
call   1040 <strcpy@plt>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1090 <strcat@plt>
jmp    1286 <expand_path+0x116>
mov    -0x20(%rbp),%rdi
mov    $0x2f,%esi
call   1070 <strchr@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1234 <expand_path+0xc4>
mov    -0x20(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x20(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    -0x40(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rax
mov    -0x20(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%esi
call   1060 <home_dir@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1267 <expand_path+0xf7>
mov    -0x10(%rbp),%rdi
call   1080 <perish@plt>
jmp    1281 <expand_path+0x111>
mov    -0x18(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x18(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1090 <strcat@plt>
jmp    1286 <expand_path+0x116>
jmp    128b <expand_path+0x11b>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<expand_path>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
test   %rdi,%rdi
je     11b1 <expand_path+0x41>
mov    %rdi,%rbx
cmpb   $0x7e,(%rdi)
jne    11b6 <expand_path+0x46>
mov    %rsi,%r14
lea    0x1(%rbx),%r15
cmpb   $0x2f,0x1(%rbx)
jne    11bb <expand_path+0x4b>
lea    0xe67(%rip),%rdi
call   1030 <getenv@plt>
mov    %r14,%rdi
mov    %rax,%rsi
call   1040 <strcpy@plt>
mov    %r14,%rdi
mov    %r15,%rsi
jmp    1202 <expand_path+0x92>
xor    %r14d,%r14d
jmp    1207 <expand_path+0x97>
mov    %rbx,%r14
jmp    1207 <expand_path+0x97>
mov    %r15,%rdi
mov    $0x2f,%esi
call   1070 <strchr@plt>
mov    %rax,%r12
test   %rax,%rax
jne    11de <expand_path+0x6e>
mov    %r15,%rdi
call   1050 <strlen@plt>
movslq %eax,%r12
add    %r15,%r12
mov    %r12d,%esi
sub    %r15d,%esi
mov    %r15,%rdi
call   1060 <home_dir@plt>
test   %rax,%rax
je     1216 <expand_path+0xa6>
mov    %r14,%rdi
mov    %rax,%rsi
call   1040 <strcpy@plt>
mov    %r14,%rdi
mov    %r12,%rsi
call   1090 <strcat@plt>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    %rbx,%rdi
call   1080 <perish@plt>
jmp    1207 <expand_path+0x97>

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

LLVM IR (-O2):
@.str = [5 x i8] c"HOME\00"
define i8* @expand_path(i8* %0, i8* %1) {
%3 = icmp eq i8* %0, null
br i1 %3, label %35, label %4
4: ; preds = %2
%5 = load i8, i8* %0
%6 = icmp eq i8 %5, 126
br i1 %6, label %7, label %35
7: ; preds = %4
%8 = getelementptr inbounds i8, i8* %0, i64 1
%9 = load i8, i8* %8
%10 = icmp eq i8 %9, 47
br i1 %10, label %11, label %15
11: ; preds = %7
%12 = tail call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%13 = tail call i32 @strcpy(i8* %1, i8* %12)
%14 = tail call i32 @strcat(i8* %1, i8* %8)
br label %35
15: ; preds = %7
%16 = tail call i8* @strchr(i8* %8, i8 signext 47)
%17 = icmp eq i8* %16, null
br i1 %17, label %18, label %22
18: ; preds = %15
%19 = tail call i32 @strlen(i8* %8)
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds i8, i8* %8, i64 %20
br label %22
22: ; preds = %18, %15
%23 = phi i8* [ %16, %15 ], [ %21, %18 ]
%24 = ptrtoint i8* %23 to i64
%25 = ptrtoint i8* %8 to i64
%26 = sub i64 %24, %25
%27 = trunc i64 %26 to i32
%28 = tail call i8* @home_dir(i8* %8, i32 %27)
%29 = icmp eq i8* %28, null
br i1 %29, label %30, label %32
30: ; preds = %22
%31 = tail call i32 @perish(i8* %0)
br label %35
32: ; preds = %22
%33 = tail call i32 @strcpy(i8* %1, i8* %28)
%34 = tail call i32 @strcat(i8* %1, i8* %23)
br label %35
35: ; preds = %11, %32, %30, %2, %4
%36 = phi i8* [ %0, %4 ], [ null, %2 ], [ %1, %30 ], [ %1, %32 ], [ %1, %11 ]
ret i8* %36
}