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

/* Variables and functions */
scalar_t__ DT_CHR;
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int openDevice(struct dirent *);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ printf(char *, ...);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ serialClose(int);
int /*<<< orphan*/ sleep(int);
scalar_t__ startsWith(int /*<<< orphan*/, char *);
int main(int argc, char **argv) {
  int found = 0;
  DIR *baseDir;
  struct dirent *dir;
  printf("--------------------------------------------------\n");
  baseDir = opendir("/dev");
  if (baseDir) {
    while ((dir = readdir(baseDir)) != NULL) {
      if (dir->d_type == DT_CHR) {
        if (startsWith(dir->d_name, "ttyUSB") ||
            startsWith(dir->d_name, "ttyACM")) {
          int fd1 = 0;
          int fd2 = 0;
          if (fd1 = openDevice(dir)) {
            found++;
          } else {
            sleep(1);
            if (fd2 = openDevice(dir)) {
              found++;
            }
          }
          if (fd1)
            serialClose(fd1);
          if (fd2)
            serialClose(fd2);
        }
      }
    }
    closedir(baseDir);
  }
  printf("--------------------------------------------------\n%d device(s) "
         "found.\n",
         found);
  return (0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
lea    0xe5c(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
lea    0xe82(%rip),%rdi
call   1060 <opendir@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     12cd <main+0x14d>
jmp    11cb <main+0x4b>
mov    -0x20(%rbp),%rdi
call   1090 <readdir@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     12c4 <main+0x144>
mov    -0x28(%rbp),%rax
mov    (%rax),%rax
mov    0x2df0(%rip),%rcx
cmp    (%rcx),%rax
jne    12bf <main+0x13f>
mov    -0x28(%rbp),%rax
mov    0x8(%rax),%edi
lea    0xe32(%rip),%rsi
call   1040 <startsWith@plt>
cmp    $0x0,%rax
jne    1233 <main+0xb3>
mov    -0x28(%rbp),%rax
mov    0x8(%rax),%edi
lea    0xe1c(%rip),%rsi
call   1040 <startsWith@plt>
cmp    $0x0,%rax
je     12ba <main+0x13a>
movl   $0x0,-0x2c(%rbp)
movl   $0x0,-0x30(%rbp)
mov    -0x28(%rbp),%rdi
call   1050 <openDevice@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0x0,%eax
je     1264 <main+0xe4>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1291 <main+0x111>
mov    $0x1,%edi
call   10a0 <sleep@plt>
mov    -0x28(%rbp),%rdi
call   1050 <openDevice@plt>
mov    %eax,-0x30(%rbp)
cmp    $0x0,%eax
je     128c <main+0x10c>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1291 <main+0x111>
cmpl   $0x0,-0x2c(%rbp)
je     12a3 <main+0x123>
mov    -0x2c(%rbp),%edi
call   1030 <serialClose@plt>
cmpl   $0x0,-0x30(%rbp)
je     12b5 <main+0x135>
mov    -0x30(%rbp),%edi
call   1030 <serialClose@plt>
jmp    12ba <main+0x13a>
jmp    12bf <main+0x13f>
jmp    11cb <main+0x4b>
mov    -0x20(%rbp),%rdi
call   1080 <closedir@plt>
mov    -0x14(%rbp),%esi
lea    0xd70(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
lea    0xeb9(%rip),%rdi
call   1040 <puts@plt>
lea    0xe52(%rip),%rdi
call   1070 <opendir@plt>
test   %rax,%rax
je     1263 <main+0xd3>
mov    %rax,%rbx
mov    %rax,%rdi
call   10a0 <readdir@plt>
test   %rax,%rax
je     1268 <main+0xd8>
mov    %rax,%rbp
xor    %r12d,%r12d
mov    0x2e03(%rip),%r13
lea    0xe21(%rip),%r14
lea    0xe21(%rip),%r15
jmp    120b <main+0x7b>
nopl   (%rax)
add    $0x1,%r12d
mov    %eax,%edi
call   1030 <serialClose@plt>
mov    %rbx,%rdi
call   10a0 <readdir@plt>
mov    %rax,%rbp
test   %rax,%rax
je     126b <main+0xdb>
mov    0x0(%rbp),%rax
cmp    0x0(%r13),%rax
jne    11fb <main+0x6b>
mov    0x8(%rbp),%edi
mov    %r14,%rsi
call   1050 <startsWith@plt>
test   %rax,%rax
jne    1235 <main+0xa5>
mov    0x8(%rbp),%edi
mov    %r15,%rsi
call   1050 <startsWith@plt>
test   %rax,%rax
je     11fb <main+0x6b>
mov    %rbp,%rdi
call   1060 <openDevice@plt>
test   %eax,%eax
jne    11f0 <main+0x60>
mov    $0x1,%edi
call   10b0 <sleep@plt>
mov    %rbp,%rdi
call   1060 <openDevice@plt>
xor    %ecx,%ecx
test   %eax,%eax
setne  %cl
add    %ecx,%r12d
test   %eax,%eax
jne    11f4 <main+0x64>
jmp    11fb <main+0x6b>
xor    %r12d,%r12d
jmp    1273 <main+0xe3>
xor    %r12d,%r12d
mov    %rbx,%rdi
call   1090 <closedir@plt>
lea    0xd99(%rip),%rdi
mov    %r12d,%esi
xor    %eax,%eax
call   1080 <printf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i64, i32 }
@.str = [52 x i8] c"--------------------------------------------------\0A\00"
@.str.1 = [5 x i8] c"/dev\00"
@DT_CHR = global i64 0
@.str.2 = [7 x i8] c"ttyUSB\00"
@.str.3 = [7 x i8] c"ttyACM\00"
@.str.4 = [72 x i8] c"--------------------------------------------------\0A%d device(s) found.\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32*
%8 = alloca %struct.dirent*
%9 = alloca i32
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %6
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str, i64 0, i64 0))
%12 = call i32* @opendir(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
store i32* %12, i32** %7
%13 = load i32*, i32** %7
%14 = icmp ne i32* %13, null
br i1 %14, label %15, label %72
15: ; preds = %2
br label %16
16: ; preds = %68, %15
%17 = load i32*, i32** %7
%18 = call %struct.dirent* @readdir(i32* %17)
store %struct.dirent* %18, %struct.dirent** %8
%19 = icmp ne %struct.dirent* %18, null
br i1 %19, label %20, label %69
20: ; preds = %16
%21 = load %struct.dirent*, %struct.dirent** %8
%22 = getelementptr inbounds %struct.dirent, %struct.dirent* %21, i32 0, i32 0
%23 = load i64, i64* %22
%24 = load i64, i64* @DT_CHR
%25 = icmp eq i64 %23, %24
br i1 %25, label %26, label %68
26: ; preds = %20
%27 = load %struct.dirent*, %struct.dirent** %8
%28 = getelementptr inbounds %struct.dirent, %struct.dirent* %27, i32 0, i32 1
%29 = load i32, i32* %28
%30 = call i64 @startsWith(i32 %29, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%31 = icmp ne i64 %30, 0
br i1 %31, label %38, label %32
32: ; preds = %26
%33 = load %struct.dirent*, %struct.dirent** %8
%34 = getelementptr inbounds %struct.dirent, %struct.dirent* %33, i32 0, i32 1
%35 = load i32, i32* %34
%36 = call i64 @startsWith(i32 %35, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%37 = icmp ne i64 %36, 0
br i1 %37, label %38, label %67
38: ; preds = %32, %26
store i32 0, i32* %9
store i32 0, i32* %10
%39 = load %struct.dirent*, %struct.dirent** %8
%40 = call i32 @openDevice(%struct.dirent* %39)
store i32 %40, i32* %9
%41 = icmp ne i32 %40, 0
br i1 %41, label %42, label %45
42: ; preds = %38
%43 = load i32, i32* %6
%44 = add nsw i32 %43, 1
store i32 %44, i32* %6
br label %54
45: ; preds = %38
%46 = call i32 @sleep(i32 1)
%47 = load %struct.dirent*, %struct.dirent** %8
%48 = call i32 @openDevice(%struct.dirent* %47)
store i32 %48, i32* %10
%49 = icmp ne i32 %48, 0
br i1 %49, label %50, label %53
50: ; preds = %45
%51 = load i32, i32* %6
%52 = add nsw i32 %51, 1
store i32 %52, i32* %6
br label %53
53: ; preds = %50, %45
br label %54
54: ; preds = %53, %42
%55 = load i32, i32* %9
%56 = icmp ne i32 %55, 0
br i1 %56, label %57, label %60
57: ; preds = %54
%58 = load i32, i32* %9
%59 = call i32 @serialClose(i32 %58)
br label %60
60: ; preds = %57, %54
%61 = load i32, i32* %10
%62 = icmp ne i32 %61, 0
br i1 %62, label %63, label %66
63: ; preds = %60
%64 = load i32, i32* %10
%65 = call i32 @serialClose(i32 %64)
br label %66
66: ; preds = %63, %60
br label %67
67: ; preds = %66, %32
br label %68
68: ; preds = %67, %20
br label %16
69: ; preds = %16
%70 = load i32*, i32** %7
%71 = call i32 @closedir(i32* %70)
br label %72
72: ; preds = %69, %2
%73 = load i32, i32* %6
%74 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([72 x i8], [72 x i8]* @.str.4, i64 0, i64 0), i32 %73)
ret i32 0
}

LLVM IR (-O2):
%struct.dirent = type { i64, i32 }
@.str.1 = [5 x i8] c"/dev\00"
@DT_CHR = global i64 0
@.str.2 = [7 x i8] c"ttyUSB\00"
@.str.3 = [7 x i8] c"ttyACM\00"
@.str.4 = [72 x i8] c"--------------------------------------------------\0A%d device(s) found.\0A\00"
@str = [51 x i8] c"--------------------------------------------------\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([51 x i8], [51 x i8]* @str, i64 0, i64 0))
%4 = tail call i32* @opendir(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %48, label %6
6: ; preds = %2
%7 = tail call %struct.dirent* @readdir(i32* %4)
%8 = icmp eq %struct.dirent* %7, null
br i1 %8, label %45, label %9
9: ; preds = %6, %41
%10 = phi %struct.dirent* [ %43, %41 ], [ %7, %6 ]
%11 = phi i32 [ %42, %41 ], [ 0, %6 ]
%12 = getelementptr inbounds %struct.dirent, %struct.dirent* %10, i64 0, i32 0
%13 = load i64, i64* %12
%14 = load i64, i64* @DT_CHR
%15 = icmp eq i64 %13, %14
br i1 %15, label %16, label %41
16: ; preds = %9
%17 = getelementptr inbounds %struct.dirent, %struct.dirent* %10, i64 0, i32 1
%18 = load i32, i32* %17
%19 = tail call i64 @startsWith(i32 %18, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%20 = icmp eq i64 %19, 0
br i1 %20, label %21, label %25
21: ; preds = %16
%22 = load i32, i32* %17
%23 = tail call i64 @startsWith(i32 %22, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%24 = icmp eq i64 %23, 0
br i1 %24, label %41, label %25
25: ; preds = %21, %16
%26 = tail call i32 @openDevice(%struct.dirent* %10)
%27 = icmp eq i32 %26, 0
br i1 %27, label %30, label %28
28: ; preds = %25
%29 = add nsw i32 %11, 1
br label %37
30: ; preds = %25
%31 = tail call i32 @sleep(i32 1)
%32 = tail call i32 @openDevice(%struct.dirent* %10)
%33 = icmp ne i32 %32, 0
%34 = zext i1 %33 to i32
%35 = add nsw i32 %11, %34
%36 = icmp eq i32 %32, 0
br i1 %36, label %41, label %37
37: ; preds = %30, %28
%38 = phi i32 [ %26, %28 ], [ %32, %30 ]
%39 = phi i32 [ %29, %28 ], [ %35, %30 ]
%40 = tail call i32 @serialClose(i32 %38)
br label %41
41: ; preds = %37, %30, %21, %9
%42 = phi i32 [ %11, %21 ], [ %11, %9 ], [ %35, %30 ], [ %39, %37 ]
%43 = tail call %struct.dirent* @readdir(i32* %4)
%44 = icmp eq %struct.dirent* %43, null
br i1 %44, label %45, label %9
45: ; preds = %41, %6
%46 = phi i32 [ 0, %6 ], [ %42, %41 ]
%47 = tail call i32 @closedir(i32* %4)
br label %48
48: ; preds = %45, %2
%49 = phi i32 [ %46, %45 ], [ 0, %2 ]
%50 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([72 x i8], [72 x i8]* @.str.4, i64 0, i64 0), i32 %49)
ret i32 0
}