#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(int);
int /*<<< orphan*/ strcpy(char *, char const *);
int strlen(char const *);
int /*<<< orphan*/ strncpy(char *, char const *, int);
char *strrchr(char const *, char);
const char *replace_suffix(const char *filename, const char *new_suffix) {
  char *enda = strrchr(filename, '.');
  if (enda == 0) {
    int flen = strlen(filename);
    int len = flen + strlen(new_suffix) + 1;
    char *result = malloc(len);
    strcpy(result, filename);
    strcpy(result + flen, new_suffix);
    return result;
  } else {
    int flen = enda - filename;
    int len = flen + strlen(new_suffix) + 1;
    char *result = malloc(len);
    strncpy(result, filename, flen);
    strcpy(result + flen, new_suffix);
    return result;
  }
}


x86 assembly (clang, -O0):
<replace_suffix>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x2e,%esi
call   1060 <strrchr@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11df <replace_suffix+0x8f>
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x44(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    -0x44(%rbp),%eax
add    %ecx,%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%edi
call   1070 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x30(%rbp),%rdi
movslq -0x24(%rbp),%rax
add    %rax,%rdi
mov    -0x18(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1241 <replace_suffix+0xf1>
mov    -0x20(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%eax
mov    %eax,-0x48(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    -0x48(%rbp),%eax
add    %ecx,%eax
add    $0x1,%eax
mov    %eax,-0x38(%rbp)
mov    -0x38(%rbp),%edi
call   1070 <malloc@plt>
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x34(%rbp),%edx
call   1030 <strncpy@plt>
mov    -0x40(%rbp),%rdi
movslq -0x34(%rbp),%rax
add    %rax,%rdi
mov    -0x18(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x40(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<replace_suffix>:
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     1198 <replace_suffix+0x48>
mov    %rax,%rbx
sub    %r15,%rbx
mov    %r14,%rdi
call   1050 <strlen@plt>
lea    (%rbx,%rax,1),%edi
add    $0x1,%edi
call   1070 <malloc@plt>
mov    %rax,%r12
mov    %rax,%rdi
mov    %r15,%rsi
mov    %ebx,%edx
call   1030 <strncpy@plt>
jmp    11c3 <replace_suffix+0x73>
mov    %r15,%rdi
call   1050 <strlen@plt>
mov    %eax,%ebx
mov    %r14,%rdi
call   1050 <strlen@plt>
lea    (%rbx,%rax,1),%edi
add    $0x1,%edi
call   1070 <malloc@plt>
mov    %rax,%r12
mov    %rax,%rdi
mov    %r15,%rsi
call   1040 <strcpy@plt>
movslq %ebx,%rdi
add    %r12,%rdi
mov    %r14,%rsi
call   1040 <strcpy@plt>
mov    %r12,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

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