#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 *pw_dir;
};

/* Variables and functions */
int /*<<< orphan*/ CleanString(char *);
int /*<<< orphan*/ geteuid();
struct passwd *getpwnam(char *);
struct passwd *getpwuid(int /*<<< orphan*/);
int /*<<< orphan*/ sprintf(char *, char *, char *);
int /*<<< orphan*/ strcat(char *, char *);
char *strchr(char *, char);
int strlen(char *);
scalar_t__ strstr(char *, char *);
int FixFilename(char *file, char *buf) {
  struct passwd *pw = NULL;
  char *s;
  char *t = NULL;
  if (strstr(file, ".."))
    return (-1);
  CleanString(file);
  if (file[0] == '~' && file[1] != '/') {
    t = strchr(file, '/');
    if (t) {
      *t = '\0';
      pw = getpwnam(&file[1]);
      *t = '/';
      t = t + 1;
    }
  } else if (file[0] == '/' && file[1] == '~') {
    t = strchr(&file[1], '/');
    if (t) {
      *t = '\0';
      pw = getpwnam(&file[2]);
      *t = '/';
      t = t + 1;
    }
  }
  if (!t)
    pw = getpwuid(geteuid());
  sprintf(buf, "%s/public_html", pw->pw_dir);
  s = buf + strlen(buf) - 1;
  if (*s != '/')
    strcat(buf, "/");
  if (t)
    s = t;
  else
    s = file;
  while (*s == '/')
    s++;
  strcat(buf, s);
  return (0);
}


x86 assembly (clang, -O0):
<FixFilename>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe45(%rip),%rsi
call   10b0 <strstr@plt>
cmp    $0x0,%rax
je     11d6 <FixFilename+0x46>
movl   $0xffffffff,-0x4(%rbp)
jmp    1397 <FixFilename+0x207>
mov    -0x10(%rbp),%rdi
call   1030 <CleanString@plt>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x7e,%eax
jne    124d <FixFilename+0xbd>
mov    -0x10(%rbp),%rax
movsbl 0x1(%rax),%eax
cmp    $0x2f,%eax
je     124d <FixFilename+0xbd>
mov    -0x10(%rbp),%rdi
mov    $0x2f,%esi
call   1060 <strchr@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     1248 <FixFilename+0xb8>
mov    -0x30(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
add    $0x1,%rdi
call   1080 <getpwnam@plt>
mov    %rax,-0x20(%rbp)
mov    -0x30(%rbp),%rax
movb   $0x2f,(%rax)
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
jmp    12c4 <FixFilename+0x134>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
jne    12bf <FixFilename+0x12f>
mov    -0x10(%rbp),%rax
movsbl 0x1(%rax),%eax
cmp    $0x7e,%eax
jne    12bf <FixFilename+0x12f>
mov    -0x10(%rbp),%rdi
add    $0x1,%rdi
mov    $0x2f,%esi
call   1060 <strchr@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     12ba <FixFilename+0x12a>
mov    -0x30(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
add    $0x2,%rdi
call   1080 <getpwnam@plt>
mov    %rax,-0x20(%rbp)
mov    -0x30(%rbp),%rax
movb   $0x2f,(%rax)
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
jmp    12bf <FixFilename+0x12f>
jmp    12c4 <FixFilename+0x134>
cmpq   $0x0,-0x30(%rbp)
jne    12e1 <FixFilename+0x151>
mov    $0x0,%al
call   1070 <geteuid@plt>
mov    %eax,%edi
call   1040 <getpwuid@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rax
mov    (%rax),%rdx
lea    0xd10(%rip),%rsi
call   10a0 <sprintf@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    -0x38(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
je     133d <FixFilename+0x1ad>
mov    -0x18(%rbp),%rdi
lea    0xcda(%rip),%rsi
call   1090 <strcat@plt>
cmpq   $0x0,-0x30(%rbp)
je     1355 <FixFilename+0x1c5>
mov    -0x30(%rbp),%rax
mov    %rax,-0x28(%rbp)
jmp    135d <FixFilename+0x1cd>
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
jmp    1362 <FixFilename+0x1d2>
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
jne    1383 <FixFilename+0x1f3>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    1362 <FixFilename+0x1d2>
mov    -0x18(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1090 <strcat@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<FixFilename>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%rbx
lea    0xe5e(%rip),%rsi
call   10b0 <strstr@plt>
mov    %rax,%rcx
mov    $0xffffffff,%eax
test   %rcx,%rcx
jne    1287 <FixFilename+0xf7>
mov    %rbx,%rdi
call   1030 <CleanString@plt>
mov    (%rbx),%al
cmp    $0x2f,%al
je     11ee <FixFilename+0x5e>
cmp    $0x7e,%al
jne    1226 <FixFilename+0x96>
cmpb   $0x2f,0x1(%rbx)
je     1226 <FixFilename+0x96>
mov    %rbx,%rdi
mov    $0x2f,%esi
call   1060 <strchr@plt>
test   %rax,%rax
je     1226 <FixFilename+0x96>
mov    %rax,%r15
add    $0x1,%rbx
movb   $0x0,(%rax)
jmp    1211 <FixFilename+0x81>
cmpb   $0x7e,0x1(%rbx)
jne    1226 <FixFilename+0x96>
lea    0x1(%rbx),%rdi
mov    $0x2f,%esi
call   1060 <strchr@plt>
test   %rax,%rax
je     1226 <FixFilename+0x96>
mov    %rax,%r15
movb   $0x0,(%rax)
add    $0x2,%rbx
mov    %rbx,%rdi
call   1080 <getpwnam@plt>
movb   $0x2f,(%r15)
add    $0x1,%r15
mov    %r15,%rbx
jmp    1234 <FixFilename+0xa4>
xor    %eax,%eax
call   1070 <geteuid@plt>
mov    %eax,%edi
call   1040 <getpwuid@plt>
mov    (%rax),%rdx
lea    0xdc5(%rip),%rsi
mov    %r14,%rdi
call   10a0 <sprintf@plt>
mov    %r14,%rdi
call   1050 <strlen@plt>
cltq
cmpb   $0x2f,-0x1(%r14,%rax,1)
je     1267 <FixFilename+0xd7>
lea    0xdb3(%rip),%rsi
mov    %r14,%rdi
call   1090 <strcat@plt>
add    $0xffffffffffffffff,%rbx
nopl   0x0(%rax,%rax,1)
cmpb   $0x2f,0x1(%rbx)
lea    0x1(%rbx),%rbx
je     1270 <FixFilename+0xe0>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1090 <strcat@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.passwd = type { i8* }
@.str = [3 x i8] c"..\00"
@.str.1 = [15 x i8] c"%s/public_html\00"
@.str.2 = [2 x i8] c"/\00"
define i32 @FixFilename(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca %struct.passwd*
%7 = alloca i8*
%8 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
store %struct.passwd* null, %struct.passwd** %6
store i8* null, i8** %8
%9 = load i8*, i8** %4
%10 = call i64 @strstr(i8* %9, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%11 = icmp ne i64 %10, 0
br i1 %11, label %12, label %13
12: ; preds = %2
store i32 -1, i32* %3
br label %114
13: ; preds = %2
%14 = load i8*, i8** %4
%15 = call i32 @CleanString(i8* %14)
%16 = load i8*, i8** %4
%17 = getelementptr inbounds i8, i8* %16, i64 0
%18 = load i8, i8* %17
%19 = sext i8 %18 to i32
%20 = icmp eq i32 %19, 126
br i1 %20, label %21, label %41
21: ; preds = %13
%22 = load i8*, i8** %4
%23 = getelementptr inbounds i8, i8* %22, i64 1
%24 = load i8, i8* %23
%25 = sext i8 %24 to i32
%26 = icmp ne i32 %25, 47
br i1 %26, label %27, label %41
27: ; preds = %21
%28 = load i8*, i8** %4
%29 = call i8* @strchr(i8* %28, i8 signext 47)
store i8* %29, i8** %8
%30 = load i8*, i8** %8
%31 = icmp ne i8* %30, null
br i1 %31, label %32, label %40
32: ; preds = %27
%33 = load i8*, i8** %8
store i8 0, i8* %33
%34 = load i8*, i8** %4
%35 = getelementptr inbounds i8, i8* %34, i64 1
%36 = call %struct.passwd* @getpwnam(i8* %35)
store %struct.passwd* %36, %struct.passwd** %6
%37 = load i8*, i8** %8
store i8 47, i8* %37
%38 = load i8*, i8** %8
%39 = getelementptr inbounds i8, i8* %38, i64 1
store i8* %39, i8** %8
br label %40
40: ; preds = %32, %27
br label %69
41: ; preds = %21, %13
%42 = load i8*, i8** %4
%43 = getelementptr inbounds i8, i8* %42, i64 0
%44 = load i8, i8* %43
%45 = sext i8 %44 to i32
%46 = icmp eq i32 %45, 47
br i1 %46, label %47, label %68
47: ; preds = %41
%48 = load i8*, i8** %4
%49 = getelementptr inbounds i8, i8* %48, i64 1
%50 = load i8, i8* %49
%51 = sext i8 %50 to i32
%52 = icmp eq i32 %51, 126
br i1 %52, label %53, label %68
53: ; preds = %47
%54 = load i8*, i8** %4
%55 = getelementptr inbounds i8, i8* %54, i64 1
%56 = call i8* @strchr(i8* %55, i8 signext 47)
store i8* %56, i8** %8
%57 = load i8*, i8** %8
%58 = icmp ne i8* %57, null
br i1 %58, label %59, label %67
59: ; preds = %53
%60 = load i8*, i8** %8
store i8 0, i8* %60
%61 = load i8*, i8** %4
%62 = getelementptr inbounds i8, i8* %61, i64 2
%63 = call %struct.passwd* @getpwnam(i8* %62)
store %struct.passwd* %63, %struct.passwd** %6
%64 = load i8*, i8** %8
store i8 47, i8* %64
%65 = load i8*, i8** %8
%66 = getelementptr inbounds i8, i8* %65, i64 1
store i8* %66, i8** %8
br label %67
67: ; preds = %59, %53
br label %68
68: ; preds = %67, %47, %41
br label %69
69: ; preds = %68, %40
%70 = load i8*, i8** %8
%71 = icmp ne i8* %70, null
br i1 %71, label %75, label %72
72: ; preds = %69
%73 = call i32 (...) @geteuid()
%74 = call %struct.passwd* @getpwuid(i32 %73)
store %struct.passwd* %74, %struct.passwd** %6
br label %75
75: ; preds = %72, %69
%76 = load i8*, i8** %5
%77 = load %struct.passwd*, %struct.passwd** %6
%78 = getelementptr inbounds %struct.passwd, %struct.passwd* %77, i32 0, i32 0
%79 = load i8*, i8** %78
%80 = call i32 @sprintf(i8* %76, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8* %79)
%81 = load i8*, i8** %5
%82 = load i8*, i8** %5
%83 = call i32 @strlen(i8* %82)
%84 = sext i32 %83 to i64
%85 = getelementptr inbounds i8, i8* %81, i64 %84
%86 = getelementptr inbounds i8, i8* %85, i64 -1
store i8* %86, i8** %7
%87 = load i8*, i8** %7
%88 = load i8, i8* %87
%89 = sext i8 %88 to i32
%90 = icmp ne i32 %89, 47
br i1 %90, label %91, label %94
91: ; preds = %75
%92 = load i8*, i8** %5
%93 = call i32 @strcat(i8* %92, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
br label %94
94: ; preds = %91, %75
%95 = load i8*, i8** %8
%96 = icmp ne i8* %95, null
br i1 %96, label %97, label %99
97: ; preds = %94
%98 = load i8*, i8** %8
store i8* %98, i8** %7
br label %101
99: ; preds = %94
%100 = load i8*, i8** %4
store i8* %100, i8** %7
br label %101
101: ; preds = %99, %97
br label %102
102: ; preds = %107, %101
%103 = load i8*, i8** %7
%104 = load i8, i8* %103
%105 = sext i8 %104 to i32
%106 = icmp eq i32 %105, 47
br i1 %106, label %107, label %110
107: ; preds = %102
%108 = load i8*, i8** %7
%109 = getelementptr inbounds i8, i8* %108, i32 1
store i8* %109, i8** %7
br label %102
110: ; preds = %102
%111 = load i8*, i8** %5
%112 = load i8*, i8** %7
%113 = call i32 @strcat(i8* %111, i8* %112)
store i32 0, i32* %3
br label %114
114: ; preds = %110, %12
%115 = load i32, i32* %3
ret i32 %115
}

LLVM IR (-O2):
%struct.passwd = type { i8* }
@.str = [3 x i8] c"..\00"
@.str.1 = [15 x i8] c"%s/public_html\00"
@.str.2 = [2 x i8] c"/\00"
define i32 @FixFilename(i8* %0, i8* %1) {
%3 = tail call i64 @strstr(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i64 %3, 0
br i1 %4, label %5, label %54
5: ; preds = %2
%6 = tail call i32 @CleanString(i8* %0)
%7 = load i8, i8* %0
switch i8 %7, label %29 [
i8 126, label %8
i8 47, label %18
]
8: ; preds = %5
%9 = getelementptr inbounds i8, i8* %0, i64 1
%10 = load i8, i8* %9
%11 = icmp eq i8 %10, 47
br i1 %11, label %29, label %12
12: ; preds = %8
%13 = tail call i8* @strchr(i8* %0, i8 signext 47)
%14 = icmp eq i8* %13, null
br i1 %14, label %29, label %15
15: ; preds = %12
store i8 0, i8* %13
%16 = tail call %struct.passwd* @getpwnam(i8* %9)
store i8 47, i8* %13
%17 = getelementptr inbounds i8, i8* %13, i64 1
br label %32
18: ; preds = %5
%19 = getelementptr inbounds i8, i8* %0, i64 1
%20 = load i8, i8* %19
%21 = icmp eq i8 %20, 126
br i1 %21, label %22, label %29
22: ; preds = %18
%23 = tail call i8* @strchr(i8* %19, i8 signext 47)
%24 = icmp eq i8* %23, null
br i1 %24, label %29, label %25
25: ; preds = %22
store i8 0, i8* %23
%26 = getelementptr inbounds i8, i8* %0, i64 2
%27 = tail call %struct.passwd* @getpwnam(i8* %26)
store i8 47, i8* %23
%28 = getelementptr inbounds i8, i8* %23, i64 1
br label %32
29: ; preds = %5, %8, %12, %22, %18
%30 = tail call i32 (...) @geteuid()
%31 = tail call %struct.passwd* @getpwuid(i32 %30)
br label %32
32: ; preds = %15, %25, %29
%33 = phi i8* [ %0, %29 ], [ %28, %25 ], [ %17, %15 ]
%34 = phi %struct.passwd* [ %31, %29 ], [ %27, %25 ], [ %16, %15 ]
%35 = getelementptr inbounds %struct.passwd, %struct.passwd* %34, i64 0, i32 0
%36 = load i8*, i8** %35
%37 = tail call i32 @sprintf(i8* dereferenceable(1) %1, i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8* %36)
%38 = tail call i32 @strlen(i8* %1)
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds i8, i8* %1, i64 %39
%41 = getelementptr inbounds i8, i8* %40, i64 -1
%42 = load i8, i8* %41
%43 = icmp eq i8 %42, 47
br i1 %43, label %46, label %44
44: ; preds = %32
%45 = tail call i32 @strcat(i8* %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
br label %46
46: ; preds = %44, %32
br label %47
47: ; preds = %46, %47
%48 = phi i8* [ %51, %47 ], [ %33, %46 ]
%49 = load i8, i8* %48
%50 = icmp eq i8 %49, 47
%51 = getelementptr inbounds i8, i8* %48, i64 1
br i1 %50, label %47, label %52
52: ; preds = %47
%53 = tail call i32 @strcat(i8* %1, i8* %48)
br label %54
54: ; preds = %2, %52
%55 = phi i32 [ 0, %52 ], [ -1, %2 ]
ret i32 %55
}