#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*/ asprintf(char **, char *, char *, int, char *, char const *);
char *basename(char *);
char *dirname(char *);
int /*<<< orphan*/ free(char *);
char *strdup(char const *);
scalar_t__ strlen(char *);
char *strrchr(char *, char);
char *change_extension(const char *path, const char *ext) {
  char *d = strdup(path), *f = strdup(path);
  char *dir = dirname(d), *fn = basename(f);
  char *endfn = strrchr(fn, '.');
  char *newpath;
  asprintf(&newpath, "%s/%.*s.%s", dir,
           endfn ? (int)(endfn - fn) : (int)strlen(fn), fn, ext);
  free(d);
  free(f);
  return newpath;
}


x86 assembly (clang, -O0):
<change_extension>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   1090 <strdup@plt>
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
call   1090 <strdup@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1060 <dirname@plt>
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
call   1070 <basename@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    $0x2e,%esi
call   1050 <strrchr@plt>
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x38(%rbp)
je     11ec <change_extension+0x7c>
mov    -0x38(%rbp),%rax
mov    -0x30(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,-0x4c(%rbp)
jmp    11f8 <change_extension+0x88>
mov    -0x30(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x4c(%rbp)
mov    -0x48(%rbp),%rdx
mov    -0x4c(%rbp),%ecx
mov    -0x30(%rbp),%r8
mov    -0x10(%rbp),%r9
lea    -0x40(%rbp),%rdi
lea    0xdee(%rip),%rsi
call   1080 <asprintf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x40(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<change_extension>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
call   1090 <strdup@plt>
mov    %rax,%r15
mov    %rbx,%rdi
call   1090 <strdup@plt>
mov    %rax,%r13
mov    %r15,%rdi
call   1060 <dirname@plt>
mov    %rax,%r12
mov    %r13,%rdi
call   1070 <basename@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    $0x2e,%esi
call   1050 <strrchr@plt>
test   %rax,%rax
je     11c3 <change_extension+0x53>
sub    %rbx,%rax
jmp    11cb <change_extension+0x5b>
mov    %rbx,%rdi
call   1040 <strlen@plt>
lea    0xe2e(%rip),%rsi
lea    0x8(%rsp),%rdi
mov    %r12,%rdx
mov    %eax,%ecx
mov    %rbx,%r8
mov    %r14,%r9
call   1080 <asprintf@plt>
mov    %r15,%rdi
call   1030 <free@plt>
mov    %r13,%rdi
call   1030 <free@plt>
mov    0x8(%rsp),%rax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [11 x i8] c"%s/%.*s.%s\00"
define i8* @change_extension(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i8*
store i8* %0, i8** %3
store i8* %1, i8** %4
%11 = load i8*, i8** %3
%12 = call i8* @strdup(i8* %11)
store i8* %12, i8** %5
%13 = load i8*, i8** %3
%14 = call i8* @strdup(i8* %13)
store i8* %14, i8** %6
%15 = load i8*, i8** %5
%16 = call i8* @dirname(i8* %15)
store i8* %16, i8** %7
%17 = load i8*, i8** %6
%18 = call i8* @basename(i8* %17)
store i8* %18, i8** %8
%19 = load i8*, i8** %8
%20 = call i8* @strrchr(i8* %19, i8 signext 46)
store i8* %20, i8** %9
%21 = load i8*, i8** %7
%22 = load i8*, i8** %9
%23 = icmp ne i8* %22, null
br i1 %23, label %24, label %31
24: ; preds = %2
%25 = load i8*, i8** %9
%26 = load i8*, i8** %8
%27 = ptrtoint i8* %25 to i64
%28 = ptrtoint i8* %26 to i64
%29 = sub i64 %27, %28
%30 = trunc i64 %29 to i32
br label %35
31: ; preds = %2
%32 = load i8*, i8** %8
%33 = call i64 @strlen(i8* %32)
%34 = trunc i64 %33 to i32
br label %35
35: ; preds = %31, %24
%36 = phi i32 [ %30, %24 ], [ %34, %31 ]
%37 = load i8*, i8** %8
%38 = load i8*, i8** %4
%39 = call i32 @asprintf(i8** %10, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* %21, i32 %36, i8* %37, i8* %38)
%40 = load i8*, i8** %5
%41 = call i32 @free(i8* %40)
%42 = load i8*, i8** %6
%43 = call i32 @free(i8* %42)
%44 = load i8*, i8** %10
ret i8* %44
}

LLVM IR (-O2):
@.str = [11 x i8] c"%s/%.*s.%s\00"
define i8* @change_extension(i8* nocapture %0, i8* %1) {
%3 = alloca i8*
%4 = tail call i8* @strdup(i8* %0)
%5 = tail call i8* @strdup(i8* %0)
%6 = tail call i8* @dirname(i8* %4)
%7 = tail call i8* @basename(i8* %5)
%8 = tail call i8* @strrchr(i8* dereferenceable(1) %7, i8 signext 46)
%9 = bitcast i8** %3 to i8*
%10 = icmp eq i8* %8, null
br i1 %10, label %15, label %11
11: ; preds = %2
%12 = ptrtoint i8* %8 to i64
%13 = ptrtoint i8* %7 to i64
%14 = sub i64 %12, %13
br label %17
15: ; preds = %2
%16 = tail call i64 @strlen(i8* dereferenceable(1) %7)
br label %17
17: ; preds = %15, %11
%18 = phi i64 [ %14, %11 ], [ %16, %15 ]
%19 = trunc i64 %18 to i32
%20 = call i32 @asprintf(i8** %3, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* %6, i32 %19, i8* %7, i8* %1)
%21 = call i32 @free(i8* %4)
%22 = call i32 @free(i8* %5)
%23 = load i8*, i8** %3
ret i8* %23
}