#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*/ sprintf(char *, char *, char const *);
int /*<<< orphan*/ strcpy(char *, char const *);
scalar_t__ strlen(char const *);
char *strrchr(char const *, char);
char *umalloc(scalar_t__);
char *ustrdup(char const *);
char *ext_replace(const char *str, const char *ext) {
  char *dot;
  dot = strrchr(str, '.');
  if (dot) {
    char *dup;
    dup = umalloc(strlen(ext) + strlen(str));
    strcpy(dup, str);
    sprintf(dup + (dot - str), ".%s", ext);
    return dup;
  } else {
    return ustrdup(str);
  }
}


x86 assembly (clang, -O0):
<ext_replace>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x2e,%esi
call   1050 <strrchr@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11ef <ext_replace+0x8f>
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x30(%rbp),%rdi
add    %rax,%rdi
call   1060 <umalloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
add    %rax,%rdi
mov    -0x18(%rbp),%rdx
lea    0xe23(%rip),%rsi
call   1080 <sprintf@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11fc <ext_replace+0x9c>
mov    -0x10(%rbp),%rdi
call   1070 <ustrdup@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ext_replace>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r15
mov    $0x2e,%esi
call   1050 <strrchr@plt>
test   %rax,%rax
je     11d3 <ext_replace+0x73>
mov    %rax,%rbx
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %rax,%r12
mov    %r15,%rdi
call   1040 <strlen@plt>
add    %rax,%r12
mov    %r12,%rdi
call   1060 <umalloc@plt>
mov    %rax,%r12
mov    %rax,%rdi
mov    %r15,%rsi
call   1030 <strcpy@plt>
sub    %r15,%rbx
add    %r12,%rbx
lea    0xe47(%rip),%rsi
mov    %rbx,%rdi
mov    %r14,%rdx
call   1080 <sprintf@plt>
mov    %r12,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    %r15,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1070 <ustrdup@plt>

LLVM IR (-O0):
@.str = [4 x i8] c".%s\00"
define i8* @ext_replace(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
%8 = load i8*, i8** %4
%9 = call i8* @strrchr(i8* %8, i8 signext 46)
store i8* %9, i8** %6
%10 = load i8*, i8** %6
%11 = icmp ne i8* %10, null
br i1 %11, label %12, label %32
12: ; preds = %2
%13 = load i8*, i8** %5
%14 = call i64 @strlen(i8* %13)
%15 = load i8*, i8** %4
%16 = call i64 @strlen(i8* %15)
%17 = add nsw i64 %14, %16
%18 = call i8* @umalloc(i64 %17)
store i8* %18, i8** %7
%19 = load i8*, i8** %7
%20 = load i8*, i8** %4
%21 = call i32 @strcpy(i8* %19, i8* %20)
%22 = load i8*, i8** %7
%23 = load i8*, i8** %6
%24 = load i8*, i8** %4
%25 = ptrtoint i8* %23 to i64
%26 = ptrtoint i8* %24 to i64
%27 = sub i64 %25, %26
%28 = getelementptr inbounds i8, i8* %22, i64 %27
%29 = load i8*, i8** %5
%30 = call i32 @sprintf(i8* %28, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %29)
%31 = load i8*, i8** %7
store i8* %31, i8** %3
br label %35
32: ; preds = %2
%33 = load i8*, i8** %4
%34 = call i8* @ustrdup(i8* %33)
store i8* %34, i8** %3
br label %35
35: ; preds = %32, %12
%36 = load i8*, i8** %3
ret i8* %36
}

LLVM IR (-O2):
@.str = [4 x i8] c".%s\00"
define i8* @ext_replace(i8* %0, i8* %1) {
%3 = tail call i8* @strrchr(i8* dereferenceable(1) %0, i8 signext 46)
%4 = icmp eq i8* %3, null
br i1 %4, label %16, label %5
5: ; preds = %2
%6 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%7 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%8 = add nsw i64 %7, %6
%9 = tail call i8* @umalloc(i64 %8)
%10 = tail call i32 @strcpy(i8* %9, i8* %0)
%11 = ptrtoint i8* %3 to i64
%12 = ptrtoint i8* %0 to i64
%13 = sub i64 %11, %12
%14 = getelementptr inbounds i8, i8* %9, i64 %13
%15 = tail call i32 @sprintf(i8* dereferenceable(1) %14, i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %1)
br label %18
16: ; preds = %2
%17 = tail call i8* @ustrdup(i8* %0)
br label %18
18: ; preds = %16, %5
%19 = phi i8* [ %9, %5 ], [ %17, %16 ]
ret i8* %19
}