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

/* Variables and functions */
scalar_t__ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ perror(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
size_t readlink(char *, char *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, char *, ...);
int /*<<< orphan*/ stdout;
scalar_t__ strncmp(char *, char *, int);
int /*<<< orphan*/ strncpy(char *, char *, int);
char *strrchr(char *, char);
int udisk_devlist_get(char *dirpath, char dev_list[][255 + 1]) {
  struct dirent *direntp;
  int i;
  char buf[255];
  char path[255];
  char *p;
  ssize_t n;
  DIR *dirp;
  if ((dirp = opendir(dirpath)) == NULL) {
    fprintf(stdout, "opendir %s failed\n", dirpath);
    exit(1);
  } else {
    fprintf(stdout, "opendir %s successfully\n", dirpath);
  }
  i = 0;
  errno = 0;
  while ((direntp = readdir(dirp)) != NULL) {
    if (direntp->d_name[0] == '.') {
      continue;
    }
    snprintf(path, sizeof(path) - 1, "%s/%s", dirpath, direntp->d_name);
    if ((n = readlink(path, buf, sizeof(buf) - 1)) < 0) {
      perror("readlink failed");
      exit(1);
    }
    buf[n] = '\0';
    p = strrchr(buf, '/');
    if (p != NULL && strncmp(p, "/sda", 4) != 0) {
      snprintf(dev_list[i], 255, "/dev%s", p);
      i++;
    }
  }
  if (errno != 0) {
    perror("readdir failed");
    exit(1);
  }
  strncpy(dev_list[i], "", 1);
  return 0;
}


x86 assembly (clang, -O0):
<udisk_devlist_get>:
push   %rbp
mov    %rsp,%rbp
sub    $0x240,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   1060 <opendir@plt>
mov    %rax,-0x238(%rbp)
cmp    $0x0,%rax
jne    11f0 <udisk_devlist_get+0x50>
mov    0x2e04(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe1f(%rip),%rsi
call   1090 <fprintf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    0x2de1(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe0f(%rip),%rsi
call   1090 <fprintf@plt>
movl   $0x0,-0x1c(%rbp)
mov    0x2dc9(%rip),%rax
movq   $0x0,(%rax)
mov    -0x238(%rbp),%rdi
call   10a0 <readdir@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     1342 <udisk_devlist_get+0x1a2>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
movsbl (%rax),%eax
cmp    $0x2e,%eax
jne    1250 <udisk_devlist_get+0xb0>
jmp    121e <udisk_devlist_get+0x7e>
lea    -0x220(%rbp),%rdi
mov    -0x8(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    (%rax),%r8
mov    $0xfe,%esi
lea    0xdbe(%rip),%rdx
mov    $0x0,%al
call   1070 <snprintf@plt>
lea    -0x220(%rbp),%rdi
lea    -0x120(%rbp),%rsi
mov    $0xfe,%edx
call   1050 <readlink@plt>
mov    %rax,-0x230(%rbp)
cmp    $0x0,%rax
jae    12b4 <udisk_devlist_get+0x114>
lea    0xd8d(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x230(%rbp),%rax
movb   $0x0,-0x120(%rbp,%rax,1)
lea    -0x120(%rbp),%rdi
mov    $0x2f,%esi
call   1080 <strrchr@plt>
mov    %rax,-0x228(%rbp)
cmpq   $0x0,-0x228(%rbp)
je     133d <udisk_devlist_get+0x19d>
mov    -0x228(%rbp),%rdi
lea    0xd4b(%rip),%rsi
mov    $0x4,%edx
call   1040 <strncmp@plt>
cmp    $0x0,%rax
je     133d <udisk_devlist_get+0x19d>
mov    -0x10(%rbp),%rdi
movslq -0x1c(%rbp),%rax
shl    $0x8,%rax
add    %rax,%rdi
mov    -0x228(%rbp),%rcx
mov    $0xff,%esi
lea    0xd1a(%rip),%rdx
mov    $0x0,%al
call   1070 <snprintf@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    121e <udisk_devlist_get+0x7e>
mov    0x2c97(%rip),%rax
cmpq   $0x0,(%rax)
je     1369 <udisk_devlist_get+0x1c9>
lea    0xcf4(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x10(%rbp),%rdi
movslq -0x1c(%rbp),%rax
shl    $0x8,%rax
add    %rax,%rdi
lea    0xc93(%rip),%rsi
mov    $0x1,%edx
call   1030 <strncpy@plt>
xor    %eax,%eax
add    $0x240,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<udisk_devlist_get>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x208,%rsp
mov    %rsi,%r14
mov    %rdi,%r15
call   1060 <opendir@plt>
mov    %rax,%rbx
mov    0x2e12(%rip),%rax
mov    (%rax),%edi
test   %rbx,%rbx
je     12ef <udisk_devlist_get+0x14f>
lea    0xe3b(%rip),%rsi
mov    %r15,%rdx
call   1090 <fprintf@plt>
mov    0x2df9(%rip),%rax
movq   $0x0,(%rax)
mov    %rsp,%rbp
xor    %r13d,%r13d
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %rbx,%rdi
call   10a0 <readdir@plt>
test   %rax,%rax
je     12b0 <udisk_devlist_get+0x110>
mov    (%rax),%r8
cmpb   $0x2e,(%r8)
je     1200 <udisk_devlist_get+0x60>
mov    %rbp,%r12
lea    0x100(%rsp),%rbp
mov    %rbp,%rdi
mov    $0xfe,%esi
lea    0xdf8(%rip),%rdx
mov    %r15,%rcx
xor    %eax,%eax
call   1070 <snprintf@plt>
mov    %rbp,%rdi
mov    %r12,%rbp
mov    %r12,%rsi
mov    $0xfe,%edx
call   1050 <readlink@plt>
movb   $0x0,(%rsp,%rax,1)
mov    %r12,%rdi
mov    $0x2f,%esi
call   1080 <strrchr@plt>
test   %rax,%rax
je     1200 <udisk_devlist_get+0x60>
mov    %rax,%r12
mov    %rax,%rdi
lea    0xdbe(%rip),%rsi
mov    $0x4,%edx
call   1040 <strncmp@plt>
test   %rax,%rax
je     1200 <udisk_devlist_get+0x60>
mov    %r13d,%edi
shl    $0x8,%rdi
add    %r14,%rdi
mov    $0xff,%esi
lea    0xd9a(%rip),%rdx
mov    %r12,%rcx
xor    %eax,%eax
call   1070 <snprintf@plt>
add    $0x1,%r13d
jmp    1200 <udisk_devlist_get+0x60>
mov    0x2d29(%rip),%rax
cmpq   $0x0,(%rax)
jne    1308 <udisk_devlist_get+0x168>
mov    %r13d,%eax
shl    $0x8,%rax
add    %rax,%r14
lea    0xd44(%rip),%rsi
mov    %r14,%rdi
mov    $0x1,%edx
call   1030 <strncpy@plt>
xor    %eax,%eax
add    $0x208,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd0a(%rip),%rsi
mov    %r15,%rdx
call   1090 <fprintf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
lea    0xd2f(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
%struct.dirent = type { i8* }
@stdout = global i32 0
@.str = [19 x i8] c"opendir %s failed\0A\00"
@.str.1 = [25 x i8] c"opendir %s successfully\0A\00"
@errno = global i64 0
@.str.2 = [6 x i8] c"%s/%s\00"
@.str.3 = [16 x i8] c"readlink failed\00"
@.str.4 = [5 x i8] c"/sda\00"
@.str.5 = [7 x i8] c"/dev%s\00"
@.str.6 = [15 x i8] c"readdir failed\00"
@.str.7 = [1 x i8] zeroinitializer
define i32 @udisk_devlist_get(i8* %0, [256 x i8]* %1) {
%3 = alloca i8*
%4 = alloca [256 x i8]*
%5 = alloca %struct.dirent*
%6 = alloca i32
%7 = alloca [255 x i8]
%8 = alloca [255 x i8]
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i32*
store i8* %0, i8** %3
store [256 x i8]* %1, [256 x i8]** %4
%12 = load i8*, i8** %3
%13 = call i32* @opendir(i8* %12)
store i32* %13, i32** %11
%14 = icmp eq i32* %13, null
br i1 %14, label %15, label %20
15: ; preds = %2
%16 = load i32, i32* @stdout
%17 = load i8*, i8** %3
%18 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %17)
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %2
%21 = load i32, i32* @stdout
%22 = load i8*, i8** %3
%23 = call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %22)
br label %24
24: ; preds = %20
store i32 0, i32* %6
store i64 0, i64* @errno
br label %25
25: ; preds = %73, %37, %24
%26 = load i32*, i32** %11
%27 = call %struct.dirent* @readdir(i32* %26)
store %struct.dirent* %27, %struct.dirent** %5
%28 = icmp ne %struct.dirent* %27, null
br i1 %28, label %29, label %74
29: ; preds = %25
%30 = load %struct.dirent*, %struct.dirent** %5
%31 = getelementptr inbounds %struct.dirent, %struct.dirent* %30, i32 0, i32 0
%32 = load i8*, i8** %31
%33 = getelementptr inbounds i8, i8* %32, i64 0
%34 = load i8, i8* %33
%35 = sext i8 %34 to i32
%36 = icmp eq i32 %35, 46
br i1 %36, label %37, label %38
37: ; preds = %29
br label %25
38: ; preds = %29
%39 = getelementptr inbounds [255 x i8], [255 x i8]* %8, i64 0, i64 0
%40 = load i8*, i8** %3
%41 = load %struct.dirent*, %struct.dirent** %5
%42 = getelementptr inbounds %struct.dirent, %struct.dirent* %41, i32 0, i32 0
%43 = load i8*, i8** %42
%44 = call i32 (i8*, i32, i8*, i8*, ...) @snprintf(i8* %39, i32 254, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %40, i8* %43)
%45 = getelementptr inbounds [255 x i8], [255 x i8]* %8, i64 0, i64 0
%46 = getelementptr inbounds [255 x i8], [255 x i8]* %7, i64 0, i64 0
%47 = call i64 @readlink(i8* %45, i8* %46, i32 254)
store i64 %47, i64* %10
%48 = icmp ult i64 %47, 0
br i1 %48, label %49, label %52
49: ; preds = %38
%50 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
%51 = call i32 @exit(i32 1)
unreachable
52: ; preds = %38
%53 = load i64, i64* %10
%54 = getelementptr inbounds [255 x i8], [255 x i8]* %7, i64 0, i64 %53
store i8 0, i8* %54
%55 = getelementptr inbounds [255 x i8], [255 x i8]* %7, i64 0, i64 0
%56 = call i8* @strrchr(i8* %55, i8 signext 47)
store i8* %56, i8** %9
%57 = load i8*, i8** %9
%58 = icmp ne i8* %57, null
br i1 %58, label %59, label %73
59: ; preds = %52
%60 = load i8*, i8** %9
%61 = call i64 @strncmp(i8* %60, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i32 4)
%62 = icmp ne i64 %61, 0
br i1 %62, label %63, label %73
63: ; preds = %59
%64 = load [256 x i8]*, [256 x i8]** %4
%65 = load i32, i32* %6
%66 = sext i32 %65 to i64
%67 = getelementptr inbounds [256 x i8], [256 x i8]* %64, i64 %66
%68 = getelementptr inbounds [256 x i8], [256 x i8]* %67, i64 0, i64 0
%69 = load i8*, i8** %9
%70 = call i32 (i8*, i32, i8*, i8*, ...) @snprintf(i8* %68, i32 255, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0), i8* %69)
%71 = load i32, i32* %6
%72 = add nsw i32 %71, 1
store i32 %72, i32* %6
br label %73
73: ; preds = %63, %59, %52
br label %25
74: ; preds = %25
%75 = load i64, i64* @errno
%76 = icmp ne i64 %75, 0
br i1 %76, label %77, label %80
77: ; preds = %74
%78 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.6, i64 0, i64 0))
%79 = call i32 @exit(i32 1)
unreachable
80: ; preds = %74
%81 = load [256 x i8]*, [256 x i8]** %4
%82 = load i32, i32* %6
%83 = sext i32 %82 to i64
%84 = getelementptr inbounds [256 x i8], [256 x i8]* %81, i64 %83
%85 = getelementptr inbounds [256 x i8], [256 x i8]* %84, i64 0, i64 0
%86 = call i32 @strncpy(i8* %85, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.7, i64 0, i64 0), i32 1)
ret i32 0
}

LLVM IR (-O2):
%struct.dirent = type { i8* }
@stdout = global i32 0
@.str = [19 x i8] c"opendir %s failed\0A\00"
@.str.1 = [25 x i8] c"opendir %s successfully\0A\00"
@errno = global i64 0
@.str.2 = [6 x i8] c"%s/%s\00"
@.str.4 = [5 x i8] c"/sda\00"
@.str.5 = [7 x i8] c"/dev%s\00"
@.str.6 = [15 x i8] c"readdir failed\00"
@.str.7 = [1 x i8] zeroinitializer
define i32 @udisk_devlist_get(i8* %0, [256 x i8]* %1) {
%3 = alloca [255 x i8]
%4 = alloca [255 x i8]
%5 = getelementptr inbounds [255 x i8], [255 x i8]* %3, i64 0, i64 0
%6 = getelementptr inbounds [255 x i8], [255 x i8]* %4, i64 0, i64 0
%7 = tail call i32* @opendir(i8* %0)
%8 = icmp eq i32* %7, null
%9 = load i32, i32* @stdout
br i1 %8, label %10, label %13
10: ; preds = %2
%11 = tail call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %0)
%12 = tail call i32 @exit(i32 1)
unreachable
13: ; preds = %2
%14 = tail call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %0)
store i64 0, i64* @errno
br label %15
15: ; preds = %36, %13
%16 = phi i32 [ %40, %36 ], [ 0, %13 ]
br label %17
17: ; preds = %35, %15
br label %18
18: ; preds = %17, %21
%19 = call %struct.dirent* @readdir(i32* %7)
%20 = icmp eq %struct.dirent* %19, null
br i1 %20, label %41, label %21
21: ; preds = %18
%22 = getelementptr inbounds %struct.dirent, %struct.dirent* %19, i64 0, i32 0
%23 = load i8*, i8** %22
%24 = load i8, i8* %23
%25 = icmp eq i8 %24, 46
br i1 %25, label %18, label %26
26: ; preds = %21
%27 = call i32 (i8*, i32, i8*, i8*, ...) @snprintf(i8* %6, i32 254, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %0, i8* %23)
%28 = call i64 @readlink(i8* %6, i8* %5, i32 254)
%29 = getelementptr inbounds [255 x i8], [255 x i8]* %3, i64 0, i64 %28
store i8 0, i8* %29
%30 = call i8* @strrchr(i8* %5, i8 signext 47)
%31 = icmp eq i8* %30, null
br i1 %31, label %35, label %32
32: ; preds = %26
%33 = call i64 @strncmp(i8* %30, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i32 4)
%34 = icmp eq i64 %33, 0
br i1 %34, label %35, label %36
35: ; preds = %32, %26
br label %17
36: ; preds = %32
%37 = zext i32 %16 to i64
%38 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 %37, i64 0
%39 = call i32 (i8*, i32, i8*, i8*, ...) @snprintf(i8* %38, i32 255, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0), i8* %30)
%40 = add nuw nsw i32 %16, 1
br label %15
41: ; preds = %18
%42 = load i64, i64* @errno
%43 = icmp eq i64 %42, 0
br i1 %43, label %47, label %44
44: ; preds = %41
%45 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.6, i64 0, i64 0))
%46 = call i32 @exit(i32 1)
unreachable
47: ; preds = %41
%48 = zext i32 %16 to i64
%49 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 %48, i64 0
%50 = call i32 @strncpy(i8* %49, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.7, i64 0, i64 0), i32 1)
ret i32 0
}