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

/* Variables and functions */
char *calloc(int, int);
scalar_t__ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ file_isdir(char *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ *opendir(char const *);
int /*<<< orphan*/ printf(char *, char *, ...);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcmp(char *, char *);
char *strerror(int /*<<< orphan*/);
int strlen(char const *);
int /*<<< orphan*/ strncpy(char *, char const *, int);
int module_locate(const char *d_name) {
  DIR *handle;
  struct dirent *handle_next;
  char *path;
  handle = opendir(d_name);
  if (!handle) {
    printf("modules opendir: %s\n", strerror(errno));
    return (-1);
  }
  while ((handle_next = readdir(handle)) != NULL) {
    if (!(!strcmp(handle_next->d_name, "..") ||
          !strcmp(handle_next->d_name, "."))) {
      path = calloc(
          sizeof(char) * (strlen(handle_next->d_name) + strlen(d_name) + 2), 1);
      strncpy(path, d_name, strlen(d_name));
      if (path[strlen(path) - 1] != '/')
        strcat(path, "/");
      strcat(path, handle_next->d_name);
      printf("-> %s\n", path);
      if (!file_isdir(path)) {
        module_locate(path);
      } else {
        if ((strlen(handle_next->d_name) > 3) &&
            (path[strlen(path) - 2] == 's') &&
            (path[strlen(path) - 1] == 'o')) {
          printf(" _%s_%s_\n", handle_next->d_name, path);
        }
      }
      free(path);
    }
  }
  if (closedir(handle)) {
    printf("closedir %s\n", strerror(errno));
    return (-1);
  }
  return (0);
}


x86 assembly (clang, -O0):
<module_locate>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <opendir@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    121f <module_locate+0x4f>
mov    0x2de5(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
mov    %rax,%rsi
lea    0xdf4(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1429 <module_locate+0x259>
jmp    1224 <module_locate+0x54>
mov    -0x18(%rbp),%rdi
call   10b0 <readdir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     13e4 <module_locate+0x214>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
lea    0xdcc(%rip),%rsi
call   10a0 <strcmp@plt>
cmp    $0x0,%eax
je     13df <module_locate+0x20f>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
lea    0xdb1(%rip),%rsi
call   10a0 <strcmp@plt>
cmp    $0x0,%eax
je     13df <module_locate+0x20f>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
call   1060 <strlen@plt>
mov    %eax,-0x44(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,%ecx
mov    -0x44(%rbp),%eax
add    %ecx,%eax
add    $0x2,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
mov    $0x1,%esi
call   1090 <calloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0x40(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    %eax,%edx
call   1040 <strncpy@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,%ecx
mov    -0x30(%rbp),%rax
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x2f,%eax
je     130d <module_locate+0x13d>
mov    -0x28(%rbp),%rdi
lea    0xd10(%rip),%rsi
call   10d0 <strcat@plt>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rax
mov    (%rax),%rsi
call   10d0 <strcat@plt>
mov    -0x28(%rbp),%rsi
lea    0xcf2(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x28(%rbp),%rdi
call   10c0 <file_isdir@plt>
cmp    $0x0,%eax
jne    134f <module_locate+0x17f>
mov    -0x28(%rbp),%rdi
call   10f0 <module_locate@plt>
jmp    13d6 <module_locate+0x206>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
call   1060 <strlen@plt>
cmp    $0x3,%eax
jle    13d1 <module_locate+0x201>
mov    -0x28(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x28(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,%ecx
mov    -0x50(%rbp),%rax
sub    $0x2,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x73,%eax
jne    13d1 <module_locate+0x201>
mov    -0x28(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x28(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,%ecx
mov    -0x58(%rbp),%rax
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x6f,%eax
jne    13d1 <module_locate+0x201>
mov    -0x20(%rbp),%rax
mov    (%rax),%rsi
mov    -0x28(%rbp),%rdx
lea    0xc57(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
jmp    13d6 <module_locate+0x206>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
jmp    1224 <module_locate+0x54>
mov    -0x18(%rbp),%rdi
call   1080 <closedir@plt>
cmp    $0x0,%rax
je     1422 <module_locate+0x252>
mov    0x2be2(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
mov    %rax,%rsi
lea    0xc1c(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1429 <module_locate+0x259>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<module_locate>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r12
call   1050 <opendir@plt>
test   %rax,%rax
je     134c <module_locate+0x17c>
mov    %rax,%r14
mov    %rax,%rdi
call   10b0 <readdir@plt>
test   %rax,%rax
je     1336 <module_locate+0x166>
mov    %rax,%rbp
lea    0xe0b(%rip),%r15
jmp    1230 <module_locate+0x60>
mov    %r13,%rdi
call   10f0 <module_locate@plt>
mov    %r13,%rdi
call   1030 <free@plt>
mov    %r14,%rdi
call   10b0 <readdir@plt>
mov    %rax,%rbp
test   %rax,%rax
je     1336 <module_locate+0x166>
mov    0x0(%rbp),%rbx
mov    %rbx,%rdi
mov    %r15,%rsi
call   10a0 <strcmp@plt>
test   %eax,%eax
je     121c <module_locate+0x4c>
mov    %rbx,%rdi
lea    0xdc9(%rip),%rsi
call   10a0 <strcmp@plt>
test   %eax,%eax
je     121c <module_locate+0x4c>
mov    %rbx,%rdi
call   1060 <strlen@plt>
mov    %eax,%ebx
mov    %r12,%rdi
call   1060 <strlen@plt>
lea    (%rbx,%rax,1),%edi
add    $0x2,%edi
mov    $0x1,%esi
call   1090 <calloc@plt>
mov    %rax,%r13
mov    %r12,%rdi
call   1060 <strlen@plt>
mov    %r13,%rdi
mov    %r12,%rsi
mov    %eax,%edx
call   1040 <strncpy@plt>
mov    %r13,%rdi
call   1060 <strlen@plt>
cltq
cmpb   $0x2f,-0x1(%rax,%r13,1)
je     12b1 <module_locate+0xe1>
mov    %r13,%rdi
lea    0xd6c(%rip),%rsi
call   10d0 <strcat@plt>
mov    0x0(%rbp),%rsi
mov    %r13,%rdi
call   10d0 <strcat@plt>
lea    0xd56(%rip),%rdi
mov    %r13,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
mov    %r13,%rdi
call   10c0 <file_isdir@plt>
test   %eax,%eax
je     120c <module_locate+0x3c>
mov    0x0(%rbp),%rdi
call   1060 <strlen@plt>
cmp    $0x4,%eax
jl     1214 <module_locate+0x44>
mov    %r13,%rdi
call   1060 <strlen@plt>
cltq
cmpb   $0x73,-0x2(%rax,%r13,1)
jne    1214 <module_locate+0x44>
mov    %r13,%rdi
call   1060 <strlen@plt>
cltq
cmpb   $0x6f,-0x1(%rax,%r13,1)
jne    1214 <module_locate+0x44>
mov    0x0(%rbp),%rsi
lea    0xcfa(%rip),%rdi
mov    %r13,%rdx
xor    %eax,%eax
call   1070 <printf@plt>
jmp    1214 <module_locate+0x44>
mov    %r14,%rdi
call   1080 <closedir@plt>
test   %rax,%rax
je     1375 <module_locate+0x1a5>
lea    0xce1(%rip),%rbx
jmp    1353 <module_locate+0x183>
lea    0xcad(%rip),%rbx
mov    0x2c86(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
mov    %rbx,%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
mov    $0xffffffff,%eax
jmp    1377 <module_locate+0x1a7>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i8* }
@.str = [21 x i8] c"modules opendir: %s\0A\00"
@errno = global i32 0
@.str.1 = [3 x i8] c"..\00"
@.str.2 = [2 x i8] c".\00"
@.str.3 = [2 x i8] c"/\00"
@.str.4 = [7 x i8] c"-> %s\0A\00"
@.str.5 = [10 x i8] c" _%s_%s_\0A\00"
@.str.6 = [13 x i8] c"closedir %s\0A\00"
define i32 @module_locate(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca %struct.dirent*
%6 = alloca i8*
store i8* %0, i8** %3
%7 = load i8*, i8** %3
%8 = call i32* @opendir(i8* %7)
store i32* %8, i32** %4
%9 = load i32*, i32** %4
%10 = icmp ne i32* %9, null
br i1 %10, label %15, label %11
11: ; preds = %1
%12 = load i32, i32* @errno
%13 = call i8* @strerror(i32 %12)
%14 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %13)
store i32 -1, i32* %2
br label %122
15: ; preds = %1
br label %16
16: ; preds = %112, %15
%17 = load i32*, i32** %4
%18 = call %struct.dirent* @readdir(i32* %17)
store %struct.dirent* %18, %struct.dirent** %5
%19 = icmp ne %struct.dirent* %18, null
br i1 %19, label %20, label %113
20: ; preds = %16
%21 = load %struct.dirent*, %struct.dirent** %5
%22 = getelementptr inbounds %struct.dirent, %struct.dirent* %21, i32 0, i32 0
%23 = load i8*, i8** %22
%24 = call i32 @strcmp(i8* %23, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%25 = icmp ne i32 %24, 0
br i1 %25, label %26, label %112
26: ; preds = %20
%27 = load %struct.dirent*, %struct.dirent** %5
%28 = getelementptr inbounds %struct.dirent, %struct.dirent* %27, i32 0, i32 0
%29 = load i8*, i8** %28
%30 = call i32 @strcmp(i8* %29, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%31 = icmp ne i32 %30, 0
br i1 %31, label %32, label %112
32: ; preds = %26
%33 = load %struct.dirent*, %struct.dirent** %5
%34 = getelementptr inbounds %struct.dirent, %struct.dirent* %33, i32 0, i32 0
%35 = load i8*, i8** %34
%36 = call i32 @strlen(i8* %35)
%37 = load i8*, i8** %3
%38 = call i32 @strlen(i8* %37)
%39 = add nsw i32 %36, %38
%40 = add nsw i32 %39, 2
%41 = sext i32 %40 to i64
%42 = mul i64 1, %41
%43 = trunc i64 %42 to i32
%44 = call i8* @calloc(i32 %43, i32 1)
store i8* %44, i8** %6
%45 = load i8*, i8** %6
%46 = load i8*, i8** %3
%47 = load i8*, i8** %3
%48 = call i32 @strlen(i8* %47)
%49 = call i32 @strncpy(i8* %45, i8* %46, i32 %48)
%50 = load i8*, i8** %6
%51 = load i8*, i8** %6
%52 = call i32 @strlen(i8* %51)
%53 = sub nsw i32 %52, 1
%54 = sext i32 %53 to i64
%55 = getelementptr inbounds i8, i8* %50, i64 %54
%56 = load i8, i8* %55
%57 = sext i8 %56 to i32
%58 = icmp ne i32 %57, 47
br i1 %58, label %59, label %62
59: ; preds = %32
%60 = load i8*, i8** %6
%61 = call i32 @strcat(i8* %60, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
br label %62
62: ; preds = %59, %32
%63 = load i8*, i8** %6
%64 = load %struct.dirent*, %struct.dirent** %5
%65 = getelementptr inbounds %struct.dirent, %struct.dirent* %64, i32 0, i32 0
%66 = load i8*, i8** %65
%67 = call i32 @strcat(i8* %63, i8* %66)
%68 = load i8*, i8** %6
%69 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0), i8* %68)
%70 = load i8*, i8** %6
%71 = call i32 @file_isdir(i8* %70)
%72 = icmp ne i32 %71, 0
br i1 %72, label %76, label %73
73: ; preds = %62
%74 = load i8*, i8** %6
%75 = call i32 @module_locate(i8* %74)
br label %109
76: ; preds = %62
%77 = load %struct.dirent*, %struct.dirent** %5
%78 = getelementptr inbounds %struct.dirent, %struct.dirent* %77, i32 0, i32 0
%79 = load i8*, i8** %78
%80 = call i32 @strlen(i8* %79)
%81 = icmp sgt i32 %80, 3
br i1 %81, label %82, label %108
82: ; preds = %76
%83 = load i8*, i8** %6
%84 = load i8*, i8** %6
%85 = call i32 @strlen(i8* %84)
%86 = sub nsw i32 %85, 2
%87 = sext i32 %86 to i64
%88 = getelementptr inbounds i8, i8* %83, i64 %87
%89 = load i8, i8* %88
%90 = sext i8 %89 to i32
%91 = icmp eq i32 %90, 115
br i1 %91, label %92, label %108
92: ; preds = %82
%93 = load i8*, i8** %6
%94 = load i8*, i8** %6
%95 = call i32 @strlen(i8* %94)
%96 = sub nsw i32 %95, 1
%97 = sext i32 %96 to i64
%98 = getelementptr inbounds i8, i8* %93, i64 %97
%99 = load i8, i8* %98
%100 = sext i8 %99 to i32
%101 = icmp eq i32 %100, 111
br i1 %101, label %102, label %108
102: ; preds = %92
%103 = load %struct.dirent*, %struct.dirent** %5
%104 = getelementptr inbounds %struct.dirent, %struct.dirent* %103, i32 0, i32 0
%105 = load i8*, i8** %104
%106 = load i8*, i8** %6
%107 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0), i8* %105, i8* %106)
br label %108
108: ; preds = %102, %92, %82, %76
br label %109
109: ; preds = %108, %73
%110 = load i8*, i8** %6
%111 = call i32 @free(i8* %110)
br label %112
112: ; preds = %109, %26, %20
br label %16
113: ; preds = %16
%114 = load i32*, i32** %4
%115 = call i64 @closedir(i32* %114)
%116 = icmp ne i64 %115, 0
br i1 %116, label %117, label %121
117: ; preds = %113
%118 = load i32, i32* @errno
%119 = call i8* @strerror(i32 %118)
%120 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.6, i64 0, i64 0), i8* %119)
store i32 -1, i32* %2
br label %122
121: ; preds = %113
store i32 0, i32* %2
br label %122
122: ; preds = %121, %117, %11
%123 = load i32, i32* %2
ret i32 %123
}

LLVM IR (-O2):
%struct.dirent = type { i8* }
@.str = [21 x i8] c"modules opendir: %s\0A\00"
@errno = global i32 0
@.str.1 = [3 x i8] c"..\00"
@.str.2 = [2 x i8] c".\00"
@.str.3 = [2 x i8] c"/\00"
@.str.4 = [7 x i8] c"-> %s\0A\00"
@.str.5 = [10 x i8] c" _%s_%s_\0A\00"
@.str.6 = [13 x i8] c"closedir %s\0A\00"
define i32 @module_locate(i8* %0) {
%2 = tail call i32* @opendir(i8* %0)
%3 = icmp eq i32* %2, null
br i1 %3, label %69, label %4
4: ; preds = %1
%5 = tail call %struct.dirent* @readdir(i32* %2)
%6 = icmp eq %struct.dirent* %5, null
br i1 %6, label %66, label %7
7: ; preds = %4, %63
%8 = phi %struct.dirent* [ %64, %63 ], [ %5, %4 ]
%9 = getelementptr inbounds %struct.dirent, %struct.dirent* %8, i64 0, i32 0
%10 = load i8*, i8** %9
%11 = tail call i32 @strcmp(i8* dereferenceable(1) %10, i8* dereferenceable(3) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%12 = icmp eq i32 %11, 0
br i1 %12, label %63, label %13
13: ; preds = %7
%14 = tail call i32 @strcmp(i8* dereferenceable(1) %10, i8* dereferenceable(2) getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%15 = icmp eq i32 %14, 0
br i1 %15, label %63, label %16
16: ; preds = %13
%17 = tail call i32 @strlen(i8* %10)
%18 = tail call i32 @strlen(i8* %0)
%19 = add i32 %17, 2
%20 = add i32 %19, %18
%21 = tail call i8* @calloc(i32 %20, i32 1)
%22 = tail call i32 @strlen(i8* %0)
%23 = tail call i32 @strncpy(i8* %21, i8* %0, i32 %22)
%24 = tail call i32 @strlen(i8* %21)
%25 = add nsw i32 %24, -1
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds i8, i8* %21, i64 %26
%28 = load i8, i8* %27
%29 = icmp eq i8 %28, 47
br i1 %29, label %32, label %30
30: ; preds = %16
%31 = tail call i32 @strcat(i8* %21, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
br label %32
32: ; preds = %30, %16
%33 = load i8*, i8** %9
%34 = tail call i32 @strcat(i8* %21, i8* %33)
%35 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0), i8* %21)
%36 = tail call i32 @file_isdir(i8* %21)
%37 = icmp eq i32 %36, 0
br i1 %37, label %38, label %40
38: ; preds = %32
%39 = tail call i32 @module_locate(i8* %21)
br label %61
40: ; preds = %32
%41 = load i8*, i8** %9
%42 = tail call i32 @strlen(i8* %41)
%43 = icmp sgt i32 %42, 3
br i1 %43, label %44, label %61
44: ; preds = %40
%45 = tail call i32 @strlen(i8* %21)
%46 = add nsw i32 %45, -2
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds i8, i8* %21, i64 %47
%49 = load i8, i8* %48
%50 = icmp eq i8 %49, 115
br i1 %50, label %51, label %61
51: ; preds = %44
%52 = tail call i32 @strlen(i8* %21)
%53 = add nsw i32 %52, -1
%54 = sext i32 %53 to i64
%55 = getelementptr inbounds i8, i8* %21, i64 %54
%56 = load i8, i8* %55
%57 = icmp eq i8 %56, 111
br i1 %57, label %58, label %61
58: ; preds = %51
%59 = load i8*, i8** %9
%60 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0), i8* %59, i8* %21)
br label %61
61: ; preds = %40, %44, %51, %58, %38
%62 = tail call i32 @free(i8* %21)
br label %63
63: ; preds = %61, %13, %7
%64 = tail call %struct.dirent* @readdir(i32* %2)
%65 = icmp eq %struct.dirent* %64, null
br i1 %65, label %66, label %7
66: ; preds = %63, %4
%67 = tail call i64 @closedir(i32* %2)
%68 = icmp eq i64 %67, 0
br i1 %68, label %74, label %69
69: ; preds = %66, %1
%70 = phi i8* [ getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), %1 ], [ getelementptr inbounds ([13 x i8], [13 x i8]* @.str.6, i64 0, i64 0), %66 ]
%71 = load i32, i32* @errno
%72 = tail call i8* @strerror(i32 %71)
%73 = tail call i32 (i8*, i8*, ...) @printf(i8* %70, i8* %72)
br label %74
74: ; preds = %69, %66
%75 = phi i32 [ 0, %66 ], [ -1, %69 ]
ret i32 %75
}