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

/* Variables and functions */
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
scalar_t__ isSerialPortDev(int /*<<< orphan*/);
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int SerialPortCount(void) {
  DIR *dirPtr;
  struct dirent *entryPtr;
  int cnt = 0;
  if ((dirPtr = opendir("/dev")) == NULL)
    return 0;
  while ((entryPtr = readdir(dirPtr)) != NULL) {
    if (isSerialPortDev(entryPtr->d_name))
      cnt++;
  }
  closedir(dirPtr);
  return cnt;
}


x86 assembly (clang, -O0):
<SerialPortCount>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x1c(%rbp)
lea    0xeaa(%rip),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    1175 <SerialPortCount+0x35>
movl   $0x0,-0x4(%rbp)
jmp    11c3 <SerialPortCount+0x83>
jmp    117a <SerialPortCount+0x3a>
mov    -0x10(%rbp),%rdi
call   1050 <readdir@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     11b4 <SerialPortCount+0x74>
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
call   1060 <isSerialPortDev@plt>
cmp    $0x0,%rax
je     11af <SerialPortCount+0x6f>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    117a <SerialPortCount+0x3a>
mov    -0x10(%rbp),%rdi
call   1040 <closedir@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<SerialPortCount>:
push   %rbp
push   %rbx
push   %rax
lea    0xeb6(%rip),%rdi
call   1030 <opendir@plt>
test   %rax,%rax
je     1195 <SerialPortCount+0x55>
mov    %rax,%rbx
mov    %rax,%rdi
call   1050 <readdir@plt>
xor    %ebp,%ebp
test   %rax,%rax
je     118b <SerialPortCount+0x4b>
cs nopw 0x0(%rax,%rax,1)
mov    (%rax),%edi
call   1060 <isSerialPortDev@plt>
cmp    $0x1,%rax
sbb    $0xffffffff,%ebp
mov    %rbx,%rdi
call   1050 <readdir@plt>
test   %rax,%rax
jne    1170 <SerialPortCount+0x30>
mov    %rbx,%rdi
call   1040 <closedir@plt>
jmp    1197 <SerialPortCount+0x57>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i32 }
@.str = [5 x i8] c"/dev\00"
define i32 @SerialPortCount() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca %struct.dirent*
%4 = alloca i32
store i32 0, i32* %4
%5 = call i32* @opendir(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i32* %5, i32** %2
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %8
7: ; preds = %0
store i32 0, i32* %1
br label %27
8: ; preds = %0
br label %9
9: ; preds = %22, %8
%10 = load i32*, i32** %2
%11 = call %struct.dirent* @readdir(i32* %10)
store %struct.dirent* %11, %struct.dirent** %3
%12 = icmp ne %struct.dirent* %11, null
br i1 %12, label %13, label %23
13: ; preds = %9
%14 = load %struct.dirent*, %struct.dirent** %3
%15 = getelementptr inbounds %struct.dirent, %struct.dirent* %14, i32 0, i32 0
%16 = load i32, i32* %15
%17 = call i64 @isSerialPortDev(i32 %16)
%18 = icmp ne i64 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %13
%20 = load i32, i32* %4
%21 = add nsw i32 %20, 1
store i32 %21, i32* %4
br label %22
22: ; preds = %19, %13
br label %9
23: ; preds = %9
%24 = load i32*, i32** %2
%25 = call i32 @closedir(i32* %24)
%26 = load i32, i32* %4
store i32 %26, i32* %1
br label %27
27: ; preds = %23, %7
%28 = load i32, i32* %1
ret i32 %28
}

LLVM IR (-O2):
%struct.dirent = type { i32 }
@.str = [5 x i8] c"/dev\00"
define i32 @SerialPortCount() {
%1 = tail call i32* @opendir(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%2 = icmp eq i32* %1, null
br i1 %2, label %20, label %3
3: ; preds = %0
%4 = tail call %struct.dirent* @readdir(i32* %1)
%5 = icmp eq %struct.dirent* %4, null
br i1 %5, label %17, label %6
6: ; preds = %3, %6
%7 = phi %struct.dirent* [ %15, %6 ], [ %4, %3 ]
%8 = phi i32 [ %14, %6 ], [ 0, %3 ]
%9 = getelementptr inbounds %struct.dirent, %struct.dirent* %7, i64 0, i32 0
%10 = load i32, i32* %9
%11 = tail call i64 @isSerialPortDev(i32 %10)
%12 = icmp ne i64 %11, 0
%13 = zext i1 %12 to i32
%14 = add nuw nsw i32 %8, %13
%15 = tail call %struct.dirent* @readdir(i32* %1)
%16 = icmp eq %struct.dirent* %15, null
br i1 %16, label %17, label %6
17: ; preds = %6, %3
%18 = phi i32 [ 0, %3 ], [ %14, %6 ]
%19 = tail call i32 @closedir(i32* %1)
br label %20
20: ; preds = %0, %17
%21 = phi i32 [ %18, %17 ], [ 0, %0 ]
ret i32 %21
}