#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 *malloc(scalar_t__);
char *realloc(char *, scalar_t__);
char *strcat(char *, char *const);
int /*<<< orphan*/ strcpy(char *, char *const);
scalar_t__ strlen(char *const);
char *strrchr(char *, char);
char *replace_extension(char *const input_filename, char *const new_extension) {
  char *filename = malloc(strlen(input_filename) + 1);
  strcpy(filename, input_filename);
  char *dot_position = strrchr(filename, '.');
  if (dot_position != NULL) {
    *dot_position = '\0';
  }
  filename = realloc(filename, strlen(filename) + strlen(new_extension) + 1);
  filename = strcat(filename, new_extension);
  return filename;
}


x86 assembly (clang, -O0):
<replace_extension>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,%rdi
add    $0x1,%rdi
call   1060 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x18(%rbp),%rdi
mov    $0x2e,%esi
call   1050 <strrchr@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11ba <replace_extension+0x5a>
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x30(%rbp),%rsi
mov    -0x28(%rbp),%rdi
add    %rax,%rsi
add    $0x1,%rsi
call   1070 <realloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1080 <strcat@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<replace_extension>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%r15
call   1040 <strlen@plt>
lea    0x1(%rax),%rdi
call   1060 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r15,%rsi
call   1030 <strcpy@plt>
mov    %rbx,%rdi
mov    $0x2e,%esi
call   1050 <strrchr@plt>
test   %rax,%rax
je     119c <replace_extension+0x3c>
movb   $0x0,(%rax)
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %rax,%r15
mov    %r14,%rdi
call   1040 <strlen@plt>
lea    (%r15,%rax,1),%rsi
add    $0x1,%rsi
mov    %rbx,%rdi
call   1070 <realloc@plt>
mov    %rax,%rdi
mov    %r14,%rsi
pop    %rbx
pop    %r14
pop    %r15
jmp    1080 <strcat@plt>

LLVM IR (-O0):
define i8* @replace_extension(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 = call i64 @strlen(i8* %7)
%9 = add nsw i64 %8, 1
%10 = call i8* @malloc(i64 %9)
store i8* %10, i8** %5
%11 = load i8*, i8** %5
%12 = load i8*, i8** %3
%13 = call i32 @strcpy(i8* %11, i8* %12)
%14 = load i8*, i8** %5
%15 = call i8* @strrchr(i8* %14, i8 signext 46)
store i8* %15, i8** %6
%16 = load i8*, i8** %6
%17 = icmp ne i8* %16, null
br i1 %17, label %18, label %20
18: ; preds = %2
%19 = load i8*, i8** %6
store i8 0, i8* %19
br label %20
20: ; preds = %18, %2
%21 = load i8*, i8** %5
%22 = load i8*, i8** %5
%23 = call i64 @strlen(i8* %22)
%24 = load i8*, i8** %4
%25 = call i64 @strlen(i8* %24)
%26 = add nsw i64 %23, %25
%27 = add nsw i64 %26, 1
%28 = call i8* @realloc(i8* %21, i64 %27)
store i8* %28, i8** %5
%29 = load i8*, i8** %5
%30 = load i8*, i8** %4
%31 = call i8* @strcat(i8* %29, i8* %30)
store i8* %31, i8** %5
%32 = load i8*, i8** %5
ret i8* %32
}

LLVM IR (-O2):
define i8* @replace_extension(i8* %0, i8* nocapture %1) {
%3 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%4 = add nsw i64 %3, 1
%5 = tail call i8* @malloc(i64 %4)
%6 = tail call i32 @strcpy(i8* %5, i8* %0)
%7 = tail call i8* @strrchr(i8* dereferenceable(1) %5, i8 signext 46)
%8 = icmp eq i8* %7, null
br i1 %8, label %10, label %9
9: ; preds = %2
store i8 0, i8* %7
br label %10
10: ; preds = %9, %2
%11 = tail call i64 @strlen(i8* dereferenceable(1) %5)
%12 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%13 = add i64 %11, 1
%14 = add i64 %13, %12
%15 = tail call i8* @realloc(i8* %5, i64 %14)
%16 = tail call i8* @strcat(i8* dereferenceable(1) %15, i8* dereferenceable(1) %1)
ret i8* %15
}