#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*/ concat(char *, int, char const *, char const *, char const *,
                          ...);
int strlen(char const *);
void concat_path(char *dst, int dstsz, const char *dir, const char *filename) {
  int dirlen = strlen(dir);
  if (dir[dirlen - 1] == '/' || *dir == '\0') {
    concat(dst, dstsz, dir, filename, NULL);
  } else {
    concat(dst, dstsz, dir, "/", filename, NULL);
  }
}


x86 assembly (clang, -O0):
<concat_path>:
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    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x24(%rbp)
mov    -0x18(%rbp),%rax
mov    -0x24(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x2f,%eax
je     116d <concat_path+0x4d>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    118d <concat_path+0x6d>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
xor    %eax,%eax
mov    %eax,%r8d
mov    $0x0,%al
call   1040 <concat@plt>
jmp    11af <concat_path+0x8f>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%r8
lea    0xe5d(%rip),%rcx
xor    %eax,%eax
mov    %eax,%r9d
mov    $0x0,%al
call   1040 <concat@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<concat_path>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rcx,%r14
mov    %rdx,%rbx
mov    %esi,%r15d
mov    %rdi,%rbp
mov    %rdx,%rdi
call   1030 <strlen@plt>
cltq
cmpb   $0x2f,-0x1(%rax,%rbx,1)
je     1170 <concat_path+0x50>
cmpb   $0x0,(%rbx)
je     1170 <concat_path+0x50>
lea    0xeb0(%rip),%rcx
mov    %rbp,%rdi
mov    %r15d,%esi
mov    %rbx,%rdx
mov    %r14,%r8
xor    %r9d,%r9d
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <concat@plt>
mov    %rbp,%rdi
mov    %r15d,%esi
mov    %rbx,%rdx
mov    %r14,%rcx
xor    %r8d,%r8d
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <concat@plt>

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

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