#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;
};
struct dirent {
  char const *d_name;
};
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
scalar_t__ S_ISDIR(int /*<<< orphan*/);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
char *dfcaten(char const *, char const *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *, ...);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ *opendir(char const *);
struct dirent *readdir(int /*<<< orphan*/ *);
scalar_t__ stat(char *, struct stat *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcmp(char const *, char *);
int check_sane(const char *tag, const char *dir) {
  DIR *d;
  struct dirent *de;
  struct stat sb;
  int has_bindir = 0;
  int has_sbindir = 0;
  int has_libdir = 0;
  if (!strcmp(dir, ""))
    d = opendir("/");
  else
    d = opendir(dir);
  if (d) {
    while ((de = readdir(d))) {
      char *full_path = dfcaten(dir, de->d_name);
      if (stat(full_path, &sb) < 0) {
        fprintf(stderr, "Couldn't stat %s", full_path);
      } else {
        if (S_ISDIR(sb.st_mode)) {
          has_bindir |= !strcmp(de->d_name, "bin");
          has_sbindir |= !strcmp(de->d_name, "sbin");
          has_libdir |= !strcmp(de->d_name, "lib");
        }
      }
      free(full_path);
    }
    closedir(d);
  } else {
    fprintf(stderr, "%s directory %s coudn't be opened.\n", tag, dir);
    return 0;
    exit(1);
  }
  if (!has_bindir && !has_libdir && !has_sbindir) {
    fprintf(stderr,
            "%s directory %s doesn't have bin, sbin or lib subdirectory\n", tag,
            dir);
    return 0;
  }
  return 1;
}


x86 assembly (clang, -O0):
<check_sane>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x0,-0x34(%rbp)
movl   $0x0,-0x38(%rbp)
movl   $0x0,-0x3c(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe7f(%rip),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%eax
jne    11e3 <check_sane+0x53>
lea    0xe2b(%rip),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x20(%rbp)
jmp    11f0 <check_sane+0x60>
mov    -0x18(%rbp),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1307 <check_sane+0x177>
jmp    1200 <check_sane+0x70>
mov    -0x20(%rbp),%rdi
call   10b0 <readdir@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     12f9 <check_sane+0x169>
mov    -0x18(%rbp),%rdi
mov    -0x28(%rbp),%rax
mov    (%rax),%rsi
call   1050 <dfcaten@plt>
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   10a0 <stat@plt>
cmp    $0x0,%rax
jge    1262 <check_sane+0xd2>
mov    0x2d97(%rip),%rax
mov    (%rax),%edi
mov    -0x48(%rbp),%rdx
lea    0xdac(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
jmp    12eb <check_sane+0x15b>
mov    -0x30(%rbp),%edi
call   1060 <S_ISDIR@plt>
cmp    $0x0,%rax
je     12e6 <check_sane+0x156>
mov    -0x28(%rbp),%rax
mov    (%rax),%rdi
lea    0xd92(%rip),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
and    $0x1,%al
movzbl %al,%eax
or     -0x34(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x28(%rbp),%rax
mov    (%rax),%rdi
lea    0xd6b(%rip),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
and    $0x1,%al
movzbl %al,%eax
or     -0x38(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    -0x28(%rbp),%rax
mov    (%rax),%rdi
lea    0xd4a(%rip),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
and    $0x1,%al
movzbl %al,%eax
or     -0x3c(%rbp),%eax
mov    %eax,-0x3c(%rbp)
jmp    12eb <check_sane+0x15b>
mov    -0x48(%rbp),%rdi
call   1030 <free@plt>
jmp    1200 <check_sane+0x70>
mov    -0x20(%rbp),%rdi
call   1070 <closedir@plt>
jmp    1332 <check_sane+0x1a2>
mov    0x2cd2(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
lea    0xcfd(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1382 <check_sane+0x1f2>
cmpl   $0x0,-0x34(%rbp)
jne    137b <check_sane+0x1eb>
cmpl   $0x0,-0x3c(%rbp)
jne    137b <check_sane+0x1eb>
cmpl   $0x0,-0x38(%rbp)
jne    137b <check_sane+0x1eb>
mov    0x2c89(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
lea    0xcd8(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1382 <check_sane+0x1f2>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_sane>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rsi,%rbx
mov    %rdi,%rbp
cmpb   $0x0,(%rsi)
je     11ae <check_sane+0x1e>
mov    %rbx,%rdi
jmp    11b5 <check_sane+0x25>
lea    0xe4b(%rip),%rdi
call   1040 <opendir@plt>
mov    %rax,%r13
test   %rax,%rax
je     12bd <check_sane+0x12d>
mov    %rbp,0x20(%rsp)
mov    %r13,%rdi
call   10b0 <readdir@plt>
test   %rax,%rax
mov    %rbx,0x18(%rsp)
je     12d7 <check_sane+0x147>
mov    %rax,%r14
xor    %r12d,%r12d
movl   $0x0,0xc(%rsp)
xor    %r15d,%r15d
jmp    1236 <check_sane+0xa6>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    0x2dd9(%rip),%rax
mov    (%rax),%edi
lea    0xdf2(%rip),%rsi
mov    %rbp,%rdx
xor    %eax,%eax
call   1090 <fprintf@plt>
mov    %rbp,%rdi
call   1030 <free@plt>
mov    %r13,%rdi
call   10b0 <readdir@plt>
mov    %rax,%r14
test   %rax,%rax
je     12e5 <check_sane+0x155>
mov    (%r14),%rsi
mov    %rbx,%rdi
call   1050 <dfcaten@plt>
mov    %rax,%rbp
mov    %rax,%rdi
lea    0x10(%rsp),%rsi
call   10a0 <stat@plt>
test   %rax,%rax
js     1200 <check_sane+0x70>
mov    0x10(%rsp),%edi
call   1060 <S_ISDIR@plt>
test   %rax,%rax
je     121a <check_sane+0x8a>
mov    (%r14),%rbx
mov    %rbx,%rdi
lea    0xda3(%rip),%rsi
call   1080 <strcmp@plt>
xor    %ecx,%ecx
test   %eax,%eax
sete   %cl
or     %ecx,%r15d
mov    %rbx,%rdi
lea    0xd89(%rip),%rsi
call   1080 <strcmp@plt>
xor    %ecx,%ecx
test   %eax,%eax
sete   %cl
or     %ecx,0xc(%rsp)
mov    %rbx,%rdi
mov    0x18(%rsp),%rbx
lea    0xd6f(%rip),%rsi
call   1080 <strcmp@plt>
xor    %ecx,%ecx
test   %eax,%eax
sete   %cl
or     %ecx,%r12d
jmp    121a <check_sane+0x8a>
mov    0x2d1c(%rip),%rax
mov    (%rax),%edi
lea    0xd4f(%rip),%rsi
mov    %rbx,%rcx
xor    %ebx,%ebx
mov    %rbp,%rdx
jmp    131f <check_sane+0x18f>
xor    %r15d,%r15d
movl   $0x0,0xc(%rsp)
xor    %r12d,%r12d
mov    %r13,%rdi
call   1070 <closedir@plt>
mov    $0x1,%ebx
test   %r15d,%r15d
jne    1326 <check_sane+0x196>
test   %r12d,%r12d
jne    1326 <check_sane+0x196>
cmpl   $0x0,0xc(%rsp)
jne    1326 <check_sane+0x196>
mov    0x2cd6(%rip),%rax
mov    (%rax),%edi
lea    0xd2d(%rip),%rsi
xor    %ebx,%ebx
mov    0x20(%rsp),%rdx
mov    0x18(%rsp),%rcx
xor    %eax,%eax
call   1090 <fprintf@plt>
mov    %ebx,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i8* }
%struct.stat = type { i32 }
@.str = [1 x i8] zeroinitializer
@.str.1 = [2 x i8] c"/\00"
@stderr = global i32 0
@.str.2 = [17 x i8] c"Couldn't stat %s\00"
@.str.3 = [4 x i8] c"bin\00"
@.str.4 = [5 x i8] c"sbin\00"
@.str.5 = [4 x i8] c"lib\00"
@.str.6 = [36 x i8] c"%s directory %s coudn't be opened.\0A\00"
@.str.7 = [60 x i8] c"%s directory %s doesn't have bin, sbin or lib subdirectory\0A\00"
define i32 @check_sane(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca %struct.dirent*
%8 = alloca %struct.stat
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 0, i32* %9
store i32 0, i32* %10
store i32 0, i32* %11
%13 = load i8*, i8** %5
%14 = call i32 @strcmp(i8* %13, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0))
%15 = icmp ne i32 %14, 0
br i1 %15, label %18, label %16
16: ; preds = %2
%17 = call i32* @opendir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %17, i32** %6
br label %21
18: ; preds = %2
%19 = load i8*, i8** %5
%20 = call i32* @opendir(i8* %19)
store i32* %20, i32** %6
br label %21
21: ; preds = %18, %16
%22 = load i32*, i32** %6
%23 = icmp ne i32* %22, null
br i1 %23, label %24, label %82
24: ; preds = %21
br label %25
25: ; preds = %76, %24
%26 = load i32*, i32** %6
%27 = call %struct.dirent* @readdir(i32* %26)
store %struct.dirent* %27, %struct.dirent** %7
%28 = icmp ne %struct.dirent* %27, null
br i1 %28, label %29, label %79
29: ; preds = %25
%30 = load i8*, i8** %5
%31 = load %struct.dirent*, %struct.dirent** %7
%32 = getelementptr inbounds %struct.dirent, %struct.dirent* %31, i32 0, i32 0
%33 = load i8*, i8** %32
%34 = call i8* @dfcaten(i8* %30, i8* %33)
store i8* %34, i8** %12
%35 = load i8*, i8** %12
%36 = call i64 @stat(i8* %35, %struct.stat* %8)
%37 = icmp slt i64 %36, 0
br i1 %37, label %38, label %42
38: ; preds = %29
%39 = load i32, i32* @stderr
%40 = load i8*, i8** %12
%41 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %39, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i8* %40)
br label %76
42: ; preds = %29
%43 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%44 = load i32, i32* %43
%45 = call i64 @S_ISDIR(i32 %44)
%46 = icmp ne i64 %45, 0
br i1 %46, label %47, label %75
47: ; preds = %42
%48 = load %struct.dirent*, %struct.dirent** %7
%49 = getelementptr inbounds %struct.dirent, %struct.dirent* %48, i32 0, i32 0
%50 = load i8*, i8** %49
%51 = call i32 @strcmp(i8* %50, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0))
%52 = icmp ne i32 %51, 0
%53 = xor i1 %52, true
%54 = zext i1 %53 to i32
%55 = load i32, i32* %9
%56 = or i32 %55, %54
store i32 %56, i32* %9
%57 = load %struct.dirent*, %struct.dirent** %7
%58 = getelementptr inbounds %struct.dirent, %struct.dirent* %57, i32 0, i32 0
%59 = load i8*, i8** %58
%60 = call i32 @strcmp(i8* %59, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
%61 = icmp ne i32 %60, 0
%62 = xor i1 %61, true
%63 = zext i1 %62 to i32
%64 = load i32, i32* %10
%65 = or i32 %64, %63
store i32 %65, i32* %10
%66 = load %struct.dirent*, %struct.dirent** %7
%67 = getelementptr inbounds %struct.dirent, %struct.dirent* %66, i32 0, i32 0
%68 = load i8*, i8** %67
%69 = call i32 @strcmp(i8* %68, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0))
%70 = icmp ne i32 %69, 0
%71 = xor i1 %70, true
%72 = zext i1 %71 to i32
%73 = load i32, i32* %11
%74 = or i32 %73, %72
store i32 %74, i32* %11
br label %75
75: ; preds = %47, %42
br label %76
76: ; preds = %75, %38
%77 = load i8*, i8** %12
%78 = call i32 @free(i8* %77)
br label %25
79: ; preds = %25
%80 = load i32*, i32** %6
%81 = call i32 @closedir(i32* %80)
br label %87
82: ; preds = %21
%83 = load i32, i32* @stderr
%84 = load i8*, i8** %4
%85 = load i8*, i8** %5
%86 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %83, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.6, i64 0, i64 0), i8* %84, i8* %85)
store i32 0, i32* %3
br label %102
87: ; preds = %79
%88 = load i32, i32* %9
%89 = icmp ne i32 %88, 0
br i1 %89, label %101, label %90
90: ; preds = %87
%91 = load i32, i32* %11
%92 = icmp ne i32 %91, 0
br i1 %92, label %101, label %93
93: ; preds = %90
%94 = load i32, i32* %10
%95 = icmp ne i32 %94, 0
br i1 %95, label %101, label %96
96: ; preds = %93
%97 = load i32, i32* @stderr
%98 = load i8*, i8** %4
%99 = load i8*, i8** %5
%100 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %97, i8* getelementptr inbounds ([60 x i8], [60 x i8]* @.str.7, i64 0, i64 0), i8* %98, i8* %99)
store i32 0, i32* %3
br label %102
101: ; preds = %93, %90, %87
store i32 1, i32* %3
br label %102
102: ; preds = %101, %96, %82
%103 = load i32, i32* %3
ret i32 %103
}

LLVM IR (-O2):
%struct.stat = type { i32 }
%struct.dirent = type { i8* }
@.str.1 = [2 x i8] c"/\00"
@stderr = global i32 0
@.str.2 = [17 x i8] c"Couldn't stat %s\00"
@.str.3 = [4 x i8] c"bin\00"
@.str.4 = [5 x i8] c"sbin\00"
@.str.5 = [4 x i8] c"lib\00"
@.str.6 = [36 x i8] c"%s directory %s coudn't be opened.\0A\00"
@.str.7 = [60 x i8] c"%s directory %s doesn't have bin, sbin or lib subdirectory\0A\00"
define i32 @check_sane(i8* %0, i8* %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = load i8, i8* %1
%6 = icmp eq i8 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %2
%8 = tail call i32* @opendir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
br label %11
9: ; preds = %2
%10 = tail call i32* @opendir(i8* %1)
br label %11
11: ; preds = %9, %7
%12 = phi i32* [ %10, %9 ], [ %8, %7 ]
%13 = icmp eq i32* %12, null
br i1 %13, label %67, label %14
14: ; preds = %11
%15 = call %struct.dirent* @readdir(i32* %12)
%16 = icmp eq %struct.dirent* %15, null
br i1 %16, label %57, label %17
17: ; preds = %14
%18 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
br label %19
19: ; preds = %17, %50
%20 = phi %struct.dirent* [ %15, %17 ], [ %55, %50 ]
%21 = phi i32 [ 0, %17 ], [ %53, %50 ]
%22 = phi i32 [ 0, %17 ], [ %52, %50 ]
%23 = phi i32 [ 0, %17 ], [ %51, %50 ]
%24 = getelementptr inbounds %struct.dirent, %struct.dirent* %20, i64 0, i32 0
%25 = load i8*, i8** %24
%26 = call i8* @dfcaten(i8* %1, i8* %25)
%27 = call i64 @stat(i8* %26, %struct.stat* %3)
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %19
%30 = load i32, i32* @stderr
%31 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %30, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i8* %26)
br label %50
32: ; preds = %19
%33 = load i32, i32* %18
%34 = call i64 @S_ISDIR(i32 %33)
%35 = icmp eq i64 %34, 0
br i1 %35, label %50, label %36
36: ; preds = %32
%37 = load i8*, i8** %24
%38 = call i32 @strcmp(i8* dereferenceable(1) %37, i8* dereferenceable(4) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0))
%39 = icmp eq i32 %38, 0
%40 = zext i1 %39 to i32
%41 = or i32 %23, %40
%42 = call i32 @strcmp(i8* dereferenceable(1) %37, i8* dereferenceable(5) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
%43 = icmp eq i32 %42, 0
%44 = zext i1 %43 to i32
%45 = or i32 %22, %44
%46 = call i32 @strcmp(i8* dereferenceable(1) %37, i8* dereferenceable(4) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0))
%47 = icmp eq i32 %46, 0
%48 = zext i1 %47 to i32
%49 = or i32 %21, %48
br label %50
50: ; preds = %32, %36, %29
%51 = phi i32 [ %23, %29 ], [ %41, %36 ], [ %23, %32 ]
%52 = phi i32 [ %22, %29 ], [ %45, %36 ], [ %22, %32 ]
%53 = phi i32 [ %21, %29 ], [ %49, %36 ], [ %21, %32 ]
%54 = call i32 @free(i8* %26)
%55 = call %struct.dirent* @readdir(i32* %12)
%56 = icmp eq %struct.dirent* %55, null
br i1 %56, label %57, label %19
57: ; preds = %50, %14
%58 = phi i32 [ 0, %14 ], [ %51, %50 ]
%59 = phi i32 [ 0, %14 ], [ %52, %50 ]
%60 = phi i32 [ 0, %14 ], [ %53, %50 ]
%61 = call i32 @closedir(i32* %12)
%62 = icmp ne i32 %58, 0
%63 = icmp ne i32 %60, 0
%64 = select i1 %62, i1 true, i1 %63
%65 = icmp ne i32 %59, 0
%66 = select i1 %64, i1 true, i1 %65
br i1 %66, label %73, label %70
67: ; preds = %11
%68 = load i32, i32* @stderr
%69 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %68, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.6, i64 0, i64 0), i8* %0, i8* %1)
br label %73
70: ; preds = %57
%71 = load i32, i32* @stderr
%72 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %71, i8* getelementptr inbounds ([60 x i8], [60 x i8]* @.str.7, i64 0, i64 0), i8* %0, i8* %1)
br label %73
73: ; preds = %57, %70, %67
%74 = phi i32 [ 0, %70 ], [ 0, %67 ], [ 1, %57 ]
ret i32 %74
}