#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 */
int /*<<< orphan*/ free(char *);
char *getenv(char *);
int /*<<< orphan*/ relpath(char *, char *);
char *xrealpath(char const *);
char *xstrdup(int /*<<< orphan*/);
char *get_relpath(const char *target) {
  char *root = getenv("REDO_ROOT");
  char *abstarget = xrealpath(target);
  if (!abstarget)
    return NULL;
  char *path = xstrdup(relpath(abstarget, root));
  free(abstarget);
  return path;
}


x86 assembly (clang, -O0):
<get_relpath>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
lea    0xe9d(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <xrealpath@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1191 <get_relpath+0x41>
movq   $0x0,-0x8(%rbp)
jmp    11ba <get_relpath+0x6a>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1060 <relpath@plt>
mov    %eax,%edi
call   1070 <xstrdup@plt>
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <free@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_relpath>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
lea    0xea2(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%r14
mov    %rbx,%rdi
call   1050 <xrealpath@plt>
test   %rax,%rax
je     1198 <get_relpath+0x48>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r14,%rsi
call   1060 <relpath@plt>
mov    %eax,%edi
call   1070 <xstrdup@plt>
mov    %rax,%r14
mov    %rbx,%rdi
call   1040 <free@plt>
mov    %r14,%rax
jmp    119a <get_relpath+0x4a>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [10 x i8] c"REDO_ROOT\00"
define i8* @get_relpath(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
store i8* %0, i8** %3
%7 = call i8* @getenv(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
store i8* %7, i8** %4
%8 = load i8*, i8** %3
%9 = call i8* @xrealpath(i8* %8)
store i8* %9, i8** %5
%10 = load i8*, i8** %5
%11 = icmp ne i8* %10, null
br i1 %11, label %13, label %12
12: ; preds = %1
store i8* null, i8** %2
br label %21
13: ; preds = %1
%14 = load i8*, i8** %5
%15 = load i8*, i8** %4
%16 = call i32 @relpath(i8* %14, i8* %15)
%17 = call i8* @xstrdup(i32 %16)
store i8* %17, i8** %6
%18 = load i8*, i8** %5
%19 = call i32 @free(i8* %18)
%20 = load i8*, i8** %6
store i8* %20, i8** %2
br label %21
21: ; preds = %13, %12
%22 = load i8*, i8** %2
ret i8* %22
}

LLVM IR (-O2):
@.str = [10 x i8] c"REDO_ROOT\00"
define i8* @get_relpath(i8* %0) {
%2 = tail call i8* @getenv(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%3 = tail call i8* @xrealpath(i8* %0)
%4 = icmp eq i8* %3, null
br i1 %4, label %9, label %5
5: ; preds = %1
%6 = tail call i32 @relpath(i8* %3, i8* %2)
%7 = tail call i8* @xstrdup(i32 %6)
%8 = tail call i32 @free(i8* %3)
br label %9
9: ; preds = %1, %5
%10 = phi i8* [ %7, %5 ], [ null, %1 ]
ret i8* %10
}