#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*/ bcopy(char *, char *, int);
char *malloc(int);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
char *strrchr(char *, char);
char *misc_replace_ext(char *filename, char *ext) {
  char *ptr = strrchr(filename, '.');
  char *new;
  int base_len;
  if (ptr == NULL) {
    base_len = strlen(filename);
  } else {
    base_len = (int)(ptr - filename);
  }
  if ((new = malloc(base_len + 1 + strlen(ext) + 1)) == NULL)
    return NULL;
  if (ptr != filename)
    bcopy((filename), (new), (base_len));
  new[base_len] = '.';
  strcpy(new + base_len + 1, ext);
  return new;
}


x86 assembly (clang, -O0):
<misc_replace_ext>:
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)
jne    118e <misc_replace_ext+0x3e>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x2c(%rbp)
jmp    119c <misc_replace_ext+0x4c>
mov    -0x20(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x30(%rbp),%edi
add    %eax,%edi
add    $0x1,%edi
call   1060 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    11d6 <misc_replace_ext+0x86>
movq   $0x0,-0x8(%rbp)
jmp    1220 <misc_replace_ext+0xd0>
mov    -0x20(%rbp),%rax
cmp    -0x10(%rbp),%rax
je     11f4 <misc_replace_ext+0xa4>
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x2c(%rbp),%edx
call   1070 <bcopy@plt>
mov    -0x28(%rbp),%rax
movslq -0x2c(%rbp),%rcx
movb   $0x2e,(%rax,%rcx,1)
mov    -0x28(%rbp),%rdi
movslq -0x2c(%rbp),%rax
add    %rax,%rdi
add    $0x1,%rdi
mov    -0x18(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<misc_replace_ext>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rsi,%r14
mov    %rdi,%r12
mov    $0x2e,%esi
call   1050 <strrchr@plt>
mov    %rax,%r15
test   %rax,%rax
je     1179 <misc_replace_ext+0x29>
mov    %r15d,%r13d
sub    %r12d,%r13d
jmp    1184 <misc_replace_ext+0x34>
mov    %r12,%rdi
call   1040 <strlen@plt>
mov    %eax,%r13d
mov    %r14,%rdi
call   1040 <strlen@plt>
lea    (%rax,%r13,1),%edi
add    $0x2,%edi
call   1070 <malloc@plt>
mov    %rax,%rbx
test   %rax,%rax
je     11ca <misc_replace_ext+0x7a>
cmp    %r12,%r15
je     11b3 <misc_replace_ext+0x63>
mov    %rbx,%rdi
mov    %r12,%rsi
mov    %r13,%rdx
call   1060 <memcpy@plt>
movslq %r13d,%rax
movb   $0x2e,(%rbx,%rax,1)
lea    (%rbx,%rax,1),%rdi
add    $0x1,%rdi
mov    %r14,%rsi
call   1030 <strcpy@plt>
mov    %rbx,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @misc_replace_ext(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
%9 = load i8*, i8** %4
%10 = call i8* @strrchr(i8* %9, i8 signext 46)
store i8* %10, i8** %6
%11 = load i8*, i8** %6
%12 = icmp eq i8* %11, null
br i1 %12, label %13, label %16
13: ; preds = %2
%14 = load i8*, i8** %4
%15 = call i32 @strlen(i8* %14)
store i32 %15, i32* %8
br label %23
16: ; preds = %2
%17 = load i8*, i8** %6
%18 = load i8*, i8** %4
%19 = ptrtoint i8* %17 to i64
%20 = ptrtoint i8* %18 to i64
%21 = sub i64 %19, %20
%22 = trunc i64 %21 to i32
store i32 %22, i32* %8
br label %23
23: ; preds = %16, %13
%24 = load i32, i32* %8
%25 = add nsw i32 %24, 1
%26 = load i8*, i8** %5
%27 = call i32 @strlen(i8* %26)
%28 = add nsw i32 %25, %27
%29 = add nsw i32 %28, 1
%30 = call i8* @malloc(i32 %29)
store i8* %30, i8** %7
%31 = icmp eq i8* %30, null
br i1 %31, label %32, label %33
32: ; preds = %23
store i8* null, i8** %3
br label %55
33: ; preds = %23
%34 = load i8*, i8** %6
%35 = load i8*, i8** %4
%36 = icmp ne i8* %34, %35
br i1 %36, label %37, label %42
37: ; preds = %33
%38 = load i8*, i8** %4
%39 = load i8*, i8** %7
%40 = load i32, i32* %8
%41 = call i32 @bcopy(i8* %38, i8* %39, i32 %40)
br label %42
42: ; preds = %37, %33
%43 = load i8*, i8** %7
%44 = load i32, i32* %8
%45 = sext i32 %44 to i64
%46 = getelementptr inbounds i8, i8* %43, i64 %45
store i8 46, i8* %46
%47 = load i8*, i8** %7
%48 = load i32, i32* %8
%49 = sext i32 %48 to i64
%50 = getelementptr inbounds i8, i8* %47, i64 %49
%51 = getelementptr inbounds i8, i8* %50, i64 1
%52 = load i8*, i8** %5
%53 = call i32 @strcpy(i8* %51, i8* %52)
%54 = load i8*, i8** %7
store i8* %54, i8** %3
br label %55
55: ; preds = %42, %32
%56 = load i8*, i8** %3
ret i8* %56
}

LLVM IR (-O2):
define i8* @misc_replace_ext(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 %5, label %7
5: ; preds = %2
%6 = tail call i32 @strlen(i8* %0)
br label %12
7: ; preds = %2
%8 = ptrtoint i8* %3 to i64
%9 = ptrtoint i8* %0 to i64
%10 = sub i64 %8, %9
%11 = trunc i64 %10 to i32
br label %12
12: ; preds = %7, %5
%13 = phi i32 [ %6, %5 ], [ %11, %7 ]
%14 = tail call i32 @strlen(i8* %1)
%15 = add i32 %13, 2
%16 = add i32 %15, %14
%17 = tail call i8* @malloc(i32 %16)
%18 = icmp eq i8* %17, null
br i1 %18, label %27, label %19
19: ; preds = %12
%20 = icmp eq i8* %3, %0
br i1 %20, label %22, label %21
21: ; preds = %19
tail call void @llvm.memcpy.p0i8.p0i8.i32(i8* align 1 %17, i8* align 1 %0, i32 %13, i1 false)
br label %22
22: ; preds = %21, %19
%23 = sext i32 %13 to i64
%24 = getelementptr inbounds i8, i8* %17, i64 %23
store i8 46, i8* %24
%25 = getelementptr inbounds i8, i8* %24, i64 1
%26 = tail call i32 @strcpy(i8* %25, i8* %1)
br label %27
27: ; preds = %12, %22
ret i8* %17
}