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

/* Variables and functions */
int /*<<< orphan*/ S_ISDIR(int /*<<< orphan*/);
int /*<<< orphan*/ basename(char *);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
scalar_t__ compare_type(int /*<<< orphan*/, char);
scalar_t__ errno;
int /*<<< orphan*/ fnmatch(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ free(char *);
int lstat(char *, struct stat *);
char *malloc(scalar_t__);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ perror_prefix(char *);
int /*<<< orphan*/ puts(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ sprintf(char *, char *, char *, char *);
scalar_t__ strcmp(char *, char *);
scalar_t__ strlen(char *);
void searchdir(char *dirname, char *findme, char type) {
  DIR *dir;
  struct dirent *direntp;
  struct stat statp;
  if (lstat(dirname, &statp) == -1) {
    perror_prefix(dirname);
    return;
  }
  if ((!findme || !fnmatch(findme, basename(dirname), 0)) &&
      compare_type(statp.st_mode, type)) {
    puts(dirname);
  }
  if (!S_ISDIR(statp.st_mode))
    return;
  if ((dir = opendir(dirname)) == NULL) {
    perror_prefix(dirname);
    return;
  }
  while (true) {
    errno = 0;
    direntp = readdir(dir);
    if (direntp == NULL) {
      if (errno != 0)
        perror_prefix(dirname);
      break;
    }
    if (strcmp(direntp->d_name, ".") != 0 &&
        strcmp(direntp->d_name, "..") != 0) {
      char *current_path =
          malloc(strlen(dirname) + strlen(direntp->d_name) + 2);
      sprintf(current_path, "%s/%s", dirname, direntp->d_name);
      searchdir(current_path, findme, type);
      free(current_path);
    }
  }
  closedir(dir);
}


x86 assembly (clang, -O0):
<searchdir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %dl,%al
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %al,-0x11(%rbp)
mov    -0x8(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   10b0 <lstat@plt>
cmp    $0xffffffff,%eax
jne    1239 <searchdir+0x39>
mov    -0x8(%rbp),%rdi
call   1110 <perror_prefix@plt>
jmp    13b9 <searchdir+0x1b9>
cmpq   $0x0,-0x10(%rbp)
je     126b <searchdir+0x6b>
mov    -0x10(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x8(%rbp),%rdi
call   10d0 <basename@plt>
mov    -0x40(%rbp),%rdi
mov    %eax,%esi
xor    %edx,%edx
call   1090 <fnmatch@plt>
cmp    $0x0,%eax
jne    128a <searchdir+0x8a>
mov    -0x30(%rbp),%edi
movsbl -0x11(%rbp),%esi
call   1070 <compare_type@plt>
cmp    $0x0,%rax
je     128a <searchdir+0x8a>
mov    -0x8(%rbp),%rdi
call   1040 <puts@plt>
mov    -0x30(%rbp),%edi
call   1080 <S_ISDIR@plt>
cmp    $0x0,%eax
jne    12a0 <searchdir+0xa0>
jmp    13b9 <searchdir+0x1b9>
mov    -0x8(%rbp),%rdi
call   1050 <opendir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    12c5 <searchdir+0xc5>
mov    -0x8(%rbp),%rdi
call   1110 <perror_prefix@plt>
jmp    13b9 <searchdir+0x1b9>
jmp    12ca <searchdir+0xca>
mov    0x2d0f(%rip),%rax
movq   $0x0,(%rax)
mov    -0x20(%rbp),%rdi
call   10e0 <readdir@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    130f <searchdir+0x10f>
mov    0x2ce9(%rip),%rax
cmpq   $0x0,(%rax)
je     130a <searchdir+0x10a>
mov    -0x8(%rbp),%rdi
call   1110 <perror_prefix@plt>
jmp    13b0 <searchdir+0x1b0>
mov    -0x28(%rbp),%rax
mov    (%rax),%rdi
lea    0xce4(%rip),%rsi
call   10c0 <strcmp@plt>
cmp    $0x0,%rax
je     13ab <searchdir+0x1ab>
mov    -0x28(%rbp),%rax
mov    (%rax),%rdi
lea    0xcc6(%rip),%rsi
call   10c0 <strcmp@plt>
cmp    $0x0,%rax
je     13ab <searchdir+0x1ab>
mov    -0x8(%rbp),%rdi
call   1060 <strlen@plt>
mov    %rax,-0x48(%rbp)
mov    -0x28(%rbp),%rax
mov    (%rax),%rdi
call   1060 <strlen@plt>
mov    -0x48(%rbp),%rdi
add    %rax,%rdi
add    $0x2,%rdi
call   10f0 <malloc@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
mov    -0x8(%rbp),%rdx
mov    -0x28(%rbp),%rax
mov    (%rax),%rcx
lea    0xc77(%rip),%rsi
call   1120 <sprintf@plt>
mov    -0x38(%rbp),%rdi
mov    -0x10(%rbp),%rsi
movsbl -0x11(%rbp),%edx
call   1100 <searchdir@plt>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
jmp    12ca <searchdir+0xca>
mov    -0x20(%rbp),%rdi
call   10a0 <closedir@plt>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<searchdir>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edx,%r15d
mov    %rsi,%r14
mov    %rdi,%rbp
mov    %rsp,%rsi
call   10b0 <lstat@plt>
cmp    $0xffffffff,%eax
je     1386 <searchdir+0x186>
test   %r14,%r14
je     124a <searchdir+0x4a>
mov    %rbp,%rdi
call   10d0 <basename@plt>
mov    %r14,%rdi
mov    %eax,%esi
xor    %edx,%edx
call   1090 <fnmatch@plt>
test   %eax,%eax
je     124a <searchdir+0x4a>
mov    (%rsp),%ebx
jmp    1265 <searchdir+0x65>
mov    (%rsp),%ebx
movsbl %r15b,%esi
mov    %ebx,%edi
call   1070 <compare_type@plt>
test   %rax,%rax
je     1265 <searchdir+0x65>
mov    %rbp,%rdi
call   1040 <puts@plt>
mov    %ebx,%edi
call   1080 <S_ISDIR@plt>
test   %eax,%eax
je     139c <searchdir+0x19c>
mov    %rbp,%rdi
call   1050 <opendir@plt>
test   %rax,%rax
je     1386 <searchdir+0x186>
mov    %rax,%r12
mov    %rbp,0x8(%rsp)
mov    0x2d4c(%rip),%rbx
movq   $0x0,(%rbx)
mov    %rax,%rdi
call   10e0 <readdir@plt>
test   %rax,%rax
je     13ab <searchdir+0x1ab>
mov    %rax,%r13
lea    0xd4b(%rip),%rbp
movsbl %r15b,%eax
mov    %eax,0x4(%rsp)
mov    %r12,0x10(%rsp)
jmp    12eb <searchdir+0xeb>
cs nopw 0x0(%rax,%rax,1)
nop
movq   $0x0,(%rbx)
mov    %r12,%rdi
call   10e0 <readdir@plt>
mov    %rax,%r13
test   %rax,%rax
je     13ab <searchdir+0x1ab>
mov    0x0(%r13),%rdi
mov    %rbp,%rsi
call   10c0 <strcmp@plt>
test   %rax,%rax
je     12d0 <searchdir+0xd0>
mov    0x0(%r13),%rdi
lea    0xcf9(%rip),%rsi
call   10c0 <strcmp@plt>
test   %rax,%rax
je     12d0 <searchdir+0xd0>
mov    0x8(%rsp),%r12
mov    %r12,%rdi
call   1060 <strlen@plt>
mov    %r14,%rbx
mov    %rax,%r14
mov    %rbp,%r15
mov    0x0(%r13),%rbp
mov    %rbp,%rdi
call   1060 <strlen@plt>
lea    (%r14,%rax,1),%rdi
add    $0x2,%rdi
mov    %rbx,%r14
call   10f0 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
lea    0xcb3(%rip),%rsi
mov    %r12,%rdx
mov    0x10(%rsp),%r12
mov    %rbp,%rcx
mov    %r15,%rbp
call   1120 <sprintf@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    0x4(%rsp),%edx
call   1100 <searchdir@plt>
mov    %rbx,%rdi
mov    0x2c64(%rip),%rbx
call   1030 <free@plt>
jmp    12d0 <searchdir+0xd0>
mov    %rbp,%rdi
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1110 <perror_prefix@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
cmpq   $0x0,(%rbx)
je     13bb <searchdir+0x1bb>
mov    0x8(%rsp),%rdi
call   1110 <perror_prefix@plt>
mov    %r12,%rdi
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    10a0 <closedir@plt>

LLVM IR (-O0):
%struct.dirent = type { i8* }
%struct.stat = type { i32 }
@errno = global i64 0
@.str = [2 x i8] c".\00"
@.str.1 = [3 x i8] c"..\00"
@.str.2 = [6 x i8] c"%s/%s\00"
define void @searchdir(i8* %0, i8* %1, i8 signext %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8
%7 = alloca i32*
%8 = alloca %struct.dirent*
%9 = alloca %struct.stat
%10 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
store i8 %2, i8* %6
%11 = load i8*, i8** %4
%12 = call i32 @lstat(i8* %11, %struct.stat* %9)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %17
14: ; preds = %3
%15 = load i8*, i8** %4
%16 = call i32 @perror_prefix(i8* %15)
br label %98
17: ; preds = %3
%18 = load i8*, i8** %5
%19 = icmp ne i8* %18, null
br i1 %19, label %20, label %26
20: ; preds = %17
%21 = load i8*, i8** %5
%22 = load i8*, i8** %4
%23 = call i32 @basename(i8* %22)
%24 = call i32 @fnmatch(i8* %21, i32 %23, i32 0)
%25 = icmp ne i32 %24, 0
br i1 %25, label %35, label %26
26: ; preds = %20, %17
%27 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%28 = load i32, i32* %27
%29 = load i8, i8* %6
%30 = call i64 @compare_type(i32 %28, i8 signext %29)
%31 = icmp ne i64 %30, 0
br i1 %31, label %32, label %35
32: ; preds = %26
%33 = load i8*, i8** %4
%34 = call i32 @puts(i8* %33)
br label %35
35: ; preds = %32, %26, %20
%36 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%37 = load i32, i32* %36
%38 = call i32 @S_ISDIR(i32 %37)
%39 = icmp ne i32 %38, 0
br i1 %39, label %41, label %40
40: ; preds = %35
br label %98
41: ; preds = %35
%42 = load i8*, i8** %4
%43 = call i32* @opendir(i8* %42)
store i32* %43, i32** %7
%44 = icmp eq i32* %43, null
br i1 %44, label %45, label %48
45: ; preds = %41
%46 = load i8*, i8** %4
%47 = call i32 @perror_prefix(i8* %46)
br label %98
48: ; preds = %41
br label %49
49: ; preds = %48, %94
store i64 0, i64* @errno
%50 = load i32*, i32** %7
%51 = call %struct.dirent* @readdir(i32* %50)
store %struct.dirent* %51, %struct.dirent** %8
%52 = load %struct.dirent*, %struct.dirent** %8
%53 = icmp eq %struct.dirent* %52, null
br i1 %53, label %54, label %61
54: ; preds = %49
%55 = load i64, i64* @errno
%56 = icmp ne i64 %55, 0
br i1 %56, label %57, label %60
57: ; preds = %54
%58 = load i8*, i8** %4
%59 = call i32 @perror_prefix(i8* %58)
br label %60
60: ; preds = %57, %54
br label %95
61: ; preds = %49
%62 = load %struct.dirent*, %struct.dirent** %8
%63 = getelementptr inbounds %struct.dirent, %struct.dirent* %62, i32 0, i32 0
%64 = load i8*, i8** %63
%65 = call i64 @strcmp(i8* %64, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%66 = icmp ne i64 %65, 0
br i1 %66, label %67, label %94
67: ; preds = %61
%68 = load %struct.dirent*, %struct.dirent** %8
%69 = getelementptr inbounds %struct.dirent, %struct.dirent* %68, i32 0, i32 0
%70 = load i8*, i8** %69
%71 = call i64 @strcmp(i8* %70, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%72 = icmp ne i64 %71, 0
br i1 %72, label %73, label %94
73: ; preds = %67
%74 = load i8*, i8** %4
%75 = call i64 @strlen(i8* %74)
%76 = load %struct.dirent*, %struct.dirent** %8
%77 = getelementptr inbounds %struct.dirent, %struct.dirent* %76, i32 0, i32 0
%78 = load i8*, i8** %77
%79 = call i64 @strlen(i8* %78)
%80 = add nsw i64 %75, %79
%81 = add nsw i64 %80, 2
%82 = call i8* @malloc(i64 %81)
store i8* %82, i8** %10
%83 = load i8*, i8** %10
%84 = load i8*, i8** %4
%85 = load %struct.dirent*, %struct.dirent** %8
%86 = getelementptr inbounds %struct.dirent, %struct.dirent* %85, i32 0, i32 0
%87 = load i8*, i8** %86
%88 = call i32 @sprintf(i8* %83, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %84, i8* %87)
%89 = load i8*, i8** %10
%90 = load i8*, i8** %5
%91 = load i8, i8* %6
%92 = load i8*, i8** %10
%93 = call i32 @free(i8* %92)
br label %94
94: ; preds = %73, %67, %61
br label %49
95: ; preds = %60
%96 = load i32*, i32** %7
%97 = call i32 @closedir(i32* %96)
br label %98
98: ; preds = %95, %45, %40, %14
ret void
}

LLVM IR (-O2):
%struct.stat = type { i32 }
%struct.dirent = type { i8* }
@errno = global i64 0
@.str = [2 x i8] c".\00"
@.str.1 = [3 x i8] c"..\00"
@.str.2 = [6 x i8] c"%s/%s\00"
define void @searchdir(i8* %0, i8* %1, i8 signext %2) {
%4 = alloca %struct.stat
%5 = bitcast %struct.stat* %4 to i8*
%6 = call i32 @lstat(i8* %0, %struct.stat* %4)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %10
8: ; preds = %3
%9 = tail call i32 @perror_prefix(i8* %0)
br label %67
10: ; preds = %3
%11 = icmp eq i8* %1, null
br i1 %11, label %19, label %12
12: ; preds = %10
%13 = tail call i32 @basename(i8* %0)
%14 = tail call i32 @fnmatch(i8* %1, i32 %13, i32 0)
%15 = icmp eq i32 %14, 0
br i1 %15, label %19, label %16
16: ; preds = %12
%17 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%18 = load i32, i32* %17
br label %26
19: ; preds = %12, %10
%20 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%21 = load i32, i32* %20
%22 = tail call i64 @compare_type(i32 %21, i8 signext %2)
%23 = icmp eq i64 %22, 0
br i1 %23, label %26, label %24
24: ; preds = %19
%25 = tail call i32 @puts(i8* dereferenceable(1) %0)
br label %26
26: ; preds = %16, %24, %19
%27 = phi i32 [ %18, %16 ], [ %21, %24 ], [ %21, %19 ]
%28 = tail call i32 @S_ISDIR(i32 %27)
%29 = icmp eq i32 %28, 0
br i1 %29, label %67, label %30
30: ; preds = %26
%31 = tail call i32* @opendir(i8* %0)
%32 = icmp eq i32* %31, null
br i1 %32, label %36, label %33
33: ; preds = %30
store i64 0, i64* @errno
%34 = tail call %struct.dirent* @readdir(i32* %31)
%35 = icmp eq %struct.dirent* %34, null
br i1 %35, label %38, label %43
36: ; preds = %30
%37 = tail call i32 @perror_prefix(i8* %0)
br label %67
38: ; preds = %62, %33
%39 = load i64, i64* @errno
%40 = icmp eq i64 %39, 0
br i1 %40, label %65, label %41
41: ; preds = %38
%42 = tail call i32 @perror_prefix(i8* %0)
br label %65
43: ; preds = %33, %62
%44 = phi %struct.dirent* [ %63, %62 ], [ %34, %33 ]
%45 = getelementptr inbounds %struct.dirent, %struct.dirent* %44, i64 0, i32 0
%46 = load i8*, i8** %45
%47 = tail call i64 @strcmp(i8* %46, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%48 = icmp eq i64 %47, 0
br i1 %48, label %62, label %49
49: ; preds = %43
%50 = load i8*, i8** %45
%51 = tail call i64 @strcmp(i8* %50, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%52 = icmp eq i64 %51, 0
br i1 %52, label %62, label %53
53: ; preds = %49
%54 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%55 = load i8*, i8** %45
%56 = tail call i64 @strlen(i8* dereferenceable(1) %55)
%57 = add i64 %54, 2
%58 = add i64 %57, %56
%59 = tail call i8* @malloc(i64 %58)
%60 = tail call i32 @sprintf(i8* dereferenceable(1) %59, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %0, i8* %55)
tail call void @searchdir(i8* %59, i8* %1, i8 signext %2)
%61 = tail call i32 @free(i8* %59)
br label %62
62: ; preds = %53, %49, %43
store i64 0, i64* @errno
%63 = tail call %struct.dirent* @readdir(i32* %31)
%64 = icmp eq %struct.dirent* %63, null
br i1 %64, label %38, label %43
65: ; preds = %38, %41
%66 = tail call i32 @closedir(i32* %31)
br label %67
67: ; preds = %26, %65, %36, %8
ret void
}