#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 */
int /*<<< orphan*/ EINVAL;
int /*<<< orphan*/ ENAMETOOLONG;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ strcat(char *, char *);
size_t strlen(char const *);
int /*<<< orphan*/ strncat(char *, char const *, size_t);
int /*<<< orphan*/ strncpy(char *, char const *, size_t);
char *join(char *outbuf, size_t outbuf_size, const char *path,
           const char *filename) {
  if (outbuf && (outbuf_size > 0) && path && filename) {
    size_t len_path = strlen(path);
    size_t len_name = strlen(filename);
    if (path[len_path - 1] == '/') {
      len_path--;
    }
    if (filename[0] == '/') {
      filename++;
      len_name--;
    }
    if ((len_path + len_name + 1) < outbuf_size) {
      strncpy(outbuf, path, len_path);
      strcat(outbuf, "/");
      strncat(outbuf, filename, len_name);
    } else {
      errno = ENAMETOOLONG;
      outbuf = NULL;
    }
  } else {
    errno = EINVAL;
    outbuf = NULL;
  }
  return outbuf;
}


x86 assembly (clang, -O0):
<join>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     1257 <join+0x117>
cmpq   $0x0,-0x10(%rbp)
jbe    1257 <join+0x117>
cmpq   $0x0,-0x18(%rbp)
je     1257 <join+0x117>
cmpq   $0x0,-0x20(%rbp)
je     1257 <join+0x117>
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rax
mov    -0x28(%rbp),%rcx
sub    $0x1,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x2f,%eax
jne    11c3 <join+0x83>
mov    -0x28(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
jne    11eb <join+0xab>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
mov    -0x30(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rax
add    -0x30(%rbp),%rax
add    $0x1,%rax
cmp    -0x10(%rbp),%rax
jae    1238 <join+0xf8>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1030 <strncpy@plt>
mov    -0x8(%rbp),%rdi
lea    0xde3(%rip),%rsi
call   1060 <strcat@plt>
mov    -0x8(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x30(%rbp),%rdx
call   1050 <strncat@plt>
jmp    1252 <join+0x112>
mov    0x2d91(%rip),%rax
mov    (%rax),%ecx
mov    0x2d98(%rip),%rax
mov    %ecx,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    1271 <join+0x131>
mov    0x2d7a(%rip),%rax
mov    (%rax),%ecx
mov    0x2d79(%rip),%rax
mov    %ecx,(%rax)
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<join>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %rdi,%rdi
je     11e5 <join+0xa5>
mov    %rsi,%r12
test   %rsi,%rsi
je     11e5 <join+0xa5>
mov    %rdx,%r15
test   %rdx,%rdx
je     11e5 <join+0xa5>
mov    %rcx,%r14
test   %rcx,%rcx
je     11e5 <join+0xa5>
mov    %rdi,%rbp
mov    %r15,%rdi
call   1040 <strlen@plt>
mov    %rax,%rbx
mov    %r14,%rdi
call   1040 <strlen@plt>
lea    -0x1(%rbx),%rdx
cmpb   $0x2f,-0x1(%r15,%rbx,1)
cmovne %rbx,%rdx
mov    %rax,%r13
mov    (%r14),%al
xor    %ecx,%ecx
cmp    $0x2f,%al
sete   %cl
sub    %rcx,%r13
lea    (%rdx,%r13,1),%rcx
add    $0x1,%rcx
cmp    %r12,%rcx
jae    11ee <join+0xae>
xor    %ecx,%ecx
cmp    $0x2f,%al
sete   %cl
add    %rcx,%r14
mov    %rbp,%rdi
mov    %r15,%rsi
call   1030 <strncpy@plt>
lea    0xe33(%rip),%rsi
mov    %rbp,%rdi
call   1060 <strcat@plt>
mov    %rbp,%rdi
mov    %r14,%rsi
mov    %r13,%rdx
call   1050 <strncat@plt>
jmp    1202 <join+0xc2>
mov    0x2dec(%rip),%rax
jmp    11f5 <join+0xb5>
mov    0x2ddb(%rip),%rax
mov    (%rax),%eax
mov    0x2de2(%rip),%rcx
mov    %eax,(%rcx)
xor    %ebp,%ebp
mov    %rbp,%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"
@ENAMETOOLONG = global i32 0
@errno = global i32 0
@EINVAL = global i32 0
define i8* @join(i8* %0, i64 %1, i8* %2, i8* %3) {
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i64
store i8* %0, i8** %5
store i64 %1, i64* %6
store i8* %2, i8** %7
store i8* %3, i8** %8
%11 = load i8*, i8** %5
%12 = icmp ne i8* %11, null
br i1 %12, label %13, label %69
13: ; preds = %4
%14 = load i64, i64* %6
%15 = icmp ugt i64 %14, 0
br i1 %15, label %16, label %69
16: ; preds = %13
%17 = load i8*, i8** %7
%18 = icmp ne i8* %17, null
br i1 %18, label %19, label %69
19: ; preds = %16
%20 = load i8*, i8** %8
%21 = icmp ne i8* %20, null
br i1 %21, label %22, label %69
22: ; preds = %19
%23 = load i8*, i8** %7
%24 = call i64 @strlen(i8* %23)
store i64 %24, i64* %9
%25 = load i8*, i8** %8
%26 = call i64 @strlen(i8* %25)
store i64 %26, i64* %10
%27 = load i8*, i8** %7
%28 = load i64, i64* %9
%29 = sub i64 %28, 1
%30 = getelementptr inbounds i8, i8* %27, i64 %29
%31 = load i8, i8* %30
%32 = sext i8 %31 to i32
%33 = icmp eq i32 %32, 47
br i1 %33, label %34, label %37
34: ; preds = %22
%35 = load i64, i64* %9
%36 = add i64 %35, -1
store i64 %36, i64* %9
br label %37
37: ; preds = %34, %22
%38 = load i8*, i8** %8
%39 = getelementptr inbounds i8, i8* %38, i64 0
%40 = load i8, i8* %39
%41 = sext i8 %40 to i32
%42 = icmp eq i32 %41, 47
br i1 %42, label %43, label %48
43: ; preds = %37
%44 = load i8*, i8** %8
%45 = getelementptr inbounds i8, i8* %44, i32 1
store i8* %45, i8** %8
%46 = load i64, i64* %10
%47 = add i64 %46, -1
store i64 %47, i64* %10
br label %48
48: ; preds = %43, %37
%49 = load i64, i64* %9
%50 = load i64, i64* %10
%51 = add i64 %49, %50
%52 = add i64 %51, 1
%53 = load i64, i64* %6
%54 = icmp ult i64 %52, %53
br i1 %54, label %55, label %66
55: ; preds = %48
%56 = load i8*, i8** %5
%57 = load i8*, i8** %7
%58 = load i64, i64* %9
%59 = call i32 @strncpy(i8* %56, i8* %57, i64 %58)
%60 = load i8*, i8** %5
%61 = call i32 @strcat(i8* %60, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%62 = load i8*, i8** %5
%63 = load i8*, i8** %8
%64 = load i64, i64* %10
%65 = call i32 @strncat(i8* %62, i8* %63, i64 %64)
br label %68
66: ; preds = %48
%67 = load i32, i32* @ENAMETOOLONG
store i32 %67, i32* @errno
store i8* null, i8** %5
br label %68
68: ; preds = %66, %55
br label %71
69: ; preds = %19, %16, %13, %4
%70 = load i32, i32* @EINVAL
store i32 %70, i32* @errno
store i8* null, i8** %5
br label %71
71: ; preds = %69, %68
%72 = load i8*, i8** %5
ret i8* %72
}

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