#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 */
typedef int /*<<< orphan*/ path;
typedef int /*<<< orphan*/ cwd;

/* Variables and functions */
int /*<<< orphan*/ canonicalize_path(char *, int, char const *);
int /*<<< orphan*/ getcwd(char *, int);
char *getenv(char *);
int /*<<< orphan*/ is_abs_path(char const *);
int /*<<< orphan*/ makepath(char *, int, char *, char const *);
int /*<<< orphan*/ qe_pstrcat(char *, int, char const *);
int /*<<< orphan*/ qe_pstrcpy(char *, int, char *);
int /*<<< orphan*/ remove_slash(char *);
void canonicalize_absolute_path(char *buf, int buf_size, const char *path1) {
  char cwd[1024];
  char path[1024];
  char *homedir;
  if (!is_abs_path(path1)) {
    if (*path1 == '~') {
      if (path1[1] == '\0' || path1[1] == '/') {
        homedir = getenv("HOME");
        if (homedir) {
          qe_pstrcpy(path, sizeof(path), homedir);
          remove_slash(path);
          qe_pstrcat(path, sizeof(path), path1 + 1);
          path1 = path;
        }
      } else {
        qe_pstrcpy(path, sizeof(path), "/home/");
        qe_pstrcat(path, sizeof(path), path1 + 1);
        path1 = path;
      }
    } else {
      getcwd(cwd, sizeof(cwd));
      makepath(path, sizeof(path), cwd, path1);
      path1 = path;
    }
  }
  canonicalize_path(buf, buf_size, path1);
}


x86 assembly (clang, -O0):
<canonicalize_absolute_path>:
push   %rbp
mov    %rsp,%rbp
sub    $0x830,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1080 <is_abs_path@plt>
cmp    $0x0,%eax
jne    12c6 <canonicalize_absolute_path+0x146>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x7e,%eax
jne    1289 <canonicalize_absolute_path+0x109>
mov    -0x18(%rbp),%rax
movsbl 0x1(%rax),%eax
cmp    $0x0,%eax
je     11da <canonicalize_absolute_path+0x5a>
mov    -0x18(%rbp),%rax
movsbl 0x1(%rax),%eax
cmp    $0x2f,%eax
jne    1248 <canonicalize_absolute_path+0xc8>
lea    0xe1f(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x828(%rbp)
cmpq   $0x0,-0x828(%rbp)
je     1243 <canonicalize_absolute_path+0xc3>
lea    -0x820(%rbp),%rdi
mov    -0x828(%rbp),%rdx
mov    $0x400,%esi
call   1070 <qe_pstrcpy@plt>
lea    -0x820(%rbp),%rdi
call   1050 <remove_slash@plt>
lea    -0x820(%rbp),%rdi
mov    -0x18(%rbp),%rdx
add    $0x1,%rdx
mov    $0x400,%esi
call   1040 <qe_pstrcat@plt>
lea    -0x820(%rbp),%rax
mov    %rax,-0x18(%rbp)
jmp    1284 <canonicalize_absolute_path+0x104>
lea    -0x820(%rbp),%rdi
mov    $0x400,%esi
lea    0xdaa(%rip),%rdx
call   1070 <qe_pstrcpy@plt>
lea    -0x820(%rbp),%rdi
mov    -0x18(%rbp),%rdx
add    $0x1,%rdx
mov    $0x400,%esi
call   1040 <qe_pstrcat@plt>
lea    -0x820(%rbp),%rax
mov    %rax,-0x18(%rbp)
jmp    12c1 <canonicalize_absolute_path+0x141>
lea    -0x420(%rbp),%rdi
mov    $0x400,%esi
call   1060 <getcwd@plt>
lea    -0x820(%rbp),%rdi
lea    -0x420(%rbp),%rdx
mov    -0x18(%rbp),%rcx
mov    $0x400,%esi
call   10a0 <makepath@plt>
lea    -0x820(%rbp),%rax
mov    %rax,-0x18(%rbp)
jmp    12c6 <canonicalize_absolute_path+0x146>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x18(%rbp),%rdx
call   1090 <canonicalize_path@plt>
add    $0x830,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<canonicalize_absolute_path>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x800,%rsp
mov    %rdx,%rbp
mov    %esi,%r14d
mov    %rdi,%rbx
mov    %rdx,%rdi
call   1080 <is_abs_path@plt>
test   %eax,%eax
jne    1242 <canonicalize_absolute_path+0xc2>
cmpb   $0x7e,0x0(%rbp)
jne    11eb <canonicalize_absolute_path+0x6b>
lea    0x1(%rbp),%r15
mov    0x1(%rbp),%al
cmp    $0x2f,%al
je     11bd <canonicalize_absolute_path+0x3d>
test   %al,%al
jne    121b <canonicalize_absolute_path+0x9b>
lea    0xe3c(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     1242 <canonicalize_absolute_path+0xc2>
mov    %rsp,%rbp
mov    %rbp,%rdi
mov    $0x400,%esi
mov    %rax,%rdx
call   1070 <qe_pstrcpy@plt>
mov    %rbp,%rdi
call   1050 <remove_slash@plt>
jmp    1232 <canonicalize_absolute_path+0xb2>
mov    %rsp,%r15
lea    0x400(%rsp),%r12
mov    %r12,%rdi
mov    $0x400,%esi
call   1060 <getcwd@plt>
mov    %r15,%rdi
mov    $0x400,%esi
mov    %r12,%rdx
mov    %rbp,%rcx
call   10a0 <makepath@plt>
mov    %r15,%rbp
jmp    1242 <canonicalize_absolute_path+0xc2>
mov    %rsp,%rbp
lea    0xde0(%rip),%rdx
mov    %rbp,%rdi
mov    $0x400,%esi
call   1070 <qe_pstrcpy@plt>
mov    %rbp,%rdi
mov    $0x400,%esi
mov    %r15,%rdx
call   1040 <qe_pstrcat@plt>
mov    %rbx,%rdi
mov    %r14d,%esi
mov    %rbp,%rdx
call   1090 <canonicalize_path@plt>
add    $0x800,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [5 x i8] c"HOME\00"
@.str.1 = [7 x i8] c"/home/\00"
define void @canonicalize_absolute_path(i8* %0, i32 %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca [1024 x i8]
%8 = alloca [1024 x i8]
%9 = alloca i8*
store i8* %0, i8** %4
store i32 %1, i32* %5
store i8* %2, i8** %6
%10 = load i8*, i8** %6
%11 = call i32 @is_abs_path(i8* %10)
%12 = icmp ne i32 %11, 0
br i1 %12, label %64, label %13
13: ; preds = %3
%14 = load i8*, i8** %6
%15 = load i8, i8* %14
%16 = sext i8 %15 to i32
%17 = icmp eq i32 %16, 126
br i1 %17, label %18, label %55
18: ; preds = %13
%19 = load i8*, i8** %6
%20 = getelementptr inbounds i8, i8* %19, i64 1
%21 = load i8, i8* %20
%22 = sext i8 %21 to i32
%23 = icmp eq i32 %22, 0
br i1 %23, label %30, label %24
24: ; preds = %18
%25 = load i8*, i8** %6
%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 %46
30: ; preds = %24, %18
%31 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i8* %31, i8** %9
%32 = load i8*, i8** %9
%33 = icmp ne i8* %32, null
br i1 %33, label %34, label %45
34: ; preds = %30
%35 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%36 = load i8*, i8** %9
%37 = call i32 @qe_pstrcpy(i8* %35, i32 1024, i8* %36)
%38 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%39 = call i32 @remove_slash(i8* %38)
%40 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%41 = load i8*, i8** %6
%42 = getelementptr inbounds i8, i8* %41, i64 1
%43 = call i32 @qe_pstrcat(i8* %40, i32 1024, i8* %42)
%44 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
store i8* %44, i8** %6
br label %45
45: ; preds = %34, %30
br label %54
46: ; preds = %24
%47 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%48 = call i32 @qe_pstrcpy(i8* %47, i32 1024, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%49 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%50 = load i8*, i8** %6
%51 = getelementptr inbounds i8, i8* %50, i64 1
%52 = call i32 @qe_pstrcat(i8* %49, i32 1024, i8* %51)
%53 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
store i8* %53, i8** %6
br label %54
54: ; preds = %46, %45
br label %63
55: ; preds = %13
%56 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%57 = call i32 @getcwd(i8* %56, i32 1024)
%58 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%59 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%60 = load i8*, i8** %6
%61 = call i32 @makepath(i8* %58, i32 1024, i8* %59, i8* %60)
%62 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
store i8* %62, i8** %6
br label %63
63: ; preds = %55, %54
br label %64
64: ; preds = %63, %3
%65 = load i8*, i8** %4
%66 = load i32, i32* %5
%67 = load i8*, i8** %6
%68 = call i32 @canonicalize_path(i8* %65, i32 %66, i8* %67)
ret void
}

LLVM IR (-O2):
@.str = [5 x i8] c"HOME\00"
@.str.1 = [7 x i8] c"/home/\00"
define void @canonicalize_absolute_path(i8* %0, i32 %1, i8* %2) {
%4 = alloca [1024 x i8]
%5 = alloca [1024 x i8]
%6 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%7 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%8 = tail call i32 @is_abs_path(i8* %2)
%9 = icmp eq i32 %8, 0
br i1 %9, label %10, label %29
10: ; preds = %3
%11 = load i8, i8* %2
%12 = icmp eq i8 %11, 126
br i1 %12, label %13, label %26
13: ; preds = %10
%14 = getelementptr inbounds i8, i8* %2, i64 1
%15 = load i8, i8* %14
switch i8 %15, label %23 [
i8 0, label %16
i8 47, label %16
]
16: ; preds = %13, %13
%17 = tail call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%18 = icmp eq i8* %17, null
br i1 %18, label %29, label %19
19: ; preds = %16
%20 = call i32 @qe_pstrcpy(i8* %7, i32 1024, i8* %17)
%21 = call i32 @remove_slash(i8* %7)
%22 = call i32 @qe_pstrcat(i8* %7, i32 1024, i8* %14)
br label %29
23: ; preds = %13
%24 = call i32 @qe_pstrcpy(i8* %7, i32 1024, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%25 = call i32 @qe_pstrcat(i8* %7, i32 1024, i8* %14)
br label %29
26: ; preds = %10
%27 = call i32 @getcwd(i8* %6, i32 1024)
%28 = call i32 @makepath(i8* %7, i32 1024, i8* %6, i8* %2)
br label %29
29: ; preds = %26, %16, %19, %23, %3
%30 = phi i8* [ %2, %3 ], [ %7, %19 ], [ %2, %16 ], [ %7, %23 ], [ %7, %26 ]
%31 = call i32 @canonicalize_path(i8* %0, i32 %1, i8* %30)
ret void
}