#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 *extract_fileext(char const *);
scalar_t__ malloc_e(int);
int /*<<< orphan*/ memcpy(char *, char const *, int);
int strlen(char const *);
char *replace_fileext(const char *path, const char *ext) {
  const char *p = extract_fileext(path);
  int m = p - path;
  int n = strlen(ext);
  char *s = (char *)malloc_e(m + n + 2);
  memcpy(s, path, m);
  s[m] = '.';
  memcpy(s + m + 1, ext, n + 1);
  return s;
}


x86 assembly (clang, -O0):
<replace_fileext>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <extract_fileext@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    -0x8(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%edi
add    -0x20(%rbp),%edi
add    $0x2,%edi
call   1060 <malloc_e@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1050 <memcpy@plt>
mov    -0x28(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movb   $0x2e,(%rax,%rcx,1)
mov    -0x28(%rbp),%rdi
movslq -0x1c(%rbp),%rax
add    %rax,%rdi
add    $0x1,%rdi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%edx
add    $0x1,%edx
call   1050 <memcpy@plt>
mov    -0x28(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<replace_fileext>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rsi,%r14
mov    %rdi,%r15
call   1030 <extract_fileext@plt>
mov    %rax,%rbx
sub    %r15,%rbx
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %eax,%r12d
lea    (%r12,%rbx,1),%edi
add    $0x2,%edi
call   1060 <malloc_e@plt>
mov    %rax,%r13
mov    %rax,%rdi
mov    %r15,%rsi
mov    %ebx,%edx
call   1050 <memcpy@plt>
movslq %ebx,%rax
movb   $0x2e,0x0(%r13,%rax,1)
lea    (%rax,%r13,1),%rdi
add    $0x1,%rdi
add    $0x1,%r12d
mov    %r14,%rsi
mov    %r12d,%edx
call   1050 <memcpy@plt>
mov    %r13,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @replace_fileext(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
store i8* %0, i8** %3
store i8* %1, i8** %4
%9 = load i8*, i8** %3
%10 = call i8* @extract_fileext(i8* %9)
store i8* %10, i8** %5
%11 = load i8*, i8** %5
%12 = load i8*, i8** %3
%13 = ptrtoint i8* %11 to i64
%14 = ptrtoint i8* %12 to i64
%15 = sub i64 %13, %14
%16 = trunc i64 %15 to i32
store i32 %16, i32* %6
%17 = load i8*, i8** %4
%18 = call i32 @strlen(i8* %17)
store i32 %18, i32* %7
%19 = load i32, i32* %6
%20 = load i32, i32* %7
%21 = add nsw i32 %19, %20
%22 = add nsw i32 %21, 2
%23 = call i64 @malloc_e(i32 %22)
%24 = inttoptr i64 %23 to i8*
store i8* %24, i8** %8
%25 = load i8*, i8** %8
%26 = load i8*, i8** %3
%27 = load i32, i32* %6
%28 = call i32 @memcpy(i8* %25, i8* %26, i32 %27)
%29 = load i8*, i8** %8
%30 = load i32, i32* %6
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i8, i8* %29, i64 %31
store i8 46, i8* %32
%33 = load i8*, i8** %8
%34 = load i32, i32* %6
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds i8, i8* %33, i64 %35
%37 = getelementptr inbounds i8, i8* %36, i64 1
%38 = load i8*, i8** %4
%39 = load i32, i32* %7
%40 = add nsw i32 %39, 1
%41 = call i32 @memcpy(i8* %37, i8* %38, i32 %40)
%42 = load i8*, i8** %8
ret i8* %42
}

LLVM IR (-O2):
define i8* @replace_fileext(i8* %0, i8* %1) {
%3 = tail call i8* @extract_fileext(i8* %0)
%4 = ptrtoint i8* %3 to i64
%5 = ptrtoint i8* %0 to i64
%6 = sub i64 %4, %5
%7 = trunc i64 %6 to i32
%8 = tail call i32 @strlen(i8* %1)
%9 = add i32 %8, 2
%10 = add i32 %9, %7
%11 = tail call i64 @malloc_e(i32 %10)
%12 = inttoptr i64 %11 to i8*
%13 = tail call i32 @memcpy(i8* %12, i8* %0, i32 %7)
%14 = shl i64 %6, 32
%15 = ashr exact i64 %14, 32
%16 = getelementptr inbounds i8, i8* %12, i64 %15
store i8 46, i8* %16
%17 = getelementptr inbounds i8, i8* %16, i64 1
%18 = add nsw i32 %8, 1
%19 = tail call i32 @memcpy(i8* %17, i8* %1, i32 %18)
ret i8* %12
}