#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 */
typedef int /*<<< orphan*/ tmp;
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 *);
int /*<<< orphan*/ strncpy(char *, char *, int);
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(sizeof(tmp[jj]));
    strncpy(ret[jj], tmp[jj], sizeof(tmp[jj]));
  }
  *driveNum = j;
  return ret;
}


x86 assembly (clang, -O0):
<get_drives>:
push   %rbp
mov    %rsp,%rbp
sub    $0x230,%rsp
mov    %rdi,-0x10(%rbp)
lea    0xe7a(%rip),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11dd <get_drives+0x6d>
lea    0xe77(%rip),%rdi
call   1090 <write_debug@plt>
lea    0xe90(%rip),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11d8 <get_drives+0x68>
mov    -0x10(%rbp),%rax
movl   $0x0,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    133b <get_drives+0x1cb>
jmp    11dd <get_drives+0x6d>
movl   $0x0,-0x22c(%rbp)
mov    -0x18(%rbp),%rdi
call   1070 <readdir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     1288 <get_drives+0x118>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
lea    0xe3d(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
je     1283 <get_drives+0x113>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
lea    0xe1f(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
je     1283 <get_drives+0x113>
mov    $0x8,%edi
call   1080 <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    11e7 <get_drives+0x77>
mov    -0x18(%rbp),%rdi
call   1050 <closedir@plt>
movslq -0x22c(%rbp),%rax
shl    $0x3,%rax
mov    %eax,%edi
call   1080 <malloc@plt>
mov    %rax,-0x228(%rbp)
movl   $0x0,-0x230(%rbp)
mov    -0x230(%rbp),%eax
cmp    -0x22c(%rbp),%eax
jge    1324 <get_drives+0x1b4>
mov    $0x8,%edi
call   1080 <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),%rdi
movslq -0x230(%rbp),%rax
mov    -0x220(%rbp,%rax,8),%rsi
ff
mov    $0x8,%edx
call   1030 <strncpy@plt>
mov    -0x230(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x230(%rbp)
jmp    12b4 <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    $0x230,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_drives>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x208,%rsp
mov    %rdi,%rbp
lea    0xe75(%rip),%rdi
call   1040 <opendir@plt>
mov    %rax,%r13
test   %rax,%rax
jne    11bc <get_drives+0x4c>
lea    0xe79(%rip),%rdi
call   1090 <write_debug@plt>
lea    0xe92(%rip),%rdi
call   1040 <opendir@plt>
mov    %rax,%r13
test   %rax,%rax
je     128d <get_drives+0x11d>
mov    %r13,%rdi
call   1070 <readdir@plt>
test   %rax,%rax
je     1237 <get_drives+0xc7>
mov    %rax,%rbx
xor    %r14d,%r14d
lea    0xe73(%rip),%r15
lea    0xe6b(%rip),%r12
jmp    11f0 <get_drives+0x80>
nop
mov    %r13,%rdi
call   1070 <readdir@plt>
mov    %rax,%rbx
test   %rax,%rax
je     123a <get_drives+0xca>
mov    (%rbx),%rdi
mov    %r15,%rsi
call   1060 <strcmp@plt>
test   %rax,%rax
je     11e0 <get_drives+0x70>
mov    (%rbx),%rdi
mov    %r12,%rsi
call   1060 <strcmp@plt>
test   %rax,%rax
je     11e0 <get_drives+0x70>
mov    $0x8,%edi
call   1080 <malloc@plt>
movslq %r14d,%r14
mov    %rax,(%rsp,%r14,8)
mov    (%rbx),%rsi
mov    %rax,%rdi
mov    $0x8,%edx
call   1030 <strncpy@plt>
add    $0x1,%r14d
jmp    11e0 <get_drives+0x70>
xor    %r14d,%r14d
mov    %r13,%rdi
call   1050 <closedir@plt>
lea    0x0(,%r14,8),%edi
call   1080 <malloc@plt>
mov    %rax,%r15
test   %r14d,%r14d
jle    1293 <get_drives+0x123>
mov    %rbp,%r12
mov    %r14d,%ebp
xor    %ebx,%ebx
nop
mov    $0x8,%edi
call   1080 <malloc@plt>
mov    %rax,(%r15,%rbx,8)
mov    (%rsp,%rbx,8),%rsi
mov    %rax,%rdi
mov    $0x8,%edx
call   1030 <strncpy@plt>
add    $0x1,%rbx
cmp    %rbx,%rbp
jne    1260 <get_drives+0xf0>
mov    %r12,%rbp
jmp    1293 <get_drives+0x123>
xor    %r14d,%r14d
xor    %r15d,%r15d
mov    %r14d,0x0(%rbp)
mov    %r15,%rax
add    $0x208,%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 %90
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, i32 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 = %83, %54
%64 = load i32, i32* %9
%65 = load i32, i32* %8
%66 = icmp slt i32 %64, %65
br i1 %66, label %67, label %86
67: ; preds = %63
%68 = call i8* @malloc(i32 8)
%69 = load i8**, i8*** %7
%70 = load i32, i32* %9
%71 = sext i32 %70 to i64
%72 = getelementptr inbounds i8*, i8** %69, i64 %71
store i8* %68, i8** %72
%73 = load i8**, i8*** %7
%74 = load i32, i32* %9
%75 = sext i32 %74 to i64
%76 = getelementptr inbounds i8*, i8** %73, i64 %75
%77 = load i8*, i8** %76
%78 = load i32, i32* %9
%79 = sext i32 %78 to i64
%80 = getelementptr inbounds [64 x i8*], [64 x i8*]* %6, i64 0, i64 %79
%81 = load i8*, i8** %80
%82 = call i32 @strncpy(i8* %77, i8* %81, i32 8)
br label %83
83: ; preds = %67
%84 = load i32, i32* %9
%85 = add nsw i32 %84, 1
store i32 %85, i32* %9
br label %63
86: ; preds = %63
%87 = load i32, i32* %8
%88 = load i32*, i32** %3
store i32 %87, i32* %88
%89 = load i8**, i8*** %7
store i8** %89, i8*** %2
br label %90
90: ; preds = %86, %18
%91 = load i8**, i8*** %2
ret i8** %91
}

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 %54, 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, i32 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 %54
43: ; preds = %36
%44 = zext i32 %37 to i64
br label %45
45: ; preds = %43, %45
%46 = phi i64 [ 0, %43 ], [ %52, %45 ]
%47 = tail call dereferenceable_or_null(8) i8* @malloc(i32 8)
%48 = getelementptr inbounds i8*, i8** %41, i64 %46
store i8* %47, i8** %48
%49 = getelementptr inbounds [64 x i8*], [64 x i8*]* %2, i64 0, i64 %46
%50 = load i8*, i8** %49
%51 = tail call i32 @strncpy(i8* %47, i8* %50, i32 8)
%52 = add nuw nsw i64 %46, 1
%53 = icmp eq i64 %52, %44
br i1 %53, label %54, label %45
54: ; preds = %45, %36, %6
%55 = phi i32 [ 0, %6 ], [ %37, %36 ], [ %37, %45 ]
%56 = phi i8** [ null, %6 ], [ %41, %36 ], [ %41, %45 ]
store i32 %55, i32* %0
ret i8** %56
}