void test_relative_filename(void) {
  static const struct rel_test {
    const char *cwd;
    const char *path;
    const char *result;
  } tests[] = {
      {"/", "/", "/"},
      {"/", "/file", "file"},
      {"/a/b/c/d", "/a/b/file", "../../file"},
      {"/a/b/c/d/e", "/a/b/file", "/a/b/file"},
      {"/a/foobar", "/a/foo/file", "../foo/file"},
  };
  int i;
  for (i = 0; i < ((unsigned long)sizeof(tests) / sizeof(tests[0])); i++) {
    const struct rel_test *t = &tests[i];
    char *result = relative_filename(t->path, t->cwd);
    if (!streq(t->result, result))
      fail("relative_filename(%s, %s) -> %s, expected %s\n", t->path, t->cwd,
           result, t->result);
    free(result);
  }
}


x86 assembly (clang, -O0):
<test_relative_filename>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movslq -0x4(%rbp),%rax
cmp    $0x5,%rax
jae    11eb <test_relative_filename+0xab>
movslq -0x4(%rbp),%rcx
lea    0x2c38(%rip),%rax
imul   $0x18,%rcx,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
mov    $0x0,%al
call   1060 <relative_filename@plt>
cltq
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
mov    -0x18(%rbp),%rsi
mov    $0x0,%al
call   1050 <streq@plt>
cmp    $0x0,%eax
jne    11d4 <test_relative_filename+0x94>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
mov    -0x18(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%r8
lea    0xe80(%rip),%rdi
mov    $0x0,%al
call   1040 <fail@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4(%rbp)
jmp    114f <test_relative_filename+0xf>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_relative_filename>:
push   %r14
push   %rbx
push   %rax
lea    0xeb5(%rip),%r14
mov    %r14,%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1060 <relative_filename@plt>
movslq %eax,%rbx
mov    %r14,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <streq@plt>
test   %eax,%eax
jne    118a <test_relative_filename+0x4a>
lea    0xeda(%rip),%rdi
lea    0xe86(%rip),%rdx
mov    %rdx,%rsi
mov    %rbx,%rcx
mov    %rdx,%r8
xor    %eax,%eax
call   1040 <fail@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
lea    0xe81(%rip),%rdi
lea    0xe60(%rip),%rsi
xor    %eax,%eax
call   1060 <relative_filename@plt>
movslq %eax,%rbx
lea    0xe6a(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <streq@plt>
test   %eax,%eax
jne    11e5 <test_relative_filename+0xa5>
lea    0xe87(%rip),%rdi
lea    0xe4d(%rip),%rsi
lea    0xe2c(%rip),%rdx
lea    0xe40(%rip),%r8
mov    %rbx,%rcx
xor    %eax,%eax
call   1040 <fail@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
lea    0xe17(%rip),%rdi
lea    0xe07(%rip),%rsi
xor    %eax,%eax
call   1060 <relative_filename@plt>
movslq %eax,%rbx
lea    0xe09(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <streq@plt>
test   %eax,%eax
jne    1240 <test_relative_filename+0x100>
lea    0xe2c(%rip),%rdi
lea    0xde3(%rip),%rsi
lea    0xdd3(%rip),%rdx
lea    0xddf(%rip),%r8
mov    %rbx,%rcx
xor    %eax,%eax
call   1040 <fail@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
lea    0xdbc(%rip),%r14
lea    0xdca(%rip),%rsi
mov    %r14,%rdi
xor    %eax,%eax
call   1060 <relative_filename@plt>
movslq %eax,%rbx
mov    %r14,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <streq@plt>
test   %eax,%eax
jne    1296 <test_relative_filename+0x156>
lea    0xdd2(%rip),%rdi
lea    0xd9e(%rip),%rdx
lea    0xd82(%rip),%rsi
mov    %rbx,%rcx
mov    %rsi,%r8
xor    %eax,%eax
call   1040 <fail@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
lea    0xd90(%rip),%rdi
lea    0xd7f(%rip),%rsi
xor    %eax,%eax
call   1060 <relative_filename@plt>
movslq %eax,%rbx
lea    0xd84(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <streq@plt>
test   %eax,%eax
jne    12f1 <test_relative_filename+0x1b1>
lea    0xd7b(%rip),%rdi
lea    0xd5c(%rip),%rsi
lea    0xd4b(%rip),%rdx
lea    0xd5a(%rip),%r8
mov    %rbx,%rcx
xor    %eax,%eax
call   1040 <fail@plt>
mov    %rbx,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1030 <free@plt>

LLVM IR (-O0):
%struct.rel_test = type { i8*, i8*, i8* }
@test_relative_filename.tests = internal constant [5 x %struct.rel_test] [%struct.rel_test { i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i32 0, i32 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i32 0, i32 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i32 0, i32 0) }, %struct.rel_test { i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i32 0, i32 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i32 0, i32 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i32 0, i32 0) }, %struct.rel_test { i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i32 0, i32 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i32 0, i32 0), i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.5, i32 0, i32 0) }, %struct.rel_test { i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.6, i32 0, i32 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i32 0, i32 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i32 0, i32 0) }, %struct.rel_test { i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.7, i32 0, i32 0), i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.8, i32 0, i32 0), i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.9, i32 0, i32 0) }]
@.str = [2 x i8] c"/\00"
@.str.1 = [6 x i8] c"/file\00"
@.str.2 = [5 x i8] c"file\00"
@.str.3 = [9 x i8] c"/a/b/c/d\00"
@.str.4 = [10 x i8] c"/a/b/file\00"
@.str.5 = [11 x i8] c"../../file\00"
@.str.6 = [11 x i8] c"/a/b/c/d/e\00"
@.str.7 = [10 x i8] c"/a/foobar\00"
@.str.8 = [12 x i8] c"/a/foo/file\00"
@.str.9 = [12 x i8] c"../foo/file\00"
@.str.10 = [46 x i8] c"relative_filename(%s, %s) -> %s, expected %s\0A\00"
define void @test_relative_filename() {
%1 = alloca i32
%2 = alloca %struct.rel_test*
%3 = alloca i8*
store i32 0, i32* %1
br label %4
4: ; preds = %41, %0
%5 = load i32, i32* %1
%6 = sext i32 %5 to i64
%7 = icmp ult i64 %6, 5
br i1 %7, label %8, label %44
8: ; preds = %4
%9 = load i32, i32* %1
%10 = sext i32 %9 to i64
%11 = getelementptr inbounds [5 x %struct.rel_test], [5 x %struct.rel_test]* @test_relative_filename.tests, i64 0, i64 %10
store %struct.rel_test* %11, %struct.rel_test** %2
%12 = load %struct.rel_test*, %struct.rel_test** %2
%13 = getelementptr inbounds %struct.rel_test, %struct.rel_test* %12, i32 0, i32 1
%14 = load i8*, i8** %13
%15 = load %struct.rel_test*, %struct.rel_test** %2
%16 = getelementptr inbounds %struct.rel_test, %struct.rel_test* %15, i32 0, i32 0
%17 = load i8*, i8** %16
%18 = call i32 (i8*, i8*, ...) bitcast (i32 (...)* @relative_filename to i32 (i8*, i8*, ...)*)(i8* %14, i8* %17)
%19 = sext i32 %18 to i64
%20 = inttoptr i64 %19 to i8*
store i8* %20, i8** %3
%21 = load %struct.rel_test*, %struct.rel_test** %2
%22 = getelementptr inbounds %struct.rel_test, %struct.rel_test* %21, i32 0, i32 2
%23 = load i8*, i8** %22
%24 = load i8*, i8** %3
%25 = call i32 (i8*, i8*, ...) bitcast (i32 (...)* @streq to i32 (i8*, i8*, ...)*)(i8* %23, i8* %24)
%26 = icmp ne i32 %25, 0
br i1 %26, label %39, label %27
27: ; preds = %8
%28 = load %struct.rel_test*, %struct.rel_test** %2
%29 = getelementptr inbounds %struct.rel_test, %struct.rel_test* %28, i32 0, i32 1
%30 = load i8*, i8** %29
%31 = load %struct.rel_test*, %struct.rel_test** %2
%32 = getelementptr inbounds %struct.rel_test, %struct.rel_test* %31, i32 0, i32 0
%33 = load i8*, i8** %32
%34 = load i8*, i8** %3
%35 = load %struct.rel_test*, %struct.rel_test** %2
%36 = getelementptr inbounds %struct.rel_test, %struct.rel_test* %35, i32 0, i32 2
%37 = load i8*, i8** %36
%38 = call i32 (i8*, i8*, i8*, i8*, i8*, ...) bitcast (i32 (...)* @fail to i32 (i8*, i8*, i8*, i8*, i8*, ...)*)(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.10, i64 0, i64 0), i8* %30, i8* %33, i8* %34, i8* %37)
br label %39
39: ; preds = %27, %8
%40 = load i8*, i8** %3
br label %41
41: ; preds = %39
%42 = load i32, i32* %1
%43 = add nsw i32 %42, 1
store i32 %43, i32* %1
br label %4
44: ; preds = %4
ret void
}

LLVM IR (-O2):
%struct.rel_test = type { i8*, i8*, i8* }
@.str = [2 x i8] c"/\00"
@.str.1 = [6 x i8] c"/file\00"
@.str.2 = [5 x i8] c"file\00"
@.str.3 = [9 x i8] c"/a/b/c/d\00"
@.str.4 = [10 x i8] c"/a/b/file\00"
@.str.5 = [11 x i8] c"../../file\00"
@.str.6 = [11 x i8] c"/a/b/c/d/e\00"
@.str.7 = [10 x i8] c"/a/foobar\00"
@.str.8 = [12 x i8] c"/a/foo/file\00"
@.str.9 = [12 x i8] c"../foo/file\00"
@.str.10 = [46 x i8] c"relative_filename(%s, %s) -> %s, expected %s\0A\00"
define void @test_relative_filename() {
%1 = tail call i32 (i8*, i8*, ...) bitcast (i32 (...)* @relative_filename to i32 (i8*, i8*, ...)*)(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%2 = sext i32 %1 to i64
%3 = inttoptr i64 %2 to i8*
%4 = tail call i32 (i8*, i8*, ...) bitcast (i32 (...)* @streq to i32 (i8*, i8*, ...)*)(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8* %3)
%5 = icmp eq i32 %4, 0
br i1 %5, label %6, label %8
6: ; preds = %0
%7 = tail call i32 (i8*, i8*, i8*, i8*, i8*, ...) bitcast (i32 (...)* @fail to i32 (i8*, i8*, i8*, i8*, i8*, ...)*)(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.10, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8* %3, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
br label %8
8: ; preds = %6, %0
tail call void @free(i8* %3)
%9 = tail call i32 (i8*, i8*, ...) bitcast (i32 (...)* @relative_filename to i32 (i8*, i8*, ...)*)(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%10 = sext i32 %9 to i64
%11 = inttoptr i64 %10 to i8*
%12 = tail call i32 (i8*, i8*, ...) bitcast (i32 (...)* @streq to i32 (i8*, i8*, ...)*)(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8* %11)
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %8
%15 = tail call i32 (i8*, i8*, i8*, i8*, i8*, ...) bitcast (i32 (...)* @fail to i32 (i8*, i8*, i8*, i8*, i8*, ...)*)(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.10, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8* %11, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %16
16: ; preds = %14, %8
tail call void @free(i8* %11)
%17 = tail call i32 (i8*, i8*, ...) bitcast (i32 (...)* @relative_filename to i32 (i8*, i8*, ...)*)(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
%18 = sext i32 %17 to i64
%19 = inttoptr i64 %18 to i8*
%20 = tail call i32 (i8*, i8*, ...) bitcast (i32 (...)* @streq to i32 (i8*, i8*, ...)*)(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.5, i64 0, i64 0), i8* %19)
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %16
%23 = tail call i32 (i8*, i8*, i8*, i8*, i8*, ...) bitcast (i32 (...)* @fail to i32 (i8*, i8*, i8*, i8*, i8*, ...)*)(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.10, i64 0, i64 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i8* %19, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.5, i64 0, i64 0))
br label %24
24: ; preds = %22, %16
tail call void @free(i8* %19)
%25 = tail call i32 (i8*, i8*, ...) bitcast (i32 (...)* @relative_filename to i32 (i8*, i8*, ...)*)(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.6, i64 0, i64 0))
%26 = sext i32 %25 to i64
%27 = inttoptr i64 %26 to i8*
%28 = tail call i32 (i8*, i8*, ...) bitcast (i32 (...)* @streq to i32 (i8*, i8*, ...)*)(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i8* %27)
%29 = icmp eq i32 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %24
%31 = tail call i32 (i8*, i8*, i8*, i8*, i8*, ...) bitcast (i32 (...)* @fail to i32 (i8*, i8*, i8*, i8*, i8*, ...)*)(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.10, i64 0, i64 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.6, i64 0, i64 0), i8* %27, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0))
br label %32
32: ; preds = %30, %24
tail call void @free(i8* %27)
%33 = tail call i32 (i8*, i8*, ...) bitcast (i32 (...)* @relative_filename to i32 (i8*, i8*, ...)*)(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.8, i64 0, i64 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.7, i64 0, i64 0))
%34 = sext i32 %33 to i64
%35 = inttoptr i64 %34 to i8*
%36 = tail call i32 (i8*, i8*, ...) bitcast (i32 (...)* @streq to i32 (i8*, i8*, ...)*)(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.9, i64 0, i64 0), i8* %35)
%37 = icmp eq i32 %36, 0
br i1 %37, label %38, label %40
38: ; preds = %32
%39 = tail call i32 (i8*, i8*, i8*, i8*, i8*, ...) bitcast (i32 (...)* @fail to i32 (i8*, i8*, i8*, i8*, i8*, ...)*)(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.10, i64 0, i64 0), i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.8, i64 0, i64 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.7, i64 0, i64 0), i8* %35, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.9, i64 0, i64 0))
br label %40
40: ; preds = %38, %32
tail call void @free(i8* %35)
ret void
}