#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 stat {
  int /*<<< orphan*/ st_rdev;
  int /*<<< orphan*/ st_mode;
};
struct dirent {
  char *d_name;
};
typedef int /*<<< orphan*/ filename;
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
int /*<<< orphan*/ LOG_CRIT;
int /*<<< orphan*/ S_ISCHR(int /*<<< orphan*/);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int major(int /*<<< orphan*/);
int minor(int /*<<< orphan*/);
int /*<<< orphan*/ my_syslog(int /*<<< orphan*/, char *);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ printf(char *, char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ snprintf(char *, int, char *, char *, char *);
int stat(char *, struct stat *);
int /*<<< orphan*/ strcpy(char *, char *);
const char *find_mptctl_device(void) {
  DIR *dir;
  struct dirent *dirent;
  static char matching_filename[256];
  int count = 0;
  matching_filename[0] = 0;
  dir = opendir("/dev");
  if (!dir)
    return NULL;
  while ((dirent = readdir(dir)) != NULL) {
    char filename[512];
    struct stat stbuf;
    snprintf(filename, sizeof(filename), "%s/%s", "/dev", dirent->d_name);
    int ret = stat(filename, &stbuf);
    if (ret < 0)
      continue;
    if (!S_ISCHR(stbuf.st_mode))
      continue;
    if (major(stbuf.st_rdev) == 10 &&
        (minor(stbuf.st_rdev) == 221 || minor(stbuf.st_rdev) == 222)) {
      printf("Found control device: %s\n", filename);
      count++;
      strcpy(matching_filename, filename);
    }
  }
  closedir(dir);
  if (count > 1) {
    my_syslog(LOG_CRIT,
              "More than one control file found, cannot auto-select one!");
    matching_filename[0] = 0;
  }
  return matching_filename[0] ? matching_filename : NULL;
}


x86 assembly (clang, -O0):
<find_mptctl_device>:
push   %rbp
mov    %rsp,%rbp
sub    $0x230,%rsp
movl   $0x0,-0x1c(%rbp)
movb   $0x0,0x2ec7(%rip)
lea    0xe30(%rip),%rdi
call   1050 <opendir@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11f1 <find_mptctl_device+0x41>
movq   $0x0,-0x8(%rbp)
jmp    1335 <find_mptctl_device+0x185>
jmp    11f6 <find_mptctl_device+0x46>
mov    -0x10(%rbp),%rdi
call   10c0 <readdir@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     12eb <find_mptctl_device+0x13b>
lea    -0x220(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%r8
mov    $0x200,%esi
lea    0xdde(%rip),%rdx
lea    0xdd2(%rip),%rcx
call   1080 <snprintf@plt>
lea    -0x220(%rbp),%rdi
lea    -0x228(%rbp),%rsi
call   10b0 <stat@plt>
mov    %eax,-0x22c(%rbp)
cmpl   $0x0,-0x22c(%rbp)
jge    125e <find_mptctl_device+0xae>
jmp    11f6 <find_mptctl_device+0x46>
mov    -0x224(%rbp),%edi
call   10d0 <S_ISCHR@plt>
cmp    $0x0,%eax
jne    1277 <find_mptctl_device+0xc7>
jmp    11f6 <find_mptctl_device+0x46>
mov    -0x228(%rbp),%edi
call   1090 <major@plt>
cmp    $0xa,%eax
jne    12e6 <find_mptctl_device+0x136>
mov    -0x228(%rbp),%edi
call   1060 <minor@plt>
cmp    $0xdd,%eax
je     12b7 <find_mptctl_device+0x107>
mov    -0x228(%rbp),%edi
call   1060 <minor@plt>
cmp    $0xde,%eax
jne    12e6 <find_mptctl_device+0x136>
lea    -0x220(%rbp),%rsi
lea    0xd46(%rip),%rdi
call   1070 <printf@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
lea    -0x220(%rbp),%rsi
lea    0x2daf(%rip),%rdi
call   1040 <strcpy@plt>
jmp    11f6 <find_mptctl_device+0x46>
mov    -0x10(%rbp),%rdi
call   10a0 <closedir@plt>
cmpl   $0x1,-0x1c(%rbp)
jle    131a <find_mptctl_device+0x16a>
mov    0x2ce3(%rip),%rax
mov    (%rax),%edi
lea    0xd17(%rip),%rsi
call   1030 <my_syslog@plt>
movb   $0x0,0x2d76(%rip)
movsbl 0x2d6f(%rip),%edx
xor    %eax,%eax
lea    0x2d66(%rip),%rcx
cmp    $0x0,%edx
cmovne %rcx,%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x230,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<find_mptctl_device>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x218,%rsp
movb   $0x0,0x2ec8(%rip)
lea    0xe31(%rip),%rdi
call   1050 <opendir@plt>
test   %rax,%rax
je     12e0 <find_mptctl_device+0x130>
mov    %rax,%r12
mov    %rax,%rdi
call   10c0 <readdir@plt>
test   %rax,%rax
je     12e4 <find_mptctl_device+0x134>
movl   $0x0,0x4(%rsp)
lea    0xe05(%rip),%r14
lea    0xdf9(%rip),%r15
lea    0x10(%rsp),%rbx
lea    0x8(%rsp),%r13
jmp    124d <find_mptctl_device+0x9d>
lea    0xdf1(%rip),%rdi
mov    %rbx,%rsi
call   1070 <printf@plt>
addl   $0x1,0x4(%rsp)
lea    0x2e62(%rip),%rdi
mov    %rbx,%rsi
call   1040 <strcpy@plt>
cs nopw 0x0(%rax,%rax,1)
mov    %r12,%rdi
call   10c0 <readdir@plt>
test   %rax,%rax
je     12b5 <find_mptctl_device+0x105>
mov    (%rax),%r8
mov    %rbx,%rdi
mov    $0x200,%esi
mov    %r14,%rdx
mov    %r15,%rcx
call   1080 <snprintf@plt>
mov    %rbx,%rdi
mov    %r13,%rsi
call   10b0 <stat@plt>
test   %eax,%eax
js     1240 <find_mptctl_device+0x90>
mov    0xc(%rsp),%edi
call   10d0 <S_ISCHR@plt>
test   %eax,%eax
je     1240 <find_mptctl_device+0x90>
mov    0x8(%rsp),%ebp
mov    %ebp,%edi
call   1090 <major@plt>
cmp    $0xa,%eax
jne    1240 <find_mptctl_device+0x90>
mov    %ebp,%edi
call   1060 <minor@plt>
cmp    $0xdd,%eax
je     1213 <find_mptctl_device+0x63>
mov    %ebp,%edi
call   1060 <minor@plt>
cmp    $0xde,%eax
je     1213 <find_mptctl_device+0x63>
jmp    1240 <find_mptctl_device+0x90>
mov    %r12,%rdi
call   10a0 <closedir@plt>
cmpl   $0x1,0x4(%rsp)
jle    12ec <find_mptctl_device+0x13c>
mov    0x2d1d(%rip),%rax
mov    (%rax),%edi
lea    0xd51(%rip),%rsi
call   1030 <my_syslog@plt>
movb   $0x0,0x2db0(%rip)
xor    %eax,%eax
jmp    1300 <find_mptctl_device+0x150>
mov    %r12,%rdi
call   10a0 <closedir@plt>
lea    0x2d9d(%rip),%rcx
xor    %eax,%eax
cmpb   $0x0,0x2d94(%rip)
cmovne %rcx,%rax
add    $0x218,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i8* }
%struct.stat = type { i32, i32 }
@find_mptctl_device.matching_filename = internal global [256 x i8] zeroinitializer
@.str = [5 x i8] c"/dev\00"
@.str.1 = [6 x i8] c"%s/%s\00"
@.str.2 = [26 x i8] c"Found control device: %s\0A\00"
@LOG_CRIT = global i32 0
@.str.3 = [58 x i8] c"More than one control file found, cannot auto-select one!\00"
define i8* @find_mptctl_device() {
%1 = alloca i8*
%2 = alloca i32*
%3 = alloca %struct.dirent*
%4 = alloca i32
%5 = alloca [512 x i8]
%6 = alloca %struct.stat
%7 = alloca i32
store i32 0, i32* %4
store i8 0, i8* getelementptr inbounds ([256 x i8], [256 x i8]* @find_mptctl_device.matching_filename, i64 0, i64 0)
%8 = call i32* @opendir(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i32* %8, i32** %2
%9 = load i32*, i32** %2
%10 = icmp ne i32* %9, null
br i1 %10, label %12, label %11
11: ; preds = %0
store i8* null, i8** %1
br label %71
12: ; preds = %0
br label %13
13: ; preds = %56, %33, %27, %12
%14 = load i32*, i32** %2
%15 = call %struct.dirent* @readdir(i32* %14)
store %struct.dirent* %15, %struct.dirent** %3
%16 = icmp ne %struct.dirent* %15, null
br i1 %16, label %17, label %57
17: ; preds = %13
%18 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 0
%19 = load %struct.dirent*, %struct.dirent** %3
%20 = getelementptr inbounds %struct.dirent, %struct.dirent* %19, i32 0, i32 0
%21 = load i8*, i8** %20
%22 = call i32 @snprintf(i8* %18, i32 512, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %21)
%23 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 0
%24 = call i32 @stat(i8* %23, %struct.stat* %6)
store i32 %24, i32* %7
%25 = load i32, i32* %7
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %28
27: ; preds = %17
br label %13
28: ; preds = %17
%29 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 1
%30 = load i32, i32* %29
%31 = call i32 @S_ISCHR(i32 %30)
%32 = icmp ne i32 %31, 0
br i1 %32, label %34, label %33
33: ; preds = %28
br label %13
34: ; preds = %28
%35 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%36 = load i32, i32* %35
%37 = call i32 @major(i32 %36)
%38 = icmp eq i32 %37, 10
br i1 %38, label %39, label %56
39: ; preds = %34
%40 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%41 = load i32, i32* %40
%42 = call i32 @minor(i32 %41)
%43 = icmp eq i32 %42, 221
br i1 %43, label %49, label %44
44: ; preds = %39
%45 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%46 = load i32, i32* %45
%47 = call i32 @minor(i32 %46)
%48 = icmp eq i32 %47, 222
br i1 %48, label %49, label %56
49: ; preds = %44, %39
%50 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 0
%51 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i8* %50)
%52 = load i32, i32* %4
%53 = add nsw i32 %52, 1
store i32 %53, i32* %4
%54 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 0
%55 = call i32 @strcpy(i8* getelementptr inbounds ([256 x i8], [256 x i8]* @find_mptctl_device.matching_filename, i64 0, i64 0), i8* %54)
br label %56
56: ; preds = %49, %44, %34
br label %13
57: ; preds = %13
%58 = load i32*, i32** %2
%59 = call i32 @closedir(i32* %58)
%60 = load i32, i32* %4
%61 = icmp sgt i32 %60, 1
br i1 %61, label %62, label %65
62: ; preds = %57
%63 = load i32, i32* @LOG_CRIT
%64 = call i32 @my_syslog(i32 %63, i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.3, i64 0, i64 0))
store i8 0, i8* getelementptr inbounds ([256 x i8], [256 x i8]* @find_mptctl_device.matching_filename, i64 0, i64 0)
br label %65
65: ; preds = %62, %57
%66 = load i8, i8* getelementptr inbounds ([256 x i8], [256 x i8]* @find_mptctl_device.matching_filename, i64 0, i64 0)
%67 = sext i8 %66 to i32
%68 = icmp ne i32 %67, 0
%69 = zext i1 %68 to i64
%70 = select i1 %68, i8* getelementptr inbounds ([256 x i8], [256 x i8]* @find_mptctl_device.matching_filename, i64 0, i64 0), i8* null
store i8* %70, i8** %1
br label %71
71: ; preds = %65, %11
%72 = load i8*, i8** %1
ret i8* %72
}

LLVM IR (-O2):
%struct.stat = type { i32, i32 }
%struct.dirent = type { i8* }
@find_mptctl_device.matching_filename = internal global [256 x i8] zeroinitializer
@.str = [5 x i8] c"/dev\00"
@.str.1 = [6 x i8] c"%s/%s\00"
@.str.2 = [26 x i8] c"Found control device: %s\0A\00"
@LOG_CRIT = global i32 0
@.str.3 = [58 x i8] c"More than one control file found, cannot auto-select one!\00"
define i8* @find_mptctl_device() {
%1 = alloca [512 x i8]
%2 = alloca %struct.stat
store i8 0, i8* getelementptr inbounds ([256 x i8], [256 x i8]* @find_mptctl_device.matching_filename, i64 0, i64 0)
%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 %55, label %5
5: ; preds = %0
%6 = call %struct.dirent* @readdir(i32* %3)
%7 = icmp eq %struct.dirent* %6, null
br i1 %7, label %8, label %10
8: ; preds = %5
%9 = call i32 @closedir(i32* %3)
br label %48
10: ; preds = %5
%11 = getelementptr inbounds [512 x i8], [512 x i8]* %1, i64 0, i64 0
%12 = bitcast %struct.stat* %2 to i8*
%13 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 1
%14 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
br label %15
15: ; preds = %10, %41
%16 = phi %struct.dirent* [ %6, %10 ], [ %43, %41 ]
%17 = phi i32 [ 0, %10 ], [ %42, %41 ]
%18 = getelementptr inbounds %struct.dirent, %struct.dirent* %16, i64 0, i32 0
%19 = load i8*, i8** %18
%20 = call i32 @snprintf(i8* %11, i32 512, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %19)
%21 = call i32 @stat(i8* %11, %struct.stat* %2)
%22 = icmp slt i32 %21, 0
br i1 %22, label %41, label %23
23: ; preds = %15
%24 = load i32, i32* %13
%25 = call i32 @S_ISCHR(i32 %24)
%26 = icmp eq i32 %25, 0
br i1 %26, label %41, label %27
27: ; preds = %23
%28 = load i32, i32* %14
%29 = call i32 @major(i32 %28)
%30 = icmp eq i32 %29, 10
br i1 %30, label %31, label %41
31: ; preds = %27
%32 = call i32 @minor(i32 %28)
%33 = icmp eq i32 %32, 221
br i1 %33, label %37, label %34
34: ; preds = %31
%35 = call i32 @minor(i32 %28)
%36 = icmp eq i32 %35, 222
br i1 %36, label %37, label %41
37: ; preds = %34, %31
%38 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i8* %11)
%39 = add nsw i32 %17, 1
%40 = call i32 @strcpy(i8* getelementptr inbounds ([256 x i8], [256 x i8]* @find_mptctl_device.matching_filename, i64 0, i64 0), i8* %11)
br label %41
41: ; preds = %27, %34, %37, %23, %15
%42 = phi i32 [ %17, %15 ], [ %17, %23 ], [ %39, %37 ], [ %17, %34 ], [ %17, %27 ]
%43 = call %struct.dirent* @readdir(i32* %3)
%44 = icmp eq %struct.dirent* %43, null
br i1 %44, label %45, label %15
45: ; preds = %41
%46 = call i32 @closedir(i32* %3)
%47 = icmp sgt i32 %42, 1
br i1 %47, label %52, label %48
48: ; preds = %8, %45
%49 = load i8, i8* getelementptr inbounds ([256 x i8], [256 x i8]* @find_mptctl_device.matching_filename, i64 0, i64 0)
%50 = icmp eq i8 %49, 0
%51 = select i1 %50, i8* null, i8* getelementptr inbounds ([256 x i8], [256 x i8]* @find_mptctl_device.matching_filename, i64 0, i64 0)
br label %55
52: ; preds = %45
%53 = load i32, i32* @LOG_CRIT
%54 = call i32 @my_syslog(i32 %53, i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.3, i64 0, i64 0))
store i8 0, i8* getelementptr inbounds ([256 x i8], [256 x i8]* @find_mptctl_device.matching_filename, i64 0, i64 0)
br label %55
55: ; preds = %52, %48, %0
%56 = phi i8* [ null, %0 ], [ %51, %48 ], [ null, %52 ]
ret i8* %56
}