#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 *last_char_is(char const *, char);
int /*<<< orphan*/ sprintf(char *, char *, char const *, char *, char const *);
scalar_t__ strlen(char const *);
char *xmalloc(scalar_t__);
char *concat_path_file(const char *path, const char *filename) {
  char *lc, *str;
  if (!path)
    path = "";
  lc = last_char_is(path, '/');
  while (*filename == '/')
    filename++;
  str = xmalloc(strlen(path) + (lc == 0 ? 1 : 0) + strlen(filename) + 1);
  sprintf(str, "%s%s%s", path, (lc == NULL ? "/" : ""), filename);
  return str;
}


x86 assembly (clang, -O0):
<concat_path_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    1166 <concat_path_file+0x26>
lea    0xea6(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
mov    $0x2f,%esi
call   1050 <last_char_is@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
jne    1199 <concat_path_file+0x59>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    1178 <concat_path_file+0x38>
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x18(%rbp),%rsi
xor    %ecx,%ecx
mov    $0x1,%edx
cmp    $0x0,%rsi
cmove  %edx,%ecx
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x28(%rbp),%rdi
add    %rax,%rdi
add    $0x1,%rdi
call   1040 <xmalloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%rdx
mov    -0x18(%rbp),%rsi
lea    0xe1a(%rip),%rcx
lea    0xe12(%rip),%rax
cmp    $0x0,%rsi
cmove  %rax,%rcx
mov    -0x10(%rbp),%r8
lea    0xdf8(%rip),%rsi
call   1060 <sprintf@plt>
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<concat_path_file>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rsi,%rbx
mov    %rdi,%r14
test   %rdi,%rdi
lea    0xeaf(%rip),%r15
cmove  %r15,%r14
mov    %r14,%rdi
mov    $0x2f,%esi
call   1050 <last_char_is@plt>
mov    %rax,%r12
add    $0xffffffffffffffff,%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
cmpb   $0x2f,0x1(%rbx)
lea    0x1(%rbx),%rbx
je     1180 <concat_path_file+0x40>
mov    %r14,%rdi
call   1030 <strlen@plt>
mov    %rax,%r13
mov    %rbx,%rdi
call   1030 <strlen@plt>
xor    %ecx,%ecx
test   %r12,%r12
sete   %cl
lea    0xe5b(%rip),%rdx
cmove  %rdx,%r15
add    %r13,%rcx
lea    (%rax,%rcx,1),%rdi
add    $0x1,%rdi
call   1040 <xmalloc@plt>
mov    %rax,%r12
lea    0xe36(%rip),%rsi
mov    %rax,%rdi
mov    %r14,%rdx
mov    %r15,%rcx
mov    %rbx,%r8
call   1060 <sprintf@plt>
mov    %r12,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [1 x i8] zeroinitializer
@.str.1 = [7 x i8] c"%s%s%s\00"
@.str.2 = [2 x i8] c"/\00"
define i8* @concat_path_file(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
store i8* %0, i8** %3
store i8* %1, i8** %4
%7 = load i8*, i8** %3
%8 = icmp ne i8* %7, null
br i1 %8, label %10, label %9
9: ; preds = %2
store i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0), i8** %3
br label %10
10: ; preds = %9, %2
%11 = load i8*, i8** %3
%12 = call i8* @last_char_is(i8* %11, i8 signext 47)
store i8* %12, i8** %5
br label %13
13: ; preds = %18, %10
%14 = load i8*, i8** %4
%15 = load i8, i8* %14
%16 = sext i8 %15 to i32
%17 = icmp eq i32 %16, 47
br i1 %17, label %18, label %21
18: ; preds = %13
%19 = load i8*, i8** %4
%20 = getelementptr inbounds i8, i8* %19, i32 1
store i8* %20, i8** %4
br label %13
21: ; preds = %13
%22 = load i8*, i8** %3
%23 = call i64 @strlen(i8* %22)
%24 = load i8*, i8** %5
%25 = icmp eq i8* %24, null
%26 = zext i1 %25 to i64
%27 = select i1 %25, i32 1, i32 0
%28 = sext i32 %27 to i64
%29 = add nsw i64 %23, %28
%30 = load i8*, i8** %4
%31 = call i64 @strlen(i8* %30)
%32 = add nsw i64 %29, %31
%33 = add nsw i64 %32, 1
%34 = call i8* @xmalloc(i64 %33)
store i8* %34, i8** %6
%35 = load i8*, i8** %6
%36 = load i8*, i8** %3
%37 = load i8*, i8** %5
%38 = icmp eq i8* %37, null
%39 = zext i1 %38 to i64
%40 = select i1 %38, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0)
%41 = load i8*, i8** %4
%42 = call i32 @sprintf(i8* %35, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i8* %36, i8* %40, i8* %41)
%43 = load i8*, i8** %6
ret i8* %43
}

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