#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__);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
scalar_t__ strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, int);
char *strrchr(char *, char);
char *replace_extension(char *fname, char *newext) {
  char *p;
  if ((p = strrchr(fname, '.')) == NULL) {
    p = malloc(strlen(fname) + strlen(newext) + 1);
    strcpy(p, fname);
    strcat(p, newext);
  } else {
    int n;
    n = p - fname;
    p = malloc(n + strlen(newext) + 1);
    strncpy(p, fname, n);
    p[n] = '\0';
    strcat(p, newext);
  }
  return p;
}


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
mov    $0x2e,%esi
call   1060 <strrchr@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    11d5 <replace_extension+0x75>
mov    -0x8(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x28(%rbp),%rdi
add    %rax,%rdi
add    $0x1,%rdi
call   1070 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1080 <strcat@plt>
jmp    1231 <replace_extension+0xd1>
mov    -0x18(%rbp),%rax
mov    -0x8(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,-0x1c(%rbp)
movslq -0x1c(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x30(%rbp),%rdi
add    %rax,%rdi
add    $0x1,%rdi
call   1070 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1030 <strncpy@plt>
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1080 <strcat@plt>
mov    -0x18(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<replace_extension>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r15
mov    $0x2e,%esi
call   1060 <strrchr@plt>
test   %rax,%rax
je     11b0 <replace_extension+0x50>
sub    %r15,%rax
movslq %eax,%r12
mov    %r14,%rdi
call   1050 <strlen@plt>
lea    (%r12,%rax,1),%rdi
add    $0x1,%rdi
call   1070 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r15,%rsi
mov    %r12d,%edx
call   1030 <strncpy@plt>
movb   $0x0,(%rbx,%r12,1)
jmp    11de <replace_extension+0x7e>
mov    %r15,%rdi
call   1050 <strlen@plt>
mov    %rax,%rbx
mov    %r14,%rdi
call   1050 <strlen@plt>
lea    (%rbx,%rax,1),%rdi
add    $0x1,%rdi
call   1070 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r15,%rsi
call   1040 <strcpy@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1080 <strcat@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
define i8* @replace_extension(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 %12
5: ; preds = %2
%6 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%7 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%8 = add i64 %6, 1
%9 = add i64 %8, %7
%10 = tail call i8* @malloc(i64 %9)
%11 = tail call i32 @strcpy(i8* %10, i8* %0)
br label %25
12: ; preds = %2
%13 = ptrtoint i8* %3 to i64
%14 = ptrtoint i8* %0 to i64
%15 = sub i64 %13, %14
%16 = trunc i64 %15 to i32
%17 = shl i64 %15, 32
%18 = ashr exact i64 %17, 32
%19 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%20 = add nsw i64 %18, 1
%21 = add i64 %20, %19
%22 = tail call i8* @malloc(i64 %21)
%23 = tail call i32 @strncpy(i8* %22, i8* %0, i32 %16)
%24 = getelementptr inbounds i8, i8* %22, i64 %18
store i8 0, i8* %24
br label %25
25: ; preds = %12, %5
%26 = phi i8* [ %22, %12 ], [ %10, %5 ]
%27 = tail call i32 @strcat(i8* %26, i8* %1)
ret i8* %26
}