#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*/ krash_strncat(char *, size_t, char const *, size_t);
char *malloc(size_t);
size_t strlen(char const *);
char *krash_io_create_filename(char const *dirname, char const *filename,
                               size_t filename_length) {
  size_t dirlen = strlen(dirname);
  size_t dest_size = dirlen + filename_length + 1;
  if (dirname[dirlen] != '/') {
    dest_size += 1;
  }
  char *name = malloc(dest_size);
  if (!name) {
    return NULL;
  }
  name[0] = '\0';
  krash_strncat(name, dest_size, dirname, dirlen);
  if (dirname[dirlen] != '/') {
    krash_strncat(name, dest_size, "/", 1);
  }
  krash_strncat(name, dest_size, filename, filename_length);
  return name;
}


x86 assembly (clang, -O0):
<krash_io_create_filename>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
add    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x28(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x2f,%eax
je     1182 <krash_io_create_filename+0x52>
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
call   1050 <malloc@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    11a7 <krash_io_create_filename+0x77>
movq   $0x0,-0x8(%rbp)
jmp    120e <krash_io_create_filename+0xde>
mov    -0x38(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x38(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x10(%rbp),%rdx
mov    -0x28(%rbp),%rcx
call   1030 <krash_strncat@plt>
mov    -0x10(%rbp),%rax
mov    -0x28(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x2f,%eax
je     11f1 <krash_io_create_filename+0xc1>
mov    -0x38(%rbp),%rdi
mov    -0x30(%rbp),%rsi
lea    0xe19(%rip),%rdx
mov    $0x1,%ecx
call   1030 <krash_strncat@plt>
mov    -0x38(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
call   1030 <krash_strncat@plt>
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<krash_io_create_filename>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r15
mov    %rsi,%r14
mov    %rdi,%r13
call   1040 <strlen@plt>
mov    %rax,%rbp
xor    %eax,%eax
cmpb   $0x2f,0x0(%r13,%rbp,1)
lea    0x0(%rbp,%r15,1),%rbx
sete   %al
sub    %rax,%rbx
add    $0x2,%rbx
mov    %rbx,%rdi
call   1050 <malloc@plt>
mov    %rax,%r12
test   %rax,%rax
je     11b9 <krash_io_create_filename+0x89>
movb   $0x0,(%r12)
mov    %r12,%rdi
mov    %rbx,%rsi
mov    %r13,%rdx
mov    %rbp,%rcx
call   1030 <krash_strncat@plt>
cmpb   $0x2f,0x0(%r13,%rbp,1)
je     11a8 <krash_io_create_filename+0x78>
lea    0xe68(%rip),%rdx
mov    $0x1,%ecx
mov    %r12,%rdi
mov    %rbx,%rsi
call   1030 <krash_strncat@plt>
mov    %r12,%rdi
mov    %rbx,%rsi
mov    %r14,%rdx
mov    %r15,%rcx
call   1030 <krash_strncat@plt>
mov    %r12,%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"
define i8* @krash_io_create_filename(i8* %0, i8* %1, i64 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i64
%10 = alloca i8*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%11 = load i8*, i8** %5
%12 = call i64 @strlen(i8* %11)
store i64 %12, i64* %8
%13 = load i64, i64* %8
%14 = load i64, i64* %7
%15 = add i64 %13, %14
%16 = add i64 %15, 1
store i64 %16, i64* %9
%17 = load i8*, i8** %5
%18 = load i64, i64* %8
%19 = getelementptr inbounds i8, i8* %17, i64 %18
%20 = load i8, i8* %19
%21 = sext i8 %20 to i32
%22 = icmp ne i32 %21, 47
br i1 %22, label %23, label %26
23: ; preds = %3
%24 = load i64, i64* %9
%25 = add i64 %24, 1
store i64 %25, i64* %9
br label %26
26: ; preds = %23, %3
%27 = load i64, i64* %9
%28 = call i8* @malloc(i64 %27)
store i8* %28, i8** %10
%29 = load i8*, i8** %10
%30 = icmp ne i8* %29, null
br i1 %30, label %32, label %31
31: ; preds = %26
store i8* null, i8** %4
br label %57
32: ; preds = %26
%33 = load i8*, i8** %10
%34 = getelementptr inbounds i8, i8* %33, i64 0
store i8 0, i8* %34
%35 = load i8*, i8** %10
%36 = load i64, i64* %9
%37 = load i8*, i8** %5
%38 = load i64, i64* %8
%39 = call i32 @krash_strncat(i8* %35, i64 %36, i8* %37, i64 %38)
%40 = load i8*, i8** %5
%41 = load i64, i64* %8
%42 = getelementptr inbounds i8, i8* %40, i64 %41
%43 = load i8, i8* %42
%44 = sext i8 %43 to i32
%45 = icmp ne i32 %44, 47
br i1 %45, label %46, label %50
46: ; preds = %32
%47 = load i8*, i8** %10
%48 = load i64, i64* %9
%49 = call i32 @krash_strncat(i8* %47, i64 %48, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i64 1)
br label %50
50: ; preds = %46, %32
%51 = load i8*, i8** %10
%52 = load i64, i64* %9
%53 = load i8*, i8** %6
%54 = load i64, i64* %7
%55 = call i32 @krash_strncat(i8* %51, i64 %52, i8* %53, i64 %54)
%56 = load i8*, i8** %10
store i8* %56, i8** %4
br label %57
57: ; preds = %50, %31
%58 = load i8*, i8** %4
ret i8* %58
}

LLVM IR (-O2):
@.str = [2 x i8] c"/\00"
define i8* @krash_io_create_filename(i8* %0, i8* %1, i64 %2) {
%4 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%5 = add i64 %4, %2
%6 = getelementptr inbounds i8, i8* %0, i64 %4
%7 = load i8, i8* %6
%8 = icmp eq i8 %7, 47
%9 = select i1 %8, i64 1, i64 2
%10 = add i64 %5, %9
%11 = tail call i8* @malloc(i64 %10)
%12 = icmp eq i8* %11, null
br i1 %12, label %21, label %13
13: ; preds = %3
store i8 0, i8* %11
%14 = tail call i32 @krash_strncat(i8* %11, i64 %10, i8* %0, i64 %4)
%15 = load i8, i8* %6
%16 = icmp eq i8 %15, 47
br i1 %16, label %19, label %17
17: ; preds = %13
%18 = tail call i32 @krash_strncat(i8* %11, i64 %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i64 1)
br label %19
19: ; preds = %17, %13
%20 = tail call i32 @krash_strncat(i8* %11, i64 %10, i8* %1, i64 %2)
br label %21
21: ; preds = %3, %19
ret i8* %11
}