#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 *calloc(int, int);
int /*<<< orphan*/ strncpy(char *, char *, int);
char *strrchr(char *, char);
char *remove_extension(char *basename) {
  char *output;
  if (basename == NULL)
    return NULL;
  char *lastdot = strrchr(basename, '.');
  if (lastdot == NULL)
    lastdot = strrchr(basename, '\0');
  if ((output = calloc(lastdot - basename + 3, 1)) == NULL)
    return NULL;
  strncpy(output, basename, lastdot - basename);
  return output;
}


x86 assembly (clang, -O0):
<remove_extension>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1154 <remove_extension+0x24>
movq   $0x0,-0x8(%rbp)
jmp    11d8 <remove_extension+0xa8>
mov    -0x10(%rbp),%rdi
mov    $0x2e,%esi
call   1040 <strrchr@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1180 <remove_extension+0x50>
mov    -0x10(%rbp),%rdi
xor    %esi,%esi
call   1040 <strrchr@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
add    $0x3,%rax
mov    %eax,%edi
mov    $0x1,%esi
call   1050 <calloc@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    11b6 <remove_extension+0x86>
movq   $0x0,-0x8(%rbp)
jmp    11d8 <remove_extension+0xa8>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%edx
call   1030 <strncpy@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<remove_extension>:
push   %r15
push   %r14
push   %rbx
test   %rdi,%rdi
je     1194 <remove_extension+0x54>
mov    %rdi,%r14
mov    $0x2e,%esi
call   1050 <strrchr@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    116d <remove_extension+0x2d>
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %rax,%rbx
add    %r14,%rbx
sub    %r14d,%ebx
lea    0x3(%rbx),%edi
mov    $0x1,%esi
call   1060 <calloc@plt>
test   %rax,%rax
je     1194 <remove_extension+0x54>
mov    %rax,%r15
mov    %rax,%rdi
mov    %r14,%rsi
mov    %ebx,%edx
call   1030 <strncpy@plt>
jmp    1197 <remove_extension+0x57>
xor    %r15d,%r15d
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @remove_extension(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = icmp eq i8* %6, null
br i1 %7, label %8, label %9
8: ; preds = %1
store i8* null, i8** %2
br label %39
9: ; preds = %1
%10 = load i8*, i8** %3
%11 = call i8* @strrchr(i8* %10, i8 signext 46)
store i8* %11, i8** %5
%12 = load i8*, i8** %5
%13 = icmp eq i8* %12, null
br i1 %13, label %14, label %17
14: ; preds = %9
%15 = load i8*, i8** %3
%16 = call i8* @strrchr(i8* %15, i8 signext 0)
store i8* %16, i8** %5
br label %17
17: ; preds = %14, %9
%18 = load i8*, i8** %5
%19 = load i8*, i8** %3
%20 = ptrtoint i8* %18 to i64
%21 = ptrtoint i8* %19 to i64
%22 = sub i64 %20, %21
%23 = add nsw i64 %22, 3
%24 = trunc i64 %23 to i32
%25 = call i8* @calloc(i32 %24, i32 1)
store i8* %25, i8** %4
%26 = icmp eq i8* %25, null
br i1 %26, label %27, label %28
27: ; preds = %17
store i8* null, i8** %2
br label %39
28: ; preds = %17
%29 = load i8*, i8** %4
%30 = load i8*, i8** %3
%31 = load i8*, i8** %5
%32 = load i8*, i8** %3
%33 = ptrtoint i8* %31 to i64
%34 = ptrtoint i8* %32 to i64
%35 = sub i64 %33, %34
%36 = trunc i64 %35 to i32
%37 = call i32 @strncpy(i8* %29, i8* %30, i32 %36)
%38 = load i8*, i8** %4
store i8* %38, i8** %2
br label %39
39: ; preds = %28, %27, %8
%40 = load i8*, i8** %2
ret i8* %40
}

LLVM IR (-O2):
define i8* @remove_extension(i8* %0) {
%2 = icmp eq i8* %0, null
br i1 %2, label %20, label %3
3: ; preds = %1
%4 = tail call i8* @strrchr(i8* %0, i8 signext 46)
%5 = icmp eq i8* %4, null
br i1 %5, label %6, label %9
6: ; preds = %3
%7 = tail call i64 @strlen(i8* %0)
%8 = getelementptr i8, i8* %0, i64 %7
br label %9
9: ; preds = %6, %3
%10 = phi i8* [ %8, %6 ], [ %4, %3 ]
%11 = ptrtoint i8* %10 to i64
%12 = ptrtoint i8* %0 to i64
%13 = sub i64 %11, %12
%14 = trunc i64 %13 to i32
%15 = add i32 %14, 3
%16 = tail call i8* @calloc(i32 %15, i32 1)
%17 = icmp eq i8* %16, null
br i1 %17, label %20, label %18
18: ; preds = %9
%19 = tail call i32 @strncpy(i8* %16, i8* %0, i32 %14)
br label %20
20: ; preds = %18, %9, %1
%21 = phi i8* [ null, %1 ], [ %16, %18 ], [ null, %9 ]
ret i8* %21
}