#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*/ memcpy(char *, char const *, int);
int strlen(char const *);
char tolower(char const);
char const *getdatfilename(char const *filename) {
  static char buf[256];
  int n;
  n = strlen(filename);
  if (filename[n - 4] == '.' && tolower(filename[n - 3]) == 't' &&
      tolower(filename[n - 2]) == 'w' && tolower(filename[n - 1]) == 's')
    n -= 4;
  memcpy(buf, filename, n);
  memcpy(buf + n, ".dat", 5);
  return buf;
}


x86 assembly (clang, -O0):
<getdatfilename>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%rax
mov    -0xc(%rbp),%ecx
sub    $0x4,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x2e,%eax
jne    11d1 <getdatfilename+0xa1>
mov    -0x8(%rbp),%rax
mov    -0xc(%rbp),%ecx
sub    $0x3,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%edi
call   1050 <tolower@plt>
movsbl %al,%eax
cmp    $0x74,%eax
jne    11d1 <getdatfilename+0xa1>
mov    -0x8(%rbp),%rax
mov    -0xc(%rbp),%ecx
sub    $0x2,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%edi
call   1050 <tolower@plt>
movsbl %al,%eax
cmp    $0x77,%eax
jne    11d1 <getdatfilename+0xa1>
mov    -0x8(%rbp),%rax
mov    -0xc(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%edi
call   1050 <tolower@plt>
movsbl %al,%eax
cmp    $0x73,%eax
jne    11d1 <getdatfilename+0xa1>
mov    -0xc(%rbp),%eax
sub    $0x4,%eax
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%rsi
mov    -0xc(%rbp),%edx
lea    0x2e71(%rip),%rdi
call   1040 <memcpy@plt>
movslq -0xc(%rbp),%rax
lea    0x2e61(%rip),%rdi
add    %rax,%rdi
lea    0xe07(%rip),%rsi
mov    $0x5,%edx
call   1040 <memcpy@plt>
lea    0x2e46(%rip),%rax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getdatfilename>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%rbx
call   1030 <strlen@plt>
mov    %eax,%ebp
movslq %eax,%r14
cmpb   $0x2e,-0x4(%rbx,%r14,1)
jne    117f <getdatfilename+0x4f>
movsbl -0x3(%r14,%rbx,1),%edi
call   1050 <tolower@plt>
cmp    $0x74,%al
jne    117f <getdatfilename+0x4f>
movsbl -0x2(%r14,%rbx,1),%edi
call   1050 <tolower@plt>
cmp    $0x77,%al
jne    117f <getdatfilename+0x4f>
lea    -0x4(%r14),%rbp
movsbl -0x1(%r14,%rbx,1),%edi
call   1050 <tolower@plt>
cmp    $0x73,%al
cmove  %ebp,%r14d
mov    %r14d,%ebp
lea    0x2eca(%rip),%r14
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1040 <memcpy@plt>
movslq %ebp,%rdi
add    %r14,%rdi
lea    0xe60(%rip),%rsi
mov    $0x5,%edx
call   1040 <memcpy@plt>
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@getdatfilename.buf = internal global [256 x i8] zeroinitializer
@.str = [5 x i8] c".dat\00"
define i8* @getdatfilename(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
store i8* %0, i8** %2
%4 = load i8*, i8** %2
%5 = call i32 @strlen(i8* %4)
store i32 %5, i32* %3
%6 = load i8*, i8** %2
%7 = load i32, i32* %3
%8 = sub nsw i32 %7, 4
%9 = sext i32 %8 to i64
%10 = getelementptr inbounds i8, i8* %6, i64 %9
%11 = load i8, i8* %10
%12 = sext i8 %11 to i32
%13 = icmp eq i32 %12, 46
br i1 %13, label %14, label %47
14: ; preds = %1
%15 = load i8*, i8** %2
%16 = load i32, i32* %3
%17 = sub nsw i32 %16, 3
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds i8, i8* %15, i64 %18
%20 = load i8, i8* %19
%21 = call signext i8 @tolower(i8 signext %20)
%22 = sext i8 %21 to i32
%23 = icmp eq i32 %22, 116
br i1 %23, label %24, label %47
24: ; preds = %14
%25 = load i8*, i8** %2
%26 = load i32, i32* %3
%27 = sub nsw i32 %26, 2
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds i8, i8* %25, i64 %28
%30 = load i8, i8* %29
%31 = call signext i8 @tolower(i8 signext %30)
%32 = sext i8 %31 to i32
%33 = icmp eq i32 %32, 119
br i1 %33, label %34, label %47
34: ; preds = %24
%35 = load i8*, i8** %2
%36 = load i32, i32* %3
%37 = sub nsw i32 %36, 1
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds i8, i8* %35, i64 %38
%40 = load i8, i8* %39
%41 = call signext i8 @tolower(i8 signext %40)
%42 = sext i8 %41 to i32
%43 = icmp eq i32 %42, 115
br i1 %43, label %44, label %47
44: ; preds = %34
%45 = load i32, i32* %3
%46 = sub nsw i32 %45, 4
store i32 %46, i32* %3
br label %47
47: ; preds = %44, %34, %24, %14, %1
%48 = load i8*, i8** %2
%49 = load i32, i32* %3
%50 = call i32 @memcpy(i8* getelementptr inbounds ([256 x i8], [256 x i8]* @getdatfilename.buf, i64 0, i64 0), i8* %48, i32 %49)
%51 = load i32, i32* %3
%52 = sext i32 %51 to i64
%53 = getelementptr inbounds i8, i8* getelementptr inbounds ([256 x i8], [256 x i8]* @getdatfilename.buf, i64 0, i64 0), i64 %52
%54 = call i32 @memcpy(i8* %53, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 5)
ret i8* getelementptr inbounds ([256 x i8], [256 x i8]* @getdatfilename.buf, i64 0, i64 0)
}

LLVM IR (-O2):
@getdatfilename.buf = internal global [256 x i8] zeroinitializer
@.str = [5 x i8] c".dat\00"
define i8* @getdatfilename(i8* %0) {
%2 = tail call i32 @strlen(i8* %0)
%3 = add nsw i32 %2, -4
%4 = sext i32 %3 to i64
%5 = getelementptr inbounds i8, i8* %0, i64 %4
%6 = load i8, i8* %5
%7 = icmp eq i8 %6, 46
br i1 %7, label %8, label %30
8: ; preds = %1
%9 = add nsw i32 %2, -3
%10 = sext i32 %9 to i64
%11 = getelementptr inbounds i8, i8* %0, i64 %10
%12 = load i8, i8* %11
%13 = tail call signext i8 @tolower(i8 signext %12)
%14 = icmp eq i8 %13, 116
br i1 %14, label %15, label %30
15: ; preds = %8
%16 = add nsw i32 %2, -2
%17 = sext i32 %16 to i64
%18 = getelementptr inbounds i8, i8* %0, i64 %17
%19 = load i8, i8* %18
%20 = tail call signext i8 @tolower(i8 signext %19)
%21 = icmp eq i8 %20, 119
br i1 %21, label %22, label %30
22: ; preds = %15
%23 = add nsw i32 %2, -1
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds i8, i8* %0, i64 %24
%26 = load i8, i8* %25
%27 = tail call signext i8 @tolower(i8 signext %26)
%28 = icmp eq i8 %27, 115
%29 = select i1 %28, i32 %3, i32 %2
br label %30
30: ; preds = %22, %15, %8, %1
%31 = phi i32 [ %2, %15 ], [ %2, %8 ], [ %2, %1 ], [ %29, %22 ]
%32 = tail call i32 @memcpy(i8* getelementptr inbounds ([256 x i8], [256 x i8]* @getdatfilename.buf, i64 0, i64 0), i8* %0, i32 %31)
%33 = sext i32 %31 to i64
%34 = getelementptr inbounds [256 x i8], [256 x i8]* @getdatfilename.buf, i64 0, i64 %33
%35 = tail call i32 @memcpy(i8* %34, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 5)
ret i8* getelementptr inbounds ([256 x i8], [256 x i8]* @getdatfilename.buf, i64 0, i64 0)
}