#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*/ memcpy(char *, char const *, long);
int /*<<< orphan*/ strcpy(char *, char const *);
long strlen(char const *);
char *join_path(const char *path, int dirsep, const char *name) {
  long len = strlen(path);
  char *buf = (char *)malloc(sizeof(char) *
                             (len + strlen(name) + (dirsep ? 1 : 0) + 1));
  if (!buf)
    return 0;
  memcpy(buf, path, len);
  if (dirsep) {
    strcpy(buf + len, "/");
    len++;
  }
  strcpy(buf + len, name);
  return buf;
}


x86 assembly (clang, -O0):
<join_path>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,%rcx
mov    -0x38(%rbp),%rax
add    %rcx,%rax
mov    -0x14(%rbp),%esi
xor    %ecx,%ecx
mov    $0x1,%edx
cmp    $0x0,%esi
cmovne %edx,%ecx
movslq %ecx,%rcx
add    %rcx,%rax
add    $0x1,%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11bc <join_path+0x7c>
movq   $0x0,-0x8(%rbp)
jmp    1210 <join_path+0xd0>
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1050 <memcpy@plt>
cmpl   $0x0,-0x14(%rbp)
je     11f7 <join_path+0xb7>
mov    -0x30(%rbp),%rdi
add    -0x28(%rbp),%rdi
lea    0xe1a(%rip),%rsi
call   1030 <strcpy@plt>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
mov    -0x30(%rbp),%rdi
add    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<join_path>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdx,%r14
mov    %esi,%r12d
mov    %rdi,%rbp
call   1040 <strlen@plt>
mov    %rax,%r15
mov    %r14,%rdi
call   1040 <strlen@plt>
cmp    $0x1,%r12d
mov    %r15d,%ecx
sbb    $0xffffffff,%ecx
lea    (%rcx,%rax,1),%edi
add    $0x1,%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     11b5 <join_path+0x75>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %rbp,%rsi
mov    %r15,%rdx
call   1050 <memcpy@plt>
test   %r12d,%r12d
je     11a5 <join_path+0x65>
lea    (%rbx,%r15,1),%rdi
lea    0xe64(%rip),%rsi
call   1030 <strcpy@plt>
add    $0x1,%r15
mov    %rbx,%rdi
add    %r15,%rdi
mov    %r14,%rsi
call   1030 <strcpy@plt>
jmp    11b7 <join_path+0x77>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

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