#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*/ buf;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
char *index(char *, char);
int /*<<< orphan*/ memcpy(char *, char *, size_t);
int /*<<< orphan*/ memmove(char *, char *, size_t);
int /*<<< orphan*/ perror(char *);
char *str_trim(char *);
size_t strlen(char *);
scalar_t__ strncmp(char *, char *, size_t);
void replace_alias(char *path, char *cwd, size_t cwd_size, char **lastslash) {
  FILE *faliases;
  char *alias, *target, *separator;
  char buf[1024];
  size_t alias_len;
  faliases = fopen(path, "r");
  if (faliases == NULL) {
    perror(path);
    return;
  }
  while (fgets(buf, sizeof(buf), faliases)) {
    if (buf[0] == '#') {
      continue;
    }
    if ((separator = index(buf, '='))) {
      *separator = '\0';
      alias = str_trim(buf);
      target = str_trim(separator + 1);
      if (alias == NULL) {
        continue;
      }
      alias_len = strlen(alias);
      if (strncmp(target, cwd, strlen(target)) == 0) {
        memmove(cwd + alias_len, cwd + strlen(target), cwd_size - alias_len);
        memcpy(cwd, alias, alias_len);
        *lastslash = cwd + alias_len;
        break;
      }
    }
  }
  fclose(faliases);
}


x86 assembly (clang, -O0):
<replace_alias>:
push   %rbp
mov    %rsp,%rbp
sub    $0x470,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe3a(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11e8 <replace_alias+0x48>
mov    -0x8(%rbp),%rdi
call   10b0 <perror@plt>
jmp    134c <replace_alias+0x1ac>
jmp    11ed <replace_alias+0x4d>
lea    -0x440(%rbp),%rdi
mov    -0x28(%rbp),%rdx
mov    $0x400,%esi
call   1070 <fgets@plt>
cmp    $0x0,%rax
je     1343 <replace_alias+0x1a3>
movsbl -0x440(%rbp),%eax
cmp    $0x23,%eax
jne    1221 <replace_alias+0x81>
jmp    11ed <replace_alias+0x4d>
lea    -0x440(%rbp),%rdi
mov    $0x3d,%esi
call   1060 <index@plt>
mov    %rax,-0x40(%rbp)
cmp    $0x0,%rax
je     133e <replace_alias+0x19e>
mov    -0x40(%rbp),%rax
movb   $0x0,(%rax)
lea    -0x440(%rbp),%rdi
call   10c0 <str_trim@plt>
mov    %rax,-0x30(%rbp)
mov    -0x40(%rbp),%rdi
add    $0x1,%rdi
call   10c0 <str_trim@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1278 <replace_alias+0xd8>
jmp    11ed <replace_alias+0x4d>
mov    -0x30(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,-0x448(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,-0x458(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x450(%rbp)
mov    -0x38(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x458(%rbp),%rdi
mov    -0x450(%rbp),%rsi
mov    %rax,%rdx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    1339 <replace_alias+0x199>
mov    -0x10(%rbp),%rax
add    -0x448(%rbp),%rax
mov    %rax,-0x460(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x468(%rbp)
mov    -0x38(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x468(%rbp),%rsi
mov    -0x460(%rbp),%rdi
add    %rax,%rsi
mov    -0x18(%rbp),%rdx
sub    -0x448(%rbp),%rdx
call   1090 <memmove@plt>
mov    -0x10(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x448(%rbp),%rdx
call   1080 <memcpy@plt>
mov    -0x10(%rbp),%rcx
add    -0x448(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
jmp    1343 <replace_alias+0x1a3>
jmp    133e <replace_alias+0x19e>
jmp    11ed <replace_alias+0x4d>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
add    $0x470,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<replace_alias>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x428,%rsp
mov    %rcx,%r14
mov    %rdx,%rbp
mov    %rsi,0x8(%rsp)
mov    %rdi,%rbx
lea    0xe3a(%rip),%rsi
call   10a0 <fopen@plt>
test   %rax,%rax
je     12e4 <replace_alias+0x144>
mov    %rax,%r13
mov    %rbp,0x10(%rsp)
mov    %r14,0x18(%rsp)
lea    0x20(%rsp),%rdi
mov    $0x400,%esi
mov    %rax,%rdx
call   1070 <fgets@plt>
test   %rax,%rax
je     12ca <replace_alias+0x12a>
lea    0x20(%rsp),%r15
jmp    1229 <replace_alias+0x89>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r15,%rdi
mov    $0x400,%esi
mov    %r13,%rdx
call   1070 <fgets@plt>
test   %rax,%rax
je     12ca <replace_alias+0x12a>
cmpb   $0x23,0x20(%rsp)
je     1210 <replace_alias+0x70>
mov    %r15,%rdi
mov    $0x3d,%esi
call   1060 <index@plt>
test   %rax,%rax
je     1210 <replace_alias+0x70>
mov    %rax,%rbx
movb   $0x0,(%rax)
mov    %r15,%rdi
call   10c0 <str_trim@plt>
mov    %rax,%rbp
add    $0x1,%rbx
mov    %rbx,%rdi
call   10c0 <str_trim@plt>
test   %rbp,%rbp
je     1210 <replace_alias+0x70>
mov    %rax,%r12
mov    %rbp,%rdi
call   1050 <strlen@plt>
mov    %rax,%r14
mov    %r12,%rdi
call   1050 <strlen@plt>
mov    %r12,%rdi
mov    0x8(%rsp),%rsi
mov    %rax,%rdx
call   1030 <strncmp@plt>
test   %rax,%rax
jne    1210 <replace_alias+0x70>
mov    0x8(%rsp),%rbx
lea    (%rbx,%r14,1),%r15
mov    %r12,%rdi
call   1050 <strlen@plt>
lea    (%rax,%rbx,1),%rsi
mov    0x10(%rsp),%rdx
sub    %r14,%rdx
mov    %r15,%rdi
call   1090 <memmove@plt>
mov    %rbx,%rdi
mov    %rbp,%rsi
mov    %r14,%rdx
call   1080 <memcpy@plt>
mov    0x18(%rsp),%rax
mov    %r15,(%rax)
mov    %r13,%rdi
call   1040 <fclose@plt>
add    $0x428,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %rbx,%rdi
add    $0x428,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    10b0 <perror@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
define void @replace_alias(i8* %0, i8* %1, i64 %2, i8** %3) {
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i8**
%9 = alloca i32*
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca i8*
%13 = alloca [1024 x i8]
%14 = alloca i64
store i8* %0, i8** %5
store i8* %1, i8** %6
store i64 %2, i64* %7
store i8** %3, i8*** %8
%15 = load i8*, i8** %5
%16 = call i32* @fopen(i8* %15, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %16, i32** %9
%17 = load i32*, i32** %9
%18 = icmp eq i32* %17, null
br i1 %18, label %19, label %22
19: ; preds = %4
%20 = load i8*, i8** %5
%21 = call i32 @perror(i8* %20)
br label %82
22: ; preds = %4
br label %23
23: ; preds = %78, %47, %33, %22
%24 = getelementptr inbounds [1024 x i8], [1024 x i8]* %13, i64 0, i64 0
%25 = load i32*, i32** %9
%26 = call i64 @fgets(i8* %24, i32 1024, i32* %25)
%27 = icmp ne i64 %26, 0
br i1 %27, label %28, label %79
28: ; preds = %23
%29 = getelementptr inbounds [1024 x i8], [1024 x i8]* %13, i64 0, i64 0
%30 = load i8, i8* %29
%31 = sext i8 %30 to i32
%32 = icmp eq i32 %31, 35
br i1 %32, label %33, label %34
33: ; preds = %28
br label %23
34: ; preds = %28
%35 = getelementptr inbounds [1024 x i8], [1024 x i8]* %13, i64 0, i64 0
%36 = call i8* @index(i8* %35, i8 signext 61)
store i8* %36, i8** %12
%37 = icmp ne i8* %36, null
br i1 %37, label %38, label %78
38: ; preds = %34
%39 = load i8*, i8** %12
store i8 0, i8* %39
%40 = getelementptr inbounds [1024 x i8], [1024 x i8]* %13, i64 0, i64 0
%41 = call i8* @str_trim(i8* %40)
store i8* %41, i8** %10
%42 = load i8*, i8** %12
%43 = getelementptr inbounds i8, i8* %42, i64 1
%44 = call i8* @str_trim(i8* %43)
store i8* %44, i8** %11
%45 = load i8*, i8** %10
%46 = icmp eq i8* %45, null
br i1 %46, label %47, label %48
47: ; preds = %38
br label %23
48: ; preds = %38
%49 = load i8*, i8** %10
%50 = call i64 @strlen(i8* %49)
store i64 %50, i64* %14
%51 = load i8*, i8** %11
%52 = load i8*, i8** %6
%53 = load i8*, i8** %11
%54 = call i64 @strlen(i8* %53)
%55 = call i64 @strncmp(i8* %51, i8* %52, i64 %54)
%56 = icmp eq i64 %55, 0
br i1 %56, label %57, label %77
57: ; preds = %48
%58 = load i8*, i8** %6
%59 = load i64, i64* %14
%60 = getelementptr inbounds i8, i8* %58, i64 %59
%61 = load i8*, i8** %6
%62 = load i8*, i8** %11
%63 = call i64 @strlen(i8* %62)
%64 = getelementptr inbounds i8, i8* %61, i64 %63
%65 = load i64, i64* %7
%66 = load i64, i64* %14
%67 = sub i64 %65, %66
%68 = call i32 @memmove(i8* %60, i8* %64, i64 %67)
%69 = load i8*, i8** %6
%70 = load i8*, i8** %10
%71 = load i64, i64* %14
%72 = call i32 @memcpy(i8* %69, i8* %70, i64 %71)
%73 = load i8*, i8** %6
%74 = load i64, i64* %14
%75 = getelementptr inbounds i8, i8* %73, i64 %74
%76 = load i8**, i8*** %8
store i8* %75, i8** %76
br label %79
77: ; preds = %48
br label %78
78: ; preds = %77, %34
br label %23
79: ; preds = %57, %23
%80 = load i32*, i32** %9
%81 = call i32 @fclose(i32* %80)
br label %82
82: ; preds = %79, %19
ret void
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
define void @replace_alias(i8* nocapture %0, i8* %1, i64 %2, i8** nocapture writeonly %3) {
%5 = alloca [1024 x i8]
%6 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%7 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%8 = icmp eq i32* %7, null
br i1 %8, label %12, label %9
9: ; preds = %4
%10 = call i64 @fgets(i8* %6, i32 1024, i32* %7)
%11 = icmp eq i64 %10, 0
br i1 %11, label %40, label %14
12: ; preds = %4
%13 = tail call i32 @perror(i8* %0)
br label %42
14: ; preds = %9, %37
%15 = load i8, i8* %6
%16 = icmp eq i8 %15, 35
br i1 %16, label %37, label %17
17: ; preds = %14
%18 = call i8* @index(i8* %6, i8 signext 61)
%19 = icmp eq i8* %18, null
br i1 %19, label %37, label %20
20: ; preds = %17
store i8 0, i8* %18
%21 = call i8* @str_trim(i8* %6)
%22 = getelementptr inbounds i8, i8* %18, i64 1
%23 = call i8* @str_trim(i8* %22)
%24 = icmp eq i8* %21, null
br i1 %24, label %37, label %25
25: ; preds = %20
%26 = call i64 @strlen(i8* %21)
%27 = call i64 @strlen(i8* dereferenceable(1) %23)
%28 = call i64 @strncmp(i8* %23, i8* %1, i64 %27)
%29 = icmp eq i64 %28, 0
br i1 %29, label %30, label %37
30: ; preds = %25
%31 = getelementptr inbounds i8, i8* %1, i64 %26
%32 = call i64 @strlen(i8* dereferenceable(1) %23)
%33 = getelementptr inbounds i8, i8* %1, i64 %32
%34 = sub i64 %2, %26
%35 = call i32 @memmove(i8* %31, i8* %33, i64 %34)
%36 = call i32 @memcpy(i8* %1, i8* %21, i64 %26)
store i8* %31, i8** %3
br label %40
37: ; preds = %17, %25, %14, %20
%38 = call i64 @fgets(i8* %6, i32 1024, i32* %7)
%39 = icmp eq i64 %38, 0
br i1 %39, label %40, label %14
40: ; preds = %37, %9, %30
%41 = call i32 @fclose(i32* %7)
br label %42
42: ; preds = %40, %12
ret void
}