#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 */
scalar_t__ calloc(int, int);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
size_t strlen(char *);
char *join_paths(char *base, char *filename) {
  char *returnfile = NULL;
  int len = strlen(filename) + 1;
  if (base) {
    len += strlen(base) + 1;
  }
  returnfile = (char *)calloc(len, sizeof(char));
  if (base) {
    strcpy(returnfile, base);
    if (returnfile[strlen(returnfile)] != '/')
      strcat(returnfile, "/");
  }
  strcat(returnfile, filename);
  return returnfile;
}


x86 assembly (clang, -O0):
<join_paths>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
add    $0x1,%rax
mov    %eax,-0x1c(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     118d <join_paths+0x4d>
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,%rcx
add    $0x1,%rcx
movslq -0x1c(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%edi
mov    $0x1,%esi
call   1050 <calloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     11f0 <join_paths+0xb0>
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,%rcx
mov    -0x28(%rbp),%rax
movsbl (%rax,%rcx,1),%eax
cmp    $0x2f,%eax
je     11eb <join_paths+0xab>
mov    -0x18(%rbp),%rdi
lea    0xe1a(%rip),%rsi
call   1060 <strcat@plt>
jmp    11f0 <join_paths+0xb0>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1060 <strcat@plt>
mov    -0x18(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<join_paths>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%r15
mov    %rsi,%rdi
call   1040 <strlen@plt>
mov    %rax,%rbx
test   %r15,%r15
je     11a0 <join_paths+0x60>
mov    %r15,%rdi
call   1040 <strlen@plt>
lea    (%rbx,%rax,1),%edi
add    $0x2,%edi
mov    $0x1,%esi
call   1050 <calloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r15,%rsi
call   1030 <strcpy@plt>
mov    %rbx,%rdi
call   1040 <strlen@plt>
cmpb   $0x2f,(%rbx,%rax,1)
je     11b2 <join_paths+0x72>
lea    0xe6a(%rip),%rsi
mov    %rbx,%rdi
call   1060 <strcat@plt>
jmp    11b2 <join_paths+0x72>
add    $0x1,%ebx
mov    %ebx,%edi
mov    $0x1,%esi
call   1050 <calloc@plt>
mov    %rax,%rbx
mov    %rbx,%rdi
mov    %r14,%rsi
call   1060 <strcat@plt>
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

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

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