#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 */
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
size_t const fread(char *, int, size_t const, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
char *malloc(int);
int /*<<< orphan*/ memcmp(char *, char *, size_t const);
int /*<<< orphan*/ snprintf(char *, int, char *, char const *, char *);
int strlen(char const *);
int has_cachedir_tag(const char *name) {
  static int path_l = 1024;
  static char *path = NULL;
  int l;
  const size_t signature_l =
      sizeof "Signature: 8a477f597d28d172789f06886806bc55" - 1;
  char buf[signature_l];
  FILE *f;
  int match = 0;
  l = strlen(name) + sizeof "CACHEDIR.TAG" + 2;
  if (l > path_l || path == NULL) {
    path_l = path_l * 2;
    if (path_l < l)
      path_l = l;
    free(path);
    path = malloc(path_l);
  }
  snprintf(path, path_l, "%s/%s", name, "CACHEDIR.TAG");
  f = fopen(path, "rb");
  if (f != NULL) {
    match = ((fread(buf, 1, signature_l, f) == signature_l) &&
             !memcmp(buf, "Signature: 8a477f597d28d172789f06886806bc55",
                     signature_l));
    fclose(f);
  }
  return match;
}


x86 assembly (clang, -O0):
<has_cachedir_tag>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x2b,-0x18(%rbp)
mov    %rsp,%rax
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x8(%rbp),%rdi
call   1060 <strlen@plt>
cltq
add    $0xd,%rax
add    $0x2,%rax
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
cmp    0x2e9f(%rip),%eax
jg     11d5 <has_cachedir_tag+0x55>
cmpq   $0x0,0x2ea1(%rip)
jne    121a <has_cachedir_tag+0x9a>
mov    0x2e85(%rip),%eax
shl    $0x1,%eax
mov    %eax,0x2e7c(%rip)
mov    0x2e76(%rip),%eax
cmp    -0xc(%rbp),%eax
jge    11fc <has_cachedir_tag+0x7c>
mov    -0xc(%rbp),%eax
mov    %eax,0x2e64(%rip)
mov    0x2e6d(%rip),%rdi
call   1030 <free@plt>
mov    0x2e52(%rip),%edi
call   1090 <malloc@plt>
mov    %rax,0x2e56(%rip)
mov    0x2e4f(%rip),%rdi
mov    0x2e39(%rip),%esi
mov    -0x8(%rbp),%rcx
lea    0xdce(%rip),%rdx
lea    0xdcd(%rip),%r8
call   1070 <snprintf@plt>
mov    0x2e2b(%rip),%rdi
lea    0xdc7(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     12bd <has_cachedir_tag+0x13d>
mov    -0x28(%rbp),%rcx
lea    -0x60(%rbp),%rdi
mov    $0x1,%esi
mov    $0x2b,%edx
call   1040 <fread@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    $0x2b,%rcx
mov    %al,-0x61(%rbp)
jne    12a9 <has_cachedir_tag+0x129>
lea    0xd86(%rip),%rsi
lea    -0x60(%rbp),%rdi
mov    $0x2b,%edx
call   1080 <memcmp@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
mov    %al,-0x61(%rbp)
mov    -0x61(%rbp),%al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x28(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x2c(%rbp),%eax
mov    -0x20(%rbp),%rcx
mov    %rcx,%rsp
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<has_cachedir_tag>:
push   %rbp
push   %rbx
sub    $0x38,%rsp
mov    %rdi,%rbx
call   1060 <strlen@plt>
add    $0xf,%eax
mov    0x2ec9(%rip),%ebp
mov    0x2ed2(%rip),%rdi
cmp    %ebp,%eax
jg     11a7 <has_cachedir_tag+0x27>
test   %rdi,%rdi
jne    11ca <has_cachedir_tag+0x4a>
add    %ebp,%ebp
cmp    %eax,%ebp
cmovl  %eax,%ebp
mov    %ebp,0x2eac(%rip)
call   1030 <free@plt>
mov    %ebp,%edi
call   1080 <malloc@plt>
mov    %rax,%rdi
mov    %rax,0x2ea6(%rip)
lea    0xe2f(%rip),%rdx
lea    0xe2e(%rip),%r8
mov    %ebp,%esi
mov    %rbx,%rcx
call   1070 <snprintf@plt>
mov    0x2e87(%rip),%rdi
lea    0xe23(%rip),%rsi
call   10a0 <fopen@plt>
test   %rax,%rax
je     1240 <has_cachedir_tag+0xc0>
mov    %rax,%rbx
mov    %rsp,%rdi
mov    $0x2b,%edx
mov    $0x1,%esi
mov    %rax,%rcx
call   1040 <fread@plt>
xor    %ebp,%ebp
cmp    $0x2b,%rax
jne    1236 <has_cachedir_tag+0xb6>
lea    0xdf5(%rip),%rsi
mov    %rsp,%rdi
mov    $0x2b,%edx
call   1090 <bcmp@plt>
xor    %ebp,%ebp
test   %eax,%eax
sete   %bpl
mov    %rbx,%rdi
call   1050 <fclose@plt>
jmp    1242 <has_cachedir_tag+0xc2>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x38,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@has_cachedir_tag.path_l = internal global i32 1024
@has_cachedir_tag.path = internal global i8* null
@.str = [6 x i8] c"%s/%s\00"
@.str.1 = [13 x i8] c"CACHEDIR.TAG\00"
@.str.2 = [3 x i8] c"rb\00"
@.str.3 = [44 x i8] c"Signature: 8a477f597d28d172789f06886806bc55\00"
define i32 @has_cachedir_tag(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i64
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i32
store i8* %0, i8** %2
store i64 43, i64* %4
%8 = call i8* @llvm.stacksave()
store i8* %8, i8** %5
%9 = alloca i8, i64 43
store i32 0, i32* %7
%10 = load i8*, i8** %2
%11 = call i32 @strlen(i8* %10)
%12 = sext i32 %11 to i64
%13 = add i64 %12, 13
%14 = add i64 %13, 2
%15 = trunc i64 %14 to i32
store i32 %15, i32* %3
%16 = load i32, i32* %3
%17 = load i32, i32* @has_cachedir_tag.path_l
%18 = icmp sgt i32 %16, %17
br i1 %18, label %22, label %19
19: ; preds = %1
%20 = load i8*, i8** @has_cachedir_tag.path
%21 = icmp eq i8* %20, null
br i1 %21, label %22, label %35
22: ; preds = %19, %1
%23 = load i32, i32* @has_cachedir_tag.path_l
%24 = mul nsw i32 %23, 2
store i32 %24, i32* @has_cachedir_tag.path_l
%25 = load i32, i32* @has_cachedir_tag.path_l
%26 = load i32, i32* %3
%27 = icmp slt i32 %25, %26
br i1 %27, label %28, label %30
28: ; preds = %22
%29 = load i32, i32* %3
store i32 %29, i32* @has_cachedir_tag.path_l
br label %30
30: ; preds = %28, %22
%31 = load i8*, i8** @has_cachedir_tag.path
%32 = call i32 @free(i8* %31)
%33 = load i32, i32* @has_cachedir_tag.path_l
%34 = call i8* @malloc(i32 %33)
store i8* %34, i8** @has_cachedir_tag.path
br label %35
35: ; preds = %30, %19
%36 = load i8*, i8** @has_cachedir_tag.path
%37 = load i32, i32* @has_cachedir_tag.path_l
%38 = load i8*, i8** %2
%39 = call i32 @snprintf(i8* %36, i32 %37, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %38, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%40 = load i8*, i8** @has_cachedir_tag.path
%41 = call i32* @fopen(i8* %40, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
store i32* %41, i32** %6
%42 = load i32*, i32** %6
%43 = icmp ne i32* %42, null
br i1 %43, label %44, label %57
44: ; preds = %35
%45 = load i32*, i32** %6
%46 = call i64 @fread(i8* %9, i32 1, i64 43, i32* %45)
%47 = icmp eq i64 %46, 43
br i1 %47, label %48, label %52
48: ; preds = %44
%49 = call i32 @memcmp(i8* %9, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.3, i64 0, i64 0), i64 43)
%50 = icmp ne i32 %49, 0
%51 = xor i1 %50, true
br label %52
52: ; preds = %48, %44
%53 = phi i1 [ false, %44 ], [ %51, %48 ]
%54 = zext i1 %53 to i32
store i32 %54, i32* %7
%55 = load i32*, i32** %6
%56 = call i32 @fclose(i32* %55)
br label %57
57: ; preds = %52, %35
%58 = load i32, i32* %7
%59 = load i8*, i8** %5
ret i32 %58
}

LLVM IR (-O2):
@has_cachedir_tag.path_l = internal unnamed_addr global i32 1024
@has_cachedir_tag.path = internal unnamed_addr global i8* null
@.str = [6 x i8] c"%s/%s\00"
@.str.1 = [13 x i8] c"CACHEDIR.TAG\00"
@.str.2 = [3 x i8] c"rb\00"
@.str.3 = [44 x i8] c"Signature: 8a477f597d28d172789f06886806bc55\00"
define i32 @has_cachedir_tag(i8* %0) {
%2 = alloca [43 x i8]
%3 = getelementptr inbounds [43 x i8], [43 x i8]* %2, i64 0, i64 0
%4 = tail call i32 @strlen(i8* %0)
%5 = add i32 %4, 15
%6 = load i32, i32* @has_cachedir_tag.path_l
%7 = icmp sgt i32 %5, %6
%8 = load i8*, i8** @has_cachedir_tag.path
%9 = icmp eq i8* %8, null
%10 = select i1 %7, i1 true, i1 %9
br i1 %10, label %11, label %17
11: ; preds = %1
%12 = shl nsw i32 %6, 1
%13 = icmp slt i32 %12, %5
%14 = select i1 %13, i32 %5, i32 %12
store i32 %14, i32* @has_cachedir_tag.path_l
%15 = tail call i32 @free(i8* %8)
%16 = tail call i8* @malloc(i32 %14)
store i8* %16, i8** @has_cachedir_tag.path
br label %17
17: ; preds = %1, %11
%18 = phi i32 [ %6, %1 ], [ %14, %11 ]
%19 = phi i8* [ %8, %1 ], [ %16, %11 ]
%20 = tail call i32 @snprintf(i8* %19, i32 %18, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %0, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%21 = load i8*, i8** @has_cachedir_tag.path
%22 = tail call i32* @fopen(i8* %21, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%23 = icmp eq i32* %22, null
br i1 %23, label %34, label %24
24: ; preds = %17
%25 = call i64 @fread(i8* %3, i32 1, i64 43, i32* %22)
%26 = icmp eq i64 %25, 43
br i1 %26, label %27, label %31
27: ; preds = %24
%28 = call i32 @bcmp(i8* dereferenceable(43) %3, i8* dereferenceable(43) getelementptr inbounds ([44 x i8], [44 x i8]* @.str.3, i64 0, i64 0), i64 43)
%29 = icmp eq i32 %28, 0
%30 = zext i1 %29 to i32
br label %31
31: ; preds = %27, %24
%32 = phi i32 [ 0, %24 ], [ %30, %27 ]
%33 = tail call i32 @fclose(i32* %22)
br label %34
34: ; preds = %31, %17
%35 = phi i32 [ %32, %31 ], [ 0, %17 ]
ret i32 %35
}