#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 *malloc(int);
int /*<<< orphan*/ memcpy(char *, char const *, int);
char *strcat(char *, char const *);
int /*<<< orphan*/ strcpy(char *, char *);
char *strdup(char const *);
int strlen(char const *);
char *strrchr(char const *, char);
char *makeIncludeRelative(const char *base, const char *rel) {
  if (rel[0] == '/')
    return strdup(rel);
  int blen = strlen(base);
  int rlen = strlen(rel);
  char *result = malloc(blen + rlen + 1);
  strcpy(result, "");
  char *start = strrchr(base, '/');
  if (start != NULL)
    memcpy(result, base, start - base + 1);
  return strcat(result, rel);
}


x86 assembly (clang, -O0):
<makeIncludeRelative>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
jne    11a2 <makeIncludeRelative+0x32>
mov    -0x18(%rbp),%rdi
call   1090 <strdup@plt>
mov    %rax,-0x8(%rbp)
jmp    1228 <makeIncludeRelative+0xb8>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%edi
add    -0x20(%rbp),%edi
add    $0x1,%edi
call   1070 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
lea    0xe29(%rip),%rsi
call   1030 <strcpy@plt>
mov    -0x10(%rbp),%rdi
mov    $0x2f,%esi
call   1050 <strrchr@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     1217 <makeIncludeRelative+0xa7>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x30(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
add    $0x1,%rax
mov    %eax,%edx
call   1060 <memcpy@plt>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1080 <strcat@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<makeIncludeRelative>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%rbx
cmpb   $0x2f,(%rsi)
jne    118a <makeIncludeRelative+0x1a>
mov    %rbx,%rdi
pop    %rbx
pop    %r14
pop    %r15
jmp    1090 <strdup@plt>
mov    %rdi,%r14
call   1040 <strlen@plt>
mov    %eax,%r15d
mov    %rbx,%rdi
call   1040 <strlen@plt>
lea    (%r15,%rax,1),%edi
add    $0x1,%edi
call   1070 <malloc@plt>
mov    %rax,%r15
lea    0xe4d(%rip),%rsi
mov    %rax,%rdi
call   1030 <strcpy@plt>
mov    %r14,%rdi
mov    $0x2f,%esi
call   1050 <strrchr@plt>
test   %rax,%rax
je     11e0 <makeIncludeRelative+0x70>
sub    %r14d,%eax
add    $0x1,%eax
mov    %r15,%rdi
mov    %r14,%rsi
mov    %eax,%edx
call   1060 <memcpy@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
pop    %rbx
pop    %r14
pop    %r15
jmp    1080 <strcat@plt>

LLVM IR (-O0):
@.str = [1 x i8] zeroinitializer
define i8* @makeIncludeRelative(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
%10 = load i8*, i8** %5
%11 = getelementptr inbounds i8, i8* %10, i64 0
%12 = load i8, i8* %11
%13 = sext i8 %12 to i32
%14 = icmp eq i32 %13, 47
br i1 %14, label %15, label %18
15: ; preds = %2
%16 = load i8*, i8** %5
%17 = call i8* @strdup(i8* %16)
store i8* %17, i8** %3
br label %49
18: ; preds = %2
%19 = load i8*, i8** %4
%20 = call i32 @strlen(i8* %19)
store i32 %20, i32* %6
%21 = load i8*, i8** %5
%22 = call i32 @strlen(i8* %21)
store i32 %22, i32* %7
%23 = load i32, i32* %6
%24 = load i32, i32* %7
%25 = add nsw i32 %23, %24
%26 = add nsw i32 %25, 1
%27 = call i8* @malloc(i32 %26)
store i8* %27, i8** %8
%28 = load i8*, i8** %8
%29 = call i32 @strcpy(i8* %28, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0))
%30 = load i8*, i8** %4
%31 = call i8* @strrchr(i8* %30, i8 signext 47)
store i8* %31, i8** %9
%32 = load i8*, i8** %9
%33 = icmp ne i8* %32, null
br i1 %33, label %34, label %45
34: ; preds = %18
%35 = load i8*, i8** %8
%36 = load i8*, i8** %4
%37 = load i8*, i8** %9
%38 = load i8*, i8** %4
%39 = ptrtoint i8* %37 to i64
%40 = ptrtoint i8* %38 to i64
%41 = sub i64 %39, %40
%42 = add nsw i64 %41, 1
%43 = trunc i64 %42 to i32
%44 = call i32 @memcpy(i8* %35, i8* %36, i32 %43)
br label %45
45: ; preds = %34, %18
%46 = load i8*, i8** %8
%47 = load i8*, i8** %5
%48 = call i8* @strcat(i8* %46, i8* %47)
store i8* %48, i8** %3
br label %49
49: ; preds = %45, %15
%50 = load i8*, i8** %3
ret i8* %50
}

LLVM IR (-O2):
@.str = [1 x i8] zeroinitializer
define i8* @makeIncludeRelative(i8* %0, i8* %1) {
%3 = load i8, i8* %1
%4 = icmp eq i8 %3, 47
br i1 %4, label %5, label %7
5: ; preds = %2
%6 = tail call i8* @strdup(i8* %1)
br label %25
7: ; preds = %2
%8 = tail call i32 @strlen(i8* %0)
%9 = tail call i32 @strlen(i8* %1)
%10 = add i32 %8, 1
%11 = add i32 %10, %9
%12 = tail call i8* @malloc(i32 %11)
%13 = tail call i32 @strcpy(i8* %12, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0))
%14 = tail call i8* @strrchr(i8* dereferenceable(1) %0, i8 signext 47)
%15 = icmp eq i8* %14, null
br i1 %15, label %23, label %16
16: ; preds = %7
%17 = ptrtoint i8* %14 to i64
%18 = ptrtoint i8* %0 to i64
%19 = sub i64 %17, %18
%20 = trunc i64 %19 to i32
%21 = add i32 %20, 1
%22 = tail call i32 @memcpy(i8* %12, i8* %0, i32 %21)
br label %23
23: ; preds = %16, %7
%24 = tail call i8* @strcat(i8* dereferenceable(1) %12, i8* %1)
br label %25
25: ; preds = %23, %5
%26 = phi i8* [ %6, %5 ], [ %12, %23 ]
ret i8* %26
}