#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 */
size_t FAT_parse_directory(char const *, size_t, size_t *, size_t *);
int /*<<< orphan*/ strcat(char *, char const *);
int /*<<< orphan*/ strcpy(char *, char *);
size_t strlen(char const *);
int FAT_make_path_relative(const char *filename, const char *relto,
                           char *output) {
  size_t startrelto, endrelto, reltolen, reltochunk;
  if (!filename || !relto || !output)
    return -1;
  while (*filename == *relto) {
    if (*filename == '\0')
      return -1;
    ++filename;
    ++relto;
  }
  reltolen = strlen(relto);
  startrelto = 0;
  do {
    reltochunk = FAT_parse_directory(relto, reltolen, &startrelto, &endrelto);
    if (reltochunk) {
      strcpy(output, "..\\");
      output += 3;
    } else
      break;
    endrelto = startrelto;
  } while (1);
  strcat(output, filename);
  return 0;
}


x86 assembly (clang, -O0):
<FAT_make_path_relative>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1175 <FAT_make_path_relative+0x35>
cmpq   $0x0,-0x18(%rbp)
je     1175 <FAT_make_path_relative+0x35>
cmpq   $0x0,-0x20(%rbp)
jne    1181 <FAT_make_path_relative+0x41>
movl   $0xffffffff,-0x4(%rbp)
jmp    125f <FAT_make_path_relative+0x11f>
jmp    1186 <FAT_make_path_relative+0x46>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
mov    -0x18(%rbp),%rcx
movsbl (%rcx),%ecx
cmp    %ecx,%eax
jne    11d5 <FAT_make_path_relative+0x95>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    11b8 <FAT_make_path_relative+0x78>
movl   $0xffffffff,-0x4(%rbp)
jmp    125f <FAT_make_path_relative+0x11f>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    1186 <FAT_make_path_relative+0x46>
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x38(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x38(%rbp),%rsi
lea    -0x28(%rbp),%rdx
lea    -0x30(%rbp),%rcx
call   1050 <FAT_parse_directory@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
je     122f <FAT_make_path_relative+0xef>
mov    -0x20(%rbp),%rdi
lea    0xde7(%rip),%rsi
call   1030 <strcpy@plt>
mov    -0x20(%rbp),%rax
add    $0x3,%rax
mov    %rax,-0x20(%rbp)
jmp    1234 <FAT_make_path_relative+0xf4>
jmp    124b <FAT_make_path_relative+0x10b>
mov    -0x28(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    $0x1,%al
test   $0x1,%al
jne    11ea <FAT_make_path_relative+0xaa>
jmp    124b <FAT_make_path_relative+0x10b>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1060 <strcat@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<FAT_make_path_relative>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    $0xffffffff,%eax
test   %rdi,%rdi
je     1220 <FAT_make_path_relative+0xe0>
test   %rsi,%rsi
je     1220 <FAT_make_path_relative+0xe0>
mov    %rdx,%rbx
test   %rdx,%rdx
je     1220 <FAT_make_path_relative+0xe0>
mov    %rdi,%r14
mov    (%rdi),%cl
cmp    (%rsi),%cl
jne    119f <FAT_make_path_relative+0x5f>
nopw   0x0(%rax,%rax,1)
test   %cl,%cl
je     1220 <FAT_make_path_relative+0xe0>
lea    0x1(%rsi),%r15
movzbl 0x1(%r14),%ecx
add    $0x1,%r14
cmp    0x1(%rsi),%cl
mov    %r15,%rsi
je     1180 <FAT_make_path_relative+0x40>
jmp    11a2 <FAT_make_path_relative+0x62>
mov    %rsi,%r15
mov    %r15,%rdi
call   1040 <strlen@plt>
mov    %rax,%r12
movq   $0x0,0x8(%rsp)
lea    0x8(%rsp),%rdx
lea    0x10(%rsp),%rcx
mov    %r15,%rdi
mov    %rax,%rsi
call   1050 <FAT_parse_directory@plt>
test   %rax,%rax
je     1213 <FAT_make_path_relative+0xd3>
lea    0x8(%rsp),%rbp
lea    0x10(%rsp),%r13
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
lea    0xe16(%rip),%rsi
call   1030 <strcpy@plt>
add    $0x3,%rbx
mov    0x8(%rsp),%rax
mov    %rax,0x10(%rsp)
mov    %r15,%rdi
mov    %r12,%rsi
mov    %rbp,%rdx
mov    %r13,%rcx
call   1050 <FAT_parse_directory@plt>
test   %rax,%rax
jne    11e0 <FAT_make_path_relative+0xa0>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1060 <strcat@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [4 x i8] c"..\\\00"
define i32 @FAT_make_path_relative(i8* %0, i8* %1, i8* %2) {
%4 = alloca i64
%5 = alloca i64
%6 = bitcast i64* %4 to i8*
%7 = bitcast i64* %5 to i8*
%8 = icmp ne i8* %0, null
%9 = icmp ne i8* %1, null
%10 = and i1 %8, %9
%11 = icmp ne i8* %2, null
%12 = and i1 %10, %11
br i1 %12, label %13, label %44
13: ; preds = %3
%14 = load i8, i8* %0
%15 = load i8, i8* %1
%16 = icmp eq i8 %14, %15
br i1 %16, label %17, label %28
17: ; preds = %13, %22
%18 = phi i8 [ %25, %22 ], [ %14, %13 ]
%19 = phi i8* [ %23, %22 ], [ %0, %13 ]
%20 = phi i8* [ %24, %22 ], [ %1, %13 ]
%21 = icmp eq i8 %18, 0
br i1 %21, label %44, label %22
22: ; preds = %17
%23 = getelementptr inbounds i8, i8* %19, i64 1
%24 = getelementptr inbounds i8, i8* %20, i64 1
%25 = load i8, i8* %23
%26 = load i8, i8* %24
%27 = icmp eq i8 %25, %26
br i1 %27, label %17, label %28
28: ; preds = %22, %13
%29 = phi i8* [ %1, %13 ], [ %24, %22 ]
%30 = phi i8* [ %0, %13 ], [ %23, %22 ]
%31 = tail call i64 @strlen(i8* %29)
store i64 0, i64* %4
%32 = call i64 @FAT_parse_directory(i8* %29, i64 %31, i64* %4, i64* %5)
%33 = icmp eq i64 %32, 0
br i1 %33, label %41, label %34
34: ; preds = %28, %34
%35 = phi i8* [ %37, %34 ], [ %2, %28 ]
%36 = call i32 @strcpy(i8* %35, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%37 = getelementptr inbounds i8, i8* %35, i64 3
%38 = load i64, i64* %4
store i64 %38, i64* %5
%39 = call i64 @FAT_parse_directory(i8* %29, i64 %31, i64* %4, i64* %5)
%40 = icmp eq i64 %39, 0
br i1 %40, label %41, label %34
41: ; preds = %34, %28
%42 = phi i8* [ %2, %28 ], [ %37, %34 ]
%43 = call i32 @strcat(i8* %42, i8* %30)
br label %44
44: ; preds = %17, %3, %41
%45 = phi i32 [ 0, %41 ], [ -1, %3 ], [ -1, %17 ]
ret i32 %45
}