#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__ malloc(int);
int /*<<< orphan*/ strcat(char *, char const *);
int /*<<< orphan*/ strcpy(char *, char const *);
int strlen(char const *);
char *join_path(const char *path, const char *filename) {
  char *result = NULL;
  if (path && filename) {
    result = (char *)malloc(strlen(path) + strlen(filename) + 2);
    if (result) {
      strcpy(result, path);
      if (*(path + (strlen(path) - 1)) != '/')
        strcat(result, "/");
      while (*filename == '/')
        filename++;
      strcat(result, filename);
    }
  }
  return result;
}


x86 assembly (clang, -O0):
<join_path>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     121e <join_path+0xde>
cmpq   $0x0,-0x10(%rbp)
je     121e <join_path+0xde>
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x1c(%rbp),%edi
add    %eax,%edi
add    $0x2,%edi
call   1050 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1219 <join_path+0xd9>
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x2f,%eax
je     11e6 <join_path+0xa6>
mov    -0x18(%rbp),%rdi
lea    0xe1f(%rip),%rsi
call   1060 <strcat@plt>
jmp    11eb <join_path+0xab>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
jne    120c <join_path+0xcc>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    11eb <join_path+0xab>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1060 <strcat@plt>
jmp    121e <join_path+0xde>
mov    -0x18(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<join_path>:
push   %r15
push   %r14
push   %rbx
xor    %r14d,%r14d
test   %rdi,%rdi
je     11ca <join_path+0x8a>
mov    %rsi,%rbx
test   %rsi,%rsi
je     11ca <join_path+0x8a>
mov    %rdi,%r15
call   1040 <strlen@plt>
mov    %eax,%r14d
mov    %rbx,%rdi
call   1040 <strlen@plt>
lea    (%r14,%rax,1),%edi
add    $0x2,%edi
call   1050 <malloc@plt>
test   %rax,%rax
je     11c7 <join_path+0x87>
mov    %rax,%r14
mov    %rax,%rdi
mov    %r15,%rsi
call   1030 <strcpy@plt>
mov    %r15,%rdi
call   1040 <strlen@plt>
cltq
cmpb   $0x2f,-0x1(%rax,%r15,1)
je     11a8 <join_path+0x68>
lea    0xe60(%rip),%rsi
mov    %r14,%rdi
call   1060 <strcat@plt>
add    $0xffffffffffffffff,%rbx
nopl   0x0(%rax)
cmpb   $0x2f,0x1(%rbx)
lea    0x1(%rbx),%rbx
je     11b0 <join_path+0x70>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1060 <strcat@plt>
jmp    11ca <join_path+0x8a>
xor    %r14d,%r14d
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

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

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