#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 */
char *absoluteFilename(char const *);
scalar_t__ eMalloc(size_t);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ strcat(char *, char const *);
char *strchr(char const *, char);
scalar_t__ strlen(char const *);
extern char *relativeFilename(const char *file, const char *dir) {
  const char *fp, *dp;
  char *absdir, *res;
  int i;
  absdir = absoluteFilename(file);
  fp = absdir;
  dp = dir;
  while (*fp++ == *dp++)
    continue;
  fp--;
  dp--;
  do {
    if (fp == absdir)
      return absdir;
    fp--;
    dp--;
  } while (*fp != '/');
  i = 0;
  while ((dp = strchr(dp + 1, '/')) != NULL)
    i += 1;
  res = (char *)eMalloc((size_t)(3 * i + strlen(fp + 1) + 1) * sizeof(char));
  res[0] = '\0';
  while (i-- > 0)
    strcat(res, "../");
  strcat(res, fp + 1);
  free(absdir);
  return res;
}


x86 assembly (clang, -O0):
<relativeFilename>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1070 <absoluteFilename@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x20(%rbp)
movsbl (%rax),%eax
mov    -0x28(%rbp),%rcx
mov    %rcx,%rdx
add    $0x1,%rdx
mov    %rdx,-0x28(%rbp)
movsbl (%rcx),%ecx
cmp    %ecx,%eax
jne    11be <relativeFilename+0x5e>
jmp    118d <relativeFilename+0x2d>
mov    -0x20(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x20(%rbp)
mov    -0x28(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x30(%rbp),%rax
jne    11f1 <relativeFilename+0x91>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12cf <relativeFilename+0x16f>
mov    -0x20(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x20(%rbp)
mov    -0x28(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
jne    11d6 <relativeFilename+0x76>
movl   $0x0,-0x3c(%rbp)
mov    -0x28(%rbp),%rdi
add    $0x1,%rdi
mov    $0x2f,%esi
call   1050 <strchr@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     124e <relativeFilename+0xee>
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
jmp    1220 <relativeFilename+0xc0>
imul   $0x3,-0x3c(%rbp),%eax
cltq
mov    %rax,-0x48(%rbp)
mov    -0x20(%rbp),%rdi
add    $0x1,%rdi
call   1040 <strlen@plt>
mov    -0x48(%rbp),%rdi
add    %rax,%rdi
add    $0x1,%rdi
shl    $0x0,%rdi
call   1060 <eMalloc@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x3c(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x3c(%rbp)
cmp    $0x0,%eax
jle    12ad <relativeFilename+0x14d>
mov    -0x38(%rbp),%rdi
lea    0xd5d(%rip),%rsi
call   1080 <strcat@plt>
jmp    1284 <relativeFilename+0x124>
mov    -0x38(%rbp),%rdi
mov    -0x20(%rbp),%rsi
add    $0x1,%rsi
call   1080 <strcat@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<relativeFilename>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%rbp
call   1070 <absoluteFilename@plt>
mov    %rax,%r14
mov    $0xffffffffffffffff,%rax
mov    %r14,%rcx
nopl   (%rax)
mov    %rax,%rdx
movzbl 0x1(%r14,%rax,1),%ebx
add    $0x1,%rcx
add    $0x1,%rax
cmp    0x1(%rbp,%rdx,1),%bl
je     1180 <relativeFilename+0x20>
neg    %rax
nopw   0x0(%rax,%rax,1)
test   %rax,%rax
je     1249 <relativeFilename+0xe9>
add    $0x1,%rax
lea    -0x1(%rcx),%r15
cmpb   $0x2f,-0x2(%rcx)
mov    %r15,%rcx
jne    11a0 <relativeFilename+0x40>
sub    %rax,%rbp
xor    %ebx,%ebx
mov    $0xfffffffe,%r12d
cs nopw 0x0(%rax,%rax,1)
nop
add    $0x1,%rbp
mov    %rbp,%rdi
mov    $0x2f,%esi
call   1050 <strchr@plt>
mov    %rax,%rbp
add    $0x1,%ebx
add    $0x3,%r12d
test   %rax,%rax
jne    11d0 <relativeFilename+0x70>
mov    %r15,%rdi
call   1040 <strlen@plt>
mov    %r12d,%edi
add    %rax,%rdi
call   1060 <eMalloc@plt>
mov    %rax,%rbp
movb   $0x0,(%rax)
cmp    $0x1,%ebx
je     1233 <relativeFilename+0xd3>
lea    0xdeb(%rip),%r12
cs nopw 0x0(%rax,%rax,1)
nop
mov    %rbp,%rdi
mov    %r12,%rsi
call   1080 <strcat@plt>
add    $0xffffffff,%ebx
cmp    $0x1,%ebx
jg     1220 <relativeFilename+0xc0>
mov    %rbp,%rdi
mov    %r15,%rsi
call   1080 <strcat@plt>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %rbp,%r14
mov    %r14,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

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