#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*/ assert(int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
scalar_t__ strlen(char *);
int /*<<< orphan*/ strncat(char *, char *, int);
void path_join(char *buffer, int buffer_size, char *dirname, char *basename) {
  int remaining = buffer_size - 1;
  memset(buffer, 0, buffer_size);
  strncat(buffer, dirname, remaining);
  remaining -= strlen(dirname);
  assert(remaining > 0);
  strncat(buffer, "/", remaining);
  remaining -= strlen("/");
  assert(remaining > 0);
  strncat(buffer, basename, remaining);
}


x86 assembly (clang, -O0):
<path_join>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0xc(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x24(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%edx
xor    %esi,%esi
call   1040 <memset@plt>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1050 <strncat@plt>
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,%rcx
movslq -0x24(%rbp),%rax
sub    %rcx,%rax
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
setg   %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    -0x8(%rbp),%rdi
mov    -0x24(%rbp),%edx
lea    0xe4d(%rip),%rsi
call   1050 <strncat@plt>
lea    0xe41(%rip),%rdi
call   1030 <strlen@plt>
mov    %rax,%rcx
movslq -0x24(%rbp),%rax
sub    %rcx,%rax
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
setg   %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    -0x8(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1050 <strncat@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<path_join>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rcx,%r14
mov    %rdx,%rbx
mov    %esi,%edx
mov    %rdi,%r15
lea    -0x1(%rdx),%ebp
xor    %esi,%esi
call   1040 <memset@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1050 <strncat@plt>
mov    %rbx,%rdi
call   1030 <strlen@plt>
sub    %eax,%ebp
xor    %edi,%edi
test   %ebp,%ebp
setg   %dil
call   1060 <assert@plt>
lea    0xe79(%rip),%rsi
mov    %r15,%rdi
mov    %ebp,%edx
call   1050 <strncat@plt>
add    $0xffffffff,%ebp
xor    %edi,%edi
test   %ebp,%ebp
setg   %dil
call   1060 <assert@plt>
mov    %r15,%rdi
mov    %r14,%rsi
mov    %ebp,%edx
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <strncat@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"/\00"
define void @path_join(i8* %0, i32 %1, i8* %2, i8* %3) {
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
store i8* %0, i8** %5
store i32 %1, i32* %6
store i8* %2, i8** %7
store i8* %3, i8** %8
%10 = load i32, i32* %6
%11 = sub nsw i32 %10, 1
store i32 %11, i32* %9
%12 = load i8*, i8** %5
%13 = load i32, i32* %6
%14 = call i32 @memset(i8* %12, i32 0, i32 %13)
%15 = load i8*, i8** %5
%16 = load i8*, i8** %7
%17 = load i32, i32* %9
%18 = call i32 @strncat(i8* %15, i8* %16, i32 %17)
%19 = load i8*, i8** %7
%20 = call i64 @strlen(i8* %19)
%21 = load i32, i32* %9
%22 = sext i32 %21 to i64
%23 = sub nsw i64 %22, %20
%24 = trunc i64 %23 to i32
store i32 %24, i32* %9
%25 = load i32, i32* %9
%26 = icmp sgt i32 %25, 0
%27 = zext i1 %26 to i32
%28 = call i32 @assert(i32 %27)
%29 = load i8*, i8** %5
%30 = load i32, i32* %9
%31 = call i32 @strncat(i8* %29, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 %30)
%32 = call i64 @strlen(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%33 = load i32, i32* %9
%34 = sext i32 %33 to i64
%35 = sub nsw i64 %34, %32
%36 = trunc i64 %35 to i32
store i32 %36, i32* %9
%37 = load i32, i32* %9
%38 = icmp sgt i32 %37, 0
%39 = zext i1 %38 to i32
%40 = call i32 @assert(i32 %39)
%41 = load i8*, i8** %5
%42 = load i8*, i8** %8
%43 = load i32, i32* %9
%44 = call i32 @strncat(i8* %41, i8* %42, i32 %43)
ret void
}

LLVM IR (-O2):
@.str = [2 x i8] c"/\00"
define void @path_join(i8* %0, i32 %1, i8* %2, i8* %3) {
%5 = add nsw i32 %1, -1
%6 = tail call i32 @memset(i8* %0, i32 0, i32 %1)
%7 = tail call i32 @strncat(i8* %0, i8* %2, i32 %5)
%8 = tail call i64 @strlen(i8* dereferenceable(1) %2)
%9 = trunc i64 %8 to i32
%10 = sub i32 %5, %9
%11 = icmp sgt i32 %10, 0
%12 = zext i1 %11 to i32
%13 = tail call i32 @assert(i32 %12)
%14 = tail call i32 @strncat(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 %10)
%15 = add i32 %10, -1
%16 = icmp sgt i32 %15, 0
%17 = zext i1 %16 to i32
%18 = tail call i32 @assert(i32 %17)
%19 = tail call i32 @strncat(i8* %0, i8* %3, i32 %15)
ret void
}