#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*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ memcpy(void *, void *, int);
char *rindex(char *, char);
int /*<<< orphan*/ stderr;
int strlen(char *);
void handle_filename(char *orig, char base[]) {
  char *loc_e = rindex(orig, '.');
  if (loc_e <= 0) {
    fprintf(stderr, "Files must end with '.bf' extension: %s\n", orig);
    exit(-1);
  }
  if (!((loc_e + 1 == rindex(orig, 'b') && loc_e + 2 == rindex(orig, 'f') &&
         rindex(orig, 'f') - orig == strlen(orig) - 1))) {
    fprintf(stderr, "Files must end with '.bf' extension: %s\n", orig);
    exit(-1);
  }
  char *loc_s = rindex(orig, '/');
  int len;
  if (loc_s > 0) {
    len = loc_e - loc_s - 1;
  } else {
    len = loc_e - orig;
  }
  base[len] = '\0';
  memcpy((void *)base, (void *)orig + strlen(orig) - len - 3, len);
}


x86 assembly (clang, -O0):
<handle_filename>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
mov    $0x2e,%esi
call   1060 <rindex@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
ja     11a0 <handle_filename+0x50>
mov    0x2e5c(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe6f(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
mov    -0x8(%rbp),%rdi
mov    $0x62,%esi
call   1060 <rindex@plt>
mov    %rax,%rcx
mov    -0x30(%rbp),%rax
cmp    %rcx,%rax
jne    122b <handle_filename+0xdb>
mov    -0x18(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x38(%rbp)
mov    -0x8(%rbp),%rdi
mov    $0x66,%esi
call   1060 <rindex@plt>
mov    %rax,%rcx
mov    -0x38(%rbp),%rax
cmp    %rcx,%rax
jne    122b <handle_filename+0xdb>
mov    -0x8(%rbp),%rdi
mov    $0x66,%esi
call   1060 <rindex@plt>
mov    -0x8(%rbp),%rcx
sub    %rcx,%rax
mov    %rax,-0x40(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x40(%rbp),%rax
sub    $0x1,%ecx
movslq %ecx,%rcx
cmp    %rcx,%rax
je     124e <handle_filename+0xfe>
mov    0x2dae(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xdc1(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
mov    -0x8(%rbp),%rdi
mov    $0x2f,%esi
call   1060 <rindex@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jbe    1282 <handle_filename+0x132>
mov    -0x18(%rbp),%rax
mov    -0x20(%rbp),%rcx
sub    %rcx,%rax
sub    $0x1,%rax
mov    %eax,-0x24(%rbp)
jmp    1290 <handle_filename+0x140>
mov    -0x18(%rbp),%rax
mov    -0x8(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x24(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x50(%rbp),%rsi
mov    -0x48(%rbp),%rdi
cltq
add    %rax,%rsi
movslq -0x24(%rbp),%rcx
xor    %eax,%eax
sub    %rcx,%rax
add    %rax,%rsi
add    $0xfffffffffffffffd,%rsi
mov    -0x24(%rbp),%edx
call   1050 <memcpy@plt>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<handle_filename>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r12
mov    $0x2e,%esi
call   1060 <rindex@plt>
test   %rax,%rax
je     121b <handle_filename+0xcb>
mov    %rax,%r15
lea    0x1(%rax),%rbx
mov    %r12,%rdi
mov    $0x62,%esi
call   1060 <rindex@plt>
cmp    %rax,%rbx
jne    121b <handle_filename+0xcb>
lea    0x2(%r15),%rbx
mov    %r12,%rdi
mov    $0x66,%esi
call   1060 <rindex@plt>
cmp    %rax,%rbx
jne    121b <handle_filename+0xcb>
mov    %r12,%rdi
mov    $0x66,%esi
call   1060 <rindex@plt>
mov    %rax,%rbx
sub    %r12,%rbx
mov    %r12,%rdi
call   1030 <strlen@plt>
add    $0xffffffff,%eax
cltq
cmp    %rax,%rbx
jne    121b <handle_filename+0xcb>
mov    %r12,%rdi
mov    $0x2f,%esi
call   1060 <rindex@plt>
mov    %r12,%rcx
neg    %rcx
test   %rax,%rax
not    %rax
cmove  %rcx,%rax
add    %r15,%rax
movslq %eax,%rbx
movb   $0x0,(%r14,%rbx,1)
mov    %r12,%rdi
call   1030 <strlen@plt>
movslq %eax,%rsi
add    %r12,%rsi
sub    %rbx,%rsi
add    $0xfffffffffffffffd,%rsi
mov    %r14,%rdi
mov    %ebx,%edx
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1050 <memcpy@plt>
mov    0x2dbe(%rip),%rax
mov    (%rax),%edi
lea    0xdd5(%rip),%rsi
mov    %r12,%rdx
call   1040 <fprintf@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [41 x i8] c"Files must end with '.bf' extension: %s\0A\00"
define void @handle_filename(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
store i8* %0, i8** %3
store i8* %1, i8** %4
%8 = load i8*, i8** %3
%9 = call i8* @rindex(i8* %8, i8 signext 46)
store i8* %9, i8** %5
%10 = load i8*, i8** %5
%11 = icmp ule i8* %10, null
br i1 %11, label %12, label %17
12: ; preds = %2
%13 = load i32, i32* @stderr
%14 = load i8*, i8** %3
%15 = call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i8* %14)
%16 = call i32 @exit(i32 -1)
unreachable
17: ; preds = %2
%18 = load i8*, i8** %5
%19 = getelementptr inbounds i8, i8* %18, i64 1
%20 = load i8*, i8** %3
%21 = call i8* @rindex(i8* %20, i8 signext 98)
%22 = icmp eq i8* %19, %21
br i1 %22, label %23, label %41
23: ; preds = %17
%24 = load i8*, i8** %5
%25 = getelementptr inbounds i8, i8* %24, i64 2
%26 = load i8*, i8** %3
%27 = call i8* @rindex(i8* %26, i8 signext 102)
%28 = icmp eq i8* %25, %27
br i1 %28, label %29, label %41
29: ; preds = %23
%30 = load i8*, i8** %3
%31 = call i8* @rindex(i8* %30, i8 signext 102)
%32 = load i8*, i8** %3
%33 = ptrtoint i8* %31 to i64
%34 = ptrtoint i8* %32 to i64
%35 = sub i64 %33, %34
%36 = load i8*, i8** %3
%37 = call i32 @strlen(i8* %36)
%38 = sub nsw i32 %37, 1
%39 = sext i32 %38 to i64
%40 = icmp eq i64 %35, %39
br i1 %40, label %46, label %41
41: ; preds = %29, %23, %17
%42 = load i32, i32* @stderr
%43 = load i8*, i8** %3
%44 = call i32 @fprintf(i32 %42, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i8* %43)
%45 = call i32 @exit(i32 -1)
unreachable
46: ; preds = %29
%47 = load i8*, i8** %3
%48 = call i8* @rindex(i8* %47, i8 signext 47)
store i8* %48, i8** %6
%49 = load i8*, i8** %6
%50 = icmp ugt i8* %49, null
br i1 %50, label %51, label %59
51: ; preds = %46
%52 = load i8*, i8** %5
%53 = load i8*, i8** %6
%54 = ptrtoint i8* %52 to i64
%55 = ptrtoint i8* %53 to i64
%56 = sub i64 %54, %55
%57 = sub nsw i64 %56, 1
%58 = trunc i64 %57 to i32
store i32 %58, i32* %7
br label %66
59: ; preds = %46
%60 = load i8*, i8** %5
%61 = load i8*, i8** %3
%62 = ptrtoint i8* %60 to i64
%63 = ptrtoint i8* %61 to i64
%64 = sub i64 %62, %63
%65 = trunc i64 %64 to i32
store i32 %65, i32* %7
br label %66
66: ; preds = %59, %51
%67 = load i8*, i8** %4
%68 = load i32, i32* %7
%69 = sext i32 %68 to i64
%70 = getelementptr inbounds i8, i8* %67, i64 %69
store i8 0, i8* %70
%71 = load i8*, i8** %4
%72 = load i8*, i8** %3
%73 = load i8*, i8** %3
%74 = call i32 @strlen(i8* %73)
%75 = sext i32 %74 to i64
%76 = getelementptr i8, i8* %72, i64 %75
%77 = load i32, i32* %7
%78 = sext i32 %77 to i64
%79 = sub i64 0, %78
%80 = getelementptr i8, i8* %76, i64 %79
%81 = getelementptr i8, i8* %80, i64 -3
%82 = load i32, i32* %7
%83 = call i32 @memcpy(i8* %71, i8* %81, i32 %82)
ret void
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [41 x i8] c"Files must end with '.bf' extension: %s\0A\00"
define void @handle_filename(i8* %0, i8* %1) {
%3 = tail call i8* @rindex(i8* %0, i8 signext 46)
%4 = icmp eq i8* %3, null
br i1 %4, label %5, label %9
5: ; preds = %2
%6 = load i32, i32* @stderr
%7 = tail call i32 @fprintf(i32 %6, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i8* %0)
%8 = tail call i32 @exit(i32 -1)
unreachable
9: ; preds = %2
%10 = getelementptr inbounds i8, i8* %3, i64 1
%11 = tail call i8* @rindex(i8* %0, i8 signext 98)
%12 = icmp eq i8* %10, %11
br i1 %12, label %13, label %26
13: ; preds = %9
%14 = getelementptr inbounds i8, i8* %3, i64 2
%15 = tail call i8* @rindex(i8* %0, i8 signext 102)
%16 = icmp eq i8* %14, %15
br i1 %16, label %17, label %26
17: ; preds = %13
%18 = tail call i8* @rindex(i8* %0, i8 signext 102)
%19 = ptrtoint i8* %18 to i64
%20 = ptrtoint i8* %0 to i64
%21 = sub i64 %19, %20
%22 = tail call i32 @strlen(i8* %0)
%23 = add nsw i32 %22, -1
%24 = sext i32 %23 to i64
%25 = icmp eq i64 %21, %24
br i1 %25, label %30, label %26
26: ; preds = %17, %13, %9
%27 = load i32, i32* @stderr
%28 = tail call i32 @fprintf(i32 %27, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i8* %0)
%29 = tail call i32 @exit(i32 -1)
unreachable
30: ; preds = %17
%31 = tail call i8* @rindex(i8* %0, i8 signext 47)
%32 = icmp eq i8* %31, null
%33 = ptrtoint i8* %3 to i64
%34 = ptrtoint i8* %31 to i64
%35 = xor i64 %34, -1
%36 = sub i64 0, %20
%37 = select i1 %32, i64 %36, i64 %35
%38 = add i64 %37, %33
%39 = trunc i64 %38 to i32
%40 = shl i64 %38, 32
%41 = ashr exact i64 %40, 32
%42 = getelementptr inbounds i8, i8* %1, i64 %41
store i8 0, i8* %42
%43 = tail call i32 @strlen(i8* %0)
%44 = sext i32 %43 to i64
%45 = getelementptr i8, i8* %0, i64 %44
%46 = sub nsw i64 0, %41
%47 = getelementptr i8, i8* %45, i64 %46
%48 = getelementptr i8, i8* %47, i64 -3
%49 = tail call i32 @memcpy(i8* %1, i8* %48, i32 %39)
ret void
}