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

/* Variables and functions */
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ *opendir(char const *);
struct dirent *readdir(int /*<<< orphan*/ *);
char *strdup(int /*<<< orphan*/);
int /*<<< orphan*/ strlen(char const *);
scalar_t__ strncmp(int /*<<< orphan*/, char const *, int /*<<< orphan*/);
int dinfo_find_entry(const char *dir, const char *searchstring, char type,
                     char **result) {
  DIR *directory = NULL;
  struct dirent *dir_entry = NULL;
  directory = opendir(dir);
  if (directory == NULL)
    return -1;
  while ((dir_entry = readdir(directory)) != NULL) {
    if ((strncmp(dir_entry->d_name, searchstring, strlen(searchstring)) == 0) &&
        (dir_entry->d_type & type)) {
      *result = strdup(dir_entry->d_name);
      if (*result == NULL)
        goto out;
      closedir(directory);
      return 0;
    }
  }
out:
  closedir(directory);
  return -1;
}


x86 assembly (clang, -O0):
<dinfo_find_entry>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %dl,%al
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %al,-0x19(%rbp)
mov    %rcx,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11ad <dinfo_find_entry+0x4d>
movl   $0xffffffff,-0x4(%rbp)
jmp    126a <dinfo_find_entry+0x10a>
jmp    11b2 <dinfo_find_entry+0x52>
mov    -0x30(%rbp),%rdi
call   1070 <readdir@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
je     1255 <dinfo_find_entry+0xf5>
mov    -0x38(%rbp),%rax
mov    0x4(%rax),%eax
mov    %eax,-0x44(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x44(%rbp),%edi
mov    -0x40(%rbp),%rsi
mov    %eax,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    1250 <dinfo_find_entry+0xf0>
mov    -0x38(%rbp),%rax
movsbl (%rax),%eax
movsbl -0x19(%rbp),%ecx
and    %ecx,%eax
cmp    $0x0,%eax
je     1250 <dinfo_find_entry+0xf0>
mov    -0x38(%rbp),%rax
mov    0x4(%rax),%edi
call   1080 <strdup@plt>
mov    %rax,%rcx
mov    -0x28(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x28(%rbp),%rax
cmpq   $0x0,(%rax)
jne    123b <dinfo_find_entry+0xdb>
jmp    125a <dinfo_find_entry+0xfa>
mov    -0x30(%rbp),%rdi
call   1060 <closedir@plt>
movl   $0x0,-0x4(%rbp)
jmp    126a <dinfo_find_entry+0x10a>
jmp    11b2 <dinfo_find_entry+0x52>
jmp    125a <dinfo_find_entry+0xfa>
mov    -0x30(%rbp),%rdi
call   1060 <closedir@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dinfo_find_entry>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r14
mov    %edx,%r15d
mov    %rsi,%r13
call   1040 <opendir@plt>
test   %rax,%rax
je     11f2 <dinfo_find_entry+0x92>
mov    %rax,%r12
mov    %rax,%rdi
call   1070 <readdir@plt>
test   %rax,%rax
je     11e3 <dinfo_find_entry+0x83>
mov    %rax,%rbx
jmp    11b0 <dinfo_find_entry+0x50>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r12,%rdi
call   1070 <readdir@plt>
mov    %rax,%rbx
test   %rax,%rax
je     11e3 <dinfo_find_entry+0x83>
mov    0x4(%rbx),%ebp
mov    %r13,%rdi
call   1050 <strlen@plt>
mov    %ebp,%edi
mov    %r13,%rsi
mov    %eax,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
jne    11a0 <dinfo_find_entry+0x40>
test   %r15b,(%rbx)
je     11a0 <dinfo_find_entry+0x40>
mov    0x4(%rbx),%edi
call   1080 <strdup@plt>
mov    %rax,(%r14)
xor    %ebx,%ebx
test   %rax,%rax
jne    11e8 <dinfo_find_entry+0x88>
mov    $0xffffffff,%ebx
mov    %r12,%rdi
call   1060 <closedir@plt>
jmp    11f7 <dinfo_find_entry+0x97>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i8, i32 }
define i32 @dinfo_find_entry(i8* %0, i8* %1, i8 signext %2, i8** %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8
%9 = alloca i8**
%10 = alloca i32*
%11 = alloca %struct.dirent*
store i8* %0, i8** %6
store i8* %1, i8** %7
store i8 %2, i8* %8
store i8** %3, i8*** %9
store i32* null, i32** %10
store %struct.dirent* null, %struct.dirent** %11
%12 = load i8*, i8** %6
%13 = call i32* @opendir(i8* %12)
store i32* %13, i32** %10
%14 = load i32*, i32** %10
%15 = icmp eq i32* %14, null
br i1 %15, label %16, label %17
16: ; preds = %4
store i32 -1, i32* %5
br label %58
17: ; preds = %4
br label %18
18: ; preds = %53, %17
%19 = load i32*, i32** %10
%20 = call %struct.dirent* @readdir(i32* %19)
store %struct.dirent* %20, %struct.dirent** %11
%21 = icmp ne %struct.dirent* %20, null
br i1 %21, label %22, label %54
22: ; preds = %18
%23 = load %struct.dirent*, %struct.dirent** %11
%24 = getelementptr inbounds %struct.dirent, %struct.dirent* %23, i32 0, i32 1
%25 = load i32, i32* %24
%26 = load i8*, i8** %7
%27 = load i8*, i8** %7
%28 = call i32 @strlen(i8* %27)
%29 = call i64 @strncmp(i32 %25, i8* %26, i32 %28)
%30 = icmp eq i64 %29, 0
br i1 %30, label %31, label %53
31: ; preds = %22
%32 = load %struct.dirent*, %struct.dirent** %11
%33 = getelementptr inbounds %struct.dirent, %struct.dirent* %32, i32 0, i32 0
%34 = load i8, i8* %33
%35 = sext i8 %34 to i32
%36 = load i8, i8* %8
%37 = sext i8 %36 to i32
%38 = and i32 %35, %37
%39 = icmp ne i32 %38, 0
br i1 %39, label %40, label %53
40: ; preds = %31
%41 = load %struct.dirent*, %struct.dirent** %11
%42 = getelementptr inbounds %struct.dirent, %struct.dirent* %41, i32 0, i32 1
%43 = load i32, i32* %42
%44 = call i8* @strdup(i32 %43)
%45 = load i8**, i8*** %9
store i8* %44, i8** %45
%46 = load i8**, i8*** %9
%47 = load i8*, i8** %46
%48 = icmp eq i8* %47, null
br i1 %48, label %49, label %50
49: ; preds = %40
br label %55
50: ; preds = %40
%51 = load i32*, i32** %10
%52 = call i32 @closedir(i32* %51)
store i32 0, i32* %5
br label %58
53: ; preds = %31, %22
br label %18
54: ; preds = %18
br label %55
55: ; preds = %54, %49
%56 = load i32*, i32** %10
%57 = call i32 @closedir(i32* %56)
store i32 -1, i32* %5
br label %58
58: ; preds = %55, %50, %16
%59 = load i32, i32* %5
ret i32 %59
}

LLVM IR (-O2):
%struct.dirent = type { i8, i32 }
define i32 @dinfo_find_entry(i8* nocapture %0, i8* %1, i8 signext %2, i8** nocapture writeonly %3) {
%5 = tail call i32* @opendir(i8* %0)
%6 = icmp eq i32* %5, null
br i1 %6, label %34, label %7
7: ; preds = %4
%8 = tail call %struct.dirent* @readdir(i32* %5)
%9 = icmp eq %struct.dirent* %8, null
br i1 %9, label %30, label %10
10: ; preds = %7, %27
%11 = phi %struct.dirent* [ %28, %27 ], [ %8, %7 ]
%12 = getelementptr inbounds %struct.dirent, %struct.dirent* %11, i64 0, i32 1
%13 = load i32, i32* %12
%14 = tail call i32 @strlen(i8* %1)
%15 = tail call i64 @strncmp(i32 %13, i8* %1, i32 %14)
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %27
17: ; preds = %10
%18 = getelementptr inbounds %struct.dirent, %struct.dirent* %11, i64 0, i32 0
%19 = load i8, i8* %18
%20 = and i8 %19, %2
%21 = icmp eq i8 %20, 0
br i1 %21, label %27, label %22
22: ; preds = %17
%23 = getelementptr inbounds %struct.dirent, %struct.dirent* %11, i64 0, i32 1
%24 = load i32, i32* %23
%25 = tail call i8* @strdup(i32 %24)
store i8* %25, i8** %3
%26 = icmp eq i8* %25, null
br i1 %26, label %30, label %31
27: ; preds = %17, %10
%28 = tail call %struct.dirent* @readdir(i32* %5)
%29 = icmp eq %struct.dirent* %28, null
br i1 %29, label %30, label %10
30: ; preds = %27, %7, %22
br label %31
31: ; preds = %22, %30
%32 = phi i32 [ -1, %30 ], [ 0, %22 ]
%33 = tail call i32 @closedir(i32* %5)
br label %34
34: ; preds = %31, %4
%35 = phi i32 [ -1, %4 ], [ %32, %31 ]
ret i32 %35
}