#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*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ printf(char *, char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ snprintf(char *, int, char *, char *);
scalar_t__ stat(char *, struct stat *);
scalar_t__ strstr(char *, char *);
int get_mtd_device(char *path, char *device, int slen) {
  struct dirent *entry;
  struct stat info;
  DIR *dir;
  int found = -1;
  if (stat(path, &info)) {
    printf("Couldn't find %s\n", path);
    return -1;
  }
  if (!S_ISDIR(info.st_mode)) {
    printf("%s isn't a directory\n", path);
    return -1;
  }
  dir = opendir(path);
  if (dir) {
    while ((entry = readdir(dir)) != NULL) {
      if (strstr(entry->d_name, "mtd") && !strstr(entry->d_name, "ro")) {
        snprintf(device, slen, "/dev/%s", entry->d_name);
        found = 0;
      }
    }
    closedir(dir);
  }
  return found;
}


x86 assembly (clang, -O0):
<get_mtd_device>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
movl   $0xffffffff,-0x3c(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1080 <stat@plt>
cmp    $0x0,%rax
je     11cd <get_mtd_device+0x4d>
mov    -0x10(%rbp),%rsi
lea    0xe44(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    129d <get_mtd_device+0x11d>
mov    -0x30(%rbp),%edi
call   1050 <S_ISDIR@plt>
cmp    $0x0,%eax
jne    11fa <get_mtd_device+0x7a>
mov    -0x10(%rbp),%rsi
lea    0xe29(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    129d <get_mtd_device+0x11d>
mov    -0x10(%rbp),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
je     1297 <get_mtd_device+0x117>
jmp    1217 <get_mtd_device+0x97>
mov    -0x38(%rbp),%rdi
call   1090 <readdir@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     128e <get_mtd_device+0x10e>
mov    -0x28(%rbp),%rax
mov    (%rax),%rdi
lea    0xdec(%rip),%rsi
call   10a0 <strstr@plt>
cmp    $0x0,%rax
je     1289 <get_mtd_device+0x109>
mov    -0x28(%rbp),%rax
mov    (%rax),%rdi
lea    0xdd3(%rip),%rsi
call   10a0 <strstr@plt>
cmp    $0x0,%rax
jne    1289 <get_mtd_device+0x109>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%esi
mov    -0x28(%rbp),%rax
mov    (%rax),%rcx
lea    0xdb2(%rip),%rdx
call   1060 <snprintf@plt>
movl   $0x0,-0x3c(%rbp)
jmp    1217 <get_mtd_device+0x97>
mov    -0x38(%rbp),%rdi
call   1070 <closedir@plt>
mov    -0x3c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_mtd_device>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%r14d
mov    %rsi,%r15
mov    %rdi,%rbx
mov    %rsp,%rsi
call   1080 <stat@plt>
test   %rax,%rax
je     11c6 <get_mtd_device+0x46>
lea    0xe58(%rip),%rdi
mov    %rbx,%rsi
call   1040 <printf@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    (%rsp),%edi
call   1050 <S_ISDIR@plt>
test   %eax,%eax
je     1250 <get_mtd_device+0xd0>
mov    %rbx,%rdi
call   1030 <opendir@plt>
test   %rax,%rax
je     11b0 <get_mtd_device+0x30>
mov    %rax,%rbp
mov    %rax,%rdi
call   1090 <readdir@plt>
test   %rax,%rax
je     125c <get_mtd_device+0xdc>
mov    %rax,%r13
mov    $0xffffffff,%ebx
lea    0xe2a(%rip),%r12
jmp    1210 <get_mtd_device+0x90>
mov    %rbp,%rdi
call   1090 <readdir@plt>
mov    %rax,%r13
test   %rax,%rax
je     1261 <get_mtd_device+0xe1>
mov    0x0(%r13),%rdi
mov    %r12,%rsi
call   10a0 <strstr@plt>
test   %rax,%rax
je     1200 <get_mtd_device+0x80>
mov    0x0(%r13),%rdi
lea    0xe00(%rip),%rsi
call   10a0 <strstr@plt>
test   %rax,%rax
jne    1200 <get_mtd_device+0x80>
mov    0x0(%r13),%rcx
mov    %r15,%rdi
mov    %r14d,%esi
lea    0xde8(%rip),%rdx
call   1060 <snprintf@plt>
xor    %ebx,%ebx
jmp    1200 <get_mtd_device+0x80>
lea    0xdbb(%rip),%rdi
jmp    11a8 <get_mtd_device+0x28>
mov    $0xffffffff,%ebx
mov    %rbp,%rdi
call   1070 <closedir@plt>
jmp    11b5 <get_mtd_device+0x35>

LLVM IR (-O0):
%struct.dirent = type { i8* }
%struct.stat = type { i32 }
@.str = [18 x i8] c"Couldn't find %s\0A\00"
@.str.1 = [22 x i8] c"%s isn't a directory\0A\00"
@.str.2 = [4 x i8] c"mtd\00"
@.str.3 = [3 x i8] c"ro\00"
@.str.4 = [8 x i8] c"/dev/%s\00"
define i32 @get_mtd_device(i8* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca %struct.dirent*
%9 = alloca %struct.stat
%10 = alloca i32*
%11 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i32 -1, i32* %11
%12 = load i8*, i8** %5
%13 = call i64 @stat(i8* %12, %struct.stat* %9)
%14 = icmp ne i64 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %3
%16 = load i8*, i8** %5
%17 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %16)
store i32 -1, i32* %4
br label %61
18: ; preds = %3
%19 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%20 = load i32, i32* %19
%21 = call i32 @S_ISDIR(i32 %20)
%22 = icmp ne i32 %21, 0
br i1 %22, label %26, label %23
23: ; preds = %18
%24 = load i8*, i8** %5
%25 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i8* %24)
store i32 -1, i32* %4
br label %61
26: ; preds = %18
%27 = load i8*, i8** %5
%28 = call i32* @opendir(i8* %27)
store i32* %28, i32** %10
%29 = load i32*, i32** %10
%30 = icmp ne i32* %29, null
br i1 %30, label %31, label %59
31: ; preds = %26
br label %32
32: ; preds = %55, %31
%33 = load i32*, i32** %10
%34 = call %struct.dirent* @readdir(i32* %33)
store %struct.dirent* %34, %struct.dirent** %8
%35 = icmp ne %struct.dirent* %34, null
br i1 %35, label %36, label %56
36: ; preds = %32
%37 = load %struct.dirent*, %struct.dirent** %8
%38 = getelementptr inbounds %struct.dirent, %struct.dirent* %37, i32 0, i32 0
%39 = load i8*, i8** %38
%40 = call i64 @strstr(i8* %39, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
%41 = icmp ne i64 %40, 0
br i1 %41, label %42, label %55
42: ; preds = %36
%43 = load %struct.dirent*, %struct.dirent** %8
%44 = getelementptr inbounds %struct.dirent, %struct.dirent* %43, i32 0, i32 0
%45 = load i8*, i8** %44
%46 = call i64 @strstr(i8* %45, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%47 = icmp ne i64 %46, 0
br i1 %47, label %55, label %48
48: ; preds = %42
%49 = load i8*, i8** %6
%50 = load i32, i32* %7
%51 = load %struct.dirent*, %struct.dirent** %8
%52 = getelementptr inbounds %struct.dirent, %struct.dirent* %51, i32 0, i32 0
%53 = load i8*, i8** %52
%54 = call i32 @snprintf(i8* %49, i32 %50, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i8* %53)
store i32 0, i32* %11
br label %55
55: ; preds = %48, %42, %36
br label %32
56: ; preds = %32
%57 = load i32*, i32** %10
%58 = call i32 @closedir(i32* %57)
br label %59
59: ; preds = %56, %26
%60 = load i32, i32* %11
store i32 %60, i32* %4
br label %61
61: ; preds = %59, %23, %15
%62 = load i32, i32* %4
ret i32 %62
}

LLVM IR (-O2):
%struct.stat = type { i32 }
%struct.dirent = type { i8* }
@.str = [18 x i8] c"Couldn't find %s\0A\00"
@.str.1 = [22 x i8] c"%s isn't a directory\0A\00"
@.str.2 = [4 x i8] c"mtd\00"
@.str.3 = [3 x i8] c"ro\00"
@.str.4 = [8 x i8] c"/dev/%s\00"
define i32 @get_mtd_device(i8* %0, i8* %1, i32 %2) {
%4 = alloca %struct.stat
%5 = bitcast %struct.stat* %4 to i8*
%6 = call i64 @stat(i8* %0, %struct.stat* %4)
%7 = icmp eq i64 %6, 0
br i1 %7, label %10, label %8
8: ; preds = %3
%9 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %44
10: ; preds = %3
%11 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%12 = load i32, i32* %11
%13 = call i32 @S_ISDIR(i32 %12)
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %10
%16 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %44
17: ; preds = %10
%18 = call i32* @opendir(i8* %0)
%19 = icmp eq i32* %18, null
br i1 %19, label %44, label %20
20: ; preds = %17
%21 = call %struct.dirent* @readdir(i32* %18)
%22 = icmp eq %struct.dirent* %21, null
br i1 %22, label %41, label %23
23: ; preds = %20, %37
%24 = phi %struct.dirent* [ %39, %37 ], [ %21, %20 ]
%25 = phi i32 [ %38, %37 ], [ -1, %20 ]
%26 = getelementptr inbounds %struct.dirent, %struct.dirent* %24, i64 0, i32 0
%27 = load i8*, i8** %26
%28 = call i64 @strstr(i8* %27, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
%29 = icmp eq i64 %28, 0
br i1 %29, label %37, label %30
30: ; preds = %23
%31 = load i8*, i8** %26
%32 = call i64 @strstr(i8* %31, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%33 = icmp eq i64 %32, 0
br i1 %33, label %34, label %37
34: ; preds = %30
%35 = load i8*, i8** %26
%36 = call i32 @snprintf(i8* %1, i32 %2, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i8* %35)
br label %37
37: ; preds = %34, %30, %23
%38 = phi i32 [ %25, %30 ], [ 0, %34 ], [ %25, %23 ]
%39 = call %struct.dirent* @readdir(i32* %18)
%40 = icmp eq %struct.dirent* %39, null
br i1 %40, label %41, label %23
41: ; preds = %37, %20
%42 = phi i32 [ -1, %20 ], [ %38, %37 ]
%43 = call i32 @closedir(i32* %18)
br label %44
44: ; preds = %17, %41, %15, %8
%45 = phi i32 [ -1, %8 ], [ -1, %15 ], [ %42, %41 ], [ -1, %17 ]
ret i32 %45
}