#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*/ assert(int /*<<< orphan*/);
int /*<<< orphan*/ log_debug(char *);
int /*<<< orphan*/ log_warn(char *);
scalar_t__ strnlen(char const *, int);
int validatefilename(const char *filename) {
  assert(filename != NULL);
  int len = (int)strnlen(filename, 50);
  if (len < 3) {
    log_warn("filename too short");
    return -1;
  }
  if (filename[len - 1] == 'o' && filename[len - 2] == 'y' &&
      filename[len - 3] == '.') {
    log_debug("filename valid");
    return 1;
  } else {
    log_warn("filename not valid");
    return -1;
  }
}


x86 assembly (clang, -O0):
<validatefilename>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0x10(%rbp),%rdi
mov    $0x32,%esi
call   1040 <strnlen@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x3,-0x14(%rbp)
jge    1191 <validatefilename+0x51>
lea    0xe80(%rip),%rdi
call   1060 <log_warn@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    120a <validatefilename+0xca>
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x6f,%eax
jne    11f7 <validatefilename+0xb7>
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%ecx
sub    $0x2,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x79,%eax
jne    11f7 <validatefilename+0xb7>
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%ecx
sub    $0x3,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x2e,%eax
jne    11f7 <validatefilename+0xb7>
lea    0xe2d(%rip),%rdi
call   1030 <log_debug@plt>
movl   $0x1,-0x4(%rbp)
jmp    120a <validatefilename+0xca>
lea    0xe24(%rip),%rdi
call   1060 <log_warn@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<validatefilename>:
push   %rbx
mov    %rdi,%rbx
xor    %edi,%edi
test   %rbx,%rbx
setne  %dil
call   1050 <assert@plt>
mov    %rbx,%rdi
mov    $0x32,%esi
call   1040 <strnlen@plt>
cmp    $0x2,%eax
jg     116d <validatefilename+0x2d>
lea    0xe95(%rip),%rdi
jmp    11a9 <validatefilename+0x69>
lea    -0x1(%rax),%ecx
cmpb   $0x6f,(%rbx,%rcx,1)
jne    11a2 <validatefilename+0x62>
mov    $0xfffffffd,%ecx
lea    (%rcx,%rax,1),%edx
add    $0x1,%edx
cmpb   $0x79,(%rbx,%rdx,1)
jne    11a2 <validatefilename+0x62>
add    %ecx,%eax
cmpb   $0x2e,(%rbx,%rax,1)
jne    11a2 <validatefilename+0x62>
lea    0xe7d(%rip),%rdi
call   1030 <log_debug@plt>
mov    $0x1,%eax
pop    %rbx
ret
lea    0xe79(%rip),%rdi
call   1060 <log_warn@plt>
mov    $0xffffffff,%eax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [19 x i8] c"filename too short\00"
@.str.1 = [15 x i8] c"filename valid\00"
@.str.2 = [19 x i8] c"filename not valid\00"
define i32 @validatefilename(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
store i8* %0, i8** %3
%5 = load i8*, i8** %3
%6 = icmp ne i8* %5, null
%7 = zext i1 %6 to i32
%8 = call i32 @assert(i32 %7)
%9 = load i8*, i8** %3
%10 = call i64 @strnlen(i8* %9, i32 50)
%11 = trunc i64 %10 to i32
store i32 %11, i32* %4
%12 = load i32, i32* %4
%13 = icmp slt i32 %12, 3
br i1 %13, label %14, label %16
14: ; preds = %1
%15 = call i32 @log_warn(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
br label %47
16: ; preds = %1
%17 = load i8*, i8** %3
%18 = load i32, i32* %4
%19 = sub nsw i32 %18, 1
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds i8, i8* %17, i64 %20
%22 = load i8, i8* %21
%23 = sext i8 %22 to i32
%24 = icmp eq i32 %23, 111
br i1 %24, label %25, label %45
25: ; preds = %16
%26 = load i8*, i8** %3
%27 = load i32, i32* %4
%28 = sub nsw i32 %27, 2
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i8, i8* %26, i64 %29
%31 = load i8, i8* %30
%32 = sext i8 %31 to i32
%33 = icmp eq i32 %32, 121
br i1 %33, label %34, label %45
34: ; preds = %25
%35 = load i8*, i8** %3
%36 = load i32, i32* %4
%37 = sub nsw i32 %36, 3
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds i8, i8* %35, i64 %38
%40 = load i8, i8* %39
%41 = sext i8 %40 to i32
%42 = icmp eq i32 %41, 46
br i1 %42, label %43, label %45
43: ; preds = %34
%44 = call i32 @log_debug(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %2
br label %47
45: ; preds = %34, %25, %16
%46 = call i32 @log_warn(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %2
br label %47
47: ; preds = %45, %43, %14
%48 = load i32, i32* %2
ret i32 %48
}

LLVM IR (-O2):
@.str = [19 x i8] c"filename too short\00"
@.str.1 = [15 x i8] c"filename valid\00"
@.str.2 = [19 x i8] c"filename not valid\00"
define i32 @validatefilename(i8* %0) {
%2 = icmp ne i8* %0, null
%3 = zext i1 %2 to i32
%4 = tail call i32 @assert(i32 %3)
%5 = tail call i64 @strnlen(i8* %0, i32 50)
%6 = trunc i64 %5 to i32
%7 = icmp slt i32 %6, 3
br i1 %7, label %8, label %10
8: ; preds = %1
%9 = tail call i32 @log_warn(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
br label %32
10: ; preds = %1
%11 = add i64 %5, 4294967295
%12 = and i64 %11, 4294967295
%13 = getelementptr inbounds i8, i8* %0, i64 %12
%14 = load i8, i8* %13
%15 = icmp eq i8 %14, 111
br i1 %15, label %16, label %30
16: ; preds = %10
%17 = add i64 %5, 4294967294
%18 = and i64 %17, 4294967295
%19 = getelementptr inbounds i8, i8* %0, i64 %18
%20 = load i8, i8* %19
%21 = icmp eq i8 %20, 121
br i1 %21, label %22, label %30
22: ; preds = %16
%23 = add i64 %5, 4294967293
%24 = and i64 %23, 4294967295
%25 = getelementptr inbounds i8, i8* %0, i64 %24
%26 = load i8, i8* %25
%27 = icmp eq i8 %26, 46
br i1 %27, label %28, label %30
28: ; preds = %22
%29 = tail call i32 @log_debug(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
br label %32
30: ; preds = %22, %16, %10
%31 = tail call i32 @log_warn(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
br label %32
32: ; preds = %30, %28, %8
%33 = phi i32 [ -1, %8 ], [ 1, %28 ], [ -1, %30 ]
ret i32 %33
}