#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 strlen(char *);
int /*<<< orphan*/ strncat(char *, char const *, int);
int /*<<< orphan*/ strncpy(char *, char const *, int);
void dir_join(char *dst, const char *dirname, const char *filename, int len) {
  strncpy(dst, dirname, len);
  if (dst[strlen(dst) - 1] != '/')
    strncat(dst, "/", len - strlen(dst));
  strncat(dst, filename, strlen(dst));
}


x86 assembly (clang, -O0):
<dir_join>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1030 <strncpy@plt>
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x2f,%eax
je     11ad <dir_join+0x7d>
mov    -0x8(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x34(%rbp),%edx
mov    -0x30(%rbp),%rdi
sub    %eax,%edx
lea    0xe58(%rip),%rsi
call   1050 <strncat@plt>
mov    -0x8(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x48(%rbp),%rdi
mov    -0x40(%rbp),%rsi
mov    %eax,%edx
call   1050 <strncat@plt>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dir_join>:
push   %rbp
push   %r14
push   %rbx
mov    %ecx,%ebp
mov    %rdx,%r14
mov    %rdi,%rbx
mov    %ecx,%edx
call   1030 <strncpy@plt>
mov    %rbx,%rdi
call   1040 <strlen@plt>
cltq
cmpb   $0x2f,-0x1(%rax,%rbx,1)
je     116f <dir_join+0x3f>
mov    %rbx,%rdi
call   1040 <strlen@plt>
sub    %eax,%ebp
lea    0xe9b(%rip),%rsi
mov    %rbx,%rdi
mov    %ebp,%edx
call   1050 <strncat@plt>
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %eax,%edx
pop    %rbx
pop    %r14
pop    %rbp
jmp    1050 <strncat@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"/\00"
define void @dir_join(i8* %0, i8* %1, i8* %2, i32 %3) {
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i8* %2, i8** %7
store i32 %3, i32* %8
%9 = load i8*, i8** %5
%10 = load i8*, i8** %6
%11 = load i32, i32* %8
%12 = call i32 @strncpy(i8* %9, i8* %10, i32 %11)
%13 = load i8*, i8** %5
%14 = load i8*, i8** %5
%15 = call i32 @strlen(i8* %14)
%16 = sub nsw i32 %15, 1
%17 = sext i32 %16 to i64
%18 = getelementptr inbounds i8, i8* %13, i64 %17
%19 = load i8, i8* %18
%20 = sext i8 %19 to i32
%21 = icmp ne i32 %20, 47
br i1 %21, label %22, label %29
22: ; preds = %4
%23 = load i8*, i8** %5
%24 = load i32, i32* %8
%25 = load i8*, i8** %5
%26 = call i32 @strlen(i8* %25)
%27 = sub nsw i32 %24, %26
%28 = call i32 @strncat(i8* %23, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 %27)
br label %29
29: ; preds = %22, %4
%30 = load i8*, i8** %5
%31 = load i8*, i8** %7
%32 = load i8*, i8** %5
%33 = call i32 @strlen(i8* %32)
%34 = call i32 @strncat(i8* %30, i8* %31, i32 %33)
ret void
}

LLVM IR (-O2):
@.str = [2 x i8] c"/\00"
define void @dir_join(i8* %0, i8* %1, i8* %2, i32 %3) {
%5 = tail call i32 @strncpy(i8* %0, i8* %1, i32 %3)
%6 = tail call i32 @strlen(i8* %0)
%7 = add nsw i32 %6, -1
%8 = sext i32 %7 to i64
%9 = getelementptr inbounds i8, i8* %0, i64 %8
%10 = load i8, i8* %9
%11 = icmp eq i8 %10, 47
br i1 %11, label %16, label %12
12: ; preds = %4
%13 = tail call i32 @strlen(i8* %0)
%14 = sub nsw i32 %3, %13
%15 = tail call i32 @strncat(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 %14)
br label %16
16: ; preds = %12, %4
%17 = tail call i32 @strlen(i8* %0)
%18 = tail call i32 @strncat(i8* %0, i8* %2, i32 %17)
ret void
}