#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*/ free(char *);
char *malloc(unsigned int);
int /*<<< orphan*/ memcpy(char *, char const *, unsigned int);
int /*<<< orphan*/ *strchr(char *, char);
int /*<<< orphan*/ *strstr(char *, char *);
int validate_file_name(const char *buf, unsigned int len) {
  int ret = 0;
  char *str = malloc(len + 1);
  memcpy(str, buf, len);
  str[len] = '\0';
  if (strchr(str, '*') != NULL || strchr(str, ':') != NULL ||
      strchr(str, '/') != NULL || strstr(str, "..") != NULL)
    ret = -1;
  free(str);
  return ret;
}


x86 assembly (clang, -O0):
<validate_file_name>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
mov    -0xc(%rbp),%edi
add    $0x1,%edi
call   1060 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0xc(%rbp),%edx
call   1050 <memcpy@plt>
mov    -0x18(%rbp),%rax
mov    -0xc(%rbp),%ecx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rdi
mov    $0x2a,%esi
call   1040 <strchr@plt>
cmp    $0x0,%rax
jne    11f2 <validate_file_name+0xa2>
mov    -0x18(%rbp),%rdi
mov    $0x3a,%esi
call   1040 <strchr@plt>
cmp    $0x0,%rax
jne    11f2 <validate_file_name+0xa2>
mov    -0x18(%rbp),%rdi
mov    $0x2f,%esi
call   1040 <strchr@plt>
cmp    $0x0,%rax
jne    11f2 <validate_file_name+0xa2>
mov    -0x18(%rbp),%rdi
lea    0xe1d(%rip),%rsi
call   1070 <strstr@plt>
cmp    $0x0,%rax
je     11f9 <validate_file_name+0xa9>
movl   $0xffffffff,-0x10(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x10(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<validate_file_name>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%ebp
mov    %rdi,%r14
lea    0x1(%rbp),%edi
call   1060 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r14,%rsi
mov    %ebp,%edx
call   1050 <memcpy@plt>
mov    %ebp,%eax
movb   $0x0,(%rbx,%rax,1)
mov    %rbx,%rdi
mov    $0x2a,%esi
call   1040 <strchr@plt>
test   %rax,%rax
jne    11c1 <validate_file_name+0x71>
mov    %rbx,%rdi
mov    $0x3a,%esi
call   1040 <strchr@plt>
test   %rax,%rax
jne    11c1 <validate_file_name+0x71>
mov    %rbx,%rdi
mov    $0x2f,%esi
call   1040 <strchr@plt>
test   %rax,%rax
jne    11c1 <validate_file_name+0x71>
lea    0xe4c(%rip),%rsi
mov    %rbx,%rdi
call   1070 <strstr@plt>
test   %rax,%rax
je     11d5 <validate_file_name+0x85>
mov    $0xffffffff,%ebp
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
xor    %ebp,%ebp
jmp    11c6 <validate_file_name+0x76>

LLVM IR (-O0):
@.str = [3 x i8] c"..\00"
define i32 @validate_file_name(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
store i8* %0, i8** %3
store i32 %1, i32* %4
store i32 0, i32* %5
%7 = load i32, i32* %4
%8 = add i32 %7, 1
%9 = call i8* @malloc(i32 %8)
store i8* %9, i8** %6
%10 = load i8*, i8** %6
%11 = load i8*, i8** %3
%12 = load i32, i32* %4
%13 = call i32 @memcpy(i8* %10, i8* %11, i32 %12)
%14 = load i8*, i8** %6
%15 = load i32, i32* %4
%16 = zext i32 %15 to i64
%17 = getelementptr inbounds i8, i8* %14, i64 %16
store i8 0, i8* %17
%18 = load i8*, i8** %6
%19 = call i32* @strchr(i8* %18, i8 signext 42)
%20 = icmp ne i32* %19, null
br i1 %20, label %33, label %21
21: ; preds = %2
%22 = load i8*, i8** %6
%23 = call i32* @strchr(i8* %22, i8 signext 58)
%24 = icmp ne i32* %23, null
br i1 %24, label %33, label %25
25: ; preds = %21
%26 = load i8*, i8** %6
%27 = call i32* @strchr(i8* %26, i8 signext 47)
%28 = icmp ne i32* %27, null
br i1 %28, label %33, label %29
29: ; preds = %25
%30 = load i8*, i8** %6
%31 = call i32* @strstr(i8* %30, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%32 = icmp ne i32* %31, null
br i1 %32, label %33, label %34
33: ; preds = %29, %25, %21, %2
store i32 -1, i32* %5
br label %34
34: ; preds = %33, %29
%35 = load i8*, i8** %6
%36 = call i32 @free(i8* %35)
%37 = load i32, i32* %5
ret i32 %37
}

LLVM IR (-O2):
@.str = [3 x i8] c"..\00"
define i32 @validate_file_name(i8* %0, i32 %1) {
%3 = add i32 %1, 1
%4 = tail call i8* @malloc(i32 %3)
%5 = tail call i32 @memcpy(i8* %4, i8* %0, i32 %1)
%6 = zext i32 %1 to i64
%7 = getelementptr inbounds i8, i8* %4, i64 %6
store i8 0, i8* %7
%8 = tail call i32* @strchr(i8* %4, i8 signext 42)
%9 = icmp eq i32* %8, null
br i1 %9, label %10, label %19
10: ; preds = %2
%11 = tail call i32* @strchr(i8* %4, i8 signext 58)
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %19
13: ; preds = %10
%14 = tail call i32* @strchr(i8* %4, i8 signext 47)
%15 = icmp eq i32* %14, null
br i1 %15, label %16, label %19
16: ; preds = %13
%17 = tail call i32* @strstr(i8* dereferenceable(1) %4, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%18 = icmp eq i32* %17, null
br i1 %18, label %20, label %19
19: ; preds = %16, %13, %10, %2
br label %20
20: ; preds = %19, %16
%21 = phi i32 [ -1, %19 ], [ 0, %16 ]
%22 = tail call i32 @free(i8* %4)
ret i32 %21
}