#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*/ *);
scalar_t__ memcmp(char *, char *, int);
int /*<<< orphan*/ omb_log(int, char *, char *);
int /*<<< orphan*/ omb_utils_create_dir_tree();
scalar_t__ omb_utils_dir_exists(char *);
scalar_t__ omb_utils_mount(char *, char *);
int omb_utils_umount(char *);
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ sprintf(char *, char *, char *, char *);
int strlen(char *);
int omb_utils_find_and_mount() {
  struct dirent *dir;
  DIR *fd = opendir("/dev");
  if (fd) {
    omb_utils_create_dir_tree();
    while ((dir = readdir(fd)) != NULL) {
      if (strlen(dir->d_name) == 4 && memcmp(dir->d_name, "sd", 2) == 0) {
        char device[255];
        sprintf(device, "%s/%s", "/dev", dir->d_name);
        omb_log(2, "check device %s", device);
        omb_utils_umount("/omb");
        if (omb_utils_mount(device, "/omb") == 0) {
          char datadir[255];
          sprintf(datadir, "%s/%s", "/omb", "open-multiboot");
          if (omb_utils_dir_exists(datadir)) {
            omb_log(2, "found data on device %s", device);
            closedir(fd);
            return 0;
          }
          if (omb_utils_umount("/omb") == -1)
            omb_log(0, "cannot umount %s", "/omb");
        }
      }
    }
    closedir(fd);
  }
  return -1;
}


x86 assembly (clang, -O0):
<omb_utils_find_and_mount>:
push   %rbp
mov    %rsp,%rbp
sub    $0x220,%rsp
lea    0xe3e(%rip),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1333 <omb_utils_find_and_mount+0x183>
mov    $0x0,%al
call   1080 <omb_utils_create_dir_tree@plt>
mov    -0x18(%rbp),%rdi
call   10b0 <readdir@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
je     132a <omb_utils_find_and_mount+0x17a>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1050 <strlen@plt>
cmp    $0x4,%eax
jne    1325 <omb_utils_find_and_mount+0x175>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
lea    0xdee(%rip),%rsi
mov    $0x2,%edx
call   10a0 <memcmp@plt>
cmp    $0x0,%rax
jne    1325 <omb_utils_find_and_mount+0x175>
lea    -0x120(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    (%rax),%rcx
lea    0xdc8(%rip),%rsi
lea    0xdb9(%rip),%rdx
call   10c0 <sprintf@plt>
lea    -0x120(%rbp),%rdx
mov    $0x2,%edi
lea    0xdaf(%rip),%rsi
call   1070 <omb_log@plt>
lea    0xdb3(%rip),%rdi
call   1030 <omb_utils_umount@plt>
lea    -0x120(%rbp),%rdi
lea    0xda0(%rip),%rsi
call   1060 <omb_utils_mount@plt>
cmp    $0x0,%rax
jne    1320 <omb_utils_find_and_mount+0x170>
lea    -0x220(%rbp),%rdi
lea    0xd6d(%rip),%rsi
lea    0xd7c(%rip),%rdx
lea    0xd7a(%rip),%rcx
call   10c0 <sprintf@plt>
lea    -0x220(%rbp),%rdi
call   10d0 <omb_utils_dir_exists@plt>
cmp    $0x0,%rax
je     12f1 <omb_utils_find_and_mount+0x141>
lea    -0x120(%rbp),%rdx
mov    $0x2,%edi
lea    0xd5b(%rip),%rsi
call   1070 <omb_log@plt>
mov    -0x18(%rbp),%rdi
call   1090 <closedir@plt>
movl   $0x0,-0x4(%rbp)
jmp    133a <omb_utils_find_and_mount+0x18a>
lea    0xd26(%rip),%rdi
call   1030 <omb_utils_umount@plt>
cmp    $0xffffffff,%eax
jne    131b <omb_utils_find_and_mount+0x16b>
xor    %edi,%edi
lea    0xd3b(%rip),%rsi
lea    0xd08(%rip),%rdx
call   1070 <omb_log@plt>
jmp    1320 <omb_utils_find_and_mount+0x170>
jmp    1325 <omb_utils_find_and_mount+0x175>
jmp    11dd <omb_utils_find_and_mount+0x2d>
mov    -0x18(%rbp),%rdi
call   1090 <closedir@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x220,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<omb_utils_find_and_mount>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x208,%rsp
lea    0xe38(%rip),%rdi
call   1040 <opendir@plt>
mov    %rax,%rbx
mov    $0xffffffff,%eax
test   %rbx,%rbx
je     12f5 <omb_utils_find_and_mount+0x145>
xor    %eax,%eax
call   1080 <omb_utils_create_dir_tree@plt>
mov    %rbx,%rdi
call   10b0 <readdir@plt>
test   %rax,%rax
je     12e8 <omb_utils_find_and_mount+0x138>
mov    %rax,%r14
lea    0xe05(%rip),%r13
lea    0xe01(%rip),%rbp
mov    %rsp,%r15
lea    0xe0d(%rip),%r12
jmp    1234 <omb_utils_find_and_mount+0x84>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %rbx,%rdi
call   10b0 <readdir@plt>
mov    %rax,%r14
test   %rax,%rax
je     12e8 <omb_utils_find_and_mount+0x138>
mov    (%r14),%rdi
call   1050 <strlen@plt>
cmp    $0x4,%eax
jne    1220 <omb_utils_find_and_mount+0x70>
mov    (%r14),%rdi
mov    %r13,%rsi
mov    $0x2,%edx
call   10a0 <memcmp@plt>
test   %rax,%rax
jne    1220 <omb_utils_find_and_mount+0x70>
mov    (%r14),%rcx
mov    %r15,%rdi
mov    %rbp,%rsi
lea    0xd9a(%rip),%rdx
call   10c0 <sprintf@plt>
mov    $0x2,%edi
lea    0xd97(%rip),%rsi
mov    %r15,%rdx
call   1070 <omb_log@plt>
mov    %r12,%rdi
call   1030 <omb_utils_umount@plt>
mov    %r15,%rdi
mov    %r12,%rsi
call   1060 <omb_utils_mount@plt>
test   %rax,%rax
jne    1220 <omb_utils_find_and_mount+0x70>
mov    %rbp,%rsi
lea    0x100(%rsp),%r14
mov    %r14,%rdi
mov    %r12,%rdx
lea    0xd74(%rip),%rcx
call   10c0 <sprintf@plt>
mov    %r14,%rdi
call   10d0 <omb_utils_dir_exists@plt>
test   %rax,%rax
jne    1307 <omb_utils_find_and_mount+0x157>
mov    %r12,%rdi
call   1030 <omb_utils_umount@plt>
cmp    $0xffffffff,%eax
jne    1220 <omb_utils_find_and_mount+0x70>
xor    %edi,%edi
lea    0xd6f(%rip),%rsi
mov    %r12,%rdx
call   1070 <omb_log@plt>
jmp    1220 <omb_utils_find_and_mount+0x70>
mov    %rbx,%rdi
call   1090 <closedir@plt>
mov    $0xffffffff,%eax
add    $0x208,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd24(%rip),%rsi
mov    %rsp,%rdx
mov    $0x2,%edi
call   1070 <omb_log@plt>
mov    %rbx,%rdi
call   1090 <closedir@plt>
xor    %eax,%eax
jmp    12f5 <omb_utils_find_and_mount+0x145>

LLVM IR (-O0):
%struct.dirent = type { i8* }
@.str = [5 x i8] c"/dev\00"
@.str.1 = [3 x i8] c"sd\00"
@.str.2 = [6 x i8] c"%s/%s\00"
@.str.3 = [16 x i8] c"check device %s\00"
@.str.4 = [5 x i8] c"/omb\00"
@.str.5 = [15 x i8] c"open-multiboot\00"
@.str.6 = [24 x i8] c"found data on device %s\00"
@.str.7 = [17 x i8] c"cannot umount %s\00"
define i32 @omb_utils_find_and_mount() {
%1 = alloca i32
%2 = alloca %struct.dirent*
%3 = alloca i32*
%4 = alloca [255 x i8]
%5 = alloca [255 x i8]
%6 = call i32* @opendir(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i32* %6, i32** %3
%7 = load i32*, i32** %3
%8 = icmp ne i32* %7, null
br i1 %8, label %9, label %61
9: ; preds = %0
%10 = call i32 (...) @omb_utils_create_dir_tree()
br label %11
11: ; preds = %57, %9
%12 = load i32*, i32** %3
%13 = call %struct.dirent* @readdir(i32* %12)
store %struct.dirent* %13, %struct.dirent** %2
%14 = icmp ne %struct.dirent* %13, null
br i1 %14, label %15, label %58
15: ; preds = %11
%16 = load %struct.dirent*, %struct.dirent** %2
%17 = getelementptr inbounds %struct.dirent, %struct.dirent* %16, i32 0, i32 0
%18 = load i8*, i8** %17
%19 = call i32 @strlen(i8* %18)
%20 = icmp eq i32 %19, 4
br i1 %20, label %21, label %57
21: ; preds = %15
%22 = load %struct.dirent*, %struct.dirent** %2
%23 = getelementptr inbounds %struct.dirent, %struct.dirent* %22, i32 0, i32 0
%24 = load i8*, i8** %23
%25 = call i64 @memcmp(i8* %24, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 2)
%26 = icmp eq i64 %25, 0
br i1 %26, label %27, label %57
27: ; preds = %21
%28 = getelementptr inbounds [255 x i8], [255 x i8]* %4, i64 0, i64 0
%29 = load %struct.dirent*, %struct.dirent** %2
%30 = getelementptr inbounds %struct.dirent, %struct.dirent* %29, i32 0, i32 0
%31 = load i8*, i8** %30
%32 = call i32 @sprintf(i8* %28, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %31)
%33 = getelementptr inbounds [255 x i8], [255 x i8]* %4, i64 0, i64 0
%34 = call i32 @omb_log(i32 2, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i8* %33)
%35 = call i32 @omb_utils_umount(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
%36 = getelementptr inbounds [255 x i8], [255 x i8]* %4, i64 0, i64 0
%37 = call i64 @omb_utils_mount(i8* %36, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
%38 = icmp eq i64 %37, 0
br i1 %38, label %39, label %56
39: ; preds = %27
%40 = getelementptr inbounds [255 x i8], [255 x i8]* %5, i64 0, i64 0
%41 = call i32 @sprintf(i8* %40, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0))
%42 = getelementptr inbounds [255 x i8], [255 x i8]* %5, i64 0, i64 0
%43 = call i64 @omb_utils_dir_exists(i8* %42)
%44 = icmp ne i64 %43, 0
br i1 %44, label %45, label %50
45: ; preds = %39
%46 = getelementptr inbounds [255 x i8], [255 x i8]* %4, i64 0, i64 0
%47 = call i32 @omb_log(i32 2, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.6, i64 0, i64 0), i8* %46)
%48 = load i32*, i32** %3
%49 = call i32 @closedir(i32* %48)
store i32 0, i32* %1
br label %62
50: ; preds = %39
%51 = call i32 @omb_utils_umount(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
%52 = icmp eq i32 %51, -1
br i1 %52, label %53, label %55
53: ; preds = %50
%54 = call i32 @omb_log(i32 0, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.7, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
br label %55
55: ; preds = %53, %50
br label %56
56: ; preds = %55, %27
br label %57
57: ; preds = %56, %21, %15
br label %11
58: ; preds = %11
%59 = load i32*, i32** %3
%60 = call i32 @closedir(i32* %59)
br label %61
61: ; preds = %58, %0
store i32 -1, i32* %1
br label %62
62: ; preds = %61, %45
%63 = load i32, i32* %1
ret i32 %63
}

LLVM IR (-O2):
%struct.dirent = type { i8* }
@.str = [5 x i8] c"/dev\00"
@.str.1 = [3 x i8] c"sd\00"
@.str.2 = [6 x i8] c"%s/%s\00"
@.str.3 = [16 x i8] c"check device %s\00"
@.str.4 = [5 x i8] c"/omb\00"
@.str.5 = [15 x i8] c"open-multiboot\00"
@.str.6 = [24 x i8] c"found data on device %s\00"
@.str.7 = [17 x i8] c"cannot umount %s\00"
define i32 @omb_utils_find_and_mount() {
%1 = alloca [255 x i8]
%2 = alloca [255 x i8]
%3 = tail call i32* @opendir(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %48, label %5
5: ; preds = %0
%6 = tail call i32 (...) @omb_utils_create_dir_tree()
%7 = call %struct.dirent* @readdir(i32* %3)
%8 = icmp eq %struct.dirent* %7, null
br i1 %8, label %43, label %9
9: ; preds = %5
%10 = getelementptr inbounds [255 x i8], [255 x i8]* %1, i64 0, i64 0
%11 = getelementptr inbounds [255 x i8], [255 x i8]* %2, i64 0, i64 0
br label %12
12: ; preds = %9, %40
%13 = phi %struct.dirent* [ %7, %9 ], [ %41, %40 ]
%14 = getelementptr inbounds %struct.dirent, %struct.dirent* %13, i64 0, i32 0
%15 = load i8*, i8** %14
%16 = call i32 @strlen(i8* %15)
%17 = icmp eq i32 %16, 4
br i1 %17, label %18, label %40
18: ; preds = %12
%19 = load i8*, i8** %14
%20 = call i64 @memcmp(i8* %19, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 2)
%21 = icmp eq i64 %20, 0
br i1 %21, label %22, label %40
22: ; preds = %18
%23 = load i8*, i8** %14
%24 = call i32 @sprintf(i8* %10, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %23)
%25 = call i32 @omb_log(i32 2, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i8* %10)
%26 = call i32 @omb_utils_umount(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
%27 = call i64 @omb_utils_mount(i8* %10, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
%28 = icmp eq i64 %27, 0
br i1 %28, label %29, label %39
29: ; preds = %22
%30 = call i32 @sprintf(i8* %11, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0))
%31 = call i64 @omb_utils_dir_exists(i8* %11)
%32 = icmp eq i64 %31, 0
br i1 %32, label %33, label %45
33: ; preds = %29
%34 = call i32 @omb_utils_umount(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %38
36: ; preds = %33
%37 = call i32 @omb_log(i32 0, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.7, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
br label %38
38: ; preds = %33, %36
br label %39
39: ; preds = %38, %22
br label %40
40: ; preds = %39, %18, %12
%41 = call %struct.dirent* @readdir(i32* %3)
%42 = icmp eq %struct.dirent* %41, null
br i1 %42, label %43, label %12
43: ; preds = %40, %5
%44 = call i32 @closedir(i32* %3)
br label %48
45: ; preds = %29
%46 = call i32 @omb_log(i32 2, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.6, i64 0, i64 0), i8* %10)
%47 = call i32 @closedir(i32* %3)
br label %48
48: ; preds = %0, %43, %45
%49 = phi i32 [ 0, %45 ], [ -1, %43 ], [ -1, %0 ]
ret i32 %49
}