#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*/ strcat(char *, char *);
int strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, int);
char *strrchr(char *, int);
char *romname2savename(char *savename, char *romname, int savenamelength) {
  if (romname == NULL) {
    return NULL;
  }
  char *suffix = strrchr(romname, (int)'.');
  if (suffix == NULL) {
    strncpy(savename, romname, savenamelength - 4);
    strcat(savename, ".sav");
  } else {
    int prefixlength = strlen(romname) - strlen(suffix);
    strncpy(savename, romname, prefixlength);
    savename[prefixlength] = '\0';
    strcat(savename, ".sav");
  }
  return savename;
}


x86 assembly (clang, -O0):
<romname2savename>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    116b <romname2savename+0x2b>
movq   $0x0,-0x8(%rbp)
jmp    1203 <romname2savename+0xc3>
mov    -0x18(%rbp),%rdi
mov    $0x2e,%esi
call   1050 <strrchr@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11b0 <romname2savename+0x70>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
sub    $0x4,%edx
call   1030 <strncpy@plt>
mov    -0x10(%rbp),%rdi
lea    0xe5a(%rip),%rsi
call   1060 <strcat@plt>
jmp    11fb <romname2savename+0xbb>
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x30(%rbp)
mov    -0x28(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x30(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x2c(%rbp),%edx
call   1030 <strncpy@plt>
mov    -0x10(%rbp),%rax
movslq -0x2c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rdi
lea    0xe0a(%rip),%rsi
call   1060 <strcat@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<romname2savename>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
test   %rsi,%rsi
je     1196 <romname2savename+0x56>
mov    %edx,%r15d
mov    %rsi,%rbx
mov    %rdi,%r14
mov    %rsi,%rdi
mov    $0x2e,%esi
call   1050 <strrchr@plt>
test   %rax,%rax
je     119b <romname2savename+0x5b>
mov    %rax,%r12
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebp
mov    %r12,%rdi
call   1040 <strlen@plt>
sub    %eax,%ebp
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1030 <strncpy@plt>
movslq %ebp,%rax
movb   $0x0,(%r14,%rax,1)
jmp    11ad <romname2savename+0x6d>
xor    %r14d,%r14d
jmp    11bc <romname2savename+0x7c>
add    $0xfffffffc,%r15d
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %r15d,%edx
call   1030 <strncpy@plt>
lea    0xe4c(%rip),%rsi
mov    %r14,%rdi
call   1060 <strcat@plt>
mov    %r14,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

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