#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*/ bcopy(char const *, char *, int);
scalar_t__ malloc(int);
scalar_t__ strcasecmp(char *, char const *);
char *strdup(char const *);
char *strrchr(char const *, char);
char *misc_trim_ext(const char *path, const char *ext) {
  char *p;
  int len;
  if ((p = strrchr(path, '.')) == NULL)
    return strdup(path);
  if (ext && strcasecmp(p + 1, ext))
    return strdup(path);
  len = p - path;
  if ((p = (char *)malloc(len + 1)) == NULL)
    return NULL;
  bcopy((path), (p), (len));
  p[len] = '\0';
  return p;
}


x86 assembly (clang, -O0):
<misc_trim_ext>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x2e,%esi
call   1040 <strrchr@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    118e <misc_trim_ext+0x3e>
mov    -0x10(%rbp),%rdi
call   1070 <strdup@plt>
mov    %rax,-0x8(%rbp)
jmp    121e <misc_trim_ext+0xce>
cmpq   $0x0,-0x18(%rbp)
je     11c6 <misc_trim_ext+0x76>
mov    -0x20(%rbp),%rdi
add    $0x1,%rdi
mov    -0x18(%rbp),%rsi
call   1030 <strcasecmp@plt>
cmp    $0x0,%rax
je     11c6 <misc_trim_ext+0x76>
mov    -0x10(%rbp),%rdi
call   1070 <strdup@plt>
mov    %rax,-0x8(%rbp)
jmp    121e <misc_trim_ext+0xce>
mov    -0x20(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%edi
add    $0x1,%edi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    11fa <misc_trim_ext+0xaa>
movq   $0x0,-0x8(%rbp)
jmp    121e <misc_trim_ext+0xce>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1060 <bcopy@plt>
mov    -0x20(%rbp),%rax
movslq -0x24(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<misc_trim_ext>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r15
mov    %rdi,%r14
mov    $0x2e,%esi
call   1040 <strrchr@plt>
test   %rax,%rax
je     1183 <misc_trim_ext+0x33>
mov    %rax,%rbx
test   %r15,%r15
je     1190 <misc_trim_ext+0x40>
lea    0x1(%rbx),%rdi
mov    %r15,%rsi
call   1030 <strcasecmp@plt>
test   %rax,%rax
je     1190 <misc_trim_ext+0x40>
mov    %r14,%rdi
pop    %rbx
pop    %r14
pop    %r15
jmp    1070 <strdup@plt>
sub    %r14,%rbx
lea    0x1(%rbx),%edi
call   1050 <malloc@plt>
test   %rax,%rax
je     11bb <misc_trim_ext+0x6b>
mov    %rax,%r15
mov    %rax,%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1060 <memmove@plt>
movslq %ebx,%rax
movb   $0x0,(%r15,%rax,1)
jmp    11be <misc_trim_ext+0x6e>
xor    %r15d,%r15d
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
define i8* @misc_trim_ext(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 %7
5: ; preds = %2
%6 = tail call i8* @strdup(i8* %0)
br label %28
7: ; preds = %2
%8 = icmp eq i8* %1, null
br i1 %8, label %15, label %9
9: ; preds = %7
%10 = getelementptr inbounds i8, i8* %3, i64 1
%11 = tail call i64 @strcasecmp(i8* %10, i8* %1)
%12 = icmp eq i64 %11, 0
br i1 %12, label %15, label %13
13: ; preds = %9
%14 = tail call i8* @strdup(i8* %0)
br label %28
15: ; preds = %9, %7
%16 = ptrtoint i8* %3 to i64
%17 = ptrtoint i8* %0 to i64
%18 = sub i64 %16, %17
%19 = trunc i64 %18 to i32
%20 = add nsw i32 %19, 1
%21 = tail call i64 @malloc(i32 %20)
%22 = inttoptr i64 %21 to i8*
%23 = icmp eq i64 %21, 0
br i1 %23, label %28, label %24
24: ; preds = %15
tail call void @llvm.memmove.p0i8.p0i8.i32(i8* align 1 %22, i8* align 1 %0, i32 %19, i1 false)
%25 = shl i64 %18, 32
%26 = ashr exact i64 %25, 32
%27 = getelementptr inbounds i8, i8* %22, i64 %26
store i8 0, i8* %27
br label %28
28: ; preds = %15, %24, %13, %5
%29 = phi i8* [ %6, %5 ], [ %14, %13 ], [ %22, %24 ], [ null, %15 ]
ret i8* %29
}