#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 {
  int dummy;
};
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
char *append_if_directory(char *, char *, struct dirent *);
int check_directory(char *, char *);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int extract_busid(char *, char *);
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
int find_busid_in_sysfs(char *dev, char *busid) {
  DIR *blockdir, *diskdir;
  struct dirent *blockde, *diskde;
  int found = 0;
  char namebuffer[256];
  char *blockend, *diskend = NULL, *partend;
  strcpy(namebuffer, "/sys/block/");
  blockdir = opendir(namebuffer);
  if (!blockdir)
    return -1;
  blockend = namebuffer + strlen(namebuffer);
  while ((blockde = readdir(blockdir))) {
    diskend = append_if_directory(namebuffer, blockend, blockde);
    if (!diskend)
      continue;
    found = check_directory(namebuffer, dev);
    if (found)
      break;
    diskdir = opendir(namebuffer);
    if (!diskdir)
      continue;
    while ((diskde = readdir(diskdir))) {
      partend = append_if_directory(namebuffer, diskend, diskde);
      if (!partend)
        continue;
      found = check_directory(namebuffer, dev);
      if (found)
        break;
    }
    closedir(diskdir);
    if (found)
      break;
  }
  closedir(blockdir);
  if (found) {
    *diskend = 0;
    return extract_busid(namebuffer, busid);
  } else
    return -1;
}


x86 assembly (clang, -O0):
<find_busid_in_sysfs>:
push   %rbp
mov    %rsp,%rbp
sub    $0x160,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x0,-0x3c(%rbp)
movq   $0x0,-0x150(%rbp)
lea    -0x140(%rbp),%rdi
lea    0xe4d(%rip),%rsi
call   1030 <strcpy@plt>
lea    -0x140(%rbp),%rdi
call   1050 <opendir@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11df <find_busid_in_sysfs+0x5f>
movl   $0xffffffff,-0x4(%rbp)
jmp    1366 <find_busid_in_sysfs+0x1e6>
lea    -0x140(%rbp),%rax
mov    %rax,-0x160(%rbp)
lea    -0x140(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,%ecx
mov    -0x160(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x148(%rbp)
mov    -0x20(%rbp),%rdi
call   1090 <readdir@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
je     132a <find_busid_in_sysfs+0x1aa>
lea    -0x140(%rbp),%rdi
mov    -0x148(%rbp),%rsi
mov    -0x30(%rbp),%rdx
call   10a0 <append_if_directory@plt>
mov    %rax,-0x150(%rbp)
cmpq   $0x0,-0x150(%rbp)
jne    1257 <find_busid_in_sysfs+0xd7>
jmp    120f <find_busid_in_sysfs+0x8f>
lea    -0x140(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1070 <check_directory@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
je     1279 <find_busid_in_sysfs+0xf9>
jmp    132a <find_busid_in_sysfs+0x1aa>
lea    -0x140(%rbp),%rdi
call   1050 <opendir@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1299 <find_busid_in_sysfs+0x119>
jmp    120f <find_busid_in_sysfs+0x8f>
jmp    129e <find_busid_in_sysfs+0x11e>
mov    -0x28(%rbp),%rdi
call   1090 <readdir@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
je     130d <find_busid_in_sysfs+0x18d>
lea    -0x140(%rbp),%rdi
mov    -0x150(%rbp),%rsi
mov    -0x38(%rbp),%rdx
call   10a0 <append_if_directory@plt>
mov    %rax,-0x158(%rbp)
cmpq   $0x0,-0x158(%rbp)
jne    12e6 <find_busid_in_sysfs+0x166>
jmp    129e <find_busid_in_sysfs+0x11e>
lea    -0x140(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1070 <check_directory@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
je     1308 <find_busid_in_sysfs+0x188>
jmp    130d <find_busid_in_sysfs+0x18d>
jmp    129e <find_busid_in_sysfs+0x11e>
mov    -0x28(%rbp),%rdi
call   1080 <closedir@plt>
cmpl   $0x0,-0x3c(%rbp)
je     1325 <find_busid_in_sysfs+0x1a5>
jmp    132a <find_busid_in_sysfs+0x1aa>
jmp    120f <find_busid_in_sysfs+0x8f>
mov    -0x20(%rbp),%rdi
call   1080 <closedir@plt>
cmpl   $0x0,-0x3c(%rbp)
je     135f <find_busid_in_sysfs+0x1df>
mov    -0x150(%rbp),%rax
movb   $0x0,(%rax)
lea    -0x140(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <extract_busid@plt>
mov    %eax,-0x4(%rbp)
jmp    1366 <find_busid_in_sysfs+0x1e6>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x160,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<find_busid_in_sysfs>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x118,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
lea    0xe62(%rip),%rsi
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
call   1030 <strcpy@plt>
mov    %rbp,%rdi
call   1050 <opendir@plt>
mov    %rax,%r13
mov    $0xffffffff,%eax
test   %r13,%r13
je     12b1 <find_busid_in_sysfs+0x131>
mov    %r14,0x8(%rsp)
lea    0x10(%rsp),%rdi
call   1060 <strlen@plt>
mov    %eax,%ebp
mov    %r13,%rdi
call   1090 <readdir@plt>
test   %rax,%rax
je     12a4 <find_busid_in_sysfs+0x124>
movslq %ebp,%rcx
lea    (%rsp,%rcx,1),%r12
add    $0x10,%r12
lea    0x10(%rsp),%rbp
jmp    1219 <find_busid_in_sysfs+0x99>
nopl   0x0(%rax,%rax,1)
mov    %r15,%rdi
call   1080 <closedir@plt>
mov    %r13,%rdi
call   1090 <readdir@plt>
test   %rax,%rax
je     12a4 <find_busid_in_sysfs+0x124>
mov    %rbp,%rdi
mov    %r12,%rsi
mov    %rax,%rdx
call   10a0 <append_if_directory@plt>
test   %rax,%rax
je     1208 <find_busid_in_sysfs+0x88>
mov    %rax,%r14
mov    %rbp,%rdi
mov    %rbx,%rsi
call   1070 <check_directory@plt>
test   %eax,%eax
jne    1287 <find_busid_in_sysfs+0x107>
mov    %rbp,%rdi
call   1050 <opendir@plt>
test   %rax,%rax
je     1208 <find_busid_in_sysfs+0x88>
mov    %rax,%r15
xchg   %ax,%ax
mov    %r15,%rdi
call   1090 <readdir@plt>
test   %rax,%rax
je     1200 <find_busid_in_sysfs+0x80>
mov    %rbp,%rdi
mov    %r14,%rsi
mov    %rax,%rdx
call   10a0 <append_if_directory@plt>
test   %rax,%rax
je     1250 <find_busid_in_sysfs+0xd0>
mov    %rbp,%rdi
mov    %rbx,%rsi
call   1070 <check_directory@plt>
test   %eax,%eax
je     1250 <find_busid_in_sysfs+0xd0>
mov    %r15,%rdi
call   1080 <closedir@plt>
mov    %r13,%rdi
call   1080 <closedir@plt>
movb   $0x0,(%r14)
lea    0x10(%rsp),%rdi
mov    0x8(%rsp),%rsi
call   1040 <extract_busid@plt>
jmp    12b1 <find_busid_in_sysfs+0x131>
mov    %r13,%rdi
call   1080 <closedir@plt>
mov    $0xffffffff,%eax
add    $0x118,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i32 }
@.str = [12 x i8] c"/sys/block/\00"
define i32 @find_busid_in_sysfs(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca %struct.dirent*
%9 = alloca %struct.dirent*
%10 = alloca i32
%11 = alloca [256 x i8]
%12 = alloca i8*
%13 = alloca i8*
%14 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 0, i32* %10
store i8* null, i8** %13
%15 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 0
%16 = call i32 @strcpy(i8* %15, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%17 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 0
%18 = call i32* @opendir(i8* %17)
store i32* %18, i32** %6
%19 = load i32*, i32** %6
%20 = icmp ne i32* %19, null
br i1 %20, label %22, label %21
21: ; preds = %2
store i32 -1, i32* %3
br label %92
22: ; preds = %2
%23 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 0
%24 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 0
%25 = call i32 @strlen(i8* %24)
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds i8, i8* %23, i64 %26
store i8* %27, i8** %12
br label %28
28: ; preds = %80, %52, %39, %22
%29 = load i32*, i32** %6
%30 = call %struct.dirent* @readdir(i32* %29)
store %struct.dirent* %30, %struct.dirent** %8
%31 = icmp ne %struct.dirent* %30, null
br i1 %31, label %32, label %81
32: ; preds = %28
%33 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 0
%34 = load i8*, i8** %12
%35 = load %struct.dirent*, %struct.dirent** %8
%36 = call i8* @append_if_directory(i8* %33, i8* %34, %struct.dirent* %35)
store i8* %36, i8** %13
%37 = load i8*, i8** %13
%38 = icmp ne i8* %37, null
br i1 %38, label %40, label %39
39: ; preds = %32
br label %28
40: ; preds = %32
%41 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 0
%42 = load i8*, i8** %4
%43 = call i32 @check_directory(i8* %41, i8* %42)
store i32 %43, i32* %10
%44 = load i32, i32* %10
%45 = icmp ne i32 %44, 0
br i1 %45, label %46, label %47
46: ; preds = %40
br label %81
47: ; preds = %40
%48 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 0
%49 = call i32* @opendir(i8* %48)
store i32* %49, i32** %7
%50 = load i32*, i32** %7
%51 = icmp ne i32* %50, null
br i1 %51, label %53, label %52
52: ; preds = %47
br label %28
53: ; preds = %47
br label %54
54: ; preds = %73, %65, %53
%55 = load i32*, i32** %7
%56 = call %struct.dirent* @readdir(i32* %55)
store %struct.dirent* %56, %struct.dirent** %9
%57 = icmp ne %struct.dirent* %56, null
br i1 %57, label %58, label %74
58: ; preds = %54
%59 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 0
%60 = load i8*, i8** %13
%61 = load %struct.dirent*, %struct.dirent** %9
%62 = call i8* @append_if_directory(i8* %59, i8* %60, %struct.dirent* %61)
store i8* %62, i8** %14
%63 = load i8*, i8** %14
%64 = icmp ne i8* %63, null
br i1 %64, label %66, label %65
65: ; preds = %58
br label %54
66: ; preds = %58
%67 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 0
%68 = load i8*, i8** %4
%69 = call i32 @check_directory(i8* %67, i8* %68)
store i32 %69, i32* %10
%70 = load i32, i32* %10
%71 = icmp ne i32 %70, 0
br i1 %71, label %72, label %73
72: ; preds = %66
br label %74
73: ; preds = %66
br label %54
74: ; preds = %72, %54
%75 = load i32*, i32** %7
%76 = call i32 @closedir(i32* %75)
%77 = load i32, i32* %10
%78 = icmp ne i32 %77, 0
br i1 %78, label %79, label %80
79: ; preds = %74
br label %81
80: ; preds = %74
br label %28
81: ; preds = %79, %46, %28
%82 = load i32*, i32** %6
%83 = call i32 @closedir(i32* %82)
%84 = load i32, i32* %10
%85 = icmp ne i32 %84, 0
br i1 %85, label %86, label %91
86: ; preds = %81
%87 = load i8*, i8** %13
store i8 0, i8* %87
%88 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 0
%89 = load i8*, i8** %5
%90 = call i32 @extract_busid(i8* %88, i8* %89)
store i32 %90, i32* %3
br label %92
91: ; preds = %81
store i32 -1, i32* %3
br label %92
92: ; preds = %91, %86, %21
%93 = load i32, i32* %3
ret i32 %93
}

LLVM IR (-O2):
%struct.dirent = type { i32 }
@.str = [12 x i8] c"/sys/block/\00"
define i32 @find_busid_in_sysfs(i8* %0, i8* %1) {
%3 = alloca [256 x i8]
%4 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%5 = call i32 @strcpy(i8* %4, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%6 = call i32* @opendir(i8* %4)
%7 = icmp eq i32* %6, null
br i1 %7, label %49, label %8
8: ; preds = %2
%9 = call i32 @strlen(i8* %4)
%10 = sext i32 %9 to i64
%11 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 %10
%12 = call %struct.dirent* @readdir(i32* %6)
%13 = icmp eq %struct.dirent* %12, null
br i1 %13, label %44, label %14
14: ; preds = %8, %18
%15 = phi %struct.dirent* [ %19, %18 ], [ %12, %8 ]
%16 = call i8* @append_if_directory(i8* %4, i8* %11, %struct.dirent* %15)
%17 = icmp eq i8* %16, null
br i1 %17, label %18, label %21
18: ; preds = %14, %24, %42
%19 = call %struct.dirent* @readdir(i32* %6)
%20 = icmp eq %struct.dirent* %19, null
br i1 %20, label %44, label %14
21: ; preds = %14
%22 = call i32 @check_directory(i8* %4, i8* %0)
%23 = icmp eq i32 %22, 0
br i1 %23, label %24, label %46
24: ; preds = %21
%25 = call i32* @opendir(i8* %4)
%26 = icmp eq i32* %25, null
br i1 %26, label %18, label %27
27: ; preds = %24
%28 = call %struct.dirent* @readdir(i32* %25)
%29 = icmp eq %struct.dirent* %28, null
br i1 %29, label %42, label %30
30: ; preds = %27, %34
%31 = phi %struct.dirent* [ %35, %34 ], [ %28, %27 ]
%32 = call i8* @append_if_directory(i8* %4, i8* %16, %struct.dirent* %31)
%33 = icmp eq i8* %32, null
br i1 %33, label %34, label %37
34: ; preds = %30, %37
%35 = call %struct.dirent* @readdir(i32* %25)
%36 = icmp eq %struct.dirent* %35, null
br i1 %36, label %42, label %30
37: ; preds = %30
%38 = call i32 @check_directory(i8* %4, i8* %0)
%39 = icmp eq i32 %38, 0
br i1 %39, label %34, label %40
40: ; preds = %37
%41 = call i32 @closedir(i32* %25)
br label %46
42: ; preds = %34, %27
%43 = call i32 @closedir(i32* %25)
br label %18
44: ; preds = %18, %8
%45 = call i32 @closedir(i32* %6)
br label %49
46: ; preds = %21, %40
%47 = call i32 @closedir(i32* %6)
store i8 0, i8* %16
%48 = call i32 @extract_busid(i8* %4, i8* %1)
br label %49
49: ; preds = %44, %2, %46
%50 = phi i32 [ %48, %46 ], [ -1, %2 ], [ -1, %44 ]
ret i32 %50
}