#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 scalar_t__ ssize_t;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
scalar_t__ read(int, char *, int);
int /*<<< orphan*/ strcpy(char *, char *);
void *strlen(char *);
scalar_t__ strncmp(char *, char *, int);
int strspn(char *, char *);
int check_directory(char *name, char *dev) {
  char buffer[20];
  char *end;
  int fd;
  ssize_t count;
  int dev_attr_len, dev_parm_len;
  unsigned int namelen;
  namelen = strlen(name);
  if ((256 - namelen) < sizeof("dev"))
    return 0;
  end = name + namelen;
  strcpy(end, "dev");
  fd = open(name, O_RDONLY);
  *end = 0;
  if (fd < 0)
    return 0;
  count = read(fd, buffer, 20);
  close(fd);
  if (count < 0)
    return 0;
  dev_attr_len = strspn(buffer, "1234567890:");
  dev_parm_len = strlen(dev);
  if (dev_attr_len != dev_parm_len)
    return 0;
  return (strncmp(dev, buffer, dev_parm_len) == 0);
}


x86 assembly (clang, -O0):
<check_directory>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x54(%rbp)
mov    $0x100,%eax
sub    -0x54(%rbp),%eax
mov    %eax,%eax
cmp    $0x4,%rax
jae    11ac <check_directory+0x3c>
movl   $0x0,-0x4(%rbp)
jmp    1286 <check_directory+0x116>
mov    -0x10(%rbp),%rax
mov    -0x54(%rbp),%ecx
add    %rcx,%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
lea    0xe3b(%rip),%rsi
call   1040 <strcpy@plt>
mov    -0x10(%rbp),%rdi
mov    0x2e13(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x38(%rbp),%rax
movb   $0x0,(%rax)
cmpl   $0x0,-0x3c(%rbp)
jge    11fc <check_directory+0x8c>
movl   $0x0,-0x4(%rbp)
jmp    1286 <check_directory+0x116>
mov    -0x3c(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x14,%edx
call   1080 <read@plt>
mov    %rax,-0x48(%rbp)
mov    -0x3c(%rbp),%edi
call   1060 <close@plt>
cmpq   $0x0,-0x48(%rbp)
jge    1230 <check_directory+0xc0>
movl   $0x0,-0x4(%rbp)
jmp    1286 <check_directory+0x116>
lea    -0x30(%rbp),%rdi
lea    0xdc9(%rip),%rsi
call   1070 <strspn@plt>
mov    %eax,-0x4c(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x50(%rbp)
mov    -0x4c(%rbp),%eax
cmp    -0x50(%rbp),%eax
je     1267 <check_directory+0xf7>
movl   $0x0,-0x4(%rbp)
jmp    1286 <check_directory+0x116>
mov    -0x18(%rbp),%rdi
lea    -0x30(%rbp),%rsi
mov    -0x50(%rbp),%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
sete   %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_directory>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
call   1050 <strlen@plt>
mov    $0x100,%ecx
sub    %eax,%ecx
xor    %r15d,%r15d
test   $0xfffffffc,%ecx
je     121e <check_directory+0xae>
mov    %eax,%r12d
lea    (%rbx,%r12,1),%rdi
lea    0xe55(%rip),%rsi
call   1040 <strcpy@plt>
mov    0x2e31(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1090 <open@plt>
movb   $0x0,(%rbx,%r12,1)
test   %eax,%eax
js     121e <check_directory+0xae>
mov    %eax,%ebp
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0x14,%edx
call   1080 <read@plt>
mov    %rax,%rbx
mov    %ebp,%edi
call   1060 <close@plt>
test   %rbx,%rbx
js     121e <check_directory+0xae>
lea    0xe13(%rip),%rsi
mov    %rsp,%rdi
call   1070 <strspn@plt>
mov    %eax,%ebx
mov    %r14,%rdi
call   1050 <strlen@plt>
cmp    %eax,%ebx
jne    121e <check_directory+0xae>
mov    %rsp,%rsi
mov    %r14,%rdi
mov    %ebx,%edx
call   1030 <strncmp@plt>
xor    %r15d,%r15d
test   %rax,%rax
sete   %r15b
mov    %r15d,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [4 x i8] c"dev\00"
@O_RDONLY = global i32 0
@.str.1 = [12 x i8] c"1234567890:\00"
define i32 @check_directory(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca [20 x i8]
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i64
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
%13 = load i8*, i8** %4
%14 = call i8* @strlen(i8* %13)
%15 = ptrtoint i8* %14 to i32
store i32 %15, i32* %12
%16 = load i32, i32* %12
%17 = sub i32 256, %16
%18 = zext i32 %17 to i64
%19 = icmp ult i64 %18, 4
br i1 %19, label %20, label %21
20: ; preds = %2
store i32 0, i32* %3
br label %61
21: ; preds = %2
%22 = load i8*, i8** %4
%23 = load i32, i32* %12
%24 = zext i32 %23 to i64
%25 = getelementptr inbounds i8, i8* %22, i64 %24
store i8* %25, i8** %7
%26 = load i8*, i8** %7
%27 = call i32 @strcpy(i8* %26, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%28 = load i8*, i8** %4
%29 = load i32, i32* @O_RDONLY
%30 = call i32 @open(i8* %28, i32 %29)
store i32 %30, i32* %8
%31 = load i8*, i8** %7
store i8 0, i8* %31
%32 = load i32, i32* %8
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %35
34: ; preds = %21
store i32 0, i32* %3
br label %61
35: ; preds = %21
%36 = load i32, i32* %8
%37 = getelementptr inbounds [20 x i8], [20 x i8]* %6, i64 0, i64 0
%38 = call i64 @read(i32 %36, i8* %37, i32 20)
store i64 %38, i64* %9
%39 = load i32, i32* %8
%40 = call i32 @close(i32 %39)
%41 = load i64, i64* %9
%42 = icmp slt i64 %41, 0
br i1 %42, label %43, label %44
43: ; preds = %35
store i32 0, i32* %3
br label %61
44: ; preds = %35
%45 = getelementptr inbounds [20 x i8], [20 x i8]* %6, i64 0, i64 0
%46 = call i32 @strspn(i8* %45, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
store i32 %46, i32* %10
%47 = load i8*, i8** %5
%48 = call i8* @strlen(i8* %47)
%49 = ptrtoint i8* %48 to i32
store i32 %49, i32* %11
%50 = load i32, i32* %10
%51 = load i32, i32* %11
%52 = icmp ne i32 %50, %51
br i1 %52, label %53, label %54
53: ; preds = %44
store i32 0, i32* %3
br label %61
54: ; preds = %44
%55 = load i8*, i8** %5
%56 = getelementptr inbounds [20 x i8], [20 x i8]* %6, i64 0, i64 0
%57 = load i32, i32* %11
%58 = call i64 @strncmp(i8* %55, i8* %56, i32 %57)
%59 = icmp eq i64 %58, 0
%60 = zext i1 %59 to i32
store i32 %60, i32* %3
br label %61
61: ; preds = %54, %53, %43, %34, %20
%62 = load i32, i32* %3
ret i32 %62
}

LLVM IR (-O2):
@.str = [4 x i8] c"dev\00"
@O_RDONLY = global i32 0
@.str.1 = [12 x i8] c"1234567890:\00"
define i32 @check_directory(i8* %0, i8* %1) {
%3 = alloca [20 x i8]
%4 = getelementptr inbounds [20 x i8], [20 x i8]* %3, i64 0, i64 0
%5 = tail call i8* @strlen(i8* %0)
%6 = ptrtoint i8* %5 to i64
%7 = sub i64 256, %6
%8 = and i64 %7, 4294967292
%9 = icmp eq i64 %8, 0
br i1 %9, label %31, label %10
10: ; preds = %2
%11 = and i64 %6, 4294967295
%12 = getelementptr inbounds i8, i8* %0, i64 %11
%13 = tail call i32 @strcpy(i8* %12, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%14 = load i32, i32* @O_RDONLY
%15 = tail call i32 @open(i8* %0, i32 %14)
store i8 0, i8* %12
%16 = icmp slt i32 %15, 0
br i1 %16, label %31, label %17
17: ; preds = %10
%18 = call i64 @read(i32 %15, i8* %4, i32 20)
%19 = tail call i32 @close(i32 %15)
%20 = icmp slt i64 %18, 0
br i1 %20, label %31, label %21
21: ; preds = %17
%22 = call i32 @strspn(i8* %4, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%23 = tail call i8* @strlen(i8* %1)
%24 = ptrtoint i8* %23 to i64
%25 = trunc i64 %24 to i32
%26 = icmp eq i32 %22, %25
br i1 %26, label %27, label %31
27: ; preds = %21
%28 = call i64 @strncmp(i8* %1, i8* %4, i32 %22)
%29 = icmp eq i64 %28, 0
%30 = zext i1 %29 to i32
br label %31
31: ; preds = %21, %17, %10, %2, %27
%32 = phi i32 [ %30, %27 ], [ 0, %2 ], [ 0, %10 ], [ 0, %17 ], [ 0, %21 ]
ret i32 %32
}