#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*/ memcpy(char *, char const *, unsigned int);
char *safe_malloc(scalar_t__, char *);
scalar_t__ strcasecmp(char *, char const *);
int /*<<< orphan*/ strcat(char *, char const *);
scalar_t__ strlen(char const *);
void *strrchr(char const *, char);
char *extend_filename(const char *base_name, const char *new_ext) {
  char *retval;
  char *ext;
  unsigned int len;
  const char *root;
  root = strrchr(base_name, '/');
  if (root == NULL)
    root = base_name;
  ext = strrchr(root, '.');
  if (ext != NULL) {
    if (strcasecmp(ext, new_ext) == 0) {
      ext = NULL;
    }
  }
  len = ext ? (ext - base_name) : (unsigned int)strlen(base_name);
  retval = safe_malloc(len + strlen(new_ext) + 1, "extending file-name");
  memcpy(retval, base_name, len);
  retval[len] = 0;
  strcat(retval, new_ext);
  return (retval);
}


x86 assembly (clang, -O0):
<extend_filename>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
mov    $0x2f,%esi
call   1060 <strrchr@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1195 <extend_filename+0x35>
mov    -0x8(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    $0x2e,%esi
call   1060 <strrchr@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11d6 <extend_filename+0x76>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strcasecmp@plt>
cmp    $0x0,%rax
jne    11d1 <extend_filename+0x71>
movq   $0x0,-0x20(%rbp)
jmp    11d6 <extend_filename+0x76>
cmpq   $0x0,-0x20(%rbp)
je     11f5 <extend_filename+0x95>
mov    -0x20(%rbp),%rax
mov    -0x8(%rbp),%rcx
sub    %rcx,%rax
mov    %rax,-0x38(%rbp)
jmp    1204 <extend_filename+0xa4>
mov    -0x8(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%eax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    %rax,-0x40(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x40(%rbp),%rdi
add    %rax,%rdi
add    $0x1,%rdi
lea    0xdd3(%rip),%rsi
call   1040 <safe_malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1070 <memcpy@plt>
mov    -0x18(%rbp),%rax
mov    -0x24(%rbp),%ecx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1080 <strcat@plt>
mov    -0x18(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<extend_filename>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rsi,%r14
mov    %rdi,%r15
mov    $0x2f,%esi
call   1060 <strrchr@plt>
test   %rax,%rax
cmove  %r15,%rax
mov    %rax,%rdi
mov    $0x2e,%esi
call   1060 <strrchr@plt>
test   %rax,%rax
je     11aa <extend_filename+0x4a>
mov    %rax,%r12
mov    %rax,%rdi
mov    %r14,%rsi
call   1030 <strcasecmp@plt>
test   %rax,%rax
je     11aa <extend_filename+0x4a>
sub    %r15,%r12
jmp    11b5 <extend_filename+0x55>
mov    %r15,%rdi
call   1050 <strlen@plt>
mov    %eax,%r12d
mov    %r12d,%r13d
mov    %r14,%rdi
call   1050 <strlen@plt>
lea    (%rax,%r13,1),%rdi
add    $0x1,%rdi
lea    0xe31(%rip),%rsi
call   1040 <safe_malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r15,%rsi
mov    %r12d,%edx
call   1070 <memcpy@plt>
movb   $0x0,(%rbx,%r13,1)
mov    %rbx,%rdi
mov    %r14,%rsi
call   1080 <strcat@plt>
mov    %rbx,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
@.str = [20 x i8] c"extending file-name\00"
define i8* @extend_filename(i8* %0, i8* %1) {
%3 = tail call i8* @strrchr(i8* dereferenceable(1) %0, i8 signext 47)
%4 = icmp eq i8* %3, null
%5 = select i1 %4, i8* %0, i8* %3
%6 = tail call i8* @strrchr(i8* dereferenceable(1) %5, i8 signext 46)
%7 = icmp eq i8* %6, null
br i1 %7, label %15, label %8
8: ; preds = %2
%9 = tail call i64 @strcasecmp(i8* %6, i8* %1)
%10 = icmp eq i64 %9, 0
br i1 %10, label %15, label %11
11: ; preds = %8
%12 = ptrtoint i8* %6 to i64
%13 = ptrtoint i8* %0 to i64
%14 = sub i64 %12, %13
br label %18
15: ; preds = %8, %2
%16 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%17 = and i64 %16, 4294967295
br label %18
18: ; preds = %15, %11
%19 = phi i64 [ %14, %11 ], [ %17, %15 ]
%20 = trunc i64 %19 to i32
%21 = and i64 %19, 4294967295
%22 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%23 = add i64 %22, 1
%24 = add i64 %23, %21
%25 = tail call i8* @safe_malloc(i64 %24, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
%26 = tail call i32 @memcpy(i8* %25, i8* %0, i32 %20)
%27 = getelementptr inbounds i8, i8* %25, i64 %21
store i8 0, i8* %27
%28 = tail call i32 @strcat(i8* %25, i8* %1)
ret i8* %25
}