#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*/ sprintf(char *, char *, char const *, ...);
size_t strlen(char const *);
char *_path_join(const char *part1, const char *part2) {
  size_t l1 = part1 ? strlen(part1) : 0;
  size_t l2 = strlen(part2);
  char *result = malloc(l1 + l2 + 10);
  if (l1 == 0) {
    sprintf(result, "%s", part2);
  } else if (part1[l1 - 1] == '/') {
    sprintf(result, "%s%s", part1, part2);
  } else {
    sprintf(result, "%s/%s", part1, part2);
  }
  return result;
}


x86 assembly (clang, -O0):
<_path_join>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     115d <_path_join+0x2d>
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x30(%rbp)
jmp    1168 <_path_join+0x38>
xor    %eax,%eax
mov    %rax,-0x30(%rbp)
jmp    1168 <_path_join+0x38>
mov    -0x30(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
add    -0x20(%rbp),%rdi
add    $0xa,%rdi
call   1040 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11b8 <_path_join+0x88>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rdx
lea    0xe5c(%rip),%rsi
mov    $0x0,%al
call   1050 <sprintf@plt>
jmp    120f <_path_join+0xdf>
mov    -0x8(%rbp),%rax
mov    -0x18(%rbp),%rcx
sub    $0x1,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x2f,%eax
jne    11f0 <_path_join+0xc0>
mov    -0x28(%rbp),%rdi
mov    -0x8(%rbp),%rdx
mov    -0x10(%rbp),%rcx
lea    0xe1c(%rip),%rsi
mov    $0x0,%al
call   1050 <sprintf@plt>
jmp    120a <_path_join+0xda>
mov    -0x28(%rbp),%rdi
mov    -0x8(%rbp),%rdx
mov    -0x10(%rbp),%rcx
lea    0xe02(%rip),%rsi
mov    $0x0,%al
call   1050 <sprintf@plt>
jmp    120f <_path_join+0xdf>
mov    -0x28(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_path_join>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r12
test   %rdi,%rdi
je     1160 <_path_join+0x20>
mov    %r12,%rdi
call   1040 <strlen@plt>
mov    %rax,%rbx
jmp    1162 <_path_join+0x22>
xor    %ebx,%ebx
mov    %r14,%rdi
call   1040 <strlen@plt>
lea    (%rbx,%rax,1),%rdi
add    $0xa,%rdi
call   1050 <malloc@plt>
mov    %rax,%r15
test   %rbx,%rbx
je     1190 <_path_join+0x50>
cmpb   $0x2f,-0x1(%rbx,%r12,1)
jne    119d <_path_join+0x5d>
lea    0xe72(%rip),%rsi
jmp    11a4 <_path_join+0x64>
mov    %r15,%rdi
mov    %r14,%rsi
call   1030 <strcpy@plt>
jmp    11b4 <_path_join+0x74>
lea    0xe61(%rip),%rsi
mov    %r15,%rdi
mov    %r12,%rdx
mov    %r14,%rcx
xor    %eax,%eax
call   1060 <sprintf@plt>
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

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