#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 *RINDEX(char *, char);
int /*<<< orphan*/ fatal(char *, char *);
scalar_t__ malloc(int);
int /*<<< orphan*/ strncpy(char *, char *, int);
char *strip_suffix(char *name) {
  char *p = RINDEX(name, '.');
  if (p) {
    int l = p - name;
    char *res = (char *)malloc(l + 1);
    strncpy(res, name, l);
    res[l] = '\0';
    return res;
  } else {
    fatal("`%s' has no valid suffix\n", name);
    return 0;
  }
}


x86 assembly (clang, -O0):
<strip_suffix>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x2e,%esi
call   1040 <RINDEX@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11af <strip_suffix+0x6f>
mov    -0x18(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%edi
add    $0x1,%edi
call   1050 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1030 <strncpy@plt>
mov    -0x28(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11c7 <strip_suffix+0x87>
mov    -0x10(%rbp),%rsi
lea    0xe46(%rip),%rdi
call   1060 <fatal@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strip_suffix>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r14
mov    $0x2e,%esi
call   1040 <RINDEX@plt>
test   %rax,%rax
je     117f <strip_suffix+0x3f>
mov    %rax,%rbx
sub    %r14,%rbx
lea    0x1(%rbx),%edi
call   1050 <malloc@plt>
mov    %rax,%r15
mov    %rax,%rdi
mov    %r14,%rsi
mov    %ebx,%edx
call   1030 <strncpy@plt>
movslq %ebx,%rax
movb   $0x0,(%r15,%rax,1)
jmp    1191 <strip_suffix+0x51>
lea    0xe7a(%rip),%rdi
mov    %r14,%rsi
call   1060 <fatal@plt>
xor    %r15d,%r15d
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [26 x i8] c"`%s' has no valid suffix\0A\00"
define i8* @strip_suffix(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
store i8* %0, i8** %3
%7 = load i8*, i8** %3
%8 = call i8* @RINDEX(i8* %7, i8 signext 46)
store i8* %8, i8** %4
%9 = load i8*, i8** %4
%10 = icmp ne i8* %9, null
br i1 %10, label %11, label %31
11: ; preds = %1
%12 = load i8*, i8** %4
%13 = load i8*, i8** %3
%14 = ptrtoint i8* %12 to i64
%15 = ptrtoint i8* %13 to i64
%16 = sub i64 %14, %15
%17 = trunc i64 %16 to i32
store i32 %17, i32* %5
%18 = load i32, i32* %5
%19 = add nsw i32 %18, 1
%20 = call i64 @malloc(i32 %19)
%21 = inttoptr i64 %20 to i8*
store i8* %21, i8** %6
%22 = load i8*, i8** %6
%23 = load i8*, i8** %3
%24 = load i32, i32* %5
%25 = call i32 @strncpy(i8* %22, i8* %23, i32 %24)
%26 = load i8*, i8** %6
%27 = load i32, i32* %5
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds i8, i8* %26, i64 %28
store i8 0, i8* %29
%30 = load i8*, i8** %6
store i8* %30, i8** %2
br label %34
31: ; preds = %1
%32 = load i8*, i8** %3
%33 = call i32 @fatal(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i8* %32)
store i8* null, i8** %2
br label %34
34: ; preds = %31, %11
%35 = load i8*, i8** %2
ret i8* %35
}

LLVM IR (-O2):
@.str = [26 x i8] c"`%s' has no valid suffix\0A\00"
define i8* @strip_suffix(i8* %0) {
%2 = tail call i8* @RINDEX(i8* %0, i8 signext 46)
%3 = icmp eq i8* %2, null
br i1 %3, label %16, label %4
4: ; preds = %1
%5 = ptrtoint i8* %2 to i64
%6 = ptrtoint i8* %0 to i64
%7 = sub i64 %5, %6
%8 = trunc i64 %7 to i32
%9 = add nsw i32 %8, 1
%10 = tail call i64 @malloc(i32 %9)
%11 = inttoptr i64 %10 to i8*
%12 = tail call i32 @strncpy(i8* %11, i8* %0, i32 %8)
%13 = shl i64 %7, 32
%14 = ashr exact i64 %13, 32
%15 = getelementptr inbounds i8, i8* %11, i64 %14
store i8 0, i8* %15
br label %18
16: ; preds = %1
%17 = tail call i32 @fatal(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %18
18: ; preds = %16, %4
%19 = phi i8* [ %11, %4 ], [ null, %16 ]
ret i8* %19
}