#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 */
char *calloc(size_t, int);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char const *);
int /*<<< orphan*/ strncat(char *, char const *, int);
size_t strnlen(char const *, int);
char *join_path(const char *dir, const char *filename) {
  size_t path_len = strnlen(dir, 500);
  char *fpath = calloc(path_len + 32, 1);
  if (path_len == 500) {
    return NULL;
  }
  strcpy(fpath, dir);
  strcat(fpath, "/");
  strncat(fpath, filename, 30);
  return fpath;
}


x86 assembly (clang, -O0):
<join_path>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x1f4,%esi
call   1040 <strnlen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
add    $0x20,%rdi
mov    $0x1,%esi
call   1060 <calloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x1f4,-0x20(%rbp)
jne    11a3 <join_path+0x53>
movq   $0x0,-0x8(%rbp)
jmp    11da <join_path+0x8a>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x28(%rbp),%rdi
lea    0xe45(%rip),%rsi
call   1070 <strcat@plt>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    $0x1e,%edx
call   1050 <strncat@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<join_path>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r15
mov    $0x1f4,%esi
call   1040 <strnlen@plt>
mov    %rax,%rbx
lea    0x20(%rax),%rdi
mov    $0x1,%esi
call   1060 <calloc@plt>
cmp    $0x1f4,%rbx
jne    1187 <join_path+0x37>
xor    %r12d,%r12d
jmp    11b4 <join_path+0x64>
mov    %rax,%r12
mov    %rax,%rdi
mov    %r15,%rsi
call   1030 <strcpy@plt>
lea    0xe64(%rip),%rsi
mov    %r12,%rdi
call   1070 <strcat@plt>
mov    %r12,%rdi
mov    %r14,%rsi
mov    $0x1e,%edx
call   1050 <strncat@plt>
mov    %r12,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [2 x i8] c"/\00"
define i8* @join_path(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
%8 = load i8*, i8** %4
%9 = call i64 @strnlen(i8* %8, i32 500)
store i64 %9, i64* %6
%10 = load i64, i64* %6
%11 = add i64 %10, 32
%12 = call i8* @calloc(i64 %11, i32 1)
store i8* %12, i8** %7
%13 = load i64, i64* %6
%14 = icmp eq i64 %13, 500
br i1 %14, label %15, label %16
15: ; preds = %2
store i8* null, i8** %3
br label %26
16: ; preds = %2
%17 = load i8*, i8** %7
%18 = load i8*, i8** %4
%19 = call i32 @strcpy(i8* %17, i8* %18)
%20 = load i8*, i8** %7
%21 = call i32 @strcat(i8* %20, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%22 = load i8*, i8** %7
%23 = load i8*, i8** %5
%24 = call i32 @strncat(i8* %22, i8* %23, i32 30)
%25 = load i8*, i8** %7
store i8* %25, i8** %3
br label %26
26: ; preds = %16, %15
%27 = load i8*, i8** %3
ret i8* %27
}

LLVM IR (-O2):
@.str = [2 x i8] c"/\00"
define i8* @join_path(i8* %0, i8* %1) {
%3 = tail call i64 @strnlen(i8* %0, i32 500)
%4 = add i64 %3, 32
%5 = tail call i8* @calloc(i64 %4, i32 1)
%6 = icmp eq i64 %3, 500
br i1 %6, label %11, label %7
7: ; preds = %2
%8 = tail call i32 @strcpy(i8* %5, i8* %0)
%9 = tail call i32 @strcat(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%10 = tail call i32 @strncat(i8* %5, i8* %1, i32 30)
br label %11
11: ; preds = %2, %7
%12 = phi i8* [ %5, %7 ], [ null, %2 ]
ret i8* %12
}