#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*/ *);
void *malloc(int);
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
scalar_t__ strcmp(char *, char *);
size_t strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, size_t);
int /*<<< orphan*/ write_debug(char *);
char **get_drives(int *driveNum) {
  DIR *d;
  struct dirent *ent;
  char *tmp[64];
  char **ret;
  int j, jj;
  d = opendir("/sys/class/scsi_generic");
  if (!d) {
    write_debug("Cannot open /sys/class/scsi_generic\n");
    d = opendir("/sys/block");
    if (!d) {
      *driveNum = 0;
      return NULL;
    }
  }
  j = 0;
  while ((ent = readdir(d))) {
    if (strcmp(ent->d_name, ".") != 0 && strcmp(ent->d_name, "..") != 0) {
      tmp[j] = malloc(sizeof(ent->d_name));
      strncpy(tmp[j], ent->d_name, sizeof(ent->d_name));
      ++j;
    }
  }
  closedir(d);
  ret = malloc(j * sizeof(char *));
  for (jj = 0; jj < j; ++jj) {
    ret[jj] = malloc(strlen(tmp[jj]) + 1);
    strncpy(ret[jj], tmp[jj], strlen(tmp[jj]));
    ret[jj][strlen(tmp[jj])] = '\0';
  }
  *driveNum = j;
  return ret;
}


x86 assembly (clang, -O0):
<get_drives>:
push   %rbp
mov    %rsp,%rbp
sub    $0x250,%rsp
mov    %rdi,-0x10(%rbp)
lea    0xe6a(%rip),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11ed <get_drives+0x6d>
lea    0xe67(%rip),%rdi
call   10a0 <write_debug@plt>
lea    0xe80(%rip),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11e8 <get_drives+0x68>
mov    -0x10(%rbp),%rax
movl   $0x0,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    13c9 <get_drives+0x249>
jmp    11ed <get_drives+0x6d>
movl   $0x0,-0x22c(%rbp)
mov    -0x18(%rbp),%rdi
call   1080 <readdir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     1298 <get_drives+0x118>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
lea    0xe2d(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
je     1293 <get_drives+0x113>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
lea    0xe0f(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
je     1293 <get_drives+0x113>
mov    $0x8,%edi
call   1090 <malloc@plt>
mov    %rax,%rcx
movslq -0x22c(%rbp),%rax
mov    %rcx,-0x220(%rbp,%rax,8)
ff
movslq -0x22c(%rbp),%rax
mov    -0x220(%rbp,%rax,8),%rdi
ff
mov    -0x20(%rbp),%rax
mov    (%rax),%rsi
mov    $0x8,%edx
call   1030 <strncpy@plt>
mov    -0x22c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x22c(%rbp)
jmp    11f7 <get_drives+0x77>
mov    -0x18(%rbp),%rdi
call   1060 <closedir@plt>
movslq -0x22c(%rbp),%rax
shl    $0x3,%rax
mov    %eax,%edi
call   1090 <malloc@plt>
mov    %rax,-0x228(%rbp)
movl   $0x0,-0x230(%rbp)
mov    -0x230(%rbp),%eax
cmp    -0x22c(%rbp),%eax
jge    13b2 <get_drives+0x232>
movslq -0x230(%rbp),%rax
mov    -0x220(%rbp,%rax,8),%rdi
ff
call   1050 <strlen@plt>
add    $0x1,%rax
mov    %eax,%edi
call   1090 <malloc@plt>
mov    %rax,%rdx
mov    -0x228(%rbp),%rax
movslq -0x230(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x228(%rbp),%rax
movslq -0x230(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
mov    %rax,-0x248(%rbp)
movslq -0x230(%rbp),%rax
mov    -0x220(%rbp,%rax,8),%rax
ff
mov    %rax,-0x240(%rbp)
movslq -0x230(%rbp),%rax
mov    -0x220(%rbp,%rax,8),%rdi
ff
call   1050 <strlen@plt>
mov    -0x248(%rbp),%rdi
mov    -0x240(%rbp),%rsi
mov    %rax,%rdx
call   1030 <strncpy@plt>
mov    -0x228(%rbp),%rax
movslq -0x230(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
mov    %rax,-0x238(%rbp)
movslq -0x230(%rbp),%rax
mov    -0x220(%rbp,%rax,8),%rdi
ff
call   1050 <strlen@plt>
mov    %rax,%rcx
mov    -0x238(%rbp),%rax
movb   $0x0,(%rax,%rcx,1)
mov    -0x230(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x230(%rbp)
jmp    12c4 <get_drives+0x144>
mov    -0x22c(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x228(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x250,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_drives>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x218,%rsp
mov    %rdi,%r14
lea    0xe65(%rip),%rdi
call   1040 <opendir@plt>
mov    %rax,%r13
test   %rax,%rax
jne    11cc <get_drives+0x4c>
lea    0xe69(%rip),%rdi
call   10a0 <write_debug@plt>
lea    0xe82(%rip),%rdi
call   1040 <opendir@plt>
mov    %rax,%r13
test   %rax,%rax
je     12cd <get_drives+0x14d>
mov    %r13,%rdi
call   1080 <readdir@plt>
test   %rax,%rax
je     1247 <get_drives+0xc7>
mov    %rax,%rbx
xor    %ebp,%ebp
lea    0xe64(%rip),%r15
lea    0xe5c(%rip),%r12
jmp    1200 <get_drives+0x80>
xchg   %ax,%ax
mov    %r13,%rdi
call   1080 <readdir@plt>
mov    %rax,%rbx
test   %rax,%rax
je     1249 <get_drives+0xc9>
mov    (%rbx),%rdi
mov    %r15,%rsi
call   1070 <strcmp@plt>
test   %rax,%rax
je     11f0 <get_drives+0x70>
mov    (%rbx),%rdi
mov    %r12,%rsi
call   1070 <strcmp@plt>
test   %rax,%rax
je     11f0 <get_drives+0x70>
mov    $0x8,%edi
call   1090 <malloc@plt>
movslq %ebp,%rbp
mov    %rax,0x10(%rsp,%rbp,8)
mov    (%rbx),%rsi
mov    $0x8,%edx
mov    %rax,%rdi
call   1030 <strncpy@plt>
add    $0x1,%ebp
jmp    11f0 <get_drives+0x70>
xor    %ebp,%ebp
mov    %r13,%rdi
call   1060 <closedir@plt>
lea    0x0(,%rbp,8),%edi
call   1090 <malloc@plt>
mov    %rax,%r15
test   %ebp,%ebp
jle    12d2 <get_drives+0x152>
mov    %r14,0x8(%rsp)
mov    %rbp,(%rsp)
mov    %ebp,%r14d
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    0x10(%rsp,%rbp,8),%r13
mov    %r13,%rdi
call   1050 <strlen@plt>
mov    %rax,%r12
lea    0x1(%rax),%edi
call   1090 <malloc@plt>
mov    %rax,%rbx
mov    %rax,(%r15,%rbp,8)
mov    %rax,%rdi
mov    %r13,%rsi
mov    %r12,%rdx
call   1030 <strncpy@plt>
mov    %r13,%rdi
call   1050 <strlen@plt>
movb   $0x0,(%rbx,%rax,1)
add    $0x1,%rbp
cmp    %rbp,%r14
jne    1280 <get_drives+0x100>
mov    0x8(%rsp),%r14
mov    (%rsp),%rbp
jmp    12d2 <get_drives+0x152>
xor    %ebp,%ebp
xor    %r15d,%r15d
mov    %ebp,(%r14)
mov    %r15,%rax
add    $0x218,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i8* }
@.str = [24 x i8] c"/sys/class/scsi_generic\00"
@.str.1 = [37 x i8] c"Cannot open /sys/class/scsi_generic\0A\00"
@.str.2 = [11 x i8] c"/sys/block\00"
@.str.3 = [2 x i8] c".\00"
@.str.4 = [3 x i8] c"..\00"
define i8** @get_drives(i32* %0) {
%2 = alloca i8**
%3 = alloca i32*
%4 = alloca i32*
%5 = alloca %struct.dirent*
%6 = alloca [64 x i8*]
%7 = alloca i8**
%8 = alloca i32
%9 = alloca i32
store i32* %0, i32** %3
%10 = call i32* @opendir(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
store i32* %10, i32** %4
%11 = load i32*, i32** %4
%12 = icmp ne i32* %11, null
br i1 %12, label %21, label %13
13: ; preds = %1
%14 = call i32 @write_debug(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0))
%15 = call i32* @opendir(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
store i32* %15, i32** %4
%16 = load i32*, i32** %4
%17 = icmp ne i32* %16, null
br i1 %17, label %20, label %18
18: ; preds = %13
%19 = load i32*, i32** %3
store i32 0, i32* %19
store i8** null, i8*** %2
br label %113
20: ; preds = %13
br label %21
21: ; preds = %20, %1
store i32 0, i32* %8
br label %22
22: ; preds = %53, %21
%23 = load i32*, i32** %4
%24 = call %struct.dirent* @readdir(i32* %23)
store %struct.dirent* %24, %struct.dirent** %5
%25 = icmp ne %struct.dirent* %24, null
br i1 %25, label %26, label %54
26: ; preds = %22
%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 i64 @strcmp(i8* %29, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%31 = icmp ne i64 %30, 0
br i1 %31, label %32, label %53
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 i64 @strcmp(i8* %35, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
%37 = icmp ne i64 %36, 0
br i1 %37, label %38, label %53
38: ; preds = %32
%39 = call i8* @malloc(i32 8)
%40 = load i32, i32* %8
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds [64 x i8*], [64 x i8*]* %6, i64 0, i64 %41
store i8* %39, i8** %42
%43 = load i32, i32* %8
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds [64 x i8*], [64 x i8*]* %6, i64 0, i64 %44
%46 = load i8*, i8** %45
%47 = load %struct.dirent*, %struct.dirent** %5
%48 = getelementptr inbounds %struct.dirent, %struct.dirent* %47, i32 0, i32 0
%49 = load i8*, i8** %48
%50 = call i32 @strncpy(i8* %46, i8* %49, i64 8)
%51 = load i32, i32* %8
%52 = add nsw i32 %51, 1
store i32 %52, i32* %8
br label %53
53: ; preds = %38, %32, %26
br label %22
54: ; preds = %22
%55 = load i32*, i32** %4
%56 = call i32 @closedir(i32* %55)
%57 = load i32, i32* %8
%58 = sext i32 %57 to i64
%59 = mul i64 %58, 8
%60 = trunc i64 %59 to i32
%61 = call i8* @malloc(i32 %60)
%62 = bitcast i8* %61 to i8**
store i8** %62, i8*** %7
store i32 0, i32* %9
br label %63
63: ; preds = %106, %54
%64 = load i32, i32* %9
%65 = load i32, i32* %8
%66 = icmp slt i32 %64, %65
br i1 %66, label %67, label %109
67: ; preds = %63
%68 = load i32, i32* %9
%69 = sext i32 %68 to i64
%70 = getelementptr inbounds [64 x i8*], [64 x i8*]* %6, i64 0, i64 %69
%71 = load i8*, i8** %70
%72 = call i64 @strlen(i8* %71)
%73 = add i64 %72, 1
%74 = trunc i64 %73 to i32
%75 = call i8* @malloc(i32 %74)
%76 = load i8**, i8*** %7
%77 = load i32, i32* %9
%78 = sext i32 %77 to i64
%79 = getelementptr inbounds i8*, i8** %76, i64 %78
store i8* %75, i8** %79
%80 = load i8**, i8*** %7
%81 = load i32, i32* %9
%82 = sext i32 %81 to i64
%83 = getelementptr inbounds i8*, i8** %80, i64 %82
%84 = load i8*, i8** %83
%85 = load i32, i32* %9
%86 = sext i32 %85 to i64
%87 = getelementptr inbounds [64 x i8*], [64 x i8*]* %6, i64 0, i64 %86
%88 = load i8*, i8** %87
%89 = load i32, i32* %9
%90 = sext i32 %89 to i64
%91 = getelementptr inbounds [64 x i8*], [64 x i8*]* %6, i64 0, i64 %90
%92 = load i8*, i8** %91
%93 = call i64 @strlen(i8* %92)
%94 = call i32 @strncpy(i8* %84, i8* %88, i64 %93)
%95 = load i8**, i8*** %7
%96 = load i32, i32* %9
%97 = sext i32 %96 to i64
%98 = getelementptr inbounds i8*, i8** %95, i64 %97
%99 = load i8*, i8** %98
%100 = load i32, i32* %9
%101 = sext i32 %100 to i64
%102 = getelementptr inbounds [64 x i8*], [64 x i8*]* %6, i64 0, i64 %101
%103 = load i8*, i8** %102
%104 = call i64 @strlen(i8* %103)
%105 = getelementptr inbounds i8, i8* %99, i64 %104
store i8 0, i8* %105
br label %106
106: ; preds = %67
%107 = load i32, i32* %9
%108 = add nsw i32 %107, 1
store i32 %108, i32* %9
br label %63
109: ; preds = %63
%110 = load i32, i32* %8
%111 = load i32*, i32** %3
store i32 %110, i32* %111
%112 = load i8**, i8*** %7
store i8** %112, i8*** %2
br label %113
113: ; preds = %109, %18
%114 = load i8**, i8*** %2
ret i8** %114
}

LLVM IR (-O2):
%struct.dirent = type { i8* }
@.str = [24 x i8] c"/sys/class/scsi_generic\00"
@.str.1 = [37 x i8] c"Cannot open /sys/class/scsi_generic\0A\00"
@.str.2 = [11 x i8] c"/sys/block\00"
@.str.3 = [2 x i8] c".\00"
@.str.4 = [3 x i8] c"..\00"
define noalias i8** @get_drives(i32* nocapture writeonly %0) {
%2 = alloca [64 x i8*]
%3 = bitcast [64 x i8*]* %2 to i8*
%4 = tail call i32* @opendir(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %10
6: ; preds = %1
%7 = tail call i32 @write_debug(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0))
%8 = tail call i32* @opendir(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
%9 = icmp eq i32* %8, null
br i1 %9, label %59, label %10
10: ; preds = %6, %1
%11 = phi i32* [ %4, %1 ], [ %8, %6 ]
%12 = tail call %struct.dirent* @readdir(i32* %11)
%13 = icmp eq %struct.dirent* %12, null
br i1 %13, label %36, label %14
14: ; preds = %10, %32
%15 = phi %struct.dirent* [ %34, %32 ], [ %12, %10 ]
%16 = phi i32 [ %33, %32 ], [ 0, %10 ]
%17 = getelementptr inbounds %struct.dirent, %struct.dirent* %15, i64 0, i32 0
%18 = load i8*, i8** %17
%19 = tail call i64 @strcmp(i8* %18, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%20 = icmp eq i64 %19, 0
br i1 %20, label %32, label %21
21: ; preds = %14
%22 = load i8*, i8** %17
%23 = tail call i64 @strcmp(i8* %22, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
%24 = icmp eq i64 %23, 0
br i1 %24, label %32, label %25
25: ; preds = %21
%26 = tail call dereferenceable_or_null(8) i8* @malloc(i32 8)
%27 = sext i32 %16 to i64
%28 = getelementptr inbounds [64 x i8*], [64 x i8*]* %2, i64 0, i64 %27
store i8* %26, i8** %28
%29 = load i8*, i8** %17
%30 = tail call i32 @strncpy(i8* %26, i8* %29, i64 8)
%31 = add nsw i32 %16, 1
br label %32
32: ; preds = %25, %21, %14
%33 = phi i32 [ %31, %25 ], [ %16, %21 ], [ %16, %14 ]
%34 = tail call %struct.dirent* @readdir(i32* %11)
%35 = icmp eq %struct.dirent* %34, null
br i1 %35, label %36, label %14
36: ; preds = %32, %10
%37 = phi i32 [ 0, %10 ], [ %33, %32 ]
%38 = tail call i32 @closedir(i32* %11)
%39 = shl i32 %37, 3
%40 = tail call i8* @malloc(i32 %39)
%41 = bitcast i8* %40 to i8**
%42 = icmp sgt i32 %37, 0
br i1 %42, label %43, label %59
43: ; preds = %36
%44 = zext i32 %37 to i64
br label %45
45: ; preds = %43, %45
%46 = phi i64 [ 0, %43 ], [ %57, %45 ]
%47 = getelementptr inbounds [64 x i8*], [64 x i8*]* %2, i64 0, i64 %46
%48 = load i8*, i8** %47
%49 = tail call i64 @strlen(i8* dereferenceable(1) %48)
%50 = trunc i64 %49 to i32
%51 = add i32 %50, 1
%52 = tail call i8* @malloc(i32 %51)
%53 = getelementptr inbounds i8*, i8** %41, i64 %46
store i8* %52, i8** %53
%54 = tail call i32 @strncpy(i8* %52, i8* %48, i64 %49)
%55 = tail call i64 @strlen(i8* dereferenceable(1) %48)
%56 = getelementptr inbounds i8, i8* %52, i64 %55
store i8 0, i8* %56
%57 = add nuw nsw i64 %46, 1
%58 = icmp eq i64 %57, %44
br i1 %58, label %59, label %45
59: ; preds = %45, %36, %6
%60 = phi i32 [ 0, %6 ], [ %37, %36 ], [ %37, %45 ]
%61 = phi i8** [ null, %6 ], [ %41, %36 ], [ %41, %45 ]
store i32 %60, i32* %0
ret i8** %61
}