char *m_replace_filename(const char *path, const char *filename) {
  char *p;
  assert(path);
  assert(filename);
  p = get_filename(path);
  if (*p) {
    int len = p - path;
    char *temp = m_xmalloc(len + 1 + strlen(filename));
    strncpy(temp, path, len);
    strcpy(temp + len, filename);
    return temp;
  } else {
    char *temp = m_xmalloc(strlen(path) + 2 + strlen(filename));
    sprintf(temp, "%s/%s", path, filename);
    return temp;
  }
}


x86 assembly (clang, -O0):
<m_replace_filename>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x0,%al
call   1070 <assert@plt>
mov    -0x18(%rbp),%rdi
mov    $0x0,%al
call   1070 <assert@plt>
mov    -0x10(%rbp),%rdi
mov    $0x0,%al
call   1050 <get_filename@plt>
cltq
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmpb   $0x0,(%rax)
je     121d <m_replace_filename+0xad>
mov    -0x20(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
cltq
mov    %rax,-0x40(%rbp)
mov    -0x18(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0x40(%rbp),%rdi
add    %rax,%rdi
mov    $0x0,%al
call   1080 <m_xmalloc@plt>
cltq
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rsi
movslq -0x24(%rbp),%rdx
call   1030 <strncpy@plt>
mov    -0x30(%rbp),%rdi
movslq -0x24(%rbp),%rax
add    %rax,%rdi
mov    -0x18(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    126d <m_replace_filename+0xfd>
mov    -0x10(%rbp),%rdi
call   1060 <strlen@plt>
add    $0x2,%rax
mov    %rax,-0x48(%rbp)
mov    -0x18(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0x48(%rbp),%rdi
add    %rax,%rdi
mov    $0x0,%al
call   1080 <m_xmalloc@plt>
cltq
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
lea    0xda2(%rip),%rsi
mov    $0x0,%al
call   1090 <sprintf@plt>
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<m_replace_filename>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r15
xor    %eax,%eax
call   1070 <assert@plt>
mov    %r14,%rdi
xor    %eax,%eax
call   1070 <assert@plt>
mov    %r15,%rdi
xor    %eax,%eax
call   1050 <get_filename@plt>
mov    %eax,%ebx
cltq
cmpb   $0x0,(%rax)
je     11e0 <m_replace_filename+0x70>
sub    %r15d,%ebx
mov    %r14,%rdi
call   1060 <strlen@plt>
movslq %ebx,%rbx
lea    (%rbx,%rax,1),%rdi
add    $0x1,%rdi
xor    %eax,%eax
call   1080 <m_xmalloc@plt>
movslq %eax,%r12
mov    %r12,%rdi
mov    %r15,%rsi
mov    %rbx,%rdx
call   1030 <strncpy@plt>
add    %r12,%rbx
mov    %rbx,%rdi
mov    %r14,%rsi
call   1040 <strcpy@plt>
jmp    121c <m_replace_filename+0xac>
mov    %r15,%rdi
call   1060 <strlen@plt>
mov    %rax,%rbx
mov    %r14,%rdi
call   1060 <strlen@plt>
lea    (%rbx,%rax,1),%rdi
add    $0x2,%rdi
xor    %eax,%eax
call   1080 <m_xmalloc@plt>
movslq %eax,%r12
lea    0xdf4(%rip),%rsi
mov    %r12,%rdi
mov    %r15,%rdx
mov    %r14,%rcx
xor    %eax,%eax
call   1090 <sprintf@plt>
mov    %r12,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [6 x i8] c"%s/%s\00"
define i8* @m_replace_filename(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
%10 = load i8*, i8** %4
%11 = call i32 (i8*, ...) bitcast (i32 (...)* @assert to i32 (i8*, ...)*)(i8* %10)
%12 = load i8*, i8** %5
%13 = call i32 (i8*, ...) bitcast (i32 (...)* @assert to i32 (i8*, ...)*)(i8* %12)
%14 = load i8*, i8** %4
%15 = call i32 (i8*, ...) bitcast (i32 (...)* @get_filename to i32 (i8*, ...)*)(i8* %14)
%16 = sext i32 %15 to i64
%17 = inttoptr i64 %16 to i8*
store i8* %17, i8** %6
%18 = load i8*, i8** %6
%19 = load i8, i8* %18
%20 = icmp ne i8 %19, 0
br i1 %20, label %21, label %49
21: ; preds = %2
%22 = load i8*, i8** %6
%23 = load i8*, i8** %4
%24 = ptrtoint i8* %22 to i64
%25 = ptrtoint i8* %23 to i64
%26 = sub i64 %24, %25
%27 = trunc i64 %26 to i32
store i32 %27, i32* %7
%28 = load i32, i32* %7
%29 = add nsw i32 %28, 1
%30 = sext i32 %29 to i64
%31 = load i8*, i8** %5
%32 = call i64 @strlen(i8* %31)
%33 = add i64 %30, %32
%34 = call i32 (i64, ...) bitcast (i32 (...)* @m_xmalloc to i32 (i64, ...)*)(i64 %33)
%35 = sext i32 %34 to i64
%36 = inttoptr i64 %35 to i8*
store i8* %36, i8** %8
%37 = load i8*, i8** %8
%38 = load i8*, i8** %4
%39 = load i32, i32* %7
%40 = sext i32 %39 to i64
%41 = call i8* @strncpy(i8* %37, i8* %38, i64 %40)
%42 = load i8*, i8** %8
%43 = load i32, i32* %7
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds i8, i8* %42, i64 %44
%46 = load i8*, i8** %5
%47 = call i8* @strcpy(i8* %45, i8* %46)
%48 = load i8*, i8** %8
store i8* %48, i8** %3
br label %64
49: ; preds = %2
%50 = load i8*, i8** %4
%51 = call i64 @strlen(i8* %50)
%52 = add i64 %51, 2
%53 = load i8*, i8** %5
%54 = call i64 @strlen(i8* %53)
%55 = add i64 %52, %54
%56 = call i32 (i64, ...) bitcast (i32 (...)* @m_xmalloc to i32 (i64, ...)*)(i64 %55)
%57 = sext i32 %56 to i64
%58 = inttoptr i64 %57 to i8*
store i8* %58, i8** %9
%59 = load i8*, i8** %9
%60 = load i8*, i8** %4
%61 = load i8*, i8** %5
%62 = call i32 (i8*, i8*, ...) @sprintf(i8* %59, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %60, i8* %61)
%63 = load i8*, i8** %9
store i8* %63, i8** %3
br label %64
64: ; preds = %49, %21
%65 = load i8*, i8** %3
ret i8* %65
}

LLVM IR (-O2):
@.str = [6 x i8] c"%s/%s\00"
define i8* @m_replace_filename(i8* %0, i8* %1) {
%3 = tail call i32 (i8*, ...) bitcast (i32 (...)* @assert to i32 (i8*, ...)*)(i8* %0)
%4 = tail call i32 (i8*, ...) bitcast (i32 (...)* @assert to i32 (i8*, ...)*)(i8* %1)
%5 = tail call i32 (i8*, ...) bitcast (i32 (...)* @get_filename to i32 (i8*, ...)*)(i8* %0)
%6 = sext i32 %5 to i64
%7 = inttoptr i64 %6 to i8*
%8 = load i8, i8* %7
%9 = icmp eq i8 %8, 0
br i1 %9, label %25, label %10
10: ; preds = %2
%11 = ptrtoint i8* %0 to i64
%12 = trunc i64 %11 to i32
%13 = sub i32 %5, %12
%14 = add nsw i32 %13, 1
%15 = sext i32 %14 to i64
%16 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%17 = add i64 %16, %15
%18 = tail call i32 (i64, ...) bitcast (i32 (...)* @m_xmalloc to i32 (i64, ...)*)(i64 %17)
%19 = sext i32 %18 to i64
%20 = inttoptr i64 %19 to i8*
%21 = sext i32 %13 to i64
%22 = tail call i8* @strncpy(i8* dereferenceable(1) %20, i8* %0, i64 %21)
%23 = getelementptr inbounds i8, i8* %20, i64 %21
%24 = tail call i8* @strcpy(i8* dereferenceable(1) %23, i8* dereferenceable(1) %1)
br label %34
25: ; preds = %2
%26 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%27 = add i64 %26, 2
%28 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%29 = add i64 %27, %28
%30 = tail call i32 (i64, ...) bitcast (i32 (...)* @m_xmalloc to i32 (i64, ...)*)(i64 %29)
%31 = sext i32 %30 to i64
%32 = inttoptr i64 %31 to i8*
%33 = tail call i32 (i8*, i8*, ...) @sprintf(i8* dereferenceable(1) %32, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %1)
br label %34
34: ; preds = %25, %10
%35 = phi i8* [ %20, %10 ], [ %32, %25 ]
ret i8* %35
}