#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 *basename(char *);
int /*<<< orphan*/ free(char *);
char *malloc(int);
int /*<<< orphan*/ strcpy(char *, char *);
char *strdup(char const *);
int strlen(char *);
char *mpk_filehandling_basename(const char *fpath) {
  char *fpath_c = strdup(fpath);
  char *bname = basename(fpath_c);
  int len;
  if ((len = strlen(bname)) < 4) {
    free(fpath_c);
    return NULL;
  }
  if (bname[len - 4] != '.') {
    free(fpath_c);
    return NULL;
  }
  bname[len - 4] = 0;
  char *package_name;
  if (!(package_name = malloc(strlen(bname) + 1))) {
    free(fpath_c);
    return NULL;
  }
  strcpy(package_name, bname);
  free(fpath_c);
  return package_name;
}


x86 assembly (clang, -O0):
<mpk_filehandling_basename>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1080 <strdup@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1060 <basename@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x4,%eax
jge    11b1 <mpk_filehandling_basename+0x51>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    1247 <mpk_filehandling_basename+0xe7>
mov    -0x20(%rbp),%rax
mov    -0x24(%rbp),%ecx
sub    $0x4,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x2e,%eax
je     11e1 <mpk_filehandling_basename+0x81>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    1247 <mpk_filehandling_basename+0xe7>
mov    -0x20(%rbp),%rax
mov    -0x24(%rbp),%ecx
sub    $0x4,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%edi
add    $0x1,%edi
call   1070 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jne    1229 <mpk_filehandling_basename+0xc9>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    1247 <mpk_filehandling_basename+0xe7>
mov    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mpk_filehandling_basename>:
push   %r15
push   %r14
push   %rbx
call   1080 <strdup@plt>
mov    %rax,%r14
mov    %rax,%rdi
call   1060 <basename@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1050 <strlen@plt>
cmp    $0x4,%eax
jl     11b7 <mpk_filehandling_basename+0x57>
add    $0xfffffffc,%eax
cmpb   $0x2e,(%rbx,%rax,1)
jne    11b7 <mpk_filehandling_basename+0x57>
movb   $0x0,(%rbx,%rax,1)
mov    %rbx,%rdi
call   1050 <strlen@plt>
lea    0x1(%rax),%edi
call   1070 <malloc@plt>
test   %rax,%rax
je     11b7 <mpk_filehandling_basename+0x57>
mov    %rax,%r15
mov    %rax,%rdi
mov    %rbx,%rsi
call   1040 <strcpy@plt>
jmp    11ba <mpk_filehandling_basename+0x5a>
xor    %r15d,%r15d
mov    %r14,%rdi
call   1030 <free@plt>
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
define i8* @mpk_filehandling_basename(i8* nocapture %0) {
%2 = tail call i8* @strdup(i8* %0)
%3 = tail call i8* @basename(i8* %2)
%4 = tail call i32 @strlen(i8* %3)
%5 = icmp slt i32 %4, 4
br i1 %5, label %19, label %6
6: ; preds = %1
%7 = add nsw i32 %4, -4
%8 = zext i32 %7 to i64
%9 = getelementptr inbounds i8, i8* %3, i64 %8
%10 = load i8, i8* %9
%11 = icmp eq i8 %10, 46
br i1 %11, label %12, label %19
12: ; preds = %6
store i8 0, i8* %9
%13 = tail call i32 @strlen(i8* %3)
%14 = add nsw i32 %13, 1
%15 = tail call i8* @malloc(i32 %14)
%16 = icmp eq i8* %15, null
br i1 %16, label %19, label %17
17: ; preds = %12
%18 = tail call i32 @strcpy(i8* %15, i8* %3)
br label %19
19: ; preds = %12, %6, %1, %17
%20 = phi i8* [ %15, %17 ], [ null, %1 ], [ null, %6 ], [ null, %12 ]
%21 = tail call i32 @free(i8* %2)
ret i8* %20
}