#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(size_t);
int snprintf(char *, int, char *, ...);
size_t strlen(char const *);
char *strstr(char *, char *);
char *card_get_ram_path(const char *rom_path) {
  size_t rom_path_len = strlen(rom_path);
  size_t ram_path_len = rom_path_len + 2;
  char *ram_path = malloc(ram_path_len);
  int ret = snprintf(ram_path, ram_path_len, "%s", rom_path);
  if (ret != (int)rom_path_len) {
    return NULL;
  }
  char *substr = strstr(ram_path, ".");
  if (substr == NULL) {
    return NULL;
  }
  ret = snprintf(substr, 5, ".sav");
  if (ret != 4) {
    return NULL;
  }
  return ram_path;
}


x86 assembly (clang, -O0):
<card_get_ram_path>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1050 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rax
mov    %eax,%esi
mov    -0x10(%rbp),%rcx
lea    0xe79(%rip),%rdx
mov    $0x0,%al
call   1040 <snprintf@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
mov    -0x18(%rbp),%rcx
cmp    %ecx,%eax
je     11ad <card_get_ram_path+0x6d>
movq   $0x0,-0x8(%rbp)
jmp    1212 <card_get_ram_path+0xd2>
mov    -0x28(%rbp),%rdi
lea    0xe4b(%rip),%rsi
call   1060 <strstr@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    11d9 <card_get_ram_path+0x99>
movq   $0x0,-0x8(%rbp)
jmp    1212 <card_get_ram_path+0xd2>
mov    -0x38(%rbp),%rdi
mov    $0x5,%esi
lea    0xe1c(%rip),%rdx
mov    $0x0,%al
call   1040 <snprintf@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x4,-0x2c(%rbp)
je     120a <card_get_ram_path+0xca>
movq   $0x0,-0x8(%rbp)
jmp    1212 <card_get_ram_path+0xd2>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<card_get_ram_path>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdi,%r13
call   1030 <strlen@plt>
mov    %rax,%r12
lea    0x2(%rax),%rbx
mov    %rbx,%rdi
call   1060 <malloc@plt>
mov    %rax,%r14
lea    0xe96(%rip),%rdx
xor    %r15d,%r15d
mov    %rax,%rdi
mov    %ebx,%esi
mov    %r13,%rcx
xor    %eax,%eax
call   1050 <snprintf@plt>
cmp    %r12d,%eax
jne    11b8 <card_get_ram_path+0x78>
mov    %r14,%rdi
mov    $0x2e,%esi
call   1040 <strchr@plt>
test   %rax,%rax
je     11b5 <card_get_ram_path+0x75>
lea    0xe69(%rip),%rdx
xor    %r15d,%r15d
mov    %rax,%rdi
mov    $0x5,%esi
xor    %eax,%eax
call   1050 <snprintf@plt>
cmp    $0x4,%eax
cmove  %r14,%r15
jmp    11b8 <card_get_ram_path+0x78>
xor    %r15d,%r15d
mov    %r15,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
@.str = [3 x i8] c"%s\00"
@.str.2 = [5 x i8] c".sav\00"
define i8* @card_get_ram_path(i8* %0) {
%2 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%3 = add i64 %2, 2
%4 = tail call i8* @malloc(i64 %3)
%5 = trunc i64 %3 to i32
%6 = tail call i32 (i8*, i32, i8*, ...) @snprintf(i8* %4, i32 %5, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %0)
%7 = trunc i64 %2 to i32
%8 = icmp eq i32 %6, %7
br i1 %8, label %9, label %16
9: ; preds = %1
%10 = tail call i8* @strchr(i8* dereferenceable(1) %4, i32 46)
%11 = icmp eq i8* %10, null
br i1 %11, label %16, label %12
12: ; preds = %9
%13 = tail call i32 (i8*, i32, i8*, ...) @snprintf(i8* %10, i32 5, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%14 = icmp eq i32 %13, 4
%15 = select i1 %14, i8* %4, i8* null
br label %16
16: ; preds = %9, %12, %1
%17 = phi i8* [ null, %1 ], [ null, %9 ], [ %15, %12 ]
ret i8* %17
}