#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(size_t);
int /*<<< orphan*/ strlcat(char *, char const *, size_t);
int /*<<< orphan*/ strlcpy(char *, char const *, size_t);
size_t strlen(char const *);
char *JoinPaths(const char *path1, const char *path2) {
  size_t len1 = strlen(path1);
  size_t len2 = strlen(path2);
  size_t totalSize = len1 + 1 + len2 + 1;
  char *outPath = malloc(totalSize);
  if (outPath == NULL) {
    return NULL;
  }
  strlcpy(outPath, path1, totalSize);
  if (len1 > 0 && len2 > 0) {
    if (path1[len1 - 1] != '/' && path2[0] != '/') {
      strlcat(outPath, "/", totalSize);
    }
  }
  strlcat(outPath, path2, totalSize);
  return outPath;
}


x86 assembly (clang, -O0):
<JoinPaths>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
add    $0x1,%rax
add    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
call   1050 <malloc@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    11a3 <JoinPaths+0x63>
movq   $0x0,-0x8(%rbp)
jmp    1225 <JoinPaths+0xe5>
mov    -0x38(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x30(%rbp),%rdx
call   1060 <strlcpy@plt>
cmpq   $0x0,-0x20(%rbp)
jbe    120c <JoinPaths+0xcc>
cmpq   $0x0,-0x28(%rbp)
jbe    120c <JoinPaths+0xcc>
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%rcx
sub    $0x1,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x2f,%eax
je     1207 <JoinPaths+0xc7>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
je     1207 <JoinPaths+0xc7>
mov    -0x38(%rbp),%rdi
mov    -0x30(%rbp),%rdx
lea    0xdfe(%rip),%rsi
call   1030 <strlcat@plt>
jmp    120c <JoinPaths+0xcc>
mov    -0x38(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x30(%rbp),%rdx
call   1030 <strlcat@plt>
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<JoinPaths>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r12
call   1040 <strlen@plt>
mov    %rax,%r13
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %rax,%rbp
lea    (%rax,%r13,1),%r15
add    $0x2,%r15
mov    %r15,%rdi
call   1050 <malloc@plt>
mov    %rax,%rbx
test   %rax,%rax
je     11c2 <JoinPaths+0x82>
mov    %rbx,%rdi
mov    %r12,%rsi
mov    %r15,%rdx
call   1060 <strlcpy@plt>
test   %r13,%r13
je     11b4 <JoinPaths+0x74>
test   %rbp,%rbp
je     11b4 <JoinPaths+0x74>
cmpb   $0x2f,-0x1(%r13,%r12,1)
je     11b4 <JoinPaths+0x74>
cmpb   $0x2f,(%r14)
je     11b4 <JoinPaths+0x74>
lea    0xe57(%rip),%rsi
mov    %rbx,%rdi
mov    %r15,%rdx
call   1030 <strlcat@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
call   1030 <strlcat@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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