#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 */
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
scalar_t__ get_mtd_device(char *, char *, int);
int /*<<< orphan*/ *opendir(char const *);
int /*<<< orphan*/ printf(char *, ...);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ show_flash_info(char *);
int /*<<< orphan*/ sprintf(char *, char *, char const *, char *);
scalar_t__ strstr(char *, char *);
int scan_for_mtd_devices(void) {
  const char *cobalt_sysfs = "/sys/bus/pci/drivers/cobalt/";
  struct dirent *cobalt_entry;
  DIR *cobalt_dir;
  cobalt_dir = opendir(cobalt_sysfs);
  if (cobalt_dir == NULL)
    return 0;
  while ((cobalt_entry = readdir(cobalt_dir)) != NULL) {
    struct dirent *video_entry;
    DIR *video_dir;
    char path[256];
    char mtd_device[20];
    if (!strstr(cobalt_entry->d_name, ":"))
      continue;
    printf("Found Cobalt card with PCI address: %s\n", cobalt_entry->d_name);
    printf("Video4Linux devices: ");
    sprintf(path, "%s%s/video4linux/", cobalt_sysfs, cobalt_entry->d_name);
    video_dir = opendir(path);
    if (video_dir) {
      while ((video_entry = readdir(video_dir)) != NULL) {
        if (strstr(video_entry->d_name, "video"))
          printf("%s ", video_entry->d_name);
      }
      closedir(video_dir);
    }
    printf("\n");
    sprintf(path, "%s%s/mtd/", cobalt_sysfs, cobalt_entry->d_name);
    if (get_mtd_device(path, mtd_device, 20) == 0) {
      show_flash_info(mtd_device);
    }
    printf("\n");
  }
  closedir(cobalt_dir);
  return 0;
}


x86 assembly (clang, -O0):
<scan_for_mtd_devices>:
push   %rbp
mov    %rsp,%rbp
sub    $0x150,%rsp
lea    0xe6e(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11ba <scan_for_mtd_devices+0x3a>
movl   $0x0,-0x4(%rbp)
jmp    132d <scan_for_mtd_devices+0x1ad>
jmp    11bf <scan_for_mtd_devices+0x3f>
mov    -0x20(%rbp),%rdi
call   1070 <readdir@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     131d <scan_for_mtd_devices+0x19d>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
lea    0xe39(%rip),%rsi
call   10a0 <strstr@plt>
cmp    $0x0,%rax
jne    11f8 <scan_for_mtd_devices+0x78>
jmp    11bf <scan_for_mtd_devices+0x3f>
mov    -0x18(%rbp),%rax
mov    (%rax),%rsi
lea    0xe19(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xe33(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0x130(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rax
mov    (%rax),%rcx
lea    0xe29(%rip),%rsi
call   1090 <sprintf@plt>
lea    -0x130(%rbp),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     12b0 <scan_for_mtd_devices+0x130>
jmp    1259 <scan_for_mtd_devices+0xd9>
mov    -0x30(%rbp),%rdi
call   1070 <readdir@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     12a7 <scan_for_mtd_devices+0x127>
mov    -0x28(%rbp),%rax
mov    (%rax),%rdi
lea    0xdf1(%rip),%rsi
call   10a0 <strstr@plt>
cmp    $0x0,%rax
je     12a2 <scan_for_mtd_devices+0x122>
mov    -0x28(%rbp),%rax
mov    (%rax),%rsi
lea    0xdda(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1259 <scan_for_mtd_devices+0xd9>
mov    -0x30(%rbp),%rdi
call   1060 <closedir@plt>
lea    0xd8e(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0x130(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rax
mov    (%rax),%rcx
lea    0xda2(%rip),%rsi
call   1090 <sprintf@plt>
lea    -0x130(%rbp),%rdi
lea    -0x150(%rbp),%rsi
mov    $0x14,%edx
call   1080 <get_mtd_device@plt>
cmp    $0x0,%rax
jne    130a <scan_for_mtd_devices+0x18a>
lea    -0x150(%rbp),%rdi
call   1030 <show_flash_info@plt>
lea    0xd34(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    11bf <scan_for_mtd_devices+0x3f>
mov    -0x20(%rbp),%rdi
call   1060 <closedir@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x150,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<scan_for_mtd_devices>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x128,%rsp
lea    0xe58(%rip),%rdi
call   1050 <opendir@plt>
test   %rax,%rax
je     1321 <scan_for_mtd_devices+0x191>
mov    %rax,%r15
mov    %rax,%rdi
call   1080 <readdir@plt>
test   %rax,%rax
je     1319 <scan_for_mtd_devices+0x189>
mov    %rax,%r14
lea    0xe2c(%rip),%rbp
lea    0xe94(%rip),%r12
lea    0xe93(%rip),%r13
jmp    120e <scan_for_mtd_devices+0x7e>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    %r15,%rdi
call   1080 <readdir@plt>
mov    %rax,%r14
test   %rax,%rax
je     1319 <scan_for_mtd_devices+0x189>
mov    (%r14),%rdi
lea    0xe05(%rip),%rsi
call   10b0 <strstr@plt>
test   %rax,%rax
je     11fa <scan_for_mtd_devices+0x6a>
mov    (%r14),%rsi
lea    0xdf3(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xe0d(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    (%r14),%rcx
mov    %r15,%rbx
lea    0x20(%rsp),%r15
mov    %r15,%rdi
lea    0xe07(%rip),%rsi
mov    %rbp,%rdx
call   10a0 <sprintf@plt>
mov    %r15,%rdi
mov    %rbx,%r15
call   1050 <opendir@plt>
test   %rax,%rax
je     12c8 <scan_for_mtd_devices+0x138>
mov    %rax,%rbp
mov    %rax,%rdi
call   1080 <readdir@plt>
test   %rax,%rax
je     12c0 <scan_for_mtd_devices+0x130>
mov    %rax,%rbx
jmp    12a0 <scan_for_mtd_devices+0x110>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %rbp,%rdi
call   1080 <readdir@plt>
mov    %rax,%rbx
test   %rax,%rax
je     12c0 <scan_for_mtd_devices+0x130>
mov    (%rbx),%rdi
mov    %r12,%rsi
call   10b0 <strstr@plt>
test   %rax,%rax
je     1290 <scan_for_mtd_devices+0x100>
mov    (%rbx),%rsi
mov    %r13,%rdi
xor    %eax,%eax
call   1060 <printf@plt>
jmp    1290 <scan_for_mtd_devices+0x100>
nop
mov    %rbp,%rdi
call   1070 <closedir@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    (%r14),%rcx
lea    0x20(%rsp),%rbx
mov    %rbx,%rdi
lea    0xd95(%rip),%rsi
lea    0xd15(%rip),%rbp
mov    %rbp,%rdx
call   10a0 <sprintf@plt>
mov    %rbx,%rdi
mov    %rsp,%rsi
mov    $0x14,%edx
call   1090 <get_mtd_device@plt>
test   %rax,%rax
jne    11f0 <scan_for_mtd_devices+0x60>
mov    %rsp,%rdi
call   1040 <show_flash_info@plt>
jmp    11f0 <scan_for_mtd_devices+0x60>
mov    %r15,%rdi
call   1070 <closedir@plt>
xor    %eax,%eax
add    $0x128,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i8* }
@.str = [29 x i8] c"/sys/bus/pci/drivers/cobalt/\00"
@.str.1 = [2 x i8] c":\00"
@.str.2 = [40 x i8] c"Found Cobalt card with PCI address: %s\0A\00"
@.str.3 = [22 x i8] c"Video4Linux devices: \00"
@.str.4 = [18 x i8] c"%s%s/video4linux/\00"
@.str.5 = [6 x i8] c"video\00"
@.str.6 = [4 x i8] c"%s \00"
@.str.7 = [2 x i8] c"\0A\00"
@.str.8 = [10 x i8] c"%s%s/mtd/\00"
define i32 @scan_for_mtd_devices() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca %struct.dirent*
%4 = alloca i32*
%5 = alloca %struct.dirent*
%6 = alloca i32*
%7 = alloca [256 x i8]
%8 = alloca [20 x i8]
store i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i8** %2
%9 = load i8*, i8** %2
%10 = call i32* @opendir(i8* %9)
store i32* %10, i32** %4
%11 = load i32*, i32** %4
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %14
13: ; preds = %0
store i32 0, i32* %1
br label %82
14: ; preds = %0
br label %15
15: ; preds = %77, %25, %14
%16 = load i32*, i32** %4
%17 = call %struct.dirent* @readdir(i32* %16)
store %struct.dirent* %17, %struct.dirent** %3
%18 = icmp ne %struct.dirent* %17, null
br i1 %18, label %19, label %79
19: ; preds = %15
%20 = load %struct.dirent*, %struct.dirent** %3
%21 = getelementptr inbounds %struct.dirent, %struct.dirent* %20, i32 0, i32 0
%22 = load i8*, i8** %21
%23 = call i64 @strstr(i8* %22, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%24 = icmp ne i64 %23, 0
br i1 %24, label %26, label %25
25: ; preds = %19
br label %15
26: ; preds = %19
%27 = load %struct.dirent*, %struct.dirent** %3
%28 = getelementptr inbounds %struct.dirent, %struct.dirent* %27, i32 0, i32 0
%29 = load i8*, i8** %28
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0), i8* %29)
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
%32 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%33 = load i8*, i8** %2
%34 = load %struct.dirent*, %struct.dirent** %3
%35 = getelementptr inbounds %struct.dirent, %struct.dirent* %34, i32 0, i32 0
%36 = load i8*, i8** %35
%37 = call i32 @sprintf(i8* %32, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0), i8* %33, i8* %36)
%38 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%39 = call i32* @opendir(i8* %38)
store i32* %39, i32** %6
%40 = load i32*, i32** %6
%41 = icmp ne i32* %40, null
br i1 %41, label %42, label %62
42: ; preds = %26
br label %43
43: ; preds = %58, %42
%44 = load i32*, i32** %6
%45 = call %struct.dirent* @readdir(i32* %44)
store %struct.dirent* %45, %struct.dirent** %5
%46 = icmp ne %struct.dirent* %45, null
br i1 %46, label %47, label %59
47: ; preds = %43
%48 = load %struct.dirent*, %struct.dirent** %5
%49 = getelementptr inbounds %struct.dirent, %struct.dirent* %48, i32 0, i32 0
%50 = load i8*, i8** %49
%51 = call i64 @strstr(i8* %50, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
%52 = icmp ne i64 %51, 0
br i1 %52, label %53, label %58
53: ; preds = %47
%54 = load %struct.dirent*, %struct.dirent** %5
%55 = getelementptr inbounds %struct.dirent, %struct.dirent* %54, i32 0, i32 0
%56 = load i8*, i8** %55
%57 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), i8* %56)
br label %58
58: ; preds = %53, %47
br label %43
59: ; preds = %43
%60 = load i32*, i32** %6
%61 = call i32 @closedir(i32* %60)
br label %62
62: ; preds = %59, %26
%63 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.7, i64 0, i64 0))
%64 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%65 = load i8*, i8** %2
%66 = load %struct.dirent*, %struct.dirent** %3
%67 = getelementptr inbounds %struct.dirent, %struct.dirent* %66, i32 0, i32 0
%68 = load i8*, i8** %67
%69 = call i32 @sprintf(i8* %64, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.8, i64 0, i64 0), i8* %65, i8* %68)
%70 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%71 = getelementptr inbounds [20 x i8], [20 x i8]* %8, i64 0, i64 0
%72 = call i64 @get_mtd_device(i8* %70, i8* %71, i32 20)
%73 = icmp eq i64 %72, 0
br i1 %73, label %74, label %77
74: ; preds = %62
%75 = getelementptr inbounds [20 x i8], [20 x i8]* %8, i64 0, i64 0
%76 = call i32 @show_flash_info(i8* %75)
br label %77
77: ; preds = %74, %62
%78 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.7, i64 0, i64 0))
br label %15
79: ; preds = %15
%80 = load i32*, i32** %4
%81 = call i32 @closedir(i32* %80)
store i32 0, i32* %1
br label %82
82: ; preds = %79, %13
%83 = load i32, i32* %1
ret i32 %83
}

LLVM IR (-O2):
%struct.dirent = type { i8* }
@.str = [29 x i8] c"/sys/bus/pci/drivers/cobalt/\00"
@.str.1 = [2 x i8] c":\00"
@.str.2 = [40 x i8] c"Found Cobalt card with PCI address: %s\0A\00"
@.str.3 = [22 x i8] c"Video4Linux devices: \00"
@.str.4 = [18 x i8] c"%s%s/video4linux/\00"
@.str.5 = [6 x i8] c"video\00"
@.str.6 = [4 x i8] c"%s \00"
@.str.8 = [10 x i8] c"%s%s/mtd/\00"
define i32 @scan_for_mtd_devices() {
%1 = alloca [256 x i8]
%2 = alloca [20 x i8]
%3 = tail call i32* @opendir(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %57, label %5
5: ; preds = %0
%6 = call %struct.dirent* @readdir(i32* %3)
%7 = icmp eq %struct.dirent* %6, null
br i1 %7, label %55, label %8
8: ; preds = %5
%9 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 0
%10 = getelementptr inbounds [20 x i8], [20 x i8]* %2, i64 0, i64 0
br label %11
11: ; preds = %8, %52
%12 = phi %struct.dirent* [ %6, %8 ], [ %53, %52 ]
%13 = getelementptr inbounds %struct.dirent, %struct.dirent* %12, i64 0, i32 0
%14 = load i8*, i8** %13
%15 = call i64 @strstr(i8* %14, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%16 = icmp eq i64 %15, 0
br i1 %16, label %52, label %17
17: ; preds = %11
%18 = load i8*, i8** %13
%19 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0), i8* %18)
%20 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
%21 = load i8*, i8** %13
%22 = call i32 @sprintf(i8* %9, i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i8* %21)
%23 = call i32* @opendir(i8* %9)
%24 = icmp eq i32* %23, null
br i1 %24, label %42, label %25
25: ; preds = %17
%26 = call %struct.dirent* @readdir(i32* %23)
%27 = icmp eq %struct.dirent* %26, null
br i1 %27, label %40, label %28
28: ; preds = %25, %37
%29 = phi %struct.dirent* [ %38, %37 ], [ %26, %25 ]
%30 = getelementptr inbounds %struct.dirent, %struct.dirent* %29, i64 0, i32 0
%31 = load i8*, i8** %30
%32 = call i64 @strstr(i8* %31, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
%33 = icmp eq i64 %32, 0
br i1 %33, label %37, label %34
34: ; preds = %28
%35 = load i8*, i8** %30
%36 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), i8* %35)
br label %37
37: ; preds = %34, %28
%38 = call %struct.dirent* @readdir(i32* %23)
%39 = icmp eq %struct.dirent* %38, null
br i1 %39, label %40, label %28
40: ; preds = %37, %25
%41 = call i32 @closedir(i32* %23)
br label %42
42: ; preds = %40, %17
%43 = call i32 @putchar(i32 10)
%44 = load i8*, i8** %13
%45 = call i32 @sprintf(i8* %9, i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.8, i64 0, i64 0), i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i8* %44)
%46 = call i64 @get_mtd_device(i8* %9, i8* %10, i32 20)
%47 = icmp eq i64 %46, 0
br i1 %47, label %48, label %50
48: ; preds = %42
%49 = call i32 @show_flash_info(i8* %10)
br label %50
50: ; preds = %48, %42
%51 = call i32 @putchar(i32 10)
br label %52
52: ; preds = %11, %50
%53 = call %struct.dirent* @readdir(i32* %3)
%54 = icmp eq %struct.dirent* %53, null
br i1 %54, label %55, label %11
55: ; preds = %52, %5
%56 = call i32 @closedir(i32* %3)
br label %57
57: ; preds = %0, %55
ret i32 0
}