#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 */
size_t PATH_MAX;
int /*<<< orphan*/ memcpy(char *, char const *, size_t);
size_t strlen(char const *);
const char *path_join(const char *path, size_t prefixlen, char buf[PATH_MAX]) {
  size_t pathlen;
  if (path[0] == '/')
    return path;
  pathlen = strlen(path);
  if (prefixlen + pathlen + 1 >= PATH_MAX)
    return NULL;
  memcpy(buf + prefixlen, path, pathlen + 1);
  return buf;
}


x86 assembly (clang, -O0):
<path_join>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
jne    1151 <path_join+0x31>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11a8 <path_join+0x88>
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rax
add    -0x28(%rbp),%rax
add    $0x1,%rax
mov    0x2e7f(%rip),%rcx
cmp    (%rcx),%rax
jb     1187 <path_join+0x67>
movq   $0x0,-0x8(%rbp)
jmp    11a8 <path_join+0x88>
mov    -0x20(%rbp),%rdi
add    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x28(%rbp),%rdx
add    $0x1,%rdx
call   1040 <memcpy@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<path_join>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%rbx
cmpb   $0x2f,(%rdi)
jne    1132 <path_join+0x12>
mov    %rbx,%r14
jmp    116e <path_join+0x4e>
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rbx,%rdi
call   1030 <strlen@plt>
lea    (%rax,%r15,1),%rcx
add    $0x1,%rcx
mov    0x2ea1(%rip),%rdx
cmp    (%rdx),%rcx
jae    116b <path_join+0x4b>
add    $0x1,%rax
add    %r14,%r15
mov    %r15,%rdi
mov    %rbx,%rsi
mov    %rax,%rdx
call   1040 <memcpy@plt>
jmp    116e <path_join+0x4e>
xor    %r14d,%r14d
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@PATH_MAX = global i64 0
define i8* @path_join(i8* %0, i64 %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i64
store i8* %0, i8** %5
store i64 %1, i64* %6
store i8* %2, i8** %7
%9 = load i64, i64* @PATH_MAX
%10 = load i8*, i8** %5
%11 = getelementptr inbounds i8, i8* %10, i64 0
%12 = load i8, i8* %11
%13 = sext i8 %12 to i32
%14 = icmp eq i32 %13, 47
br i1 %14, label %15, label %17
15: ; preds = %3
%16 = load i8*, i8** %5
store i8* %16, i8** %4
br label %36
17: ; preds = %3
%18 = load i8*, i8** %5
%19 = call i64 @strlen(i8* %18)
store i64 %19, i64* %8
%20 = load i64, i64* %6
%21 = load i64, i64* %8
%22 = add i64 %20, %21
%23 = add i64 %22, 1
%24 = load i64, i64* @PATH_MAX
%25 = icmp uge i64 %23, %24
br i1 %25, label %26, label %27
26: ; preds = %17
store i8* null, i8** %4
br label %36
27: ; preds = %17
%28 = load i8*, i8** %7
%29 = load i64, i64* %6
%30 = getelementptr inbounds i8, i8* %28, i64 %29
%31 = load i8*, i8** %5
%32 = load i64, i64* %8
%33 = add i64 %32, 1
%34 = call i32 @memcpy(i8* %30, i8* %31, i64 %33)
%35 = load i8*, i8** %7
store i8* %35, i8** %4
br label %36
36: ; preds = %27, %26, %15
%37 = load i8*, i8** %4
ret i8* %37
}

LLVM IR (-O2):
@PATH_MAX = global i64 0
define i8* @path_join(i8* %0, i64 %1, i8* %2) {
%4 = load i8, i8* %0
%5 = icmp eq i8 %4, 47
br i1 %5, label %15, label %6
6: ; preds = %3
%7 = tail call i64 @strlen(i8* %0)
%8 = add i64 %7, 1
%9 = add i64 %8, %1
%10 = load i64, i64* @PATH_MAX
%11 = icmp ult i64 %9, %10
br i1 %11, label %12, label %15
12: ; preds = %6
%13 = getelementptr inbounds i8, i8* %2, i64 %1
%14 = tail call i32 @memcpy(i8* %13, i8* %0, i64 %8)
br label %15
15: ; preds = %6, %3, %12
%16 = phi i8* [ %2, %12 ], [ %0, %3 ], [ null, %6 ]
ret i8* %16
}