#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 */
scalar_t__ CheckPrefix(char *);
int PATH_MAX;
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ sprintf(char *, char *, char *);
int stub1(char const *, void *);
int SerialFind(int (*check)(const char *port, void *data), void *data) {
  char path[PATH_MAX];
  struct dirent *entry;
  DIR *dirp;
  if (!(dirp = opendir("/dev")))
    return -1;
  while ((entry = readdir(dirp)) != NULL) {
    if (CheckPrefix(entry->d_name)) {
      sprintf(path, "/dev/%s", entry->d_name);
      if ((*check)(path, data) == 0) {
        closedir(dirp);
        return 0;
      }
    }
  }
  closedir(dirp);
  return -1;
}


x86 assembly (clang, -O0):
<SerialFind>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2e89(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x48(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x28(%rbp)
lea    0xe6d(%rip),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
jne    11b9 <SerialFind+0x69>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x3c(%rbp)
jmp    1256 <SerialFind+0x106>
jmp    11be <SerialFind+0x6e>
mov    -0x38(%rbp),%rdi
call   1060 <readdir@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
je     123f <SerialFind+0xef>
mov    -0x30(%rbp),%rax
mov    (%rax),%rdi
call   1030 <CheckPrefix@plt>
cmp    $0x0,%rax
je     123a <SerialFind+0xea>
mov    -0x48(%rbp),%rdi
mov    -0x30(%rbp),%rax
mov    (%rax),%rdx
lea    0xe08(%rip),%rsi
call   1070 <sprintf@plt>
mov    -0x48(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%rsi
call   *%rax
cmp    $0x0,%eax
jne    1235 <SerialFind+0xe5>
mov    -0x38(%rbp),%rdi
call   1050 <closedir@plt>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x3c(%rbp)
jmp    1256 <SerialFind+0x106>
jmp    123a <SerialFind+0xea>
jmp    11be <SerialFind+0x6e>
mov    -0x38(%rbp),%rdi
call   1050 <closedir@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x3c(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<SerialFind>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r15
mov    0x2e85(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
lea    0xe7b(%rip),%rdi
call   1040 <opendir@plt>
test   %rax,%rax
je     1201 <SerialFind+0xb1>
mov    %rax,%r13
mov    %rax,%rdi
call   1060 <readdir@plt>
test   %rax,%rax
je     11f2 <SerialFind+0xa2>
mov    %rax,%r12
jmp    11c0 <SerialFind+0x70>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r13,%rdi
call   1060 <readdir@plt>
mov    %rax,%r12
test   %rax,%rax
je     11f2 <SerialFind+0xa2>
mov    (%r12),%rdi
call   1030 <CheckPrefix@plt>
test   %rax,%rax
je     11b0 <SerialFind+0x60>
mov    (%r12),%rdx
mov    %rbx,%rdi
lea    0xe29(%rip),%rsi
call   1070 <sprintf@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   *%r15
test   %eax,%eax
jne    11b0 <SerialFind+0x60>
xor    %ebx,%ebx
jmp    11f7 <SerialFind+0xa7>
mov    $0xffffffff,%ebx
mov    %r13,%rdi
call   1050 <closedir@plt>
jmp    1206 <SerialFind+0xb6>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i8* }
@PATH_MAX = global i32 0
@.str = [5 x i8] c"/dev\00"
@.str.1 = [8 x i8] c"/dev/%s\00"
define i32 @SerialFind(i32 (i8*, i8*)* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32 (i8*, i8*)*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca %struct.dirent*
%9 = alloca i32*
%10 = alloca i32
store i32 (i8*, i8*)* %0, i32 (i8*, i8*)** %4
store i8* %1, i8** %5
%11 = load i32, i32* @PATH_MAX
%12 = zext i32 %11 to i64
%13 = call i8* @llvm.stacksave()
store i8* %13, i8** %6
%14 = alloca i8, i64 %12
store i64 %12, i64* %7
%15 = call i32* @opendir(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i32* %15, i32** %9
%16 = icmp ne i32* %15, null
br i1 %16, label %18, label %17
17: ; preds = %2
store i32 -1, i32* %3
store i32 1, i32* %10
br label %46
18: ; preds = %2
br label %19
19: ; preds = %42, %18
%20 = load i32*, i32** %9
%21 = call %struct.dirent* @readdir(i32* %20)
store %struct.dirent* %21, %struct.dirent** %8
%22 = icmp ne %struct.dirent* %21, null
br i1 %22, label %23, label %43
23: ; preds = %19
%24 = load %struct.dirent*, %struct.dirent** %8
%25 = getelementptr inbounds %struct.dirent, %struct.dirent* %24, i32 0, i32 0
%26 = load i8*, i8** %25
%27 = call i64 @CheckPrefix(i8* %26)
%28 = icmp ne i64 %27, 0
br i1 %28, label %29, label %42
29: ; preds = %23
%30 = load %struct.dirent*, %struct.dirent** %8
%31 = getelementptr inbounds %struct.dirent, %struct.dirent* %30, i32 0, i32 0
%32 = load i8*, i8** %31
%33 = call i32 @sprintf(i8* %14, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* %32)
%34 = load i32 (i8*, i8*)*, i32 (i8*, i8*)** %4
%35 = load i8*, i8** %5
%36 = call i32 %34(i8* %14, i8* %35)
%37 = icmp eq i32 %36, 0
br i1 %37, label %38, label %41
38: ; preds = %29
%39 = load i32*, i32** %9
%40 = call i32 @closedir(i32* %39)
store i32 0, i32* %3
store i32 1, i32* %10
br label %46
41: ; preds = %29
br label %42
42: ; preds = %41, %23
br label %19
43: ; preds = %19
%44 = load i32*, i32** %9
%45 = call i32 @closedir(i32* %44)
store i32 -1, i32* %3
store i32 1, i32* %10
br label %46
46: ; preds = %43, %38, %17
%47 = load i8*, i8** %6
%48 = load i32, i32* %3
ret i32 %48
}

LLVM IR (-O2):
%struct.dirent = type { i8* }
@PATH_MAX = global i32 0
@.str = [5 x i8] c"/dev\00"
@.str.1 = [8 x i8] c"/dev/%s\00"
define i32 @SerialFind(i32 (i8*, i8*)* nocapture %0, i8* %1) {
%3 = load i32, i32* @PATH_MAX
%4 = zext i32 %3 to i64
%5 = alloca i8, i64 %4
%6 = tail call i32* @opendir(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%7 = icmp eq i32* %6, null
br i1 %7, label %28, label %8
8: ; preds = %2
%9 = call %struct.dirent* @readdir(i32* %6)
%10 = icmp eq %struct.dirent* %9, null
br i1 %10, label %25, label %11
11: ; preds = %8, %22
%12 = phi %struct.dirent* [ %23, %22 ], [ %9, %8 ]
%13 = getelementptr inbounds %struct.dirent, %struct.dirent* %12, i64 0, i32 0
%14 = load i8*, i8** %13
%15 = call i64 @CheckPrefix(i8* %14)
%16 = icmp eq i64 %15, 0
br i1 %16, label %22, label %17
17: ; preds = %11
%18 = load i8*, i8** %13
%19 = call i32 @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* %18)
%20 = call i32 %0(i8* %5, i8* %1)
%21 = icmp eq i32 %20, 0
br i1 %21, label %25, label %22
22: ; preds = %17, %11
%23 = call %struct.dirent* @readdir(i32* %6)
%24 = icmp eq %struct.dirent* %23, null
br i1 %24, label %25, label %11
25: ; preds = %22, %17, %8
%26 = phi i32 [ -1, %8 ], [ 0, %17 ], [ -1, %22 ]
%27 = call i32 @closedir(i32* %6)
br label %28
28: ; preds = %25, %2
%29 = phi i32 [ -1, %2 ], [ %26, %25 ]
ret i32 %29
}