#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 */
struct stat {
  int /*<<< orphan*/ st_mode;
};

/* Variables and functions */
int /*<<< orphan*/ S_ISDIR(int /*<<< orphan*/);
int stat(char const *, struct stat *);
int strlen(char const *);
int check_filename(const char *filename) {
  if (strlen(filename) > 512 - 1 || strlen(filename) == 0)
    return 0;
  if (filename[0] == '.')
    return 0;
  struct stat s;
  int ret_code = stat(filename, &s);
  return (ret_code != -1 && !(S_ISDIR(s.st_mode)));
}


x86 assembly (clang, -O0):
<check_filename>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
cmp    $0x1ff,%eax
jg     1162 <check_filename+0x32>
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
cmp    $0x0,%eax
jne    116e <check_filename+0x3e>
movl   $0x0,-0x4(%rbp)
jmp    11c7 <check_filename+0x97>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2e,%eax
jne    118a <check_filename+0x5a>
movl   $0x0,-0x4(%rbp)
jmp    11c7 <check_filename+0x97>
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1050 <stat@plt>
mov    %eax,-0x1c(%rbp)
xor    %eax,%eax
cmpl   $0xffffffff,-0x1c(%rbp)
mov    %al,-0x1d(%rbp)
je     11bc <check_filename+0x8c>
mov    -0x18(%rbp),%edi
call   1040 <S_ISDIR@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
mov    %al,-0x1d(%rbp)
mov    -0x1d(%rbp),%al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_filename>:
push   %rbp
push   %rbx
push   %rax
mov    %rdi,%rbx
call   1030 <strlen@plt>
xor    %ebp,%ebp
cmp    $0x1ff,%eax
jg     1179 <check_filename+0x49>
mov    %rbx,%rdi
call   1030 <strlen@plt>
test   %eax,%eax
je     1179 <check_filename+0x49>
cmpb   $0x2e,(%rbx)
je     1179 <check_filename+0x49>
mov    %rsp,%rsi
mov    %rbx,%rdi
call   1050 <stat@plt>
cmp    $0xffffffff,%eax
je     1177 <check_filename+0x47>
mov    (%rsp),%edi
call   1040 <S_ISDIR@plt>
xor    %ebp,%ebp
test   %eax,%eax
sete   %bpl
jmp    1179 <check_filename+0x49>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
define i32 @check_filename(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.stat
%5 = alloca i32
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = call i32 @strlen(i8* %6)
%8 = icmp sgt i32 %7, 511
br i1 %8, label %13, label %9
9: ; preds = %1
%10 = load i8*, i8** %3
%11 = call i32 @strlen(i8* %10)
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %14
13: ; preds = %9, %1
store i32 0, i32* %2
br label %35
14: ; preds = %9
%15 = load i8*, i8** %3
%16 = getelementptr inbounds i8, i8* %15, i64 0
%17 = load i8, i8* %16
%18 = sext i8 %17 to i32
%19 = icmp eq i32 %18, 46
br i1 %19, label %20, label %21
20: ; preds = %14
store i32 0, i32* %2
br label %35
21: ; preds = %14
%22 = load i8*, i8** %3
%23 = call i32 @stat(i8* %22, %struct.stat* %4)
store i32 %23, i32* %5
%24 = load i32, i32* %5
%25 = icmp ne i32 %24, -1
br i1 %25, label %26, label %32
26: ; preds = %21
%27 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%28 = load i32, i32* %27
%29 = call i32 @S_ISDIR(i32 %28)
%30 = icmp ne i32 %29, 0
%31 = xor i1 %30, true
br label %32
32: ; preds = %26, %21
%33 = phi i1 [ false, %21 ], [ %31, %26 ]
%34 = zext i1 %33 to i32
store i32 %34, i32* %2
br label %35
35: ; preds = %32, %20, %13
%36 = load i32, i32* %2
ret i32 %36
}

LLVM IR (-O2):
%struct.stat = type { i32 }
define i32 @check_filename(i8* %0) {
%2 = alloca %struct.stat
%3 = tail call i32 @strlen(i8* %0)
%4 = icmp sgt i32 %3, 511
br i1 %4, label %23, label %5
5: ; preds = %1
%6 = tail call i32 @strlen(i8* %0)
%7 = icmp eq i32 %6, 0
br i1 %7, label %23, label %8
8: ; preds = %5
%9 = load i8, i8* %0
%10 = icmp eq i8 %9, 46
br i1 %10, label %23, label %11
11: ; preds = %8
%12 = bitcast %struct.stat* %2 to i8*
%13 = call i32 @stat(i8* %0, %struct.stat* %2)
%14 = icmp eq i32 %13, -1
br i1 %14, label %21, label %15
15: ; preds = %11
%16 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%17 = load i32, i32* %16
%18 = tail call i32 @S_ISDIR(i32 %17)
%19 = icmp eq i32 %18, 0
%20 = zext i1 %19 to i32
br label %21
21: ; preds = %15, %11
%22 = phi i32 [ 0, %11 ], [ %20, %15 ]
br label %23
23: ; preds = %8, %1, %5, %21
%24 = phi i32 [ %22, %21 ], [ 0, %5 ], [ 0, %1 ], [ 0, %8 ]
ret i32 %24
}