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


x86 assembly (clang, -O0):
<serial_find>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    0x2e75(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x28(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x58(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x34(%rbp)
lea    0xe4d(%rip),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x48(%rbp)
cmp    $0x0,%rax
jne    11d9 <serial_find+0x79>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x4c(%rbp)
jmp    127d <serial_find+0x11d>
jmp    11de <serial_find+0x7e>
mov    -0x48(%rbp),%rdi
call   1070 <readdir@plt>
mov    %rax,-0x40(%rbp)
cmp    $0x0,%rax
je     1266 <serial_find+0x106>
mov    -0x40(%rbp),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x34(%rbp),%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    1261 <serial_find+0x101>
mov    -0x58(%rbp),%rdi
mov    -0x40(%rbp),%rax
mov    (%rax),%rdx
lea    0xde1(%rip),%rsi
call   1080 <sprintf@plt>
mov    -0x58(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    -0x20(%rbp),%rsi
call   *%rax
cmp    $0x0,%eax
jne    125c <serial_find+0xfc>
mov    -0x48(%rbp),%rdi
call   1060 <closedir@plt>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x4c(%rbp)
jmp    127d <serial_find+0x11d>
jmp    1261 <serial_find+0x101>
jmp    11de <serial_find+0x7e>
mov    -0x48(%rbp),%rdi
call   1060 <closedir@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x4c(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<serial_find>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,-0x38(%rbp)
mov    %rsi,-0x30(%rbp)
mov    %rdi,%r12
mov    0x2e6d(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%r13
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r13
mov    %r13,%rsp
call   1050 <strlen@plt>
mov    %eax,%r14d
lea    0xe5b(%rip),%rdi
call   1040 <opendir@plt>
test   %rax,%rax
je     1228 <serial_find+0xc8>
mov    %rax,%rbx
mov    %rax,%rdi
call   1070 <readdir@plt>
test   %rax,%rax
je     1218 <serial_find+0xb8>
mov    %rax,%r15
jmp    11e0 <serial_find+0x80>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %rbx,%rdi
call   1070 <readdir@plt>
mov    %rax,%r15
test   %rax,%rax
je     1218 <serial_find+0xb8>
mov    (%r15),%rdi
mov    %r12,%rsi
mov    %r14d,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
jne    11d0 <serial_find+0x70>
mov    (%r15),%rdx
mov    %r13,%rdi
lea    0xe05(%rip),%rsi
call   1080 <sprintf@plt>
mov    %r13,%rdi
mov    -0x38(%rbp),%rsi
call   *-0x30(%rbp)
test   %eax,%eax
jne    11d0 <serial_find+0x70>
xor    %r14d,%r14d
jmp    121e <serial_find+0xbe>
mov    $0xffffffff,%r14d
mov    %rbx,%rdi
call   1060 <closedir@plt>
jmp    122e <serial_find+0xce>
mov    $0xffffffff,%r14d
mov    %r14d,%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 @serial_find(i8* %0, i32 (i8*, i8*)* %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32 (i8*, i8*)*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i32
%11 = alloca %struct.dirent*
%12 = alloca i32*
%13 = alloca i32
store i8* %0, i8** %5
store i32 (i8*, i8*)* %1, i32 (i8*, i8*)** %6
store i8* %2, i8** %7
%14 = load i32, i32* @PATH_MAX
%15 = zext i32 %14 to i64
%16 = call i8* @llvm.stacksave()
store i8* %16, i8** %8
%17 = alloca i8, i64 %15
store i64 %15, i64* %9
%18 = load i8*, i8** %5
%19 = call i32 @strlen(i8* %18)
store i32 %19, i32* %10
%20 = call i32* @opendir(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i32* %20, i32** %12
%21 = icmp ne i32* %20, null
br i1 %21, label %23, label %22
22: ; preds = %3
store i32 -1, i32* %4
store i32 1, i32* %13
br label %53
23: ; preds = %3
br label %24
24: ; preds = %49, %23
%25 = load i32*, i32** %12
%26 = call %struct.dirent* @readdir(i32* %25)
store %struct.dirent* %26, %struct.dirent** %11
%27 = icmp ne %struct.dirent* %26, null
br i1 %27, label %28, label %50
28: ; preds = %24
%29 = load %struct.dirent*, %struct.dirent** %11
%30 = getelementptr inbounds %struct.dirent, %struct.dirent* %29, i32 0, i32 0
%31 = load i8*, i8** %30
%32 = load i8*, i8** %5
%33 = load i32, i32* %10
%34 = call i64 @strncmp(i8* %31, i8* %32, i32 %33)
%35 = icmp eq i64 %34, 0
br i1 %35, label %36, label %49
36: ; preds = %28
%37 = load %struct.dirent*, %struct.dirent** %11
%38 = getelementptr inbounds %struct.dirent, %struct.dirent* %37, i32 0, i32 0
%39 = load i8*, i8** %38
%40 = call i32 @sprintf(i8* %17, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* %39)
%41 = load i32 (i8*, i8*)*, i32 (i8*, i8*)** %6
%42 = load i8*, i8** %7
%43 = call i32 %41(i8* %17, i8* %42)
%44 = icmp eq i32 %43, 0
br i1 %44, label %45, label %48
45: ; preds = %36
%46 = load i32*, i32** %12
%47 = call i32 @closedir(i32* %46)
store i32 0, i32* %4
store i32 1, i32* %13
br label %53
48: ; preds = %36
br label %49
49: ; preds = %48, %28
br label %24
50: ; preds = %24
%51 = load i32*, i32** %12
%52 = call i32 @closedir(i32* %51)
store i32 -1, i32* %4
store i32 1, i32* %13
br label %53
53: ; preds = %50, %45, %22
%54 = load i8*, i8** %8
%55 = load i32, i32* %4
ret i32 %55
}

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 @serial_find(i8* %0, i32 (i8*, i8*)* nocapture %1, i8* %2) {
%4 = load i32, i32* @PATH_MAX
%5 = zext i32 %4 to i64
%6 = alloca i8, i64 %5
%7 = tail call i32 @strlen(i8* %0)
%8 = tail call i32* @opendir(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%9 = icmp eq i32* %8, null
br i1 %9, label %30, label %10
10: ; preds = %3
%11 = call %struct.dirent* @readdir(i32* %8)
%12 = icmp eq %struct.dirent* %11, null
br i1 %12, label %27, label %13
13: ; preds = %10, %24
%14 = phi %struct.dirent* [ %25, %24 ], [ %11, %10 ]
%15 = getelementptr inbounds %struct.dirent, %struct.dirent* %14, i64 0, i32 0
%16 = load i8*, i8** %15
%17 = call i64 @strncmp(i8* %16, i8* %0, i32 %7)
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %24
19: ; preds = %13
%20 = load i8*, i8** %15
%21 = call i32 @sprintf(i8* %6, i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* %20)
%22 = call i32 %1(i8* %6, i8* %2)
%23 = icmp eq i32 %22, 0
br i1 %23, label %27, label %24
24: ; preds = %19, %13
%25 = call %struct.dirent* @readdir(i32* %8)
%26 = icmp eq %struct.dirent* %25, null
br i1 %26, label %27, label %13
27: ; preds = %24, %19, %10
%28 = phi i32 [ -1, %10 ], [ 0, %19 ], [ -1, %24 ]
%29 = call i32 @closedir(i32* %8)
br label %30
30: ; preds = %27, %3
%31 = phi i32 [ -1, %3 ], [ %28, %27 ]
ret i32 %31
}