#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 */
scalar_t__ kcm_is_path_separator(char const);
int /*<<< orphan*/ memcpy(char *, char const *, size_t);
int strlen(char const *);
char *strrchr(char const *, char);
void kcm_extract_charmap_name(const char *kcm_file_path, char *charmap_name,
                              int max_len) {
  const char *ext_separator;
  size_t to_copy;
  const char *file_name = kcm_file_path + strlen(kcm_file_path);
  while (!kcm_is_path_separator(*file_name) && (file_name != kcm_file_path)) {
    file_name--;
  }
  if (kcm_is_path_separator(*file_name)) {
    file_name++;
  }
  ext_separator = strrchr(file_name, '.');
  if (NULL == ext_separator) {
    ext_separator = file_name + strlen(file_name);
  } else if (ext_separator == file_name) {
    file_name++;
    ext_separator = file_name + strlen(file_name);
  }
  to_copy = ext_separator - file_name;
  if (to_copy > (max_len - 1)) {
    to_copy = max_len - 1;
  }
  memcpy(charmap_name, file_name, to_copy);
  charmap_name[to_copy] = '\0';
}


x86 assembly (clang, -O0):
<kcm_extract_charmap_name>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x38(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
movsbl (%rax),%edi
call   1030 <kcm_is_path_separator@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    $0x0,%rcx
mov    %al,-0x39(%rbp)
jne    11a0 <kcm_extract_charmap_name+0x60>
mov    -0x30(%rbp),%rax
cmp    -0x8(%rbp),%rax
setne  %al
mov    %al,-0x39(%rbp)
mov    -0x39(%rbp),%al
test   $0x1,%al
jne    11b0 <kcm_extract_charmap_name+0x70>
jmp    11c1 <kcm_extract_charmap_name+0x81>
mov    -0x30(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x30(%rbp)
jmp    1174 <kcm_extract_charmap_name+0x34>
mov    -0x30(%rbp),%rax
movsbl (%rax),%edi
call   1030 <kcm_is_path_separator@plt>
cmp    $0x0,%rax
je     11e3 <kcm_extract_charmap_name+0xa3>
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    $0x2e,%esi
call   1050 <strrchr@plt>
mov    %rax,-0x20(%rbp)
xor    %eax,%eax
cmp    -0x20(%rbp),%rax
jne    1227 <kcm_extract_charmap_name+0xe7>
mov    -0x30(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x30(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x48(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x20(%rbp)
jmp    1267 <kcm_extract_charmap_name+0x127>
mov    -0x20(%rbp),%rax
cmp    -0x30(%rbp),%rax
jne    1262 <kcm_extract_charmap_name+0x122>
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x30(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x50(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x20(%rbp)
jmp    1267 <kcm_extract_charmap_name+0x127>
mov    -0x20(%rbp),%rax
mov    -0x30(%rbp),%rcx
sub    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
cmp    %rcx,%rax
jbe    1298 <kcm_extract_charmap_name+0x158>
mov    -0x14(%rbp),%eax
sub    $0x1,%eax
cltq
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1060 <memcpy@plt>
mov    -0x10(%rbp),%rax
mov    -0x28(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<kcm_extract_charmap_name>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%r15d
mov    %rsi,%r14
mov    %rdi,%rbx
call   1040 <strlen@plt>
movslq %eax,%rbp
add    %rbp,%rbx
add    $0x1,%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
movsbl -0x1(%rbx),%edi
add    $0xffffffffffffffff,%rbx
call   1030 <kcm_is_path_separator@plt>
test   %rbp,%rbp
je     118b <kcm_extract_charmap_name+0x4b>
add    $0xffffffffffffffff,%rbp
test   %rax,%rax
je     1170 <kcm_extract_charmap_name+0x30>
movsbl (%rbx),%edi
call   1030 <kcm_is_path_separator@plt>
xor    %r13d,%r13d
test   %rax,%rax
setne  %r13b
lea    (%rbx,%r13,1),%r12
mov    %r12,%rdi
mov    $0x2e,%esi
call   1050 <strrchr@plt>
test   %rax,%rax
je     11c3 <kcm_extract_charmap_name+0x83>
mov    %rax,%rbp
cmp    %rax,%r12
jne    11d1 <kcm_extract_charmap_name+0x91>
lea    (%rbx,%r13,1),%r12
add    $0x1,%r12
mov    %r12,%rdi
call   1040 <strlen@plt>
movslq %eax,%rbp
add    %r12,%rbp
sub    %r12,%rbp
add    $0xffffffff,%r15d
movslq %r15d,%rax
cmp    %rax,%rbp
cmova  %rax,%rbp
mov    %r14,%rdi
mov    %r12,%rsi
mov    %rbp,%rdx
call   1060 <memcpy@plt>
movb   $0x0,(%r14,%rbp,1)
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @kcm_extract_charmap_name(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%10 = load i8*, i8** %4
%11 = load i8*, i8** %4
%12 = call i32 @strlen(i8* %11)
%13 = sext i32 %12 to i64
%14 = getelementptr inbounds i8, i8* %10, i64 %13
store i8* %14, i8** %9
br label %15
15: ; preds = %26, %3
%16 = load i8*, i8** %9
%17 = load i8, i8* %16
%18 = call i64 @kcm_is_path_separator(i8 signext %17)
%19 = icmp ne i64 %18, 0
br i1 %19, label %24, label %20
20: ; preds = %15
%21 = load i8*, i8** %9
%22 = load i8*, i8** %4
%23 = icmp ne i8* %21, %22
br label %24
24: ; preds = %20, %15
%25 = phi i1 [ false, %15 ], [ %23, %20 ]
br i1 %25, label %26, label %29
26: ; preds = %24
%27 = load i8*, i8** %9
%28 = getelementptr inbounds i8, i8* %27, i32 -1
store i8* %28, i8** %9
br label %15
29: ; preds = %24
%30 = load i8*, i8** %9
%31 = load i8, i8* %30
%32 = call i64 @kcm_is_path_separator(i8 signext %31)
%33 = icmp ne i64 %32, 0
br i1 %33, label %34, label %37
34: ; preds = %29
%35 = load i8*, i8** %9
%36 = getelementptr inbounds i8, i8* %35, i32 1
store i8* %36, i8** %9
br label %37
37: ; preds = %34, %29
%38 = load i8*, i8** %9
%39 = call i8* @strrchr(i8* %38, i8 signext 46)
store i8* %39, i8** %7
%40 = load i8*, i8** %7
%41 = icmp eq i8* null, %40
br i1 %41, label %42, label %48
42: ; preds = %37
%43 = load i8*, i8** %9
%44 = load i8*, i8** %9
%45 = call i32 @strlen(i8* %44)
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds i8, i8* %43, i64 %46
store i8* %47, i8** %7
br label %61
48: ; preds = %37
%49 = load i8*, i8** %7
%50 = load i8*, i8** %9
%51 = icmp eq i8* %49, %50
br i1 %51, label %52, label %60
52: ; preds = %48
%53 = load i8*, i8** %9
%54 = getelementptr inbounds i8, i8* %53, i32 1
store i8* %54, i8** %9
%55 = load i8*, i8** %9
%56 = load i8*, i8** %9
%57 = call i32 @strlen(i8* %56)
%58 = sext i32 %57 to i64
%59 = getelementptr inbounds i8, i8* %55, i64 %58
store i8* %59, i8** %7
br label %60
60: ; preds = %52, %48
br label %61
61: ; preds = %60, %42
%62 = load i8*, i8** %7
%63 = load i8*, i8** %9
%64 = ptrtoint i8* %62 to i64
%65 = ptrtoint i8* %63 to i64
%66 = sub i64 %64, %65
store i64 %66, i64* %8
%67 = load i64, i64* %8
%68 = load i32, i32* %6
%69 = sub nsw i32 %68, 1
%70 = sext i32 %69 to i64
%71 = icmp ugt i64 %67, %70
br i1 %71, label %72, label %76
72: ; preds = %61
%73 = load i32, i32* %6
%74 = sub nsw i32 %73, 1
%75 = sext i32 %74 to i64
store i64 %75, i64* %8
br label %76
76: ; preds = %72, %61
%77 = load i8*, i8** %5
%78 = load i8*, i8** %9
%79 = load i64, i64* %8
%80 = call i32 @memcpy(i8* %77, i8* %78, i64 %79)
%81 = load i8*, i8** %5
%82 = load i64, i64* %8
%83 = getelementptr inbounds i8, i8* %81, i64 %82
store i8 0, i8* %83
ret void
}

LLVM IR (-O2):
define void @kcm_extract_charmap_name(i8* %0, i8* %1, i32 %2) {
%4 = tail call i32 @strlen(i8* %0)
%5 = sext i32 %4 to i64
%6 = getelementptr inbounds i8, i8* %0, i64 %5
br label %7
7: ; preds = %7, %3
%8 = phi i8* [ %6, %3 ], [ %14, %7 ]
%9 = load i8, i8* %8
%10 = tail call i64 @kcm_is_path_separator(i8 signext %9)
%11 = icmp eq i64 %10, 0
%12 = icmp ne i8* %8, %0
%13 = and i1 %12, %11
%14 = getelementptr inbounds i8, i8* %8, i64 -1
br i1 %13, label %7, label %15
15: ; preds = %7
%16 = load i8, i8* %8
%17 = tail call i64 @kcm_is_path_separator(i8 signext %16)
%18 = icmp ne i64 %17, 0
%19 = zext i1 %18 to i64
%20 = getelementptr i8, i8* %8, i64 %19
%21 = tail call i8* @strrchr(i8* %20, i8 signext 46)
%22 = icmp eq i8* %21, null
br i1 %22, label %27, label %23
23: ; preds = %15
%24 = icmp eq i8* %21, %20
br i1 %24, label %25, label %32
25: ; preds = %23
%26 = getelementptr inbounds i8, i8* %20, i64 1
br label %27
27: ; preds = %15, %25
%28 = phi i8* [ %26, %25 ], [ %20, %15 ]
%29 = tail call i32 @strlen(i8* %28)
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i8, i8* %28, i64 %30
br label %32
32: ; preds = %27, %23
%33 = phi i8* [ %21, %23 ], [ %31, %27 ]
%34 = phi i8* [ %20, %23 ], [ %28, %27 ]
%35 = ptrtoint i8* %33 to i64
%36 = ptrtoint i8* %34 to i64
%37 = sub i64 %35, %36
%38 = add nsw i32 %2, -1
%39 = sext i32 %38 to i64
%40 = icmp ugt i64 %37, %39
%41 = select i1 %40, i64 %39, i64 %37
%42 = tail call i32 @memcpy(i8* %1, i8* %34, i64 %41)
%43 = getelementptr inbounds i8, i8* %1, i64 %41
store i8 0, i8* %43
ret void
}