#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 MAXPATHLEN;
int /*<<< orphan*/ am_free(char *);
char *am_strdup(char const *);
char *get_home_folder();
int /*<<< orphan*/ strcat(char *, char const *);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char const *);
char *resolve_path(const char *path) {
  char new_dir[MAXPATHLEN];
  char *homedir = NULL;
  if (path && strlen(path) > 2) {
    if (path[0] == '~' && path[1] == '/') {
      homedir = get_home_folder();
      if (homedir) {
        strcpy(new_dir, homedir);
        strcat(new_dir, ++path);
        am_free(homedir);
        return am_strdup(new_dir);
      }
    }
    return am_strdup(path);
  }
  return NULL;
}


x86 assembly (clang, -O0):
<resolve_path>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e75(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x18(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x38(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1256 <resolve_path+0xf6>
mov    -0x10(%rbp),%rdi
call   1060 <strlen@plt>
cmp    $0x2,%eax
jle    1256 <resolve_path+0xf6>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x7e,%eax
jne    123d <resolve_path+0xdd>
mov    -0x10(%rbp),%rax
movsbl 0x1(%rax),%eax
cmp    $0x2f,%eax
jne    123d <resolve_path+0xdd>
mov    $0x0,%al
call   1030 <get_home_folder@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     1238 <resolve_path+0xd8>
mov    -0x38(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x38(%rbp),%rdi
mov    -0x10(%rbp),%rsi
add    $0x1,%rsi
mov    %rsi,-0x10(%rbp)
call   1080 <strcat@plt>
mov    -0x28(%rbp),%rdi
call   1070 <am_free@plt>
mov    -0x38(%rbp),%rdi
call   1050 <am_strdup@plt>
mov    %rax,-0x8(%rbp)
movl   $0x1,-0x2c(%rbp)
jmp    1265 <resolve_path+0x105>
jmp    123d <resolve_path+0xdd>
mov    -0x10(%rbp),%rdi
call   1050 <am_strdup@plt>
mov    %rax,-0x8(%rbp)
movl   $0x1,-0x2c(%rbp)
jmp    1265 <resolve_path+0x105>
movq   $0x0,-0x8(%rbp)
movl   $0x1,-0x2c(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,%rsp
mov    -0x8(%rbp),%rax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<resolve_path>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    0x2e77(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
test   %rdi,%rdi
je     11de <resolve_path+0x7e>
mov    %rdi,%r14
call   1060 <strlen@plt>
cmp    $0x3,%eax
jl     11de <resolve_path+0x7e>
cmpb   $0x7e,(%r14)
jne    11eb <resolve_path+0x8b>
cmpb   $0x2f,0x1(%r14)
jne    11eb <resolve_path+0x8b>
xor    %eax,%eax
call   1030 <get_home_folder@plt>
test   %rax,%rax
je     11eb <resolve_path+0x8b>
mov    %rax,%r15
add    $0x1,%r14
mov    %rbx,%rdi
mov    %rax,%rsi
call   1040 <strcpy@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1080 <strcat@plt>
mov    %r15,%rdi
call   1070 <am_free@plt>
mov    %rbx,%rdi
call   1050 <am_strdup@plt>
jmp    11e0 <resolve_path+0x80>
xor    %eax,%eax
lea    -0x18(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r14,%rdi
lea    -0x18(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <am_strdup@plt>

LLVM IR (-O0):
@MAXPATHLEN = global i32 0
define i8* @resolve_path(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i32
store i8* %0, i8** %3
%8 = load i32, i32* @MAXPATHLEN
%9 = zext i32 %8 to i64
%10 = call i8* @llvm.stacksave()
store i8* %10, i8** %4
%11 = alloca i8, i64 %9
store i64 %9, i64* %5
store i8* null, i8** %6
%12 = load i8*, i8** %3
%13 = icmp ne i8* %12, null
br i1 %13, label %14, label %47
14: ; preds = %1
%15 = load i8*, i8** %3
%16 = call i32 @strlen(i8* %15)
%17 = icmp sgt i32 %16, 2
br i1 %17, label %18, label %47
18: ; preds = %14
%19 = load i8*, i8** %3
%20 = getelementptr inbounds i8, i8* %19, i64 0
%21 = load i8, i8* %20
%22 = sext i8 %21 to i32
%23 = icmp eq i32 %22, 126
br i1 %23, label %24, label %44
24: ; preds = %18
%25 = load i8*, i8** %3
%26 = getelementptr inbounds i8, i8* %25, i64 1
%27 = load i8, i8* %26
%28 = sext i8 %27 to i32
%29 = icmp eq i32 %28, 47
br i1 %29, label %30, label %44
30: ; preds = %24
%31 = call i8* (...) @get_home_folder()
store i8* %31, i8** %6
%32 = load i8*, i8** %6
%33 = icmp ne i8* %32, null
br i1 %33, label %34, label %43
34: ; preds = %30
%35 = load i8*, i8** %6
%36 = call i32 @strcpy(i8* %11, i8* %35)
%37 = load i8*, i8** %3
%38 = getelementptr inbounds i8, i8* %37, i32 1
store i8* %38, i8** %3
%39 = call i32 @strcat(i8* %11, i8* %38)
%40 = load i8*, i8** %6
%41 = call i32 @am_free(i8* %40)
%42 = call i8* @am_strdup(i8* %11)
store i8* %42, i8** %2
store i32 1, i32* %7
br label %48
43: ; preds = %30
br label %44
44: ; preds = %43, %24, %18
%45 = load i8*, i8** %3
%46 = call i8* @am_strdup(i8* %45)
store i8* %46, i8** %2
store i32 1, i32* %7
br label %48
47: ; preds = %14, %1
store i8* null, i8** %2
store i32 1, i32* %7
br label %48
48: ; preds = %47, %44, %34
%49 = load i8*, i8** %4
%50 = load i8*, i8** %2
ret i8* %50
}

LLVM IR (-O2):
@MAXPATHLEN = global i32 0
define i8* @resolve_path(i8* %0) {
%2 = load i32, i32* @MAXPATHLEN
%3 = zext i32 %2 to i64
%4 = alloca i8, i64 %3
%5 = icmp eq i8* %0, null
br i1 %5, label %26, label %6
6: ; preds = %1
%7 = tail call i32 @strlen(i8* %0)
%8 = icmp sgt i32 %7, 2
br i1 %8, label %9, label %26
9: ; preds = %6
%10 = load i8, i8* %0
%11 = icmp eq i8 %10, 126
br i1 %11, label %12, label %24
12: ; preds = %9
%13 = getelementptr inbounds i8, i8* %0, i64 1
%14 = load i8, i8* %13
%15 = icmp eq i8 %14, 47
br i1 %15, label %16, label %24
16: ; preds = %12
%17 = tail call i8* (...) @get_home_folder()
%18 = icmp eq i8* %17, null
br i1 %18, label %24, label %19
19: ; preds = %16
%20 = call i32 @strcpy(i8* %4, i8* %17)
%21 = call i32 @strcat(i8* %4, i8* %13)
%22 = call i32 @am_free(i8* %17)
%23 = call i8* @am_strdup(i8* %4)
br label %26
24: ; preds = %16, %12, %9
%25 = tail call i8* @am_strdup(i8* %0)
br label %26
26: ; preds = %1, %6, %24, %19
%27 = phi i8* [ %23, %19 ], [ %25, %24 ], [ null, %6 ], [ null, %1 ]
ret i8* %27
}