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

/* Variables and functions */
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ snprintf(char *, int, char *, char *);
scalar_t__ strcmp(char *, char *);
char *strdup(char *);
scalar_t__ strncmp(char *, char *, int);
int find_idedisks(char **dev, int maxdev) {
  DIR *dd;
  FILE *fp;
  struct dirent *d;
  char buf[256];
  int i = 0;
  if ((dd = opendir("/proc/ide")) == NULL)
    return -1;
  while ((d = readdir(dd)) != NULL) {
    if (strncmp(d->d_name, "hd", 2) != 0)
      continue;
    buf[0] = 0;
    snprintf(buf, sizeof(buf),
             "/proc/ide"
             "/%s/media",
             d->d_name);
    if ((fp = fopen(buf, "r")) == NULL)
      continue;
    if (fgets(buf, sizeof(buf), fp) == 0 || strcmp(buf, "disk\n") != 0) {
      fclose(fp);
      continue;
    }
    fclose(fp);
    snprintf(buf, sizeof(buf),
             "/dev"
             "/%s",
             d->d_name);
    dev[i++] = strdup(buf);
    if (i >= maxdev)
      break;
  }
  closedir(dd);
  if (i < maxdev)
    dev[i] = NULL;
  return 0;
}


x86 assembly (clang, -O0):
<find_idedisks>:
push   %rbp
mov    %rsp,%rbp
sub    $0x140,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movl   $0x0,-0x134(%rbp)
lea    0xe3d(%rip),%rdi
call   1050 <opendir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    11e2 <find_idedisks+0x42>
movl   $0xffffffff,-0x4(%rbp)
jmp    1359 <find_idedisks+0x1b9>
jmp    11e7 <find_idedisks+0x47>
mov    -0x20(%rbp),%rdi
call   10a0 <readdir@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
je     1327 <find_idedisks+0x187>
mov    -0x30(%rbp),%rax
mov    (%rax),%rdi
lea    0xdfe(%rip),%rsi
mov    $0x2,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
je     1225 <find_idedisks+0x85>
jmp    11e7 <find_idedisks+0x47>
movb   $0x0,-0x130(%rbp)
lea    -0x130(%rbp),%rdi
mov    -0x30(%rbp),%rax
mov    (%rax),%rcx
mov    $0x100,%esi
lea    0xdc7(%rip),%rdx
call   1060 <snprintf@plt>
lea    -0x130(%rbp),%rdi
lea    0xdc7(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    1271 <find_idedisks+0xd1>
jmp    11e7 <find_idedisks+0x47>
lea    -0x130(%rbp),%rdi
mov    -0x28(%rbp),%rdx
mov    $0x100,%esi
call   1080 <fgets@plt>
cmp    $0x0,%rax
je     12ad <find_idedisks+0x10d>
lea    -0x130(%rbp),%rdi
lea    0xd84(%rip),%rsi
call   1090 <strcmp@plt>
cmp    $0x0,%rax
je     12bb <find_idedisks+0x11b>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
jmp    11e7 <find_idedisks+0x47>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
lea    -0x130(%rbp),%rdi
mov    -0x30(%rbp),%rax
mov    (%rax),%rcx
mov    $0x100,%esi
lea    0xd4a(%rip),%rdx
call   1060 <snprintf@plt>
lea    -0x130(%rbp),%rdi
call   10c0 <strdup@plt>
mov    %rax,%rdx
mov    -0x10(%rbp),%rax
mov    -0x134(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x134(%rbp)
movslq %ecx,%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x134(%rbp),%eax
cmp    -0x14(%rbp),%eax
jl     1322 <find_idedisks+0x182>
jmp    1327 <find_idedisks+0x187>
jmp    11e7 <find_idedisks+0x47>
mov    -0x20(%rbp),%rdi
call   1070 <closedir@plt>
mov    -0x134(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    1352 <find_idedisks+0x1b2>
mov    -0x10(%rbp),%rax
movslq -0x134(%rbp),%rcx
movq   $0x0,(%rax,%rcx,8)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x140,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<find_idedisks>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x128,%rsp
mov    %esi,%ebp
mov    %rdi,%r15
lea    0xe43(%rip),%rdi
call   1050 <opendir@plt>
test   %rax,%rax
je     1324 <find_idedisks+0x184>
mov    %rax,%rbx
cmp    $0x2,%ebp
mov    $0x1,%eax
mov    %ebp,0x4(%rsp)
cmovge %ebp,%eax
mov    %rax,0x18(%rsp)
xor    %ecx,%ecx
lea    0xe1f(%rip),%r13
lea    0x20(%rsp),%rbp
lea    0xe29(%rip),%r14
mov    %r15,0x8(%rsp)
mov    %rcx,0x10(%rsp)
mov    %rbx,%rdi
call   10a0 <readdir@plt>
test   %rax,%rax
je     12fd <find_idedisks+0x15d>
mov    %rax,%r15
jmp    123c <find_idedisks+0x9c>
nopw   0x0(%rax,%rax,1)
mov    %r12,%rdi
call   1040 <fclose@plt>
mov    %rbx,%rdi
call   10a0 <readdir@plt>
mov    %rax,%r15
test   %rax,%rax
je     12fd <find_idedisks+0x15d>
mov    (%r15),%rdi
mov    %r13,%rsi
mov    $0x2,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
jne    1228 <find_idedisks+0x88>
movb   $0x0,0x20(%rsp)
mov    (%r15),%rcx
mov    %rbp,%rdi
mov    $0x100,%esi
lea    0xda5(%rip),%rdx
call   1060 <snprintf@plt>
mov    %rbp,%rdi
mov    %r14,%rsi
call   10b0 <fopen@plt>
test   %rax,%rax
je     1228 <find_idedisks+0x88>
mov    %rax,%r12
mov    %rbp,%rdi
mov    $0x100,%esi
mov    %rax,%rdx
call   1080 <fgets@plt>
test   %rax,%rax
je     1220 <find_idedisks+0x80>
mov    %rbp,%rdi
lea    0xd83(%rip),%rsi
call   1090 <strcmp@plt>
test   %rax,%rax
jne    1220 <find_idedisks+0x80>
mov    %r12,%rdi
call   1040 <fclose@plt>
mov    (%r15),%rcx
mov    %rbp,%rdi
mov    $0x100,%esi
lea    0xd61(%rip),%rdx
call   1060 <snprintf@plt>
mov    %rbp,%rdi
call   10c0 <strdup@plt>
mov    0x8(%rsp),%r15
mov    0x10(%rsp),%rcx
mov    %rax,(%r15,%rcx,8)
add    $0x1,%rcx
cmp    0x18(%rsp),%rcx
jne    11fc <find_idedisks+0x5c>
mov    %rbx,%rdi
call   1070 <closedir@plt>
xor    %eax,%eax
jmp    1329 <find_idedisks+0x189>
mov    %rbx,%rdi
call   1070 <closedir@plt>
xor    %eax,%eax
mov    0x10(%rsp),%rcx
cmp    0x4(%rsp),%ecx
jge    1329 <find_idedisks+0x189>
movslq %ecx,%rcx
mov    0x8(%rsp),%rdx
movq   $0x0,(%rdx,%rcx,8)
jmp    1329 <find_idedisks+0x189>
mov    $0xffffffff,%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 = [10 x i8] c"/proc/ide\00"
@.str.1 = [3 x i8] c"hd\00"
@.str.2 = [19 x i8] c"/proc/ide/%s/media\00"
@.str.3 = [2 x i8] c"r\00"
@.str.4 = [6 x i8] c"disk\0A\00"
@.str.5 = [8 x i8] c"/dev/%s\00"
define i32 @find_idedisks(i8** %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8**
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca %struct.dirent*
%9 = alloca [256 x i8]
%10 = alloca i32
store i8** %0, i8*** %4
store i32 %1, i32* %5
store i32 0, i32* %10
%11 = call i32* @opendir(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
store i32* %11, i32** %6
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %14
13: ; preds = %2
store i32 -1, i32* %3
br label %81
14: ; preds = %2
br label %15
15: ; preds = %68, %46, %36, %25, %14
%16 = load i32*, i32** %6
%17 = call %struct.dirent* @readdir(i32* %16)
store %struct.dirent* %17, %struct.dirent** %8
%18 = icmp ne %struct.dirent* %17, null
br i1 %18, label %19, label %69
19: ; preds = %15
%20 = load %struct.dirent*, %struct.dirent** %8
%21 = getelementptr inbounds %struct.dirent, %struct.dirent* %20, i32 0, i32 0
%22 = load i8*, i8** %21
%23 = call i64 @strncmp(i8* %22, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 2)
%24 = icmp ne i64 %23, 0
br i1 %24, label %25, label %26
25: ; preds = %19
br label %15
26: ; preds = %19
%27 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 0
store i8 0, i8* %27
%28 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 0
%29 = load %struct.dirent*, %struct.dirent** %8
%30 = getelementptr inbounds %struct.dirent, %struct.dirent* %29, i32 0, i32 0
%31 = load i8*, i8** %30
%32 = call i32 @snprintf(i8* %28, i32 256, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i8* %31)
%33 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 0
%34 = call i32* @fopen(i8* %33, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %34, i32** %7
%35 = icmp eq i32* %34, null
br i1 %35, label %36, label %37
36: ; preds = %26
br label %15
37: ; preds = %26
%38 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 0
%39 = load i32*, i32** %7
%40 = call i64 @fgets(i8* %38, i32 256, i32* %39)
%41 = icmp eq i64 %40, 0
br i1 %41, label %46, label %42
42: ; preds = %37
%43 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 0
%44 = call i64 @strcmp(i8* %43, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
%45 = icmp ne i64 %44, 0
br i1 %45, label %46, label %49
46: ; preds = %42, %37
%47 = load i32*, i32** %7
%48 = call i32 @fclose(i32* %47)
br label %15
49: ; preds = %42
%50 = load i32*, i32** %7
%51 = call i32 @fclose(i32* %50)
%52 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 0
%53 = load %struct.dirent*, %struct.dirent** %8
%54 = getelementptr inbounds %struct.dirent, %struct.dirent* %53, i32 0, i32 0
%55 = load i8*, i8** %54
%56 = call i32 @snprintf(i8* %52, i32 256, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0), i8* %55)
%57 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 0
%58 = call i8* @strdup(i8* %57)
%59 = load i8**, i8*** %4
%60 = load i32, i32* %10
%61 = add nsw i32 %60, 1
store i32 %61, i32* %10
%62 = sext i32 %60 to i64
%63 = getelementptr inbounds i8*, i8** %59, i64 %62
store i8* %58, i8** %63
%64 = load i32, i32* %10
%65 = load i32, i32* %5
%66 = icmp sge i32 %64, %65
br i1 %66, label %67, label %68
67: ; preds = %49
br label %69
68: ; preds = %49
br label %15
69: ; preds = %67, %15
%70 = load i32*, i32** %6
%71 = call i32 @closedir(i32* %70)
%72 = load i32, i32* %10
%73 = load i32, i32* %5
%74 = icmp slt i32 %72, %73
br i1 %74, label %75, label %80
75: ; preds = %69
%76 = load i8**, i8*** %4
%77 = load i32, i32* %10
%78 = sext i32 %77 to i64
%79 = getelementptr inbounds i8*, i8** %76, i64 %78
store i8* null, i8** %79
br label %80
80: ; preds = %75, %69
store i32 0, i32* %3
br label %81
81: ; preds = %80, %13
%82 = load i32, i32* %3
ret i32 %82
}

LLVM IR (-O2):
%struct.dirent = type { i8* }
@.str = [10 x i8] c"/proc/ide\00"
@.str.1 = [3 x i8] c"hd\00"
@.str.2 = [19 x i8] c"/proc/ide/%s/media\00"
@.str.3 = [2 x i8] c"r\00"
@.str.4 = [6 x i8] c"disk\0A\00"
@.str.5 = [8 x i8] c"/dev/%s\00"
define i32 @find_idedisks(i8** nocapture writeonly %0, i32 %1) {
%3 = alloca [256 x i8]
%4 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%5 = tail call i32* @opendir(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %54, label %7
7: ; preds = %2
%8 = call i32 @llvm.smax.i32(i32 %1, i32 1)
%9 = zext i32 %8 to i64
br label %10
10: ; preds = %7, %36
%11 = phi i64 [ 0, %7 ], [ %41, %36 ]
%12 = call %struct.dirent* @readdir(i32* %5)
%13 = icmp eq %struct.dirent* %12, null
br i1 %13, label %46, label %14
14: ; preds = %10, %33
%15 = phi %struct.dirent* [ %34, %33 ], [ %12, %10 ]
%16 = getelementptr inbounds %struct.dirent, %struct.dirent* %15, i64 0, i32 0
%17 = load i8*, i8** %16
%18 = call i64 @strncmp(i8* %17, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 2)
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %33
20: ; preds = %14
store i8 0, i8* %4
%21 = load i8*, i8** %16
%22 = call i32 @snprintf(i8* %4, i32 256, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i8* %21)
%23 = call i32* @fopen(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%24 = icmp eq i32* %23, null
br i1 %24, label %33, label %25
25: ; preds = %20
%26 = call i64 @fgets(i8* %4, i32 256, i32* %23)
%27 = icmp eq i64 %26, 0
br i1 %27, label %31, label %28
28: ; preds = %25
%29 = call i64 @strcmp(i8* %4, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
%30 = icmp eq i64 %29, 0
br i1 %30, label %36, label %31
31: ; preds = %28, %25
%32 = call i32 @fclose(i32* %23)
br label %33
33: ; preds = %31, %14, %20
%34 = call %struct.dirent* @readdir(i32* %5)
%35 = icmp eq %struct.dirent* %34, null
br i1 %35, label %46, label %14
36: ; preds = %28
%37 = call i32 @fclose(i32* %23)
%38 = load i8*, i8** %16
%39 = call i32 @snprintf(i8* %4, i32 256, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0), i8* %38)
%40 = call i8* @strdup(i8* %4)
%41 = add nuw nsw i64 %11, 1
%42 = getelementptr inbounds i8*, i8** %0, i64 %11
store i8* %40, i8** %42
%43 = icmp eq i64 %41, %9
br i1 %43, label %44, label %10
44: ; preds = %36
%45 = call i32 @closedir(i32* %5)
br label %54
46: ; preds = %10, %33
%47 = trunc i64 %11 to i32
%48 = call i32 @closedir(i32* %5)
%49 = icmp slt i32 %47, %1
br i1 %49, label %50, label %54
50: ; preds = %46
%51 = shl i64 %11, 32
%52 = ashr exact i64 %51, 32
%53 = getelementptr inbounds i8*, i8** %0, i64 %52
store i8* null, i8** %53
br label %54
54: ; preds = %44, %46, %50, %2
%55 = phi i32 [ -1, %2 ], [ 0, %50 ], [ 0, %46 ], [ 0, %44 ]
ret i32 %55
}